revamp aggregate and use typestruct
All checks were successful
Build and Publish / build-release (push) Successful in 1m41s

This commit is contained in:
2026-04-22 11:37:04 -05:00
parent 5f2f9e9085
commit a7160aadcf
19 changed files with 228 additions and 185 deletions

View File

@@ -30,7 +30,7 @@ defmodule PolicyService.Aggregates.PolicyApplication do
ProviderQuoteReceived,
AllQuotesReceived,
QuoteAccepted,
SolicitationSent,
SolicitationRequestSent,
PolicyIssued
}
@@ -42,16 +42,14 @@ defmodule PolicyService.Aggregates.PolicyApplication do
:applicant_info,
:policy_details,
:selected_providers,
:accepted_quote_id,
:accepted_plan_id,
:accepted_provider_id,
:solicitation_id,
:policy_number,
:accepted_by,
:provider_policy_number,
:effective_date,
:expiry_date,
:state,
quotes: %{},
pending_endorsements: %{}
endorsements: %{}
]
# ── Execute ────────────────────────────────────────────────────────────
@@ -140,19 +138,29 @@ defmodule PolicyService.Aggregates.PolicyApplication do
end
def execute(%__MODULE__{} = agg, %AcceptQuoteAndSolicit{} = cmd) do
with {:ok, quote} <-
PolicyService.Aggregates.PolicyApplication.find_quote(agg, cmd.quote_id),
{:ok, provider} <-
PolicyService.Aggregates.PolicyApplication.find_provider(agg, quote.provider_id),
{:ok, plan} <-
PolicyService.Aggregates.PolicyApplication.find_plan(quote, cmd.plan_id) do
%QuoteAccepted{
id: agg.id,
quote: quote,
plan: plan,
provider: provider,
accepted_at: DateTime.utc_now()
}
case Enum.find_value(agg.quotes, fn {provider_id, quote} ->
case Enum.find(quote.plans, &(&1.plan_id == cmd.accepted_plan_id)) do
nil -> nil
plan -> {:ok, %{quote: quote, provider: provider_id, plan: plan}}
end
end) do
nil ->
{:error, :plan_not_found}
result ->
[
%QuoteAccepted{
id: agg.id,
quote: result.quote,
plan: result.plan,
provider: result.provider,
accepted_by: cmd.accepted_by
},
%SolicitationRequestSent{
id: agg.id,
plan: result.plan
}
]
end
end
@@ -162,7 +170,7 @@ defmodule PolicyService.Aggregates.PolicyApplication do
def execute(%__MODULE__{} = agg, %RecordPolicyIssued{} = cmd) do
%PolicyIssued{
id: agg.id,
policy_number: cmd.policy_number,
provider_policy_number: cmd.provider_policy_number,
effective_date: cmd.effective_date,
expiry_date: cmd.expiry_date,
issued_at: cmd.issued_at || DateTime.utc_now()
@@ -205,21 +213,22 @@ defmodule PolicyService.Aggregates.PolicyApplication do
def apply(%__MODULE__{} = agg, %QuoteAccepted{} = e) do
%__MODULE__{
agg
| accepted_quote_id: e.quote.quote_id,
accepted_plan_id: e.plan.plan_id,
accepted_provider_id: e.provider.provider_id,
state: :solicitation_sent
| accepted_plan_id: e.plan.plan_id,
accepted_by: e.accepted_by
}
end
def apply(%__MODULE__{} = agg, %SolicitationSent{} = e) do
%__MODULE__{agg | solicitation_id: e.solicitation_id}
def apply(%__MODULE__{} = agg, %SolicitationRequestSent{} = _e) do
%__MODULE__{
agg
| state: :awaiting_policy
}
end
def apply(%__MODULE__{} = agg, %PolicyIssued{} = e) do
%__MODULE__{
agg
| policy_number: e.policy_number,
| provider_policy_number: e.provider_policy_number,
effective_date: e.effective_date,
expiry_date: e.expiry_date,
state: :issued