Skip to content

Commit ee51e2e

Browse files
committed
fix: kpi results deletion
1 parent 027e86f commit ee51e2e

3 files changed

Lines changed: 65 additions & 66 deletions

File tree

src/pages/api/v1/kpiresults.ts

Lines changed: 0 additions & 66 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import type { APIRoute } from "astro";
2+
import ApiResponse from "../../../../types/ApiResponse";
3+
import { KpiResultsService } from "../../../../bff/services/kpiresults.service";
4+
import type { IKpiResultInput } from "../../../../types";
5+
6+
const service = new KpiResultsService();
7+
8+
// DELETE /api/v1/kpiresults/:id
9+
export const DELETE: APIRoute = async ({ params }) => {
10+
try {
11+
const id = params.id;
12+
13+
if (!id || id === "") {
14+
return new ApiResponse({
15+
status: 400,
16+
error:
17+
"Provide id OR kpidefinition_id + living_lab_id + date (transport_mode_id optional)",
18+
});
19+
}
20+
21+
const deleted = await service.deleteKpiResult(id);
22+
if (!deleted) {
23+
return new ApiResponse({ status: 404, error: "KPI result not found" });
24+
}
25+
return ApiResponse.noContent();
26+
} catch (err) {
27+
console.error("Error in DELETE /api/v1/kpiresults:", err);
28+
return new ApiResponse({ error: "Internal Server Error", status: 500 });
29+
}
30+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import type { APIRoute } from "astro";
2+
import ApiResponse from "../../../../types/ApiResponse";
3+
import { KpiResultsService } from "../../../../bff/services/kpiresults.service";
4+
import type { IKpiResultInput } from "../../../../types";
5+
6+
const service = new KpiResultsService();
7+
8+
// PUT /api/v1/kpiresults
9+
// Body: { id? , kpidefinition_id, living_lab_id, value, date, transport_mode_id? }
10+
export const PUT: APIRoute = async ({ request }) => {
11+
try {
12+
const body = (await request.json()) as IKpiResultInput;
13+
const {
14+
id,
15+
kpidefinition_id,
16+
living_lab_id,
17+
value,
18+
date,
19+
transport_mode_id,
20+
} = body || {};
21+
22+
const result = await service.upsertKpiResult({
23+
id,
24+
kpidefinition_id,
25+
living_lab_id,
26+
value,
27+
date,
28+
transport_mode_id,
29+
});
30+
return new ApiResponse({ data: result });
31+
} catch (err) {
32+
console.error("Error in PUT /api/v1/kpiresults:", err);
33+
return new ApiResponse({ error: "Internal Server Error", status: 500 });
34+
}
35+
};

0 commit comments

Comments
 (0)