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

View File

@@ -19,8 +19,9 @@ defmodule WorkloadServiceWeb.Router do
scope "/v1" do
get "/tasks", TaskController, :list
get "/tasks/:id", TaskController, :show
post "/tasks/:id/respond-quote", TaskController, :respond_to_quote
post "/tasks/:id/confirm-delivery", TaskController, :confirm_delivery
post "/tasks/:id/submit", TaskController, :submit
post "/tasks/:id/approve", TaskController, :approve
post "/tasks/:id/complete", TaskController, :complete
end
end
@@ -29,4 +30,4 @@ defmodule WorkloadServiceWeb.Router do
get "/", OpenApiSpex.Plug.SwaggerUI, path: "/api/openapi"
end
end
end
end

View File

@@ -26,11 +26,40 @@ defmodule WorkloadServiceWeb.Schemas.Task do
type: :object,
properties: %{
plan_id: %Schema{type: :string},
plan_name: %Schema{type: :string},
name: %Schema{type: :string},
premium: %Schema{type: :number},
coverage_details: %Schema{type: :string},
deductible: %Schema{type: :number, nullable: true},
coverage_limit: %Schema{type: :number, nullable: true}
coverage_details: %Schema{type: :object, additionalProperties: true}
}
})
end
defmodule QuoteSubmission do
require OpenApiSpex
OpenApiSpex.schema(%{
title: "QuoteSubmission",
type: :object,
required: [:recorded_by, :quote_id],
properties: %{
recorded_by: %Schema{type: :string},
quote_id: %Schema{type: :string},
valid_until: %Schema{type: :string, format: :date},
plans: %Schema{type: :array, items: Plan},
document_url: %Schema{type: :string},
document_data: %Schema{type: :object, additionalProperties: true}
}
})
end
defmodule SolicitationSubmission do
require OpenApiSpex
OpenApiSpex.schema(%{
title: "SolicitationSubmission",
type: :object,
required: [:recorded_by],
properties: %{
recorded_by: %Schema{type: :string}
}
})
end
@@ -45,8 +74,6 @@ defmodule WorkloadServiceWeb.Schemas.Task do
id: %Schema{type: :string},
org_id: %Schema{type: :string},
application_id: %Schema{type: :string},
provider_id: %Schema{type: :string},
provider_name: %Schema{type: :string},
task_info: %Schema{type: :object},
status: %Schema{type: :string, enum: ["created", "draft", "approved", "completed"]},
created_at: %Schema{type: :string, format: :"date-time"}
@@ -64,8 +91,6 @@ defmodule WorkloadServiceWeb.Schemas.Task do
id: %Schema{type: :string},
org_id: %Schema{type: :string},
application_id: %Schema{type: :string},
provider_id: %Schema{type: :string},
provider_name: %Schema{type: :string},
task_info: %Schema{type: :object},
submission: %Schema{type: :object, nullable: true},
attachments: %Schema{type: :array, items: %Schema{type: :string}},
@@ -77,32 +102,24 @@ defmodule WorkloadServiceWeb.Schemas.Task do
})
end
defmodule QuoteResponseRequest do
defmodule SubmitRequest do
require OpenApiSpex
OpenApiSpex.schema(%{
title: "QuoteResponseRequest",
title: "SubmitRequest",
type: :object,
required: [:quote_id, :document_url],
properties: %{
quote_id: %Schema{type: :string},
plans: %Schema{type: :array, items: Plan},
valid_until: %Schema{type: :string, format: :date},
responded_by: %Schema{type: :string},
document_url: %Schema{type: :string},
document_data: %Schema{type: :object, additionalProperties: true}
}
anyOf: [QuoteSubmission, SolicitationSubmission]
})
end
defmodule ConfirmDeliveryRequest do
defmodule CompleteRequest do
require OpenApiSpex
OpenApiSpex.schema(%{
title: "ConfirmDeliveryRequest",
title: "CompleteRequest",
type: :object,
properties: %{
delivery_confirmed_by: %Schema{type: :string}
completed_by: %Schema{type: :string}
}
})
end
@@ -143,4 +160,4 @@ defmodule WorkloadServiceWeb.Schemas.Task do
}
})
end
end
end