diff --git a/lib/modules/create_invoice/invoice_data.ex b/lib/modules/create_invoice/invoice_data.ex index baa547a..3f86a91 100644 --- a/lib/modules/create_invoice/invoice_data.ex +++ b/lib/modules/create_invoice/invoice_data.ex @@ -1,42 +1,29 @@ defmodule ExSzamlazzHu.Modules.CreateInvoice.InvoiceData do - alias ExSzamlazzHu.Modules.CreateInvoice.Customer - alias ExSzamlazzHu.Modules.CreateInvoice.Item + alias ExSzamlazzHu.Modules.CreateInvoice.Settings + alias ExSzamlazzHu.Utils.Validator - @derive {Inspect, except: [:agent_key]} - - @type t :: %__MODULE__{ - agent_key: String.t(), - completion_date: String.t(), - payment_deadline: String.t(), - method_of_payment: String.t(), - order_id: String.t(), - currency: String.t(), - language: String.t(), - comment: String.t(), - is_proforma: String.t(), - is_paid: String.t(), - customer: Customer.t(), - items: list(Item.t()) - } + @type t :: %__MODULE__{} + @enforce_keys [:settings] defstruct [ - :agent_key, - :completion_date, - :payment_deadline, - :method_of_payment, - :order_id, - :currency, - :language, - :comment, - :is_proforma, - :is_paid, - :customer, + :settings, + :header, + :seller, + :buyer, + :waybill, :items ] def parse(params) do - struct(%__MODULE__{}, Map.drop(params, [:customer, :items])) - |> Map.put(:customer, Customer.parse(params[:customer])) - |> Map.put(:items, Enum.map(params[:items], &Item.parse/1)) + %__MODULE__{ + settings: params[:settings] + } + end + + def validate(struct) do + %{ + settings: &Settings.validate/1 + } + |> Validator.validate(struct) end end diff --git a/lib/modules/create_invoice/settings.ex b/lib/modules/create_invoice/settings.ex new file mode 100644 index 0000000..155af4d --- /dev/null +++ b/lib/modules/create_invoice/settings.ex @@ -0,0 +1,55 @@ +defmodule ExSzamlazzHu.Modules.CreateInvoice.Settings do + alias ExSzamlazzHu.Utils.Validator + + @type t :: %__MODULE__{} + + @enforce_keys [:e_invoice, :download_invoice] + defstruct [ + :user, + :password, + :agent_key, + :e_invoice, + :download_invoice, + :download_invoice_number_of_copies, + :response_version, + :aggregator, + :guardian, + :article_identifier_invoice, + :external_invoice_identifier + ] + + @spec parse(map()) :: t() + def parse(params) do + %__MODULE__{ + user: params[:user], + password: params[:password], + agent_key: params[:agent_key], + e_invoice: params[:e_invoice], + download_invoice: params[:download_invoice], + download_invoice_number_of_copies: params[:download_invoice_number_of_copies], + response_version: params[:response_version], + aggregator: params[:aggregator], + guardian: params[:guardian], + article_identifier_invoice: params[:article_identifier_invoice], + external_invoice_identifier: params[:external_invoice_identifier] + } + end + + @spec validate(t()) :: boolean() + def validate(struct) do + %{ + user: &(is_nil(&1) or is_binary(&1)), + password: &(is_nil(&1) or is_binary(&1)), + agent_key: &(is_nil(&1) or is_binary(&1)), + e_invoice: &is_boolean/1, + download_invoice: &is_boolean/1, + download_invoice_number_of_copies: &(is_nil(&1) or is_integer(&1)), + response_version: &(is_nil(&1) or is_integer(&1)), + aggregator: &(is_nil(&1) or is_binary(&1)), + guardian: &(is_nil(&1) or is_boolean(&1)), + article_identifier_invoice: &(is_nil(&1) or is_boolean(&1)), + external_invoice_identifier: &(is_nil(&1) or is_binary(&1)) + } + |> Validator.validate(struct) + end +end diff --git a/lib/utils/validator.ex b/lib/utils/validator.ex new file mode 100644 index 0000000..93d4ce7 --- /dev/null +++ b/lib/utils/validator.ex @@ -0,0 +1,19 @@ +defmodule ExSzamlazzHu.Utils.Validator do + def validate(rules, struct) do + Enum.reduce(rules, {true, %{}}, fn {key, validator}, {is_valid, errors} -> + case validator.(Map.get(struct, key)) do + true -> {is_valid, errors} + false -> {false, Map.put(errors, key, :invalid)} + :ok -> {is_valid, errors} + {:error, incoming_errors} -> {false, Map.put(errors, key, incoming_errors)} + end + end) + |> then(fn {is_valid, errors} -> + if is_valid do + :ok + else + {:error, errors} + end + end) + end +end