import { Request, Response, NextFunction } from "express"; import { z } from "zod"; import { db } from "@server/db"; import { resources } from "@server/db/schema"; import { eq } from "drizzle-orm"; 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"; const transferResourceParamsSchema = z .object({ resourceId: z .string() .transform(Number) .pipe(z.number().int().positive()) }) .strict(); const transferResourceBodySchema = z .object({ siteId: z.number().int().positive() }) .strict(); export async function transferResource( req: Request, res: Response, next: NextFunction ): Promise { try { const parsedParams = transferResourceParamsSchema.safeParse(req.params); if (!parsedParams.success) { return next( createHttpError( HttpCode.BAD_REQUEST, fromError(parsedParams.error).toString() ) ); } const parsedBody = transferResourceBodySchema.safeParse(req.body); if (!parsedBody.success) { return next( createHttpError( HttpCode.BAD_REQUEST, fromError(parsedBody.error).toString() ) ); } const { resourceId } = parsedParams.data; const { siteId } = parsedBody.data; const [updatedResource] = await db .update(resources) .set({ siteId }) .where(eq(resources.resourceId, resourceId)) .returning(); if (!updatedResource) { return next( createHttpError( HttpCode.NOT_FOUND, `Resource with ID ${resourceId} not found` ) ); } return response(res, { data: updatedResource, success: true, error: false, message: "Resource transferred successfully", status: HttpCode.OK }); } catch (error) { logger.error(error); return next( createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred") ); } }