Skip to content

Commit

Permalink
chore: remove unnecessary code
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioRodrigues10 committed Dec 10, 2023
1 parent 813175a commit 1cc26a6
Show file tree
Hide file tree
Showing 32 changed files with 384 additions and 384 deletions.
21 changes: 19 additions & 2 deletions lib/store/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down Expand Up @@ -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

Expand Down
10 changes: 5 additions & 5 deletions lib/store/accounts/user_notifier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand Down
138 changes: 54 additions & 84 deletions lib/store/inventory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand All @@ -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.
Expand All @@ -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 """
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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 =
Expand All @@ -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"]

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/store/inventory/order.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions lib/store/inventory/orders_products.ex
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,14 +10,15 @@ defmodule Store.Inventory.OrdersProducts do

belongs_to :order, Order
belongs_to :product, Product
field :price, :integer

timestamps()
end

@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
2 changes: 1 addition & 1 deletion lib/store/inventory/product.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule StoreWeb.Inventory.Product do
defmodule Store.Inventory.Product do
@moduledoc """
A product.
"""
Expand Down
Loading

0 comments on commit 1cc26a6

Please sign in to comment.