mirror of
https://github.com/Hestia-Homes/assessment-model.git
synced 2026-06-08 11:37:25 +00:00
Add categorisation API route and call it
This commit is contained in:
parent
161af31b63
commit
5fc3cb60e2
2 changed files with 77 additions and 9 deletions
67
src/app/api/plan/categorisation/route.ts
Normal file
67
src/app/api/plan/categorisation/route.ts
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
|
||||
const CategorisationBodySchema = z.object({
|
||||
portfolio_id: z.number(),
|
||||
scenarios_to_consider: z.array(z.number()).optional(),
|
||||
scenario_priority_order: z.array(z.number()).optional(),
|
||||
})
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
console.log("API hit");
|
||||
const body = await request.json();
|
||||
let validatedBody;
|
||||
|
||||
try {
|
||||
validatedBody = CategorisationBodySchema.parse(body);
|
||||
} catch (error) {
|
||||
console.error("Invalid input: ", error);
|
||||
return new NextResponse(JSON.stringify({ msg: "Invalid input" }), {
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
// This triggers the work distribution, but doesn't wait for the lambdas to complete categorisation
|
||||
// Instead we'll check the task ID before allowing user to select the new recommendations
|
||||
const headers = {
|
||||
"x-api-key": process.env.FASTAPI_API_KEY || "",
|
||||
Authorization: `Bearer ${
|
||||
request.cookies.get("__Secure-next-auth.session-token")?.value ||
|
||||
request.cookies.get("next-auth.session-token")?.value
|
||||
}`,
|
||||
"Content-Type": "application/json",
|
||||
};
|
||||
|
||||
const url = `${process.env.FASTAPI_API_URL}/v1/plan/categorisation`;
|
||||
|
||||
console.log("Request:", url, validatedBody);
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: "POST",
|
||||
headers: headers,
|
||||
body: JSON.stringify(validatedBody),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
console.error("Error triggering plan:", response.statusText);
|
||||
return new NextResponse(
|
||||
JSON.stringify({ msg: "Error triggering plan" }),
|
||||
{
|
||||
status: 500,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
return new NextResponse(JSON.stringify({ msg: "Categorisation job started" }), {
|
||||
status: 200,
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return new NextResponse(JSON.stringify({ msg: "Internal server error" }), {
|
||||
status: 500,
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -173,20 +173,21 @@ export function RecommendationsOptions({
|
|||
setIsApplying(true);
|
||||
|
||||
const payload = mapScenariosToPayload(selectedScenarios, portfolioId);
|
||||
console.log('API payload', payload);
|
||||
|
||||
console.log('API payload', JSON.stringify(payload));
|
||||
|
||||
const response = await fetch("/api/plan/categorisation", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
|
||||
await onApply(payload.scenarios_to_consider ? payload : {
|
||||
portfolio_id: 123,
|
||||
scenarios_to_consider: null,
|
||||
scenario_priority_order: null,
|
||||
});
|
||||
|
||||
// await onApply({
|
||||
// selectedScenarios:
|
||||
// selectedScenarios.length > 0 ? selectedScenarios.map(s => s.id) : null,
|
||||
// prioritisedScenarios:
|
||||
// selectedScenarios.length > 0 ? selectedScenarios.map(s => s.id) : null,
|
||||
// });
|
||||
|
||||
setIsApplying(false);
|
||||
setOpen(false);
|
||||
};
|
||||
|
|
@ -219,7 +220,7 @@ export function RecommendationsOptions({
|
|||
}
|
||||
`}
|
||||
>
|
||||
Calculate Recommendations
|
||||
Calculate Recommended
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue