simplify task api
All checks were successful
Build and Publish / build-release (push) Successful in 1m23s

This commit is contained in:
2026-04-17 11:42:19 -05:00
parent 202538e844
commit 01ad2270bc
13 changed files with 320 additions and 301 deletions

View File

@@ -14,10 +14,8 @@ defmodule WorkloadServiceWeb.TaskController do
page: [in: :query, type: :integer, required: false, example: 1],
page_size: [in: :query, type: :integer, required: false, example: 20],
status: [in: :query, type: :string, required: false],
policy_type: [in: :query, type: :string, required: false],
org_id: [in: :query, type: :string, required: false],
application_id: [in: :query, type: :string, required: false],
provider_id: [in: :query, type: :string, required: false]
application_id: [in: :query, type: :string, required: false]
],
responses: [
ok: {"Task list", "application/json", S.TaskListResponse},
@@ -61,20 +59,35 @@ defmodule WorkloadServiceWeb.TaskController do
end
end
operation(:respond_to_quote,
summary: "Record quote response",
operation(:submit,
summary: "Submit task data (quote response or confirm delivery)",
parameters: [
id: [in: :path, type: :string, required: true]
],
request_body: {"Quote response", "application/json", S.QuoteResponseRequest, required: true},
request_body: {"Task submission", "application/json", S.SubmitRequest, required: true},
responses: [
ok: {"Quote response recorded", "application/json", S.TaskDetailResponse},
ok: {"Task submitted", "application/json", S.TaskDetailResponse},
not_found: {"Not found", "application/json", S.ErrorResponse},
unprocessable_entity: {"Error", "application/json", S.ErrorResponse}
]
)
def respond_to_quote(conn, %{"id" => id} = params) do
def submit(conn, %{"id" => id} = params) do
task_type = get_task_type(id)
case task_type do
"quote" ->
handle_quote_submit(conn, id, params)
"solicitation" ->
handle_solicitation_submit(conn, id, params)
_ ->
conn |> put_status(:unprocessable_entity) |> json(%{error: "invalid task type"})
end
end
defp handle_quote_submit(conn, id, params) do
case Queries.get_task_by_id(id) do
{:error, :not_found} ->
conn |> put_status(:not_found) |> json(%{error: "task not found"})
@@ -86,70 +99,20 @@ defmodule WorkloadServiceWeb.TaskController do
"quote_id" => params["quote_id"],
"plans" => params["plans"],
"valid_until" => params["valid_until"],
"responded_by" => params["responded_by"],
"recorded_by" => params["recorded_by"],
"document_data" => params["document_data"]
},
attachments: [params["document_url"]] |> Enum.filter(& &1)
attachments: List.wrap(params["document_url"])
}
case CommandedApp.dispatch(command) do
:ok ->
{:ok, task} = Queries.get_task_by_id(id)
conn |> put_status(:ok) |> json(%{data: task_detail(task)})
{:error, reason} ->
conn |> put_status(:unprocessable_entity) |> json(%{error: inspect(reason)})
end
{:ok, %{status: "draft"} = _task} ->
command = %WorkloadService.Commands.QuoteTask.ApproveSubmission{
id: id
}
case CommandedApp.dispatch(command) do
:ok ->
{:ok, task} = Queries.get_task_by_id(id)
conn |> put_status(:ok) |> json(%{data: task_detail(task)})
{:error, reason} ->
conn |> put_status(:unprocessable_entity) |> json(%{error: inspect(reason)})
end
{:ok, %{status: "approved"} = _task} ->
command = %WorkloadService.Commands.QuoteTask.CompleteTask{
id: id,
completed_by: params["completed_by"] || "system"
}
case CommandedApp.dispatch(command) do
:ok ->
{:ok, task} = Queries.get_task_by_id(id)
conn |> put_status(:ok) |> json(%{data: task_detail(task)})
{:error, reason} ->
conn |> put_status(:unprocessable_entity) |> json(%{error: inspect(reason)})
end
dispatch_and_respond(conn, id, command)
{:ok, _task} ->
conn |> put_status(:unprocessable_entity) |> json(%{error: "invalid state"})
conn |> put_status(:unprocessable_entity) |> json(%{error: "invalid state for submit"})
end
end
operation(:confirm_delivery,
summary: "Confirm solicitation delivery",
parameters: [
id: [in: :path, type: :string, required: true]
],
request_body:
{"Delivery confirmation", "application/json", S.ConfirmDeliveryRequest, required: false},
responses: [
ok: {"Delivery confirmed", "application/json", S.TaskDetailResponse},
not_found: {"Not found", "application/json", S.ErrorResponse},
unprocessable_entity: {"Error", "application/json", S.ErrorResponse}
]
)
def confirm_delivery(conn, %{"id" => id} = params) do
defp handle_solicitation_submit(conn, id, params) do
case Queries.get_task_by_id(id) do
{:error, :not_found} ->
conn |> put_status(:not_found) |> json(%{error: "task not found"})
@@ -158,51 +121,122 @@ defmodule WorkloadServiceWeb.TaskController do
command = %WorkloadService.Commands.SolicitationTask.SubmitResponse{
id: id,
submission: %{
"delivery_confirmed_by" => params["delivery_confirmed_by"] || "system"
"recorded_by" => params["recorded_by"] || "system"
},
attachments: []
}
case CommandedApp.dispatch(command) do
:ok ->
{:ok, task} = Queries.get_task_by_id(id)
conn |> put_status(:ok) |> json(%{data: task_detail(task)})
{:error, reason} ->
conn |> put_status(:unprocessable_entity) |> json(%{error: inspect(reason)})
end
{:ok, %{status: "draft"} = _task} ->
command = %WorkloadService.Commands.SolicitationTask.ApproveSubmission{
id: id
}
case CommandedApp.dispatch(command) do
:ok ->
{:ok, task} = Queries.get_task_by_id(id)
conn |> put_status(:ok) |> json(%{data: task_detail(task)})
{:error, reason} ->
conn |> put_status(:unprocessable_entity) |> json(%{error: inspect(reason)})
end
{:ok, %{status: "approved"} = _task} ->
command = %WorkloadService.Commands.SolicitationTask.CompleteTask{
id: id,
completed_by: params["completed_by"] || "system"
}
case CommandedApp.dispatch(command) do
:ok ->
{:ok, task} = Queries.get_task_by_id(id)
conn |> put_status(:ok) |> json(%{data: task_detail(task)})
{:error, reason} ->
conn |> put_status(:unprocessable_entity) |> json(%{error: inspect(reason)})
end
dispatch_and_respond(conn, id, command)
{:ok, _task} ->
conn |> put_status(:unprocessable_entity) |> json(%{error: "invalid state"})
conn |> put_status(:unprocessable_entity) |> json(%{error: "invalid state for submit"})
end
end
operation(:approve,
summary: "Approve submission",
parameters: [
id: [in: :path, type: :string, required: true]
],
responses: [
ok: {"Task approved", "application/json", S.TaskDetailResponse},
not_found: {"Not found", "application/json", S.ErrorResponse},
unprocessable_entity: {"Error", "application/json", S.ErrorResponse}
]
)
def approve(conn, %{"id" => id}) do
task_type = get_task_type(id)
case task_type do
"quote" ->
handle_approve(conn, id, WorkloadService.Commands.QuoteTask.ApproveSubmission)
"solicitation" ->
handle_approve(conn, id, WorkloadService.Commands.SolicitationTask.ApproveSubmission)
_ ->
conn |> put_status(:unprocessable_entity) |> json(%{error: "invalid task type"})
end
end
defp handle_approve(conn, id, command_module) do
case Queries.get_task_by_id(id) do
{:error, :not_found} ->
conn |> put_status(:not_found) |> json(%{error: "task not found"})
{:ok, %{status: "draft"} = _task} ->
command = struct(command_module, id: id)
dispatch_and_respond(conn, id, command)
{:ok, _task} ->
conn |> put_status(:unprocessable_entity) |> json(%{error: "invalid state for approve"})
end
end
operation(:complete,
summary: "Complete task",
parameters: [
id: [in: :path, type: :string, required: true]
],
request_body: {"Complete request", "application/json", S.CompleteRequest, required: false},
responses: [
ok: {"Task completed", "application/json", S.TaskDetailResponse},
not_found: {"Not found", "application/json", S.ErrorResponse},
unprocessable_entity: {"Error", "application/json", S.ErrorResponse}
]
)
def complete(conn, %{"id" => id} = params) do
task_type = get_task_type(id)
completed_by = params["completed_by"] || "system"
case task_type do
"quote" ->
handle_complete(conn, id, completed_by, WorkloadService.Commands.QuoteTask.CompleteTask)
"solicitation" ->
handle_complete(
conn,
id,
completed_by,
WorkloadService.Commands.SolicitationTask.CompleteTask
)
_ ->
conn |> put_status(:unprocessable_entity) |> json(%{error: "invalid task type"})
end
end
defp handle_complete(conn, id, completed_by, command_module) do
case Queries.get_task_by_id(id) do
{:error, :not_found} ->
conn |> put_status(:not_found) |> json(%{error: "task not found"})
{:ok, %{status: "approved"} = _task} ->
command = struct(command_module, id: id, completed_by: completed_by)
dispatch_and_respond(conn, id, command)
{:ok, _task} ->
conn |> put_status(:unprocessable_entity) |> json(%{error: "invalid state for complete"})
end
end
defp dispatch_and_respond(conn, id, command) do
case CommandedApp.dispatch(command) do
:ok ->
{:ok, task} = Queries.get_task_by_id(id)
conn |> put_status(:ok) |> json(%{data: task_detail(task)})
{:error, reason} ->
conn |> put_status(:unprocessable_entity) |> json(%{error: inspect(reason)})
end
end
defp get_task_type(id) do
case WorkloadService.Aggregates.TaskId.parse(id) do
{:ok, %{type: type}} -> type
_ -> "unknown"
end
end
@@ -211,8 +245,6 @@ defmodule WorkloadServiceWeb.TaskController do
id: t.id,
org_id: t.org_id,
application_id: t.application_id,
provider_id: t.provider_id,
provider_name: t.provider_name,
task_info: t.task_info,
status: t.status,
created_at: t.inserted_at
@@ -224,8 +256,6 @@ defmodule WorkloadServiceWeb.TaskController do
id: t.id,
org_id: t.org_id,
application_id: t.application_id,
provider_id: t.provider_id,
provider_name: t.provider_name,
task_info: t.task_info,
submission: t.submission,
attachments: t.attachments,
@@ -246,4 +276,4 @@ defmodule WorkloadServiceWeb.TaskController do
has_prev: meta.has_previous_page?
}
end
end
end