Files
workload-service/lib/workload_service/aggregates/task_id.ex
HaimKortovich c31970ced5
All checks were successful
Build and Publish / build-release (push) Successful in 1m28s
decode using commanded
2026-04-17 12:15:00 -05:00

53 lines
1.3 KiB
Elixir

defmodule WorkloadService.Aggregates.TaskId do
@moduledoc """
Task identifier with org_id, type and task_id.
ID format: "org_id:type:task_id" (e.g., "test:quote:uuid")
"""
@derive Jason.Encoder
defstruct [:org_id, :type, :task_id]
def new(org_id, type, task_id) when type in ["quote", "solicitation"] do
%__MODULE__{
org_id: org_id,
type: type,
task_id: task_id
}
end
def parse(string) when is_binary(string) do
case String.split(string, ":", parts: 3) do
[org_id, type, task_id] when type in ["quote", "solicitation"] ->
{:ok, new(org_id, type, task_id)}
_ ->
{:error, :invalid_task_id}
end
end
def parse!(string) do
case parse(string) do
{:ok, id} -> id
{:error, reason} -> raise ArgumentError, "invalid task id #{inspect(string)}: #{reason}"
end
end
defimpl String.Chars do
def to_string(%WorkloadService.Aggregates.TaskId{
org_id: org_id,
type: type,
task_id: task_id
}) do
"#{org_id}:#{type}:#{task_id}"
end
end
defimpl Commanded.Serialization.JsonDecoder do
def decode(%{org_id: org_id, type: type, task_id: task_id}) do
WorkloadService.Aggregates.TaskId.new(org_id, type, task_id)
end
def decode(id), do: id
end
end