Files
HaimKortovich ac114869be
Some checks failed
Build and Publish / build-release (push) Failing after 24s
impl string.chars
2026-05-15 13:29:03 -05:00

50 lines
1.2 KiB
Elixir

defmodule CustomerService.Aggregates.LeadId do
@type t :: %__MODULE__{
org_id: String.t(),
type: String.t(),
lead_id: String.t()
}
@derive Jason.Encoder
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
defimpl String.Chars do
def to_string(%CustomerService.Aggregates.LeadId{
org_id: org_id,
type: type,
lead_id: lead_id
}) do
org_id <> ":" <> type <> ":" <> lead_id
end
end
end