refactor buyer and insured and add more policy types
All checks were successful
Build and Publish / build-release (push) Successful in 1m38s

This commit is contained in:
2026-04-27 14:06:28 -05:00
parent c8a58c3f58
commit 2a8f2ffc2d
27 changed files with 676 additions and 170 deletions

View File

@@ -39,7 +39,8 @@ defmodule PolicyService.Aggregates.PolicyApplication do
defstruct [
:id,
:submitted_by,
:applicant_info,
:insured,
:buyer,
:policy_details,
:selected_providers,
:accepted_plan_id,
@@ -52,13 +53,15 @@ defmodule PolicyService.Aggregates.PolicyApplication do
endorsements: %{}
]
# ── Execute ────────────────────────────────────────────────────────────
# ── Execute ──────────────────────────────────────────────────
@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_applicant(cmd.applicant_info),
PolicyService.Aggregates.PolicyApplication.validate_insured(cmd.insured),
:ok <-
PolicyService.Aggregates.PolicyApplication.validate_buyer(cmd.buyer),
:ok <- validate_details(cmd.policy_details),
:ok <-
PolicyService.Aggregates.PolicyApplication.validate_providers(
@@ -70,7 +73,8 @@ defmodule PolicyService.Aggregates.PolicyApplication do
id: cmd.id,
provider_id: provider.provider_id,
provider_email: provider.email,
applicant_info: cmd.applicant_info,
insured: cmd.insured,
buyer: cmd.buyer,
policy_details: cmd.policy_details,
requested_at: DateTime.utc_now()
}
@@ -80,7 +84,8 @@ defmodule PolicyService.Aggregates.PolicyApplication do
%PolicyApplicationSubmitted{
id: cmd.id,
submitted_by: cmd.submitted_by,
applicant_info: cmd.applicant_info,
insured: cmd.insured,
buyer: cmd.buyer,
policy_details: cmd.policy_details,
selected_providers: cmd.selected_providers,
submitted_at: DateTime.utc_now()
@@ -178,7 +183,7 @@ defmodule PolicyService.Aggregates.PolicyApplication do
}
end
# ── Apply ──────────────────────────────────────────────────────────────
# ── Apply ──────────────────────────────────────────────────────
@impl Commanded.Aggregates.Aggregate
def apply(%__MODULE__{} = agg, %PolicyApplicationSubmitted{} = e) do
@@ -186,7 +191,8 @@ defmodule PolicyService.Aggregates.PolicyApplication do
agg
| id: e.id,
submitted_by: e.submitted_by,
applicant_info: e.applicant_info,
insured: e.insured,
buyer: e.buyer,
policy_details: e.policy_details,
selected_providers: e.selected_providers,
quotes: %{},
@@ -247,12 +253,17 @@ 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_applicant(%{"name" => n, "date_of_birth" => _, "document_id" => d})
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
# Match on string keys for Company
def validate_applicant(%{
def validate_insured(%{
"type" => "corporate",
"company_name" => c,
"ruc" => r,
"legal_rep_name" => rep,
@@ -262,7 +273,29 @@ defmodule PolicyService.Aggregates.PolicyApplication do
byte_size(c) > 0 and byte_size(r) > 0,
do: :ok
def validate_applicant(_), do: {:error, :invalid_applicant_info}
def validate_insured(_), do: {:error, :invalid_insured_info}
def validate_buyer(%{
"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_buyer(%{
"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_buyer(_), do: {:error, :invalid_buyer_info}
def validate_providers(p) when is_list(p) and length(p) > 0, do: :ok
def validate_providers(_), do: {:error, :no_providers_selected}