Files
customer-service/lib/customer_service/aggregates/customer_id.ex
HaimKortovich 4519f797fd
All checks were successful
Build and Publish / build-release (push) Successful in 3m7s
partition by org_id and add auth
2026-05-15 10:08:54 -05:00

49 lines
1.4 KiB
Elixir

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