- Added `useAssetPickerController` for managing asset selection logic. - Created `AssetSelectProps` type for defining asset selection properties. - Implemented `AssetPickerDialog` for displaying the asset selection interface. - Developed `AssetPickerList` for rendering the list of selectable assets. - Introduced `AssetSelect` component for integrating the asset picker into forms. - Created `AssetList` component for shared asset listing functionality. - Added tests for workstation creation logic to ensure proper validation. - Implemented workstation asset fields and association fields for managing workstation assets. - Developed workstation lifecycle dialog for managing work profile states. - Added form progress component for visualizing multi-step forms.
148 lines
4.2 KiB
TypeScript
148 lines
4.2 KiB
TypeScript
import type { Application, RequestHandler, Response } from "express";
|
|
import { Router } from "express";
|
|
import { z } from "zod/v4";
|
|
|
|
import {
|
|
ProfileItemParamsSchema,
|
|
ProfileParamsSchema,
|
|
SaveWorkProfileRequestSchema,
|
|
} from "../../../common/work-profiles";
|
|
import type { WorkProfileUseCases } from "../../application/work-profile.use-cases";
|
|
import { buildWorkProfileResponse } from "../../application/work-profile-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 registerWorkProfileRoutes(
|
|
app: Application,
|
|
useCases: WorkProfileUseCases,
|
|
authenticate: RequestHandler
|
|
) {
|
|
const router = Router();
|
|
|
|
router.get("/:companyId", (request, response, next) => {
|
|
const params = ProfileParamsSchema.safeParse(request.params);
|
|
|
|
if (!(params.success && z.strictObject({}).safeParse(request.query).success)) {
|
|
fail(response, "INVALID_REQUEST");
|
|
|
|
return;
|
|
}
|
|
useCases
|
|
.get(params.data.companyId)
|
|
.then((result) => {
|
|
if (!result.isSuccess) {
|
|
fail(response, result.error.code);
|
|
|
|
return;
|
|
}
|
|
response.json({
|
|
profiles: result.data.profiles.map((item) => buildWorkProfileResponse(item, result.data)),
|
|
});
|
|
})
|
|
.catch(next);
|
|
});
|
|
for (const path of ["/:companyId", "/:companyId/:id/update"]) {
|
|
router.post(path, (request, response, next) => {
|
|
const params = (
|
|
path.endsWith("update") ? ProfileItemParamsSchema : ProfileParamsSchema
|
|
).safeParse(request.params);
|
|
const body = SaveWorkProfileRequestSchema.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({
|
|
profile: buildWorkProfileResponse(result.data.profile, result.data.context),
|
|
});
|
|
})
|
|
.catch(next);
|
|
});
|
|
}
|
|
router.post("/:companyId/:id/deactivate", (request, response, next) => {
|
|
const params = ProfileItemParamsSchema.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
|
|
.deactivate(params.data.companyId, params.data.id, actor.data.userId)
|
|
.then((result) => {
|
|
if (!result.isSuccess) {
|
|
fail(response, result.error.code);
|
|
|
|
return;
|
|
}
|
|
response.json({
|
|
profile: buildWorkProfileResponse(result.data.profile, result.data.context),
|
|
});
|
|
})
|
|
.catch(next);
|
|
});
|
|
router.post("/:companyId/:id/delete", (request, response, next) => {
|
|
const params = ProfileItemParamsSchema.safeParse(request.params);
|
|
|
|
if (
|
|
!(
|
|
params.success &&
|
|
z.strictObject({}).safeParse(request.body).success &&
|
|
z.strictObject({}).safeParse(request.query).success
|
|
)
|
|
) {
|
|
fail(response, "INVALID_REQUEST");
|
|
|
|
return;
|
|
}
|
|
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);
|
|
});
|
|
app.use("/api/work-profiles", authenticate, router);
|
|
}
|