init commit
This commit is contained in:
48
lib/workload_service/aggregates/task_id.ex
Normal file
48
lib/workload_service/aggregates/task_id.ex
Normal file
@@ -0,0 +1,48 @@
|
||||
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(id), do: id
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user