Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add product management functionality #59

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions tololo/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
# General application configuration
import Config

config :ex_cldr, default_backend: Tololo.Cldr

config :ash,
allow_forbidden_field_for_relationships_by_default?: true,
include_embedded_source_by_default?: false,
show_keysets_for_all_actions?: false,
default_page_type: :keyset,
policies: [no_filter_static_forbidden_reads?: false]
policies: [no_filter_static_forbidden_reads?: false],
known_types: [AshMoney.Types.Money],
custom_types: [money: AshMoney.Types.Money]

# custom_types: [ticket_status: Tololo.Support.Ticket.Types.Status]

Expand Down Expand Up @@ -67,8 +71,10 @@ config :tololo,
min_done_distance_meters: 50,
ash_domains: [
TololoCore.Deliveries,
Tololo.Extensions.TelegramBot.Ash.Users,
Tololo.Accounts
TololoCore.Products,
TololoCore.Carts,
Tololo.Accounts,
Tololo.Extensions.TelegramBot.Ash.Users
],
from_email: {"noreply", "[email protected]"},
magic_link_subject: "Your login link"
Expand Down
4 changes: 4 additions & 0 deletions tololo/core/config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Config
config :ash, known_types: [AshMoney.Types.Money], custom_types: [money: AshMoney.Types.Money]
config :ex_cldr, default_backend: TololoCore.Cldr
import_config "#{config_env()}.exs"
1 change: 1 addition & 0 deletions tololo/core/config/dev.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import Config
1 change: 1 addition & 0 deletions tololo/core/config/prod.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import Config
1 change: 1 addition & 0 deletions tololo/core/config/test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import Config
14 changes: 14 additions & 0 deletions tololo/core/lib/carts.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule TololoCore.Carts do
@moduledoc """
Domain that contains resources related to the cart system.
"""
use Ash.Domain,
otp_app: :tololo,
extensions: [AshGraphql.Domain],
validate_config_inclusion?: false

resources do
resource TololoCore.Carts.Cart
resource TololoCore.Carts.CartLine
end
end
135 changes: 135 additions & 0 deletions tololo/core/lib/carts/cart.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
defmodule TololoCore.Carts.Cart do
# @moduledoc """

# """
use Ash.Resource,
otp_app: :tololo,
domain: TololoCore.Carts,
extensions: [AshGraphql.Resource],
data_layer: AshPostgres.DataLayer,
authorizers: [Ash.Policy.Authorizer],
notifiers: [Ash.Notifier.PubSub, TololoCore.Kafka.AshNotifier]

graphql do
type :cart
end

postgres do
table "carts"
repo Tololo.Repo
end

field_policies do
field_policy :* do
description "the rest of the fields don't require any special policies"
authorize_if always()
end
end

code_interface do
define :create
define :add_variant, args: [:variant, {:optional, :quantity}, {:optional, :notes}]
define :checkout_delivery, args: [:cart, :delivery_input]
end

actions do
defaults [:read, :destroy, create: :*, update: :*]

action :checkout_delivery, :term do
argument :cart, :term, allow_nil?: false
argument :delivery_input, :map, allow_nil?: false

run fn %{arguments: %{cart: cart, delivery_input: delivery_input}}, _ ->
TololoCore.Deliveries.Delivery.initialize(
%{delivery_input | delivery_order: cart |> cart_to_map()},
authorize?: false
)
end
end

update :add_variant do
require_atomic? false
argument :variant, :struct, allow_nil?: false
argument :quantity, :integer, default: 1
argument :notes, :string, default: nil

validate fn %{data: %{currency: currency}, arguments: %{variant: %{prices: prices}}} =
changeset,
_context ->
prices = Ash.load!(prices, :currency, lazy?: true, reuse_values?: true)

if prices |> Enum.any?(fn price -> price.currency == currency end) do
:ok
else
{:error, field: :variant, message: "must have a price for #{currency}"}
end
end

change fn %{arguments: %{variant: variant, quantity: quantity, notes: notes}} = changeset,
_context ->
changeset
|> Ash.Changeset.manage_relationship(
:cart_lines,
%{variant_id: variant.id, quantity: quantity, notes: notes},
type: :create
)
end

change load([
:total,
:total_before_discount,
cart_lines: [:variant, :subtotal, :subtotal_before_discount]
])
end
end

policies do
bypass always() do
authorize_if always()
end
end

attributes do
uuid_v7_primary_key :id

attribute :status, :atom do
constraints one_of: [:active, :completed, :abandoned]
public? true
default :active
end

attribute :currency, :string do
public? true
# TODO use store config
default "CLP"
end

timestamps()
end

relationships do
has_many :cart_lines, TololoCore.Carts.CartLine do
public? true
end
end

calculations do
calculate :total,
:money,
TololoCore.Carts.TotalCalculation
end

aggregates do
sum :total_before_discount, [:cart_lines], :subtotal_before_discount
end

defp cart_to_map(cart) do
%{
cart_id: cart.id,
cart_lines:
Enum.map(cart.cart_lines, fn line ->
%{quantity: line.quantity, name: line.variant.name}
end)
}
end
end
82 changes: 82 additions & 0 deletions tololo/core/lib/carts/cart_line.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
defmodule TololoCore.Carts.CartLine do
# @moduledoc """

# """
use Ash.Resource,
otp_app: :tololo,
domain: TololoCore.Carts,
extensions: [AshGraphql.Resource],
data_layer: AshPostgres.DataLayer,
authorizers: [Ash.Policy.Authorizer],
notifiers: [Ash.Notifier.PubSub, TololoCore.Kafka.AshNotifier]

graphql do
type :cart_line
end

postgres do
table "cart_lines"
repo Tololo.Repo
end

field_policies do
field_policy :* do
description "the rest of the fields don't require any special policies"
authorize_if always()
end
end

actions do
defaults [:read, :destroy, update: :*]

create :create do
primary? true
upsert? true
upsert_identity :unique_variant
accept :*
end
end

policies do
bypass always() do
authorize_if always()
end
end

attributes do
uuid_v7_primary_key :id

attribute :quantity, :integer, public?: true, allow_nil?: false, constraints: [min: 1]
attribute :notes, :string, public?: true

timestamps()
end

relationships do
belongs_to :variant, TololoCore.Products.Variant, public?: true, allow_nil?: false
belongs_to :cart, TololoCore.Carts.Cart, public?: true, allow_nil?: false
end

calculations do
calculate :subtotal_before_discount,
:money,
expr(
first(variant.prices,
query: [
filter: currency == variant.cart_lines.cart.currency,
load: [variant: [cart_lines: [:cart]]]
],
field: :money
) *
quantity
)

calculate :subtotal,
:money,
TololoCore.Carts.DiscountsCalculation
end

identities do
identity :unique_variant, [:variant_id, :cart_id]
end
end
105 changes: 105 additions & 0 deletions tololo/core/lib/carts/discounts_calculation.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
defmodule TololoCore.Carts.DiscountsCalculation do
@moduledoc """
Discounts logic for the product management system. Includes an Ash calculation and a discount engine. Returns the lowest price after applying rules instead of combining rules. Returns normal price if there's no applicable rules.
"""
use Ash.Resource.Calculation
require Logger

@impl true
def load(_query, _opts, _context),
do: [cart: [:currency], variant: [:discount_rules, prices: [:currency, :money]]]

@impl true
def calculate(cart_lines, opts, _atom) do
Enum.map(cart_lines, fn %{variant: %{discount_rules: rules}, quantity: quantity} = cart_line ->
rules
|> Enum.map(&handle_rule(&1, cart_line))
|> Enum.min(&<=/2, fn ->
get_normal_price(cart_line)
end)
|> (fn price -> Money.mult!(price, quantity) end).()
end)
end

defp get_normal_price(%{cart: %{currency: currency}, variant: %{prices: prices}}),
do: Enum.find(prices, &(&1.currency == currency)) |> Map.get(:money)

defp calculate_price(discount, %{cart: %{currency: currency}}, :fixed),
do: Money.new!(currency, discount)

defp calculate_price(discount, cart_line, :percentage),
do: Money.mult!(get_normal_price(cart_line), 1 - discount)

def handle_rule(
%{
type: :manual,
method: method,
data: %{"enabled" => true, "discount" => discount}
},
cart_line
),
do: calculate_price(discount, cart_line, method)

def handle_rule(
%{
type: :manual,
data: %{"enabled" => false, "discount" => discount}
},
cart_line
),
do: get_normal_price(cart_line)

# weekday is an int starting from 1, where 1 = monday, 2 = tuesday, etc.
def handle_rule(
%{
type: :weekly,
method: method,
data: %{
"weekdays" => weekdays,
"discount" => discount,
"from_hour" => from_hour,
"to_hour" => to_hour
}
},
cart_line
) do
current_day = Date.day_of_week(Date.utc_today())
current_hour = NaiveDateTime.local_now().hour

if current_day in weekdays and current_hour in from_hour..to_hour,
do: calculate_price(discount, cart_line, method),
else: get_normal_price(cart_line)
end

def handle_rule(
%{
type: :weekly,
method: method,
data: %{"weekdays" => weekdays, "discount" => discount}
},
cart_line
) do
if Date.day_of_week(Date.utc_today()) in weekdays,
do: calculate_price(discount, cart_line, method),
else: get_normal_price(cart_line)
end

def handle_rule(
%{
type: :quantity,
method: method,
data: %{"more_than" => more_than, "discount" => discount}
},
%{quantity: line_quantity} = cart_line
)
when line_quantity > more_than,
do: calculate_price(discount, cart_line, method)

def handle_rule(
%{type: :quantity, data: %{"more_than" => more_than, "discount" => discount}},
%{quantity: _line_quantity} = cart_line
),
do: get_normal_price(cart_line)

def handle_rule(rule, cart_line), do: Logger.error("Got unmatched rule #{inspect(rule)}")
end
Loading
Loading