diff --git a/lib/store/accounts.ex b/lib/store/accounts.ex index 489327c..9a1c673 100644 --- a/lib/store/accounts.ex +++ b/lib/store/accounts.ex @@ -7,7 +7,6 @@ defmodule Store.Accounts do alias Store.Repo alias Store.Accounts.{User, UserToken, UserNotifier} alias StoreWeb.Emails.AuthEmails - alias Store.Mailer ## Database getters @@ -32,6 +31,25 @@ defmodule Store.Accounts do |> then(fn user -> user.email end) end + @doc """ + Gets a user email by id. + + ## Examples + + iex> get_user_email(id) + "foo@example.com" + + iex> get_user_email(nil) + nil + + """ + def get_user_email(nil), do: nil + + def get_user_email(id) do + Repo.get_by(User, id: id) + |> then(fn user -> user.email end) + end + @doc """ Gets a user by email and password. @@ -291,7 +309,6 @@ defmodule Store.Accounts do Repo.insert!(user_token) AuthEmails.confirm_account_email(confirmation_url_fun.(encoded_token), to: user.email) - |> Mailer.deliver() end end diff --git a/lib/store/accounts/user_notifier.ex b/lib/store/accounts/user_notifier.ex index a2db100..105a3c8 100644 --- a/lib/store/accounts/user_notifier.ex +++ b/lib/store/accounts/user_notifier.ex @@ -4,18 +4,18 @@ defmodule Store.Accounts.UserNotifier do alias Store.Mailer # Delivers the email using the application mailer. - defp deliver(recipient, _, body) do + defp deliver(recipient, subject, body) do email = new() |> to(recipient) |> from({"CeSIUM - Store", "noreply@store.cesium.di.uminho.pt"}) - |> subject("[CeSIUM - Store] Verifique a sua conta") + |> subject("[CeSIUM - Store] #{subject}") |> reply_to("noreply@store.cesium.di.uminho.pt") |> text_body(body) - with {:ok, _metadata} <- Mailer.deliver(email) do - {:ok, email} - end + Task.start(fn -> {:ok, _metadata} = Mailer.deliver(email) end) + + {:ok, email} end @doc """ diff --git a/lib/store/inventory.ex b/lib/store/inventory.ex index 684b4f9..007df5a 100644 --- a/lib/store/inventory.ex +++ b/lib/store/inventory.ex @@ -4,12 +4,15 @@ defmodule Store.Inventory do """ use Store.Context - alias StoreWeb.Accounts.User - alias StoreWeb.Inventory.Product + alias Store.Accounts.User + alias Store.Inventory.Product alias Store.Inventory.Order + alias Store.Inventory.OrderHistory alias Store.Inventory.OrdersProducts alias Store.Inventory + # PRODUCTS + @doc """ Returns the list of products. @@ -74,7 +77,7 @@ defmodule Store.Inventory do {:error, %Ecto.Changeset{}} """ - def update_product(%Product{} = product, attrs, after_save \\ &{:ok, &1}) do + def update_product(%Product{} = product, attrs \\ %{}, after_save \\ &{:ok, &1}) do product |> Product.changeset(attrs) |> Repo.update() @@ -98,6 +101,10 @@ defmodule Store.Inventory do |> Repo.update() end + def change_product(%Product{} = product, attrs \\ %{}) do + Product.changeset(product, attrs) + end + @doc """ Deletes a product. @@ -121,21 +128,6 @@ defmodule Store.Inventory do |> broadcast(:deleted) end - @doc """ - Returns an `%Ecto.Changeset{}` for tracking product changes. - - ## Examples - - iex> change_product(product) - %Ecto.Changeset{data: %Product{}} - - """ - def change_product(%Product{} = product, attrs \\ %{}) do - Product.changeset(product, attrs) - end - - alias Store.Inventory.Order - def list_orders(params \\ %{}) @doc """ @@ -180,12 +172,6 @@ defmodule Store.Inventory do defp status_filter(q, status), do: where(q, [o], o.status in ^status) - def update_status(order, attrs) do - order - |> Order.changeset(attrs) - |> Repo.update() - end - @doc """ Returns the list of orders_products. iex> list_orders_products() @@ -217,7 +203,11 @@ defmodule Store.Inventory do ** (Ecto.NoResultsError) """ - def get_order!(id), do: Repo.get!(Order, id) + def get_order!(id, opts) when is_list(opts) do + Order + |> apply_filters(opts) + |> Repo.get!(id) + end @doc """ Creates a order. @@ -291,6 +281,10 @@ defmodule Store.Inventory do |> Repo.update() end + def change_order(%Order{} = order, attrs \\ %{}) do + Order.changeset(order, attrs) + end + @doc """ Deletes a order. @@ -307,19 +301,6 @@ defmodule Store.Inventory do Repo.delete(order) end - @doc """ - Returns an `%Ecto.Changeset{}` for tracking order changes. - - ## Examples - - iex> change_order(order) - %Ecto.Changeset{data: %Order{}} - - """ - def change_order(%Order{} = order, attrs \\ %{}) do - Order.changeset(order, attrs) - end - @doc """ Returns a function that can be used to broadcast the given event. @@ -341,21 +322,20 @@ defmodule Store.Inventory do iex> purchase(user, product) {:error, %Ecto.Changeset{}} """ - alias Store.Accounts.User def purchase(%User{} = user, %Product{} = product, product_params) do order = get_order_draft_by_id(user.id, preloads: []) case order do %Order{} -> - handle_existing_order(order, product, product_params) + handle_existing_order(order, product, product_params, user.partnership) nil -> handle_new_order(user, product, product_params) end end - defp handle_existing_order(order, product, product_params) do + defp handle_existing_order(order, product, product_params, partnership) do quantity = String.to_integer(product_params["quantity"]) order_products = @@ -374,30 +354,46 @@ defmodule Store.Inventory do order_product.size == String.to_existing_atom(product_params["size"]) end) + price = + case partnership do + true -> product.price_partnership + false -> product.price + end + if size_found do - update_order_products(order_products, product_params, quantity) + update_order_products(order_products, price, product_params, quantity) else - add_product_to_order(order, product, product_params) + add_product_to_order(order, product, price, product_params) end {:ok, product} end end - defp update_order_products(order_products, product_params, quantity) do + defp update_order_products(order_products, price, product_params, quantity) do Enum.each(order_products, fn order_product -> if order_product.size == String.to_existing_atom(product_params["size"]) do - update_order_product(order_product, %{quantity: order_product.quantity + quantity}) + update_order_product(order_product, %{ + quantity: order_product.quantity + quantity, + price: order_product.price + price * quantity + }) end end) end - defp handle_new_order(user, product, product_params) do + defp handle_new_order(%User{} = user, %Product{} = product, product_params) do {:ok, order} = create_order(%{user_id: user.id}) - add_product_to_order(order, product, product_params) + + price = + case user.partnership do + true -> product.price_partnership + false -> product.price + end + + add_product_to_order(order, product, price, product_params) end - def add_product_to_order(%Order{} = order, %Product{} = product, product_params) do + def add_product_to_order(%Order{} = order, %Product{} = product, price, product_params) do quantity = String.to_integer(product_params["quantity"]) size = product_params["size"] @@ -408,7 +404,8 @@ defmodule Store.Inventory do order_id: order.id, product_id: product.id, quantity: quantity, - size: size + size: size, + price: price * quantity }) end end @@ -452,17 +449,6 @@ defmodule Store.Inventory do {:ok, "Product added to cart"} end - @doc """ - - - """ - - def checkout_order(order) do - order - |> Order.changeset(%{status: :ordered}) - |> Repo.update() - end - @doc """ Function which verifies that the user has 1 or more of each product in his cart. ## Examples @@ -490,12 +476,6 @@ defmodule Store.Inventory do end end - def capitalize_status(status) do - status - |> Atom.to_string() - |> String.capitalize() - end - def total_price(order) do Enum.reduce(order.products, 0, fn product, acc -> acc + product.price end) end @@ -508,21 +488,9 @@ defmodule Store.Inventory do total_price(order) - total_price_with_partnership(order) end - def total_price_cart(id) do - order = - Order - |> where(user_id: ^id) - |> where(status: :draft) - |> Repo.one() - - order_products = - OrdersProducts - |> where(order_id: ^order.id) - |> preload([:product]) - |> Repo.all() - + def total_price_cart(order_products) do Enum.reduce(order_products, 0, fn order_product, acc -> - acc + order_product.quantity * order_product.product.price + acc + order_product.price end) end @@ -552,12 +520,10 @@ defmodule Store.Inventory do |> Repo.update() end - def discount_cart(id) do - total_price_cart(id) - total_price_partnership_cart(id) + def discount_cart(id, order_products) do + total_price_cart(order_products) - total_price_partnership_cart(id) end - alias Store.Inventory.OrderHistory - def create_orders_history(order) do %OrderHistory{} |> OrderHistory.changeset(order) @@ -612,6 +578,10 @@ defmodule Store.Inventory do |> Repo.preload(:order) end + def delete_order_product(%OrdersProducts{} = order_product) do + Repo.delete(order_product) + end + defp broadcast({:error, _reason} = error, _event), do: error defp broadcast({:ok, %Product{} = product}, event) diff --git a/lib/store/inventory/order.ex b/lib/store/inventory/order.ex index dcf44c6..119a6b1 100644 --- a/lib/store/inventory/order.ex +++ b/lib/store/inventory/order.ex @@ -2,7 +2,7 @@ defmodule Store.Inventory.Order do use Store.Schema alias Store.Accounts.User - alias StoreWeb.Inventory.Product + alias Store.Inventory.Product alias Store.Inventory.OrdersProducts @required_fields ~w(user_id)a diff --git a/lib/store/inventory/orders_products.ex b/lib/store/inventory/orders_products.ex index e5eef0c..4a796dc 100644 --- a/lib/store/inventory/orders_products.ex +++ b/lib/store/inventory/orders_products.ex @@ -1,7 +1,7 @@ defmodule Store.Inventory.OrdersProducts do use Store.Schema - alias StoreWeb.Inventory.Product + alias Store.Inventory.Product alias Store.Inventory.Order schema "orders_products" do @@ -10,6 +10,7 @@ defmodule Store.Inventory.OrdersProducts do belongs_to :order, Order belongs_to :product, Product + field :price, :integer timestamps() end @@ -17,7 +18,7 @@ defmodule Store.Inventory.OrdersProducts do @doc false def changeset(orders_products, attrs) do orders_products - |> cast(attrs, [:order_id, :product_id, :quantity, :size]) - |> validate_required([:order_id, :product_id, :quantity, :size]) + |> cast(attrs, [:order_id, :product_id, :quantity, :size, :price]) + |> validate_required([:order_id, :product_id, :quantity, :size, :price]) end end diff --git a/lib/store/inventory/product.ex b/lib/store/inventory/product.ex index ad4aaf1..2a1a7ef 100644 --- a/lib/store/inventory/product.ex +++ b/lib/store/inventory/product.ex @@ -1,4 +1,4 @@ -defmodule StoreWeb.Inventory.Product do +defmodule Store.Inventory.Product do @moduledoc """ A product. """ diff --git a/lib/store/uploaders/product_image.ex b/lib/store/uploaders/product_image.ex index 975f106..ac3b557 100644 --- a/lib/store/uploaders/product_image.ex +++ b/lib/store/uploaders/product_image.ex @@ -6,7 +6,7 @@ defmodule Store.Uploaders.ProductImage do use Waffle.Definition use Waffle.Ecto.Definition - alias StoreWeb.Inventory.Product + alias Store.Inventory.Product @versions [:original, :medium, :thumb] @extension_whitelist ~w(.jpg .jpeg .png) diff --git a/lib/store/utils.ex b/lib/store/utils.ex new file mode 100644 index 0000000..7f9b140 --- /dev/null +++ b/lib/store/utils.ex @@ -0,0 +1,32 @@ +defmodule Store.Utils do + alias StoreWeb.Router.Helpers, as: Routes + + def capitalize_status(status) do + status + |> Atom.to_string() + |> String.capitalize() + end + + def draw_qr_code(order) do + internal_route = Routes.admin_order_show_path(StoreWeb.Endpoint, :show, order.id) + url = build_url() <> internal_route + + url + |> QRCodeEx.encode() + |> QRCodeEx.svg(color: "#1F2937", width: 295, background_color: :transparent) + end + + def draw_qr_code_base64(order_id) do + internal_route = Routes.admin_order_show_path(StoreWeb.Endpoint, :show, order_id) + url = build_url() <> internal_route + + url + |> QRCodeEx.encode() + |> QRCodeEx.png(color: <<0, 0, 0>>, width: 140) + |> Base.encode64() + end + + defp build_url do + "https://#{Application.fetch_env!(:store, StoreWeb.Endpoint)[:url][:host]}" + end +end diff --git a/lib/store_web/emails/auth_email.ex b/lib/store_web/emails/auth_email.ex index 527d90a..d58ae68 100644 --- a/lib/store_web/emails/auth_email.ex +++ b/lib/store_web/emails/auth_email.ex @@ -4,26 +4,33 @@ defmodule StoreWeb.Emails.AuthEmails do """ use Phoenix.Swoosh, view: StoreWeb.EmailView, layout: {StoreWeb.LayoutView, :email} + alias Store.Mailer def reset_password_email(id, to: email) do frontend_url = Application.fetch_env!(:store, StoreWeb.Endpoint)[:frontend_url] - new() - |> from({"CeSIUM - Store", "noreply@store.cesium.di.uminho.pt"}) - |> to(email) - |> subject("[CeSIUM - Store] Instruções para repor a password") - |> reply_to("noreply@store.cesium.di.uminho.pt") - |> assign(:link, frontend_url <> "/users/reset_password/" <> id) + email = + new() + |> from({"CeSIUM - Store", "noreply@store.cesium.di.uminho.pt"}) + |> to(email) + |> subject("[CeSIUM - Store] Instruções para repor a password") + |> reply_to("noreply@store.cesium.di.uminho.pt") + |> assign(:link, frontend_url <> "/users/reset_password/" <> id) + + Task.start(fn -> {:ok, _metadata} = Mailer.deliver(email) end) end def confirm_account_email(id, to: email) do - new() - |> from({"CeSIUM - Store", "noreply@store.cesium.di.uminho.pt"}) - |> to(email) - |> subject("[CeSIUM - Store] Confirmação da conta") - |> reply_to("noreply@store.cesium.di.uminho.pt") - |> assign(:link, id) - |> assign(:user, Store.Accounts.get_user_by_email(email)) - |> render_body("confirm_account.html") + email = + new() + |> from({"CeSIUM - Store", "noreply@store.cesium.di.uminho.pt"}) + |> to(email) + |> subject("[CeSIUM - Store] Confirmação da conta") + |> reply_to("noreply@store.cesium.di.uminho.pt") + |> assign(:link, id) + |> assign(:user, Store.Accounts.get_user_by_email(email)) + |> render_body("confirm_account.html") + + Task.start(fn -> {:ok, _metadata} = Mailer.deliver(email) end) end end diff --git a/lib/store_web/emails/order_email.ex b/lib/store_web/emails/order_email.ex index 29479fd..1b8c9c6 100644 --- a/lib/store_web/emails/order_email.ex +++ b/lib/store_web/emails/order_email.ex @@ -1,82 +1,101 @@ defmodule StoreWeb.Emails.OrdersEmail do use Phoenix.Swoosh, view: StoreWeb.EmailView, layout: {StoreWeb.LayoutView, :email} + alias Store.Inventory - alias Store.Repo - alias StoreWeb.Router.Helpers, as: Routes + alias Store.Mailer + alias Store.Utils def ready(id, to: email) do frontend_url = Application.fetch_env!(:store, StoreWeb.Endpoint)[:frontend_url] - new() - |> from({"CeSIUM - Store", "noreply@store.cesium.di.uminho.pt"}) - |> to(email) - |> subject("[CeSIUM - Store] A tua encomenda encontra-se pronta para levantamento!") - |> reply_to("noreply@store.cesium.di.uminho.pt") - |> assign(:link, frontend_url <> "/orders/" <> id) - |> assign(:order, Inventory.get_order!(id) |> Repo.preload(:products)) - |> assign(:qr_code_base64, draw_qr_code_base64(id)) - |> render_body("order_status_ready.html") + email = + new() + |> from({"CeSIUM - Store", "noreply@store.cesium.di.uminho.pt"}) + |> to(email) + |> subject("[CeSIUM - Store] A tua encomenda encontra-se pronta para levantamento!") + |> reply_to("noreply@store.cesium.di.uminho.pt") + |> assign(:link, frontend_url <> "/orders/" <> id) + |> assign(:order, Inventory.get_order!(id, preloads: [:products])) + |> assign(:qr_code_base64, Utils.draw_qr_code_base64(id)) + |> render_body("order_status_ready.html") + + Task.start(fn -> {:ok, _metadata} = Mailer.deliver(email) end) + + {:ok, email} end def ordered(id, to: email) do frontend_url = Application.fetch_env!(:store, StoreWeb.Endpoint)[:frontend_url] - new() - |> from({"CeSIUM - Store", "noreply@store.cesium.di.uminho.pt"}) - |> to(email) - |> subject("[CeSIUM - Store] A tua encomenda foi realizada com sucesso!") - |> reply_to("noreply@store.cesium.di.uminho.pt") - |> assign(:link, frontend_url <> "/orders/" <> id) - |> assign(:order, Inventory.get_order!(id) |> Repo.preload(:products)) - |> assign(:qr_code_base64, draw_qr_code_base64(id)) - |> render_body("order_status_ordered.html") + email = + new() + |> from({"CeSIUM - Store", "noreply@store.cesium.di.uminho.pt"}) + |> to(email) + |> subject("[CeSIUM - Store] A tua encomenda foi realizada com sucesso!") + |> reply_to("noreply@store.cesium.di.uminho.pt") + |> assign(:link, frontend_url <> "/orders/" <> id) + |> assign(:order, Inventory.get_order!(id, preloads: [:products])) + |> assign(:qr_code_base64, Utils.draw_qr_code_base64(id)) + |> render_body("order_status_ordered.html") + + Task.start(fn -> {:ok, _metadata} = Mailer.deliver(email) end) + + {:ok, email} end def paid(id, to: email) do frontend_url = Application.fetch_env!(:store, StoreWeb.Endpoint)[:frontend_url] - new() - |> from({"CeSIUM - Store", "noreply@store.cesium.di.uminho.pt"}) - |> to(email) - |> subject("[CeSIUM - Store] A tua encomenda foi paga com sucesso!") - |> reply_to("noreply@store.cesium.di.uminho.pt") - |> assign(:link, frontend_url <> "/orders/" <> id) - |> assign(:order, Inventory.get_order!(id) |> Repo.preload(:products)) - |> assign(:qr_code_base64, draw_qr_code_base64(id)) - |> render_body("order_status_paid.html") + email = + new() + |> from({"CeSIUM - Store", "noreply@store.cesium.di.uminho.pt"}) + |> to(email) + |> subject("[CeSIUM - Store] A tua encomenda foi paga com sucesso!") + |> reply_to("noreply@store.cesium.di.uminho.pt") + |> assign(:link, frontend_url <> "/orders/" <> id) + |> assign(:order, Inventory.get_order!(id, preloads: [:products])) + |> assign(:qr_code_base64, Utils.draw_qr_code_base64(id)) + |> render_body("order_status_paid.html") + + Task.start(fn -> {:ok, _metadata} = Mailer.deliver(email) end) + + {:ok, email} end def delivered(id, to: email) do frontend_url = Application.fetch_env!(:store, StoreWeb.Endpoint)[:frontend_url] - new() - |> from({"CeSIUM - Store", "noreply@store.cesium.di.uminho.pt"}) - |> to(email) - |> subject("[CeSIUM - Store] A tua encomenda foi entregue com sucesso!") - |> reply_to("noreply@store.cesium.di.uminho.pt") - |> assign(:link, frontend_url <> "/orders/" <> id) - |> assign(:order, Inventory.get_order!(id) |> Repo.preload(:products)) - |> render_body("order_status_delivered.html") + email = + new() + |> from({"CeSIUM - Store", "noreply@store.cesium.di.uminho.pt"}) + |> to(email) + |> subject("[CeSIUM - Store] A tua encomenda foi entregue com sucesso!") + |> reply_to("noreply@store.cesium.di.uminho.pt") + |> assign(:link, frontend_url <> "/orders/" <> id) + |> assign(:order, Inventory.get_order!(id, preloads: [:products])) + |> render_body("order_status_delivered.html") + + Task.start(fn -> {:ok, _metadata} = Mailer.deliver(email) end) + + {:ok, email} end def canceled(id, to: email) do frontend_url = Application.fetch_env!(:store, StoreWeb.Endpoint)[:frontend_url] - new() - |> from({"CeSIUM - Store", "noreply@store.cesium.di.uminho.pt"}) - |> to(email) - |> subject("[CeSIUM - Store] A tua encomenda foi cancelada!") - |> reply_to("noreply@store.cesium.di.uminho.pt") - |> assign(:link, frontend_url <> "/orders/" <> id) - |> assign(:order, Inventory.get_order!(id) |> Repo.preload(:products)) - |> assign(:qr_code_base64, draw_qr_code_base64(id)) - |> render_body("order_status_canceled.html") - end + email = + new() + |> from({"CeSIUM - Store", "noreply@store.cesium.di.uminho.pt"}) + |> to(email) + |> subject("[CeSIUM - Store] A tua encomenda foi cancelada!") + |> reply_to("noreply@store.cesium.di.uminho.pt") + |> assign(:link, frontend_url <> "/orders/" <> id) + |> assign(:order, Inventory.get_order!(id, preloads: [:products])) + |> assign(:qr_code_base64, Utils.draw_qr_code_base64(id)) + |> render_body("order_status_canceled.html") + + Task.start(fn -> {:ok, _metadata} = Mailer.deliver(email) end) - defp draw_qr_code_base64(order_id) do - Routes.admin_order_show_path(StoreWeb.Endpoint, :show, order_id) - |> QRCodeEx.encode() - |> QRCodeEx.png(color: <<0, 0, 0>>, width: 140) - |> Base.encode64() + {:ok, email} end end diff --git a/lib/store_web/live/backoffice/dashboard_live/index.ex b/lib/store_web/live/backoffice/dashboard_live/index.ex index c066528..33861b4 100644 --- a/lib/store_web/live/backoffice/dashboard_live/index.ex +++ b/lib/store_web/live/backoffice/dashboard_live/index.ex @@ -1,7 +1,10 @@ defmodule StoreWeb.Backoffice.DashboardLive.Index do use StoreWeb, :live_view - import Store.Inventory + alias Store.Accounts + alias Store.Inventory + alias Store.Utils + import StoreWeb.Components.Pagination @impl true @@ -26,7 +29,7 @@ defmodule StoreWeb.Backoffice.DashboardLive.Index do end defp list_history(params) do - case list_displayable_orders_history(params, preloads: [:order, :admin]) do + case Inventory.list_displayable_orders_history(params, preloads: [:order, :admin]) do {:ok, {orders, meta}} -> %{orders: orders, meta: meta} @@ -34,12 +37,4 @@ defmodule StoreWeb.Backoffice.DashboardLive.Index do %{orders: [], meta: flop} end end - - defp user_email(id) do - if id == nil do - nil - else - Accounts.get_user!(id).email - end - end end diff --git a/lib/store_web/live/backoffice/dashboard_live/index.html.heex b/lib/store_web/live/backoffice/dashboard_live/index.html.heex index 1b40ff1..9fa7f96 100644 --- a/lib/store_web/live/backoffice/dashboard_live/index.html.heex +++ b/lib/store_web/live/backoffice/dashboard_live/index.html.heex @@ -16,11 +16,11 @@
Order User
-
<%= user_email(order.order.user_id) %>
+
<%= Accounts.get_user_email(order.order.user_id) %>
Order Manager
-
<%= user_email(order.admin_id) %>
+
<%= Accounts.get_user_email(order.admin_id) %>
Status
-
<%= capitalize_status(order.status) %>
+
<%= Utils.capitalize_status(order.status) %>
diff --git a/lib/store_web/live/backoffice/order_live/index.ex b/lib/store_web/live/backoffice/order_live/index.ex index 2d13622..9c5adb0 100644 --- a/lib/store_web/live/backoffice/order_live/index.ex +++ b/lib/store_web/live/backoffice/order_live/index.ex @@ -1,11 +1,13 @@ defmodule StoreWeb.Backoffice.OrderLive.Index do use StoreWeb, :live_view - import Store.Inventory, except: [list_orders: 1] + + import StoreWeb.Components.Pagination + + alias Store.Accounts alias Store.Inventory alias Store.Inventory.Order alias Store.Uploaders - alias Store.Accounts - import StoreWeb.Components.Pagination + alias Store.Utils @impl true def mount(params, _session, socket) do @@ -25,7 +27,7 @@ defmodule StoreWeb.Backoffice.OrderLive.Index do defp apply_action(socket, :edit, %{"id" => id}) do socket |> assign(:page_title, "Edit Order") - |> assign(:order, Inventory.get_order!(id)) + |> assign(:order, Inventory.get_order!(id, preloads: [])) end defp apply_action(socket, :new, _params) do @@ -49,8 +51,4 @@ defmodule StoreWeb.Backoffice.OrderLive.Index do %{orders: [], meta: flop} end end - - defp user_email(id) do - Accounts.get_user!(id).email - end end diff --git a/lib/store_web/live/backoffice/order_live/index.html.heex b/lib/store_web/live/backoffice/order_live/index.html.heex index 4c3e637..4035f1a 100644 --- a/lib/store_web/live/backoffice/order_live/index.html.heex +++ b/lib/store_web/live/backoffice/order_live/index.html.heex @@ -12,7 +12,7 @@
Order User
-
<%= user_email(order.user_id) %>
+
<%= Accounts.get_user_email(order.user_id) %>
Date placed
@@ -23,14 +23,16 @@
Total amount
<%= if order.user.partnership == false do %> -
<%= total_price(order) %> €
+
<%= Inventory.total_price(order) %> €
<% else %> -
<%= total_price_with_partnership(order) %> €
+
+ <%= Inventory.total_price_with_partnership(order) %> € +
<% end %>
Status
-
<%= capitalize_status(order.status) %>
+
<%= Utils.capitalize_status(order.status) %>
diff --git a/lib/store_web/live/backoffice/order_live/show.ex b/lib/store_web/live/backoffice/order_live/show.ex index 4f9d9b5..a3ff274 100644 --- a/lib/store_web/live/backoffice/order_live/show.ex +++ b/lib/store_web/live/backoffice/order_live/show.ex @@ -1,16 +1,16 @@ defmodule StoreWeb.Backoffice.OrderLive.Show do use StoreWeb, :live_view - import Store.Inventory + alias Phoenix.LiveView.JS - alias Store.Repo + alias Store.Accounts alias Store.Inventory alias Store.Uploaders - alias Store.Accounts + alias Store.Utils alias StoreWeb.Emails.OrdersEmail - alias Store.Mailer + @impl true def mount(%{"id" => id}, _session, socket) do - {:ok, assign(socket, order: Inventory.get_order!(id), confirm_event: "")} + {:ok, assign(socket, order: Inventory.get_order!(id, preloads: []), confirm_event: "")} end @impl true @@ -20,25 +20,25 @@ defmodule StoreWeb.Backoffice.OrderLive.Show do |> assign(:current_page, :orders) |> assign(:page_title, page_title(socket.assigns.live_action)) |> assign(:confirm_event, "") - |> assign(:order, Inventory.get_order!(id) |> Repo.preload(:products))} + |> assign(:order, Inventory.get_order!(id, preloads: [:products]))} end @impl true def handle_event("paid", _payload, socket) do order = socket.assigns.order admin = socket.assigns.current_user - Inventory.update_status(order, %{status: :paid}) + + Inventory.update_order(order, %{status: :paid}) Inventory.create_orders_history(%{status: :paid, admin_id: admin.id, order_id: order.id}) user = Accounts.get_user!(order.user_id) - OrdersEmail.paid(order.id, to: user.email) |> Mailer.deliver() + OrdersEmail.paid(order.id, to: user.email) {:noreply, socket |> assign(:confirm_event, "") - |> put_flash(:info, "Order status updated successfly.") - |> push_redirect(to: socket.assigns.return_to)} + |> put_flash(:info, "Order status updated successfly.")} end @impl true @@ -50,13 +50,12 @@ defmodule StoreWeb.Backoffice.OrderLive.Show do Inventory.create_orders_history(%{status: :delivered, admin_id: admin.id, order_id: order.id}) user = Accounts.get_user!(order.user_id) - OrdersEmail.delivered(order.id, to: user.email) |> Mailer.deliver() + OrdersEmail.delivered(order.id, to: user.email) {:noreply, socket |> assign(:confirm_event, "") - |> put_flash(:info, "Order status updated successfly.") - |> push_redirect(to: socket.assigns.return_to)} + |> put_flash(:info, "Order status updated successfly.")} end def handle_event("ready", _payload, socket) do @@ -66,7 +65,7 @@ defmodule StoreWeb.Backoffice.OrderLive.Show do Inventory.create_orders_history(%{status: :ready, admin_id: admin.id, order_id: order.id}) user = Accounts.get_user!(order.user_id) - OrdersEmail.ready(order.id, to: user.email) |> Mailer.deliver() + OrdersEmail.ready(order.id, to: user.email) {:noreply, socket @@ -81,10 +80,6 @@ defmodule StoreWeb.Backoffice.OrderLive.Show do |> assign(:confirm_event, confirm_event)} end - defp user_email(id) do - Accounts.get_user!(id).email - end - defp page_title(:show), do: "Show Order" defp page_title(:edit), do: "Edit Order" end diff --git a/lib/store_web/live/backoffice/order_live/show.html.heex b/lib/store_web/live/backoffice/order_live/show.html.heex index af53c0b..8ae8c8e 100644 --- a/lib/store_web/live/backoffice/order_live/show.html.heex +++ b/lib/store_web/live/backoffice/order_live/show.html.heex @@ -12,7 +12,7 @@
Order User
-
<%= user_email(@order.user_id) %>
+
<%= Accounts.get_user_email(@order.user_id) %>
Total amount
-
<%= total_price(@order) %> €
+
<%= Inventory.total_price(@order) %> €
Status
-
<%= capitalize_status(@order.status) %>
+
<%= Utils.capitalize_status(@order.status) %>
@@ -133,6 +133,7 @@ + <.modal hide_close_button={true}> <%= if @confirm_event != "" do %>