82 lines
2.8 KiB
TypeScript
82 lines
2.8 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 { RelationshipRequestSchema } from "../../../common/relationships";
|
||
|
|
import type { AssetRelationshipUseCases } from "../../application/asset-relationship.use-cases";
|
||
|
|
import { buildRelationshipResponse } from "../../application/asset-relationship-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 registerRelationshipRoutes(
|
||
|
|
app: Application,
|
||
|
|
useCases: AssetRelationshipUseCases,
|
||
|
|
authenticate: RequestHandler
|
||
|
|
) {
|
||
|
|
const router = Router();
|
||
|
|
router.post("/:companyId/relationships", (request, response, next) => {
|
||
|
|
const params = InventoryParamsSchema.safeParse(request.params);
|
||
|
|
const body = RelationshipRequestSchema.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;
|
||
|
|
}
|
||
|
|
useCases
|
||
|
|
.save(params.data.companyId, body.data.values, actor.data.userId)
|
||
|
|
.then((result) => {
|
||
|
|
if (!result.isSuccess) {
|
||
|
|
fail(response, result.error.code);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
response.status(201).json({
|
||
|
|
relationship: buildRelationshipResponse(result.data.relationship, result.data.actorNames),
|
||
|
|
});
|
||
|
|
})
|
||
|
|
.catch(next);
|
||
|
|
});
|
||
|
|
router.post("/:companyId/relationships/:id/end", (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;
|
||
|
|
}
|
||
|
|
useCases
|
||
|
|
.end(params.data.companyId, params.data.id, actor.data.userId)
|
||
|
|
.then((result) => {
|
||
|
|
if (!result.isSuccess) {
|
||
|
|
fail(response, result.error.code);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
response.json({
|
||
|
|
relationship: buildRelationshipResponse(result.data.relationship, result.data.actorNames),
|
||
|
|
});
|
||
|
|
})
|
||
|
|
.catch(next);
|
||
|
|
});
|
||
|
|
app.use("/api/assets", authenticate, router);
|
||
|
|
}
|