init commit
This commit is contained in:
20
lib/workload_service_web/api_spec.ex
Normal file
20
lib/workload_service_web/api_spec.ex
Normal file
@@ -0,0 +1,20 @@
|
||||
defmodule WorkloadServiceWeb.ApiSpec do
|
||||
alias OpenApiSpex.{Info, OpenApi, Server}
|
||||
alias WorkloadServiceWeb.Endpoint
|
||||
@behaviour OpenApi
|
||||
|
||||
@impl OpenApi
|
||||
def spec do
|
||||
%OpenApi{
|
||||
servers: [
|
||||
Server.from_endpoint(Endpoint)
|
||||
],
|
||||
info: %Info{
|
||||
title: "Workload Service",
|
||||
version: "1.0"
|
||||
},
|
||||
paths: OpenApiSpex.Paths.from_router(WorkloadServiceWeb.Router)
|
||||
}
|
||||
|> OpenApiSpex.resolve_schema_modules()
|
||||
end
|
||||
end
|
||||
3
lib/workload_service_web/controllers.ex
Normal file
3
lib/workload_service_web/controllers.ex
Normal file
@@ -0,0 +1,3 @@
|
||||
defmodule WorkloadServiceWeb.Controllers do
|
||||
@moduledoc false
|
||||
end
|
||||
28
lib/workload_service_web/controllers/fallback_controller.ex
Normal file
28
lib/workload_service_web/controllers/fallback_controller.ex
Normal file
@@ -0,0 +1,28 @@
|
||||
defmodule WorkloadServiceWeb.FallbackController do
|
||||
use WorkloadServiceWeb, :controller
|
||||
|
||||
def call(conn, {:error, :not_found}) do
|
||||
conn
|
||||
|> put_status(:not_found)
|
||||
|> put_view(json: WorkloadServiceWeb.ErrorJSON)
|
||||
|> render(:"404")
|
||||
end
|
||||
|
||||
def call(conn, {:error, reason}) when is_atom(reason) do
|
||||
conn
|
||||
|> put_status(:unprocessable_entity)
|
||||
|> json(%{error: Atom.to_string(reason)})
|
||||
end
|
||||
|
||||
def call(conn, {:error, %{errors: errors}}) do
|
||||
conn
|
||||
|> put_status(:unprocessable_entity)
|
||||
|> json(%{errors: errors})
|
||||
end
|
||||
|
||||
def call(conn, {:error, reason}) do
|
||||
conn
|
||||
|> put_status(:unprocessable_entity)
|
||||
|> json(%{error: inspect(reason)})
|
||||
end
|
||||
end
|
||||
15
lib/workload_service_web/controllers/health_controller.ex
Normal file
15
lib/workload_service_web/controllers/health_controller.ex
Normal file
@@ -0,0 +1,15 @@
|
||||
defmodule WorkloadServiceWeb.HealthController do
|
||||
use WorkloadServiceWeb, :controller
|
||||
|
||||
def health(conn, _params) do
|
||||
conn
|
||||
|> put_status(:ok)
|
||||
|> json(%{status: "ok"})
|
||||
end
|
||||
|
||||
def ready(conn, _params) do
|
||||
conn
|
||||
|> put_status(:ok)
|
||||
|> json(%{status: "ready"})
|
||||
end
|
||||
end
|
||||
249
lib/workload_service_web/controllers/task_controller.ex
Normal file
249
lib/workload_service_web/controllers/task_controller.ex
Normal file
@@ -0,0 +1,249 @@
|
||||
defmodule WorkloadServiceWeb.TaskController do
|
||||
use WorkloadServiceWeb, :controller
|
||||
use OpenApiSpex.ControllerSpecs
|
||||
|
||||
alias WorkloadService.CommandedApp
|
||||
alias WorkloadService.Workload.Queries
|
||||
alias WorkloadServiceWeb.Schemas.Task, as: S
|
||||
|
||||
tags(["Tasks"])
|
||||
|
||||
operation(:list,
|
||||
summary: "List tasks",
|
||||
parameters: [
|
||||
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]
|
||||
],
|
||||
responses: [
|
||||
ok: {"Task list", "application/json", S.TaskListResponse},
|
||||
bad_request: {"Invalid params", "application/json", S.ErrorResponse}
|
||||
]
|
||||
)
|
||||
|
||||
def list(conn, params) do
|
||||
case Queries.list_tasks(params) do
|
||||
{:ok, {tasks, meta}} ->
|
||||
conn
|
||||
|> put_status(:ok)
|
||||
|> json(%{
|
||||
data: Enum.map(tasks, &task_summary/1),
|
||||
meta: meta_json(meta)
|
||||
})
|
||||
|
||||
{:error, _} ->
|
||||
conn |> put_status(:bad_request) |> json(%{error: "invalid parameters"})
|
||||
end
|
||||
end
|
||||
|
||||
operation(:show,
|
||||
summary: "Get task by ID",
|
||||
parameters: [
|
||||
id: [in: :path, type: :string, required: true]
|
||||
],
|
||||
responses: [
|
||||
ok: {"Task detail", "application/json", S.TaskDetailResponse},
|
||||
not_found: {"Not found", "application/json", S.ErrorResponse}
|
||||
]
|
||||
)
|
||||
|
||||
def show(conn, %{"id" => id}) do
|
||||
case Queries.get_task_by_id(id) do
|
||||
{:error, :not_found} ->
|
||||
conn |> put_status(:not_found) |> json(%{error: "task not found"})
|
||||
|
||||
{:ok, task} ->
|
||||
conn |> put_status(:ok) |> json(%{data: task_detail(task)})
|
||||
end
|
||||
end
|
||||
|
||||
operation(:respond_to_quote,
|
||||
summary: "Record quote response",
|
||||
parameters: [
|
||||
id: [in: :path, type: :string, required: true]
|
||||
],
|
||||
request_body: {"Quote response", "application/json", S.QuoteResponseRequest, required: true},
|
||||
responses: [
|
||||
ok: {"Quote response recorded", "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
|
||||
case Queries.get_task_by_id(id) do
|
||||
{:error, :not_found} ->
|
||||
conn |> put_status(:not_found) |> json(%{error: "task not found"})
|
||||
|
||||
{:ok, %{status: "created"} = _task} ->
|
||||
command = %WorkloadService.Commands.QuoteTask.SubmitResponse{
|
||||
id: id,
|
||||
submission: %{
|
||||
"quote_id" => params["quote_id"],
|
||||
"plans" => params["plans"],
|
||||
"valid_until" => params["valid_until"],
|
||||
"responded_by" => params["responded_by"],
|
||||
"document_data" => params["document_data"]
|
||||
},
|
||||
attachments: [params["document_url"]] |> Enum.filter(& &1)
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
{:ok, _task} ->
|
||||
conn |> put_status(:unprocessable_entity) |> json(%{error: "invalid state"})
|
||||
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
|
||||
case Queries.get_task_by_id(id) do
|
||||
{:error, :not_found} ->
|
||||
conn |> put_status(:not_found) |> json(%{error: "task not found"})
|
||||
|
||||
{:ok, %{status: "created"} = _task} ->
|
||||
command = %WorkloadService.Commands.SolicitationTask.SubmitResponse{
|
||||
id: id,
|
||||
submission: %{
|
||||
"delivery_confirmed_by" => params["delivery_confirmed_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
|
||||
|
||||
{:ok, _task} ->
|
||||
conn |> put_status(:unprocessable_entity) |> json(%{error: "invalid state"})
|
||||
end
|
||||
end
|
||||
|
||||
defp task_summary(t) 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
|
||||
}
|
||||
end
|
||||
|
||||
defp task_detail(t) 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,
|
||||
status: t.status,
|
||||
version: t.version,
|
||||
created_at: t.inserted_at,
|
||||
updated_at: t.updated_at
|
||||
}
|
||||
end
|
||||
|
||||
defp meta_json(meta) do
|
||||
%{
|
||||
total_count: meta.total_count,
|
||||
total_pages: meta.total_pages,
|
||||
current_page: meta.current_page,
|
||||
page_size: meta.page_size,
|
||||
has_next: meta.has_next_page?,
|
||||
has_prev: meta.has_previous_page?
|
||||
}
|
||||
end
|
||||
end
|
||||
33
lib/workload_service_web/endpoint.ex
Normal file
33
lib/workload_service_web/endpoint.ex
Normal file
@@ -0,0 +1,33 @@
|
||||
defmodule WorkloadServiceWeb.Endpoint do
|
||||
use Phoenix.Endpoint, otp_app: :workload_service
|
||||
|
||||
@session_options [
|
||||
store: :cookie,
|
||||
key: "_workload_service_key",
|
||||
signing_salt: "workload_salt",
|
||||
same_site: "Lax"
|
||||
]
|
||||
|
||||
plug Plug.Static,
|
||||
at: "/",
|
||||
from: :workload_service,
|
||||
gzip: false,
|
||||
only: WorkloadServiceWeb.static_paths()
|
||||
|
||||
if code_reloading? do
|
||||
plug Phoenix.CodeReloader
|
||||
end
|
||||
|
||||
plug Plug.RequestId
|
||||
plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]
|
||||
|
||||
plug Plug.Parsers,
|
||||
parsers: [:urlencoded, :multipart, :json],
|
||||
pass: ["*/*"],
|
||||
json_decoder: Phoenix.json_library()
|
||||
|
||||
plug Plug.MethodOverride
|
||||
plug Plug.Head
|
||||
plug Plug.Session, @session_options
|
||||
plug WorkloadServiceWeb.Router
|
||||
end
|
||||
5
lib/workload_service_web/error_json.ex
Normal file
5
lib/workload_service_web/error_json.ex
Normal file
@@ -0,0 +1,5 @@
|
||||
defmodule WorkloadServiceWeb.ErrorJSON do
|
||||
def render(template, _assigns) do
|
||||
%{errors: %{detail: Phoenix.Controller.status_message_from_template(template)}}
|
||||
end
|
||||
end
|
||||
32
lib/workload_service_web/router.ex
Normal file
32
lib/workload_service_web/router.ex
Normal file
@@ -0,0 +1,32 @@
|
||||
defmodule WorkloadServiceWeb.Router do
|
||||
use WorkloadServiceWeb, :router
|
||||
|
||||
alias WorkloadServiceWeb.TaskController
|
||||
alias WorkloadServiceWeb.HealthController
|
||||
|
||||
pipeline :api do
|
||||
plug OpenApiSpex.Plug.PutApiSpec, module: WorkloadServiceWeb.ApiSpec
|
||||
end
|
||||
|
||||
scope "/api" do
|
||||
pipe_through [:api]
|
||||
|
||||
get "/health", HealthController, :health
|
||||
get "/health/ready", HealthController, :ready
|
||||
|
||||
get "/openapi", OpenApiSpex.Plug.RenderSpec, []
|
||||
|
||||
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
|
||||
end
|
||||
end
|
||||
|
||||
if Mix.env() == :dev do
|
||||
scope "/swaggerui" do
|
||||
get "/", OpenApiSpex.Plug.SwaggerUI, path: "/api/openapi"
|
||||
end
|
||||
end
|
||||
end
|
||||
146
lib/workload_service_web/schemas/task.ex
Normal file
146
lib/workload_service_web/schemas/task.ex
Normal file
@@ -0,0 +1,146 @@
|
||||
defmodule WorkloadServiceWeb.Schemas.Task do
|
||||
alias OpenApiSpex.Schema
|
||||
|
||||
defmodule PaginationMeta do
|
||||
require OpenApiSpex
|
||||
|
||||
OpenApiSpex.schema(%{
|
||||
title: "PaginationMeta",
|
||||
type: :object,
|
||||
properties: %{
|
||||
total_count: %Schema{type: :integer},
|
||||
total_pages: %Schema{type: :integer},
|
||||
current_page: %Schema{type: :integer},
|
||||
page_size: %Schema{type: :integer},
|
||||
has_next: %Schema{type: :boolean},
|
||||
has_prev: %Schema{type: :boolean}
|
||||
}
|
||||
})
|
||||
end
|
||||
|
||||
defmodule Plan do
|
||||
require OpenApiSpex
|
||||
|
||||
OpenApiSpex.schema(%{
|
||||
title: "Plan",
|
||||
type: :object,
|
||||
properties: %{
|
||||
plan_id: %Schema{type: :string},
|
||||
plan_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}
|
||||
}
|
||||
})
|
||||
end
|
||||
|
||||
defmodule TaskSummary do
|
||||
require OpenApiSpex
|
||||
|
||||
OpenApiSpex.schema(%{
|
||||
title: "TaskSummary",
|
||||
type: :object,
|
||||
properties: %{
|
||||
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"}
|
||||
}
|
||||
})
|
||||
end
|
||||
|
||||
defmodule TaskDetail do
|
||||
require OpenApiSpex
|
||||
|
||||
OpenApiSpex.schema(%{
|
||||
title: "TaskDetail",
|
||||
type: :object,
|
||||
properties: %{
|
||||
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}},
|
||||
status: %Schema{type: :string, enum: ["created", "draft", "approved", "completed"]},
|
||||
version: %Schema{type: :integer},
|
||||
created_at: %Schema{type: :string, format: :"date-time"},
|
||||
updated_at: %Schema{type: :string, format: :"date-time"}
|
||||
}
|
||||
})
|
||||
end
|
||||
|
||||
defmodule QuoteResponseRequest do
|
||||
require OpenApiSpex
|
||||
|
||||
OpenApiSpex.schema(%{
|
||||
title: "QuoteResponseRequest",
|
||||
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}
|
||||
}
|
||||
})
|
||||
end
|
||||
|
||||
defmodule ConfirmDeliveryRequest do
|
||||
require OpenApiSpex
|
||||
|
||||
OpenApiSpex.schema(%{
|
||||
title: "ConfirmDeliveryRequest",
|
||||
type: :object,
|
||||
properties: %{
|
||||
delivery_confirmed_by: %Schema{type: :string}
|
||||
}
|
||||
})
|
||||
end
|
||||
|
||||
defmodule TaskListResponse do
|
||||
require OpenApiSpex
|
||||
|
||||
OpenApiSpex.schema(%{
|
||||
title: "TaskListResponse",
|
||||
type: :object,
|
||||
properties: %{
|
||||
data: %Schema{type: :array, items: TaskSummary},
|
||||
meta: PaginationMeta
|
||||
}
|
||||
})
|
||||
end
|
||||
|
||||
defmodule TaskDetailResponse do
|
||||
require OpenApiSpex
|
||||
|
||||
OpenApiSpex.schema(%{
|
||||
title: "TaskDetailResponse",
|
||||
type: :object,
|
||||
properties: %{
|
||||
data: TaskDetail
|
||||
}
|
||||
})
|
||||
end
|
||||
|
||||
defmodule ErrorResponse do
|
||||
require OpenApiSpex
|
||||
|
||||
OpenApiSpex.schema(%{
|
||||
title: "ErrorResponse",
|
||||
type: :object,
|
||||
properties: %{
|
||||
error: %Schema{type: :string}
|
||||
}
|
||||
})
|
||||
end
|
||||
end
|
||||
51
lib/workload_service_web/telemetry.ex
Normal file
51
lib/workload_service_web/telemetry.ex
Normal file
@@ -0,0 +1,51 @@
|
||||
defmodule WorkloadServiceWeb.Telemetry do
|
||||
use Supervisor
|
||||
import Telemetry.Metrics
|
||||
|
||||
def start_link(arg) do
|
||||
Supervisor.start_link(__MODULE__, arg, name: __MODULE__)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def init(_arg) do
|
||||
children = [
|
||||
{:telemetry_poller, measurements: periodic_measurements(), period: 10_000}
|
||||
]
|
||||
|
||||
Supervisor.init(children, strategy: :one_for_one)
|
||||
end
|
||||
|
||||
def metrics do
|
||||
[
|
||||
summary("phoenix.endpoint.start.system_time",
|
||||
unit: {:native, :millisecond}
|
||||
),
|
||||
summary("phoenix.endpoint.stop.duration",
|
||||
unit: {:native, :millisecond}
|
||||
),
|
||||
summary("phoenix.router_dispatch.start.system_time",
|
||||
tags: [:route],
|
||||
unit: {:native, :millisecond}
|
||||
),
|
||||
summary("phoenix.router_dispatch.exception.duration",
|
||||
tags: [:route],
|
||||
unit: {:native, :millisecond}
|
||||
),
|
||||
summary("phoenix.router_dispatch.stop.duration",
|
||||
tags: [:route],
|
||||
unit: {:native, :millisecond}
|
||||
),
|
||||
summary("workload_service.repo.query.total_time",
|
||||
unit: {:native, :millisecond}
|
||||
),
|
||||
summary("vm.memory.total", unit: {:byte, :kilobyte}),
|
||||
summary("vm.total_run_queue_lengths.total"),
|
||||
summary("vm.total_run_queue_lengths.cpu"),
|
||||
summary("vm.total_run_queue_lengths.io")
|
||||
]
|
||||
end
|
||||
|
||||
defp periodic_measurements do
|
||||
[]
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user