diff --git a/lib/policy_service/aggregates/life_policy_application.ex b/lib/policy_service/aggregates/life_policy_application.ex index 66b2444..9ea00bd 100644 --- a/lib/policy_service/aggregates/life_policy_application.ex +++ b/lib/policy_service/aggregates/life_policy_application.ex @@ -33,5 +33,11 @@ defmodule PolicyService.Aggregates.LifePolicyApplication do def validate_details(_), do: {:error, :invalid_life_details} def validate_insured(%{"type" => "corporate"}), do: {:error, :life_insurance_requires_individual} - def validate_insured(insured), do: super(insured) + + def validate_insured(%{"type" => "individual", "gender" => gender} = insured) + when is_binary(gender) and byte_size(gender) > 0 do + super(insured) + end + + def validate_insured(%{"type" => "individual"}), do: {:error, :missing_gender} end diff --git a/lib/policy_service/aggregates/policy_application.ex b/lib/policy_service/aggregates/policy_application.ex index 2e932c0..6d8b75a 100644 --- a/lib/policy_service/aggregates/policy_application.ex +++ b/lib/policy_service/aggregates/policy_application.ex @@ -58,8 +58,7 @@ defmodule PolicyService.Aggregates.PolicyApplication do @impl Commanded.Aggregates.Aggregate def execute(%__MODULE__{state: nil}, %SubmitPolicyApplication{} = cmd) do with :ok <- PolicyService.Aggregates.PolicyApplication.validate_policy_id(cmd.id), - :ok <- - PolicyService.Aggregates.PolicyApplication.validate_insured(cmd.insured), + :ok <- validate_insured(cmd.insured), :ok <- PolicyService.Aggregates.PolicyApplication.validate_buyer(cmd.buyer), :ok <- validate_details(cmd.policy_details), @@ -242,8 +241,32 @@ defmodule PolicyService.Aggregates.PolicyApplication do } end + # ── Validation ─────────────────────────────────────────────────── + + def validate_insured(%{ + "type" => "individual", + "name" => n, + "date_of_birth" => _, + "document_id" => d + }) + when is_binary(n) and is_binary(d) and byte_size(n) > 0 and byte_size(d) > 0, + do: :ok + + def validate_insured(%{ + "type" => "corporate", + "company_name" => c, + "ruc" => r, + "legal_rep_name" => rep, + "legal_rep_document" => rd + }) + when is_binary(c) and is_binary(r) and is_binary(rep) and is_binary(rd) and + byte_size(c) > 0 and byte_size(r) > 0, + do: :ok + + def validate_insured(_), do: {:error, :invalid_insured_info} + # allow each aggregate to override any callback - defoverridable execute: 2, apply: 2 + defoverridable execute: 2, apply: 2, validate_insured: 1 end end @@ -253,28 +276,6 @@ defmodule PolicyService.Aggregates.PolicyApplication do def validate_user(id) when is_binary(id) and byte_size(id) > 0, do: :ok def validate_user(_), do: {:error, :missing_user_id} - def validate_insured(%{ - "type" => "individual", - "name" => n, - "date_of_birth" => _, - "document_id" => d - }) - when is_binary(n) and is_binary(d) and byte_size(n) > 0 and byte_size(d) > 0, - do: :ok - - def validate_insured(%{ - "type" => "corporate", - "company_name" => c, - "ruc" => r, - "legal_rep_name" => rep, - "legal_rep_document" => rd - }) - when is_binary(c) and is_binary(r) and is_binary(rep) and is_binary(rd) and - byte_size(c) > 0 and byte_size(r) > 0, - do: :ok - - def validate_insured(_), do: {:error, :invalid_insured_info} - def validate_buyer(%{ "type" => "individual", "name" => n, diff --git a/lib/policy_service/projections/policy.ex b/lib/policy_service/projections/policy.ex index a5b5ab6..b0bf156 100644 --- a/lib/policy_service/projections/policy.ex +++ b/lib/policy_service/projections/policy.ex @@ -35,7 +35,7 @@ defmodule PolicyService.Projections.PolicyApplication do max_limit: 100, custom_fields: [ search: [ - filter: {PolicyService.Projections.PolicyApplicationFilters, :search, []}, + filter: {PolicyService.Filters.PolicyApplicationFilters, :search, []}, ecto_type: :string, operators: [:==] ]