Files
sankofa-hw-infra/apps/api/src/routes/v1/maintenances.ts
defiQUG 93df3c8c20
Some checks failed
CI / lint-and-test (push) Has been cancelled
Initial commit: add .gitignore and README
2026-02-09 21:51:50 -08:00

30 lines
1.6 KiB
TypeScript

import type { FastifyInstance } from "fastify";
import { eq, and } from "drizzle-orm";
import { maintenances as maintenancesTable } from "@sankofa/schema";
export async function maintenancesRoutes(app: FastifyInstance) {
const db = app.db;
app.get("/", async (req, reply) => {
const orgId = app.orgId(req);
const list = await db.select().from(maintenancesTable).where(eq(maintenancesTable.orgId, orgId));
return reply.send(list);
});
app.get<{ Params: { id: string } }>("/:id", async (req, reply) => {
const orgId = app.orgId(req);
const [row] = await db.select().from(maintenancesTable).where(and(eq(maintenancesTable.id, req.params.id), eq(maintenancesTable.orgId, orgId)));
if (!row) return reply.notFound();
return reply.send(row);
});
app.post<{ Body: { assetId: string; type: string; vendorTicketRef?: string; description?: string } }>("/", async (req, reply) => {
const orgId = app.orgId(req);
const [inserted] = await db.insert(maintenancesTable).values({ orgId, assetId: req.body.assetId, type: req.body.type, vendorTicketRef: req.body.vendorTicketRef ?? null, description: req.body.description ?? null }).returning();
return reply.code(201).send(inserted);
});
app.patch<{ Params: { id: string }; Body: { status?: string } }>("/:id", async (req, reply) => {
const orgId = app.orgId(req);
const [updated] = await db.update(maintenancesTable).set({ ...req.body, updatedAt: new Date() }).where(and(eq(maintenancesTable.id, req.params.id), eq(maintenancesTable.orgId, orgId))).returning();
if (!updated) return reply.notFound();
return reply.send(updated);
});
}