114 lines
3.5 KiB
TypeScript
114 lines
3.5 KiB
TypeScript
import type { Application, RequestHandler, Response } from "express";
|
|
import { Router } from "express";
|
|
import { z } from "zod/v4";
|
|
|
|
import { AssetParamsSchema, InventoryParamsSchema } from "../../../common/inventory";
|
|
import { SaveWorkstationRequestSchema } from "../../../common/workstations";
|
|
import type { WorkstationUseCases } from "../../application/workstation.use-cases";
|
|
import { buildWorkstationResponse } from "../../application/workstation-response.builder";
|
|
|
|
function fail(response: Response, code: string) {
|
|
response
|
|
.status(code === "INVALID_REQUEST" ? 400 : code.endsWith("NOT_FOUND") ? 404 : 409)
|
|
.json({ code, requestId: response.getHeader("x-request-id") });
|
|
}
|
|
|
|
export function registerWorkstationRoutes(
|
|
app: Application,
|
|
useCases: WorkstationUseCases,
|
|
authenticate: RequestHandler
|
|
) {
|
|
const router = Router();
|
|
|
|
for (const path of ["/:companyId/workstations", "/:companyId/workstations/:id/update"]) {
|
|
router.post(path, (request, response, next) => {
|
|
const params = (
|
|
path.endsWith("update") ? AssetParamsSchema : InventoryParamsSchema
|
|
).safeParse(request.params);
|
|
const body = SaveWorkstationRequestSchema.safeParse(request.body);
|
|
const actor = z
|
|
.object({ userId: z.uuid() })
|
|
.safeParse("session" in request ? request.session : undefined);
|
|
|
|
if (
|
|
!(
|
|
params.success &&
|
|
body.success &&
|
|
actor.success &&
|
|
z.strictObject({}).safeParse(request.query).success
|
|
)
|
|
) {
|
|
fail(response, "INVALID_REQUEST");
|
|
|
|
return;
|
|
}
|
|
const id =
|
|
"id" in params.data && typeof params.data.id === "string" ? params.data.id : undefined;
|
|
|
|
useCases
|
|
.save(params.data.companyId, id, body.data.values, actor.data.userId)
|
|
.then((result) => {
|
|
if (!result.isSuccess) {
|
|
fail(response, result.error.code);
|
|
|
|
return;
|
|
}
|
|
response.status(id ? 200 : 201).json({
|
|
workstation: buildWorkstationResponse(result.data.workstation, result.data.actorNames),
|
|
});
|
|
})
|
|
.catch(next);
|
|
});
|
|
}
|
|
for (const action of ["decommission", "delete"] as const) {
|
|
router.post(`/:companyId/workstations/:id/${action}`, (request, response, next) => {
|
|
const params = AssetParamsSchema.safeParse(request.params);
|
|
const actor = z
|
|
.object({ userId: z.uuid() })
|
|
.safeParse("session" in request ? request.session : undefined);
|
|
|
|
if (
|
|
!(
|
|
params.success &&
|
|
actor.success &&
|
|
z.strictObject({}).safeParse(request.body).success &&
|
|
z.strictObject({}).safeParse(request.query).success
|
|
)
|
|
) {
|
|
fail(response, "INVALID_REQUEST");
|
|
|
|
return;
|
|
}
|
|
if (action === "delete") {
|
|
useCases
|
|
.remove(params.data.companyId, params.data.id)
|
|
.then((result) => {
|
|
if (!result.isSuccess) {
|
|
fail(response, result.error.code);
|
|
|
|
return;
|
|
}
|
|
response.json({ deleted: true });
|
|
})
|
|
.catch(next);
|
|
|
|
return;
|
|
}
|
|
useCases
|
|
.decommission(params.data.companyId, params.data.id, actor.data.userId)
|
|
.then((result) => {
|
|
if (!result.isSuccess) {
|
|
fail(response, result.error.code);
|
|
|
|
return;
|
|
}
|
|
response.json({
|
|
workstation: buildWorkstationResponse(result.data.workstation, result.data.actorNames),
|
|
});
|
|
})
|
|
.catch(next);
|
|
});
|
|
}
|
|
app.use("/api/assets", authenticate, router);
|
|
}
|