feat: add situation graph update API route
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
import { updateCase } from "@/lib/graph/orchestrator.js";
|
||||
|
||||
function mapFailureStatus(result) {
|
||||
switch (result?.stage) {
|
||||
case "request_validation":
|
||||
case "graph_validation":
|
||||
return 400;
|
||||
case "provider":
|
||||
return 502;
|
||||
case "proposal_validation":
|
||||
case "proposal_compatibility":
|
||||
case "application":
|
||||
return 422;
|
||||
case "result_validation":
|
||||
return 500;
|
||||
default:
|
||||
return 500;
|
||||
}
|
||||
}
|
||||
|
||||
function buildFailureResponse(result) {
|
||||
return {
|
||||
success: false,
|
||||
stage: result?.stage ?? "internal",
|
||||
error: result?.error ?? "Update case failed",
|
||||
validationErrors: result?.validationErrors,
|
||||
graphValidationErrors: result?.graphValidationErrors,
|
||||
proposalErrors: result?.proposalErrors,
|
||||
providerErrors: result?.providerErrors,
|
||||
errors: result?.errors,
|
||||
diagnostics: result?.diagnostics,
|
||||
};
|
||||
}
|
||||
|
||||
export async function POST(request) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const result = await updateCase(body, { applyProposal: true });
|
||||
|
||||
if (result.success) {
|
||||
return Response.json(result, { status: 200 });
|
||||
}
|
||||
|
||||
return Response.json(buildFailureResponse(result), {
|
||||
status: mapFailureStatus(result),
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof SyntaxError) {
|
||||
return Response.json(
|
||||
{
|
||||
success: false,
|
||||
stage: "request_validation",
|
||||
error: "Invalid JSON request body",
|
||||
},
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
return Response.json(
|
||||
{
|
||||
success: false,
|
||||
stage: "internal",
|
||||
error: "Internal server error",
|
||||
},
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user