partition by org_id and add auth
All checks were successful
Build and Publish / build-release (push) Successful in 3m7s

This commit is contained in:
2026-05-15 10:08:54 -05:00
parent a0b5e0c0b3
commit 4519f797fd
26 changed files with 687 additions and 112 deletions

View File

@@ -0,0 +1,48 @@
defmodule CustomerService.Aggregates.CustomerId do
@type t :: %__MODULE__{
org_id: String.t(),
customer_type: String.t(),
customer_id: String.t()
}
@derive {Jason.Encoder, only: [:org_id, :customer_type, :customer_id]}
defstruct [:org_id, :customer_type, :customer_id]
def new(org_id, customer_type, customer_id)
when is_binary(org_id) and is_binary(customer_type) and is_binary(customer_id) do
%__MODULE__{
org_id: org_id,
customer_type: customer_type,
customer_id: customer_id
}
end
def parse(<<_::binary>> = string) do
case String.split(string, ":", parts: 3) do
[org_id, customer_type, customer_id] ->
{:ok, %__MODULE__{org_id: org_id, customer_type: customer_type, customer_id: customer_id}}
_ ->
{:error, :invalid_customer_id}
end
end
def parse! do
{:error, :invalid_customer_id}
end
def to_string(%__MODULE__{
org_id: org_id,
customer_type: customer_type,
customer_id: customer_id
}) do
org_id <> ":" <> customer_type <> ":" <> customer_id
end
defimpl Commanded.Serialization.JsonDecoder do
def decode(%{org_id: org_id, customer_type: customer_type, customer_id: customer_id}) do
CustomerService.Aggregates.CustomerId.new(org_id, customer_type, customer_id)
end
def decode(id), do: id
end
end

View File

@@ -0,0 +1,39 @@
defmodule CustomerService.Aggregates.LeadId do
@type t :: %__MODULE__{
org_id: String.t(),
type: String.t(),
lead_id: String.t()
}
@derive {Jason.Encoder, only: [:org_id, :type, :lead_id]}
defstruct [:org_id, :type, :lead_id]
def new(org_id, lead_id) when is_binary(org_id) and is_binary(lead_id) do
%__MODULE__{
org_id: org_id,
type: "lead",
lead_id: lead_id
}
end
def parse(<<_::binary>> = string) do
case String.split(string, ":", parts: 3) do
[org_id, "lead", lead_id] ->
{:ok, %__MODULE__{org_id: org_id, type: "lead", lead_id: lead_id}}
_ ->
{:error, :invalid_lead_id}
end
end
def to_string(%__MODULE__{org_id: org_id, type: "lead", lead_id: lead_id}) do
org_id <> ":" <> "lead" <> ":" <> lead_id
end
defimpl Commanded.Serialization.JsonDecoder do
def decode(%{org_id: org_id, type: "lead", lead_id: lead_id}) do
CustomerService.Aggregates.LeadId.new(org_id, lead_id)
end
def decode(id), do: id
end
end

View File

@@ -7,6 +7,21 @@ defmodule CustomerService.Application do
@impl true
def start(_type, _args) do
oidcc_child =
case Application.get_env(:customer_service, :zitadel) do
nil ->
[]
cfg ->
[
{Oidcc.ProviderConfiguration.Worker,
%{
issuer: cfg[:issuer],
name: CustomerService.ZitadelProvider
}}
]
end
children = [
CustomerService.CommandedApp,
CustomerService.Repo,
@@ -14,21 +29,14 @@ defmodule CustomerService.Application do
CustomerService.Projectors.QuickLead,
CustomerServiceWeb.Telemetry,
{DNSCluster, query: Application.get_env(:customer_service, :dns_cluster_query) || :ignore},
{Phoenix.PubSub, name: CustomerService.PubSub},
# Start a worker by calling: CustomerService.Worker.start_link(arg)
# {CustomerService.Worker, arg},
# Start to serve requests, typically the last entry
CustomerServiceWeb.Endpoint
{Phoenix.PubSub, name: CustomerService.PubSub}
| oidcc_child ++ [CustomerServiceWeb.Endpoint]
]
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: CustomerService.Supervisor]
Supervisor.start_link(children, opts)
end
# Tell Phoenix to update the endpoint configuration
# whenever the application is updated.
@impl true
def config_change(changed, _new, removed) do
CustomerServiceWeb.Endpoint.config_change(changed, removed)

View File

@@ -2,12 +2,13 @@ defmodule CustomerService.Customer.Queries do
alias CustomerService.Projections.Customer
alias CustomerService.Repo
def list_customers(params \\ %{}) do
def list_by_org(org_id, params \\ %{}) when is_binary(org_id) do
params = Map.put(params, :org_id, org_id)
Flop.validate_and_run(Customer, params, for: Customer)
end
def get_customer(id) do
case Repo.get(Customer, id) do
def get_by_org(org_id, customer_id) when is_binary(org_id) and is_binary(customer_id) do
case Repo.get_by(Customer, org_id: org_id, customer_id: customer_id) do
nil -> {:error, :not_found}
customer -> {:ok, customer}
end

View File

@@ -1,4 +1,30 @@
defmodule CustomerService.Events do
@moduledoc """
Events macro for adding JsonDecoder to domain events.
"""
alias CustomerService.Aggregates.CustomerId
alias CustomerService.Aggregates.LeadId
defmacro __using__(_opts) do
quote do
defimpl Commanded.Serialization.JsonDecoder do
def decode(%{id: %CustomerId{} = id} = event) do
%{event | id: id}
end
def decode(%{id: %LeadId{} = id} = event) do
%{event | id: id}
end
def decode(event), do: event
end
end
end
end
defmodule CustomerService.Events.CustomerCreated do
use CustomerService.Events
@derive Jason.Encoder
defstruct [
:id,
@@ -14,6 +40,7 @@ defmodule CustomerService.Events.CustomerCreated do
end
defmodule CustomerService.Events.CorporateCustomerCreated do
use CustomerService.Events
@derive Jason.Encoder
defstruct [
:id,
@@ -29,6 +56,7 @@ defmodule CustomerService.Events.CorporateCustomerCreated do
end
defmodule CustomerService.Events.CustomerUpdated do
use CustomerService.Events
@derive Jason.Encoder
defstruct [
:id,
@@ -44,6 +72,7 @@ defmodule CustomerService.Events.CustomerUpdated do
end
defmodule CustomerService.Events.CorporateCustomerUpdated do
use CustomerService.Events
@derive Jason.Encoder
defstruct [
:id,
@@ -59,6 +88,7 @@ defmodule CustomerService.Events.CorporateCustomerUpdated do
end
defmodule CustomerService.Events.QuickLeadCreated do
use CustomerService.Events
@derive Jason.Encoder
defstruct [
:id,
@@ -77,6 +107,7 @@ defmodule CustomerService.Events.QuickLeadCreated do
end
defmodule CustomerService.Events.QuickLeadUpdated do
use CustomerService.Events
@derive Jason.Encoder
defstruct [
:id,
@@ -92,6 +123,7 @@ defmodule CustomerService.Events.QuickLeadUpdated do
end
defmodule CustomerService.Events.LeadStatusUpdated do
use CustomerService.Events
@derive Jason.Encoder
defstruct [:id, :status, :previous_status, :updated_at]
end

View File

@@ -2,6 +2,18 @@ defmodule CustomerService.Lead.Queries do
alias CustomerService.Projections.QuickLead
alias CustomerService.Repo
def list_by_org(org_id, params \\ %{}) when is_binary(org_id) do
params = Map.put(params, :org_id, org_id)
Flop.validate_and_run(QuickLead, params, for: QuickLead)
end
def get_by_org(org_id, lead_id) when is_binary(org_id) and is_binary(lead_id) do
case Repo.get_by(QuickLead, org_id: org_id, lead_id: lead_id) do
nil -> {:error, :not_found}
lead -> {:ok, lead}
end
end
def list_leads(params \\ %{}) do
Flop.validate_and_run(QuickLead, params, for: QuickLead)
end

View File

@@ -4,6 +4,8 @@ defmodule CustomerService.Projections.Customer do
@derive {Jason.Encoder,
only: [
:id,
:org_id,
:customer_id,
:customer_type,
# individual
:first_name,
@@ -27,7 +29,7 @@ defmodule CustomerService.Projections.Customer do
@derive {
Flop.Schema,
filterable: [:customer_type, :email, :phone, :document_id, :ruc, :search],
filterable: [:org_id, :customer_type, :email, :phone, :document_id, :ruc, :search],
sortable: [:last_name, :legal_name, :inserted_at],
default_limit: 20,
max_limit: 100,
@@ -40,10 +42,13 @@ defmodule CustomerService.Projections.Customer do
]
}
@primary_key {:id, :binary_id, autogenerate: false}
@primary_key {:id, :string, autogenerate: false}
@timestamps_opts [type: :utc_datetime_usec]
schema "customers" do
field :org_id, :string
field :customer_id, :string
field :customer_type, :string, default: "individual"
# individual fields

View File

@@ -4,6 +4,8 @@ defmodule CustomerService.Projections.QuickLead do
@derive {Jason.Encoder,
only: [
:id,
:org_id,
:lead_id,
:name,
:email,
:phone,
@@ -22,7 +24,7 @@ defmodule CustomerService.Projections.QuickLead do
@derive {
Flop.Schema,
filterable: [:status, :priority, :source, :assigned_to, :search],
filterable: [:org_id, :status, :priority, :source, :assigned_to, :search],
sortable: [:name, :company_name, :status, :priority, :inserted_at],
default_limit: 20,
max_limit: 100,
@@ -35,10 +37,13 @@ defmodule CustomerService.Projections.QuickLead do
]
}
@primary_key {:id, :binary_id, autogenerate: false}
@primary_key {:id, :string, autogenerate: false}
@timestamps_opts [type: :utc_datetime_usec]
schema "quick_leads" do
field :org_id, :string
field :lead_id, :string
field :name, :string
field :email, :string
field :phone, :string

View File

@@ -7,10 +7,15 @@ defmodule CustomerService.Projectors.Customer do
alias CustomerService.Events
alias CustomerService.Projections.Customer
alias CustomerService.Aggregates.CustomerId
project(%Events.CustomerCreated{} = event, fn multi ->
%CustomerService.Aggregates.CustomerId{org_id: org_id, customer_id: customer_id} = event.id
Ecto.Multi.insert(multi, :customer, %Customer{
id: event.id,
id: CustomerId.to_string(event.id),
org_id: org_id,
customer_id: customer_id,
customer_type: "individual",
first_name: event.first_name,
last_name: event.last_name,
@@ -34,8 +39,12 @@ defmodule CustomerService.Projectors.Customer do
end
project(%Events.CorporateCustomerCreated{} = e, _meta, fn multi ->
%CustomerService.Aggregates.CustomerId{org_id: org_id, customer_id: customer_id} = e.id
Ecto.Multi.insert(multi, :customer, %Customer{
id: e.id,
id: CustomerId.to_string(e.id),
org_id: org_id,
customer_id: customer_id,
customer_type: "corporate",
legal_name: e.legal_name,
commercial_name: e.commercial_name,
@@ -49,7 +58,9 @@ defmodule CustomerService.Projectors.Customer do
end)
project(%Events.CustomerUpdated{} = e, _meta, fn multi ->
Ecto.Multi.update_all(multi, :customer, from(c in Customer, where: c.id == ^e.id),
composite_id = CustomerId.to_string(e.id)
Ecto.Multi.update_all(multi, :customer, from(c in Customer, where: c.id == ^composite_id),
set: [
first_name: e.first_name,
last_name: e.last_name,
@@ -64,7 +75,9 @@ defmodule CustomerService.Projectors.Customer do
end)
project(%Events.CorporateCustomerUpdated{} = e, _meta, fn multi ->
Ecto.Multi.update_all(multi, :customer, from(c in Customer, where: c.id == ^e.id),
composite_id = CustomerId.to_string(e.id)
Ecto.Multi.update_all(multi, :customer, from(c in Customer, where: c.id == ^composite_id),
set: [
legal_name: e.legal_name,
commercial_name: e.commercial_name,

View File

@@ -7,10 +7,15 @@ defmodule CustomerService.Projectors.QuickLead do
alias CustomerService.Events
alias CustomerService.Projections.QuickLead
alias CustomerService.Aggregates.LeadId
project(%Events.QuickLeadCreated{} = event, fn multi ->
%CustomerService.Aggregates.LeadId{org_id: org_id, lead_id: lead_id} = event.id
Ecto.Multi.insert(multi, :quick_lead, %QuickLead{
id: event.id,
id: LeadId.to_string(event.id),
org_id: org_id,
lead_id: lead_id,
name: event.name,
email: event.email,
phone: event.phone,
@@ -33,7 +38,9 @@ defmodule CustomerService.Projectors.QuickLead do
end)
project(%Events.QuickLeadUpdated{} = event, _meta, fn multi ->
Ecto.Multi.update_all(multi, :quick_lead, from(q in QuickLead, where: q.id == ^event.id),
composite_id = LeadId.to_string(event.id)
Ecto.Multi.update_all(multi, :quick_lead, from(q in QuickLead, where: q.id == ^composite_id),
set: [
name: event.name,
email: event.email,
@@ -48,7 +55,9 @@ defmodule CustomerService.Projectors.QuickLead do
end)
project(%Events.LeadStatusUpdated{} = event, _meta, fn multi ->
Ecto.Multi.update_all(multi, :quick_lead, from(q in QuickLead, where: q.id == ^event.id),
composite_id = LeadId.to_string(event.id)
Ecto.Multi.update_all(multi, :quick_lead, from(q in QuickLead, where: q.id == ^composite_id),
set: [
status: to_string(event.status)
]