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

list for currency selection in general settings #329

Merged
merged 7 commits into from
Nov 29, 2018

Conversation

jyotigautam
Copy link
Collaborator

Adding a dropdown on create general settings form to choose a currency from a given list of currencies.

Motivation and Context

Since we need a common currency globally set for a store, hence the currency is fetched from the store settings at every place where currency is used.

Describe your changes

  1. Added a list of currencies to choose from in general settings form.
  2. Used the currency set in general config everywhere as default currency.

Checklist

  • I have read CONTRIBUTING.md.
  • My code follows the style guidelines of this project.
  • I have commented my code, particularly in hard-to-understand areas.
  • My code does not generate any (new) credo and compile-time warnings.
  • I have updated the documentation wherever necessary.
  • I have added tests to cover my changes.

Dear Gatekeeper,

Please make sure that the commits that will be merged or rebased into the base
branch are formatted according to our standards.

The only additional requirement is that the the PR number must appear in the
commit title in brackets: (#XXX)

Changes:
Adding a dropdown on create general settings form
to choose a currency from a given list of currencies.
@codecov
Copy link

codecov bot commented Nov 28, 2018

Codecov Report

Merging #329 into develop will increase coverage by 0.02%.
The diff coverage is 71.42%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #329      +/-   ##
===========================================
+ Coverage    67.06%   67.08%   +0.02%     
===========================================
  Files          134      134              
  Lines         1497     1498       +1     
===========================================
+ Hits          1004     1005       +1     
  Misses         493      493
Impacted Files Coverage Δ
.../lib/core/tools/mailer/order_confirmation_email.ex 93.75% <ø> (ø) ⬆️
apps/snitch_core/lib/core/tools/money.ex 100% <100%> (ø) ⬆️
apps/snitch_core/lib/core/domain/package.ex 83.33% <100%> (+0.98%) ⬆️
...snitch_core/lib/core/domain/shipping_calculator.ex 100% <100%> (ø) ⬆️
..._core/lib/core/data/model/general_configuration.ex 10.71% <100%> (+6.86%) ⬆️
apps/snitch_core/lib/core/domain/order/order.ex 42.55% <20%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 42ca81f...5c6adc6. Read the comment docs.

@@ -22,7 +22,15 @@
<%= input f, :hosted_payment_url, nil, is_horizontal: true, description: "Payment URL for the store." %>
</div>
<div class="form-group row ">
<%= input f, :currency, nil, is_horizontal: true, description: "Default currency for the store." %>
<label class="col-sm-3 col-form-label">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation is not correct

general_config = Repo.all(GC) |> List.first()
if general_config != nil, do: general_config.currency, else: "USD"
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  def fetch_currency do
    case Repo.one(GC)
      nil -> "USD"
      gc -> gc.currency
    end
  end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking better.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is more succinct way.

@@ -38,7 +38,7 @@ defmodule Snitch.Domain.Order do
"""
@spec payments_total(Order.t(), String.t()) :: Money.t()
def payments_total(order, payment_state) do
{:ok, currency} = Defaults.fetch(:currency)
currency = GCModel.fetch_currency()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would really prefer to have {:ok, currency} or {:error, msg} rather than returning just as currency. this way matching becomes easier. @SagarKarwande thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use just the return value since we will be using a Repo.one query, which either gives a value or nil.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes returning tuples with success and error will help.

@ashish173 As per the definition of GeneralConfiguration.fetch_currency() we would never be able to get the error case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's okay @SagarKarwande for now. Since we always start/provision a store with a currency. I think now I agree with what @pkrawat1 is saying.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But there would be no {:error, msg} scenario, it will always return a string.

@@ -2,7 +2,13 @@ defmodule AdminAppWeb.GeneralSettingsView do
use AdminAppWeb, :view
alias Snitch.Data.Model.ProductBrand

@currencies ["USD", "INR", "GDP", "EUR"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should go in the core rather than in the admin application IMO. @pkrawat1 @SagarKarwande ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, valid currencies should be a part of core

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I know the idea behind this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any application would always depend on core and can easily access the valid currencies.

@@ -36,7 +37,8 @@ defmodule AdminAppWeb.LayoutView do
end

def check_general_settings() do
Repo.all(GeneralConfiguration) |> List.first()
count = Repo.aggregate(from(g in "snitch_general_configurations"), :count, :id)
if count == 1, do: true, else: false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is too complex to identify the presence of GC or not, please simplify this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def check_general_settings do
    case Repo.one(GC)
      nil -> false
      _ -> true
    end
  end

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah sure

@@ -2,7 +2,13 @@ defmodule AdminAppWeb.GeneralSettingsView do
use AdminAppWeb, :view
alias Snitch.Data.Model.ProductBrand

@currencies ["USD", "INR", "GDP", "EUR"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking if any other app in the umbrella would be needing the list of currencies. If it does then it will be required to move to some other place. So how about keeping it at a place in app that can expose this to all other apps.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think core should contain list of valid currencies

@@ -10,6 +10,12 @@ defmodule Snitch.Data.Model.GeneralConfiguration do
alias Snitch.Tools.Helper.ImageUploader
alias Ecto.Multi

@spec fetch_currency() :: String.t()
def fetch_currency do
general_config = Repo.all(GC) |> List.first()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why Repo.all() when we can use Repo.one()? That way you also won't have to do List.first()

Copy link
Collaborator Author

@jyotigautam jyotigautam Nov 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, then I have to handle the error raised if multiple results of Repo.one(GC) could be returned.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Repo.one only returns just one result. In any case my assumption is that we can only have one general setting with the way how the code is written.

@ashish173
Copy link
Member

@jyotigautam move the currency list to the core application. This is an important change.

@SagarKarwande
Copy link
Member

@ashish173 @pkrawat1
Initially during store setup General Setting is not configured until user does it.
If any product is created before setting General Settings and later on setting up general setting with different currency would break the Money calculations. Any thought about this?

Should the currency change be allowed after General Setting is created?

@@ -51,7 +51,7 @@
<div class="col-sm-9 input-group">
<%= text_input :selling_price, :amount, value: get_amount(f.data.selling_price), class: "form-control", name: "product[selling_price][amount]" %>
<div class="input-group-append">
<%= select :selling_price, :currency, get_currency(),value: get_currency_value(f.data.selling_price), class: "form-control", name: "product[selling_price][currency]" %>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jyotigautam
As we will be using currency from GeneralSetting. Does showing currency selection to user makes sense?

Currency for product prices can be set in changeset.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here the currency selected only shows one value i.e, from the general settings or "usd" if no general setting is set. So, the user can't choose from this list.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jyotigautam @SagarKarwande I would say we remove the select altogether and just show a label of the currency. Just like it is done with etsy store. Thoughts?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there is no point of showing it as select input when we have just one option.

@ashish173
Copy link
Member

ashish173 commented Nov 29, 2018

@SagarKarwande we are provisioning the general settings with a currency "USD" for now. So all the products which get created happen with the currency.

Currency change is another issue in itself we will revisit it at a later phase. #330


@spec get_currency_list() :: List.t()
def get_currency_list do
["USD", "INR", "GDP", "EUR"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jyotigautam let's make this array a module attribute, I see this as a constant what do you think?

@ashish173 ashish173 merged commit 74a82a9 into develop Nov 29, 2018
@ashish173 ashish173 deleted the show_currency_list branch November 29, 2018 15:00
jyotigautam added a commit that referenced this pull request Nov 29, 2018
Changes:
Adding a dropdown on create general settings form
to choose a currency from a given list of currencies.
jyotigautam added a commit that referenced this pull request Nov 29, 2018
Changes:
Adding a dropdown on create general settings form
to choose a currency from a given list of currencies.
ashish173 pushed a commit that referenced this pull request Jan 1, 2019
Since we need a common currency globally set for a store, hence the currency is fetched from the store settings at every place where currency is used.

Changes:-
Added a list of currencies to choose from in general settings form.
Used the currency set in general config everywhere as default currency.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants