mirror of
https://github.com/fosrl/pangolin.git
synced 2026-01-29 06:10:47 +00:00
159 lines
4.6 KiB
TypeScript
159 lines
4.6 KiB
TypeScript
import { Request, Response, NextFunction } from "express";
|
|
import { z } from "zod";
|
|
import { db, siteResources, clients, clientSiteResources } from "@server/db";
|
|
import response from "@server/lib/response";
|
|
import HttpCode from "@server/types/HttpCode";
|
|
import createHttpError from "http-errors";
|
|
import logger from "@server/logger";
|
|
import { fromError } from "zod-validation-error";
|
|
import { eq, and } from "drizzle-orm";
|
|
import { OpenAPITags, registry } from "@server/openApi";
|
|
import { rebuildClientAssociationsFromSiteResource } from "@server/lib/rebuildClientAssociations";
|
|
|
|
const addClientToSiteResourceBodySchema = z
|
|
.object({
|
|
clientId: z.number().int().positive()
|
|
})
|
|
.strict();
|
|
|
|
const addClientToSiteResourceParamsSchema = z
|
|
.object({
|
|
siteResourceId: z
|
|
.string()
|
|
.transform(Number)
|
|
.pipe(z.number().int().positive())
|
|
})
|
|
.strict();
|
|
|
|
registry.registerPath({
|
|
method: "post",
|
|
path: "/site-resource/{siteResourceId}/clients/add",
|
|
description:
|
|
"Add a single client to a site resource. Clients with a userId cannot be added.",
|
|
tags: [OpenAPITags.Resource, OpenAPITags.Client],
|
|
request: {
|
|
params: addClientToSiteResourceParamsSchema,
|
|
body: {
|
|
content: {
|
|
"application/json": {
|
|
schema: addClientToSiteResourceBodySchema
|
|
}
|
|
}
|
|
}
|
|
},
|
|
responses: {}
|
|
});
|
|
|
|
export async function addClientToSiteResource(
|
|
req: Request,
|
|
res: Response,
|
|
next: NextFunction
|
|
): Promise<any> {
|
|
try {
|
|
const parsedBody = addClientToSiteResourceBodySchema.safeParse(
|
|
req.body
|
|
);
|
|
if (!parsedBody.success) {
|
|
return next(
|
|
createHttpError(
|
|
HttpCode.BAD_REQUEST,
|
|
fromError(parsedBody.error).toString()
|
|
)
|
|
);
|
|
}
|
|
|
|
const { clientId } = parsedBody.data;
|
|
|
|
const parsedParams = addClientToSiteResourceParamsSchema.safeParse(
|
|
req.params
|
|
);
|
|
if (!parsedParams.success) {
|
|
return next(
|
|
createHttpError(
|
|
HttpCode.BAD_REQUEST,
|
|
fromError(parsedParams.error).toString()
|
|
)
|
|
);
|
|
}
|
|
|
|
const { siteResourceId } = parsedParams.data;
|
|
|
|
// get the site resource
|
|
const [siteResource] = await db
|
|
.select()
|
|
.from(siteResources)
|
|
.where(eq(siteResources.siteResourceId, siteResourceId))
|
|
.limit(1);
|
|
|
|
if (!siteResource) {
|
|
return next(
|
|
createHttpError(HttpCode.NOT_FOUND, "Site resource not found")
|
|
);
|
|
}
|
|
|
|
// Check if client exists and has a userId
|
|
const [client] = await db
|
|
.select()
|
|
.from(clients)
|
|
.where(eq(clients.clientId, clientId))
|
|
.limit(1);
|
|
|
|
if (!client) {
|
|
return next(
|
|
createHttpError(HttpCode.NOT_FOUND, "Client not found")
|
|
);
|
|
}
|
|
|
|
if (client.userId !== null) {
|
|
return next(
|
|
createHttpError(
|
|
HttpCode.BAD_REQUEST,
|
|
"Cannot add clients that are associated with a user"
|
|
)
|
|
);
|
|
}
|
|
|
|
// Check if client already exists in site resource
|
|
const existingEntry = await db
|
|
.select()
|
|
.from(clientSiteResources)
|
|
.where(
|
|
and(
|
|
eq(clientSiteResources.siteResourceId, siteResourceId),
|
|
eq(clientSiteResources.clientId, clientId)
|
|
)
|
|
);
|
|
|
|
if (existingEntry.length > 0) {
|
|
return next(
|
|
createHttpError(
|
|
HttpCode.CONFLICT,
|
|
"Client already assigned to site resource"
|
|
)
|
|
);
|
|
}
|
|
|
|
await db.transaction(async (trx) => {
|
|
await trx.insert(clientSiteResources).values({
|
|
clientId,
|
|
siteResourceId
|
|
});
|
|
|
|
await rebuildClientAssociationsFromSiteResource(siteResource, trx);
|
|
});
|
|
|
|
return response(res, {
|
|
data: {},
|
|
success: true,
|
|
error: false,
|
|
message: "Client added to site resource successfully",
|
|
status: HttpCode.CREATED
|
|
});
|
|
} catch (error) {
|
|
logger.error(error);
|
|
return next(
|
|
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
|
);
|
|
}
|
|
}
|