Files
workload-service/lib/workload_service/aggregates/task_id.ex
HaimKortovich 2bf99bfb37
Some checks failed
Build and Publish / build-release (push) Failing after 37s
add type specs and fix id issues
2026-04-21 12:55:38 -05:00

59 lines
1.4 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")
"""
@type t :: %__MODULE__{
org_id: String.t(),
type: String.t(),
task_id: String.t()
}
@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