-
Notifications
You must be signed in to change notification settings - Fork 18
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
Create shop orders #272
base: master
Are you sure you want to change the base?
Create shop orders #272
Changes from 1 commit
466ebb7
5c1c4f3
13f778f
387390a
f5db052
7116ab4
66c77b1
3f1bff6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -4,9 +4,14 @@ defmodule Cambiatus.Orders.Order do | |||||||||||||||||||||||
|
||||||||||||||||||||||||
alias Cambiatus.Accounts.User | ||||||||||||||||||||||||
alias Cambiatus.Orders.Item | ||||||||||||||||||||||||
alias Cambiatus.Orders | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
schema "orders" do | ||||||||||||||||||||||||
field(:payment_method, :string) | ||||||||||||||||||||||||
field(:payment_method, Ecto.Enum, | ||||||||||||||||||||||||
values: [:paypal, :bitcoin, :ethereum, :eos], | ||||||||||||||||||||||||
default: :paypal | ||||||||||||||||||||||||
) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
field(:total, :float) | ||||||||||||||||||||||||
field(:status, :string) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
@@ -18,12 +23,21 @@ defmodule Cambiatus.Orders.Order do | |||||||||||||||||||||||
end | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
@required_fields ~w(payment_method total status)a | ||||||||||||||||||||||||
@optional_fields ~w(inserted_at updated_at)a | ||||||||||||||||||||||||
@optional_fields ~w(inserted_at updated_at buyer_id)a | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
@doc false | ||||||||||||||||||||||||
def changeset(order, attrs) do | ||||||||||||||||||||||||
order | ||||||||||||||||||||||||
|> cast(attrs, @required_fields ++ @optional_fields) | ||||||||||||||||||||||||
|> validate_required(@required_fields) | ||||||||||||||||||||||||
|> validate_checkout() | ||||||||||||||||||||||||
end | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
def validate_checkout(changeset) do | ||||||||||||||||||||||||
with order_id <- get_field(changeset, :id), | ||||||||||||||||||||||||
{:ok, order} <- Orders.get_order(order_id) do | ||||||||||||||||||||||||
if Map.get(order, :status) == "cart" and get_field(changeset, :status) == "checkout" do | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be piped
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The intent of this one is to check if the order stored in the db has the status This has also changed a bit. If you want to make another review it'll be very welcome. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This cart I've added a few comments about state machines, but we should leverage its structure to make sure its valid, instead of relying on manual verification. If an order is on a certain state, we should trust our backend ingested it properly so its correct to have this state. This is an old article and probably outdated, but it contain precious concepts about this type of abstraction: https://medium.com/@joaomdmoura/state-machine-in-elixir-with-machinery-8ee6f9def2da |
||||||||||||||||||||||||
end | ||||||||||||||||||||||||
end | ||||||||||||||||||||||||
end | ||||||||||||||||||||||||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can't we simply preload the buyer? Something like this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so. Because this is getting an order where the status is
cart
and belongs to the current user. Since we're getting the order from these SQL statements I don't believe it's possible to preload the buyer.