2024-11-03 13:57:51 -05:00
|
|
|
import { Request, Response, NextFunction } from "express";
|
|
|
|
import { z } from "zod";
|
|
|
|
import { db } from "@server/db";
|
2024-11-16 22:41:43 -05:00
|
|
|
import { orgs, resources, sites } from "@server/db/schema";
|
|
|
|
import { eq, or } from "drizzle-orm";
|
2025-01-01 21:41:31 -05:00
|
|
|
import response from "@server/lib/response";
|
2024-11-03 13:57:51 -05:00
|
|
|
import HttpCode from "@server/types/HttpCode";
|
|
|
|
import createHttpError from "http-errors";
|
|
|
|
import logger from "@server/logger";
|
|
|
|
import { fromError } from "zod-validation-error";
|
2024-11-15 18:25:27 -05:00
|
|
|
import { subdomainSchema } from "@server/schemas/subdomainSchema";
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-12-21 21:01:12 -05:00
|
|
|
const updateResourceParamsSchema = z
|
|
|
|
.object({
|
|
|
|
resourceId: z
|
|
|
|
.string()
|
|
|
|
.transform(Number)
|
|
|
|
.pipe(z.number().int().positive())
|
|
|
|
})
|
|
|
|
.strict();
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-11-03 13:57:51 -05:00
|
|
|
const updateResourceBodySchema = z
|
|
|
|
.object({
|
|
|
|
name: z.string().min(1).max(255).optional(),
|
2024-11-15 18:25:27 -05:00
|
|
|
subdomain: subdomainSchema.optional(),
|
2024-11-13 20:08:05 -05:00
|
|
|
ssl: z.boolean().optional(),
|
2024-11-17 22:44:11 -05:00
|
|
|
sso: z.boolean().optional(),
|
|
|
|
blockAccess: z.boolean().optional(),
|
2024-12-21 21:01:12 -05:00
|
|
|
emailWhitelistEnabled: z.boolean().optional()
|
2024-11-14 00:00:17 -05:00
|
|
|
// siteId: z.number(),
|
2024-11-03 13:57:51 -05:00
|
|
|
})
|
2024-11-14 00:00:17 -05:00
|
|
|
.strict()
|
2024-11-03 13:57:51 -05:00
|
|
|
.refine((data) => Object.keys(data).length > 0, {
|
2024-12-21 21:01:12 -05:00
|
|
|
message: "At least one field must be provided for update"
|
2024-11-03 13:57:51 -05:00
|
|
|
});
|
2024-10-01 21:34:07 -04:00
|
|
|
|
2024-11-03 13:57:51 -05:00
|
|
|
export async function updateResource(
|
|
|
|
req: Request,
|
|
|
|
res: Response,
|
2024-12-21 21:01:12 -05:00
|
|
|
next: NextFunction
|
2024-11-03 13:57:51 -05:00
|
|
|
): Promise<any> {
|
2024-10-06 18:05:20 -04:00
|
|
|
try {
|
|
|
|
const parsedParams = updateResourceParamsSchema.safeParse(req.params);
|
|
|
|
if (!parsedParams.success) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
2024-12-21 21:01:12 -05:00
|
|
|
fromError(parsedParams.error).toString()
|
|
|
|
)
|
2024-10-06 18:05:20 -04:00
|
|
|
);
|
|
|
|
}
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
const parsedBody = updateResourceBodySchema.safeParse(req.body);
|
|
|
|
if (!parsedBody.success) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
2024-12-21 21:01:12 -05:00
|
|
|
fromError(parsedBody.error).toString()
|
|
|
|
)
|
2024-10-06 18:05:20 -04:00
|
|
|
);
|
|
|
|
}
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
const { resourceId } = parsedParams.data;
|
|
|
|
const updateData = parsedBody.data;
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-11-16 22:41:43 -05:00
|
|
|
const resource = await db
|
|
|
|
.select()
|
|
|
|
.from(resources)
|
|
|
|
.where(eq(resources.resourceId, resourceId))
|
|
|
|
.leftJoin(orgs, eq(resources.orgId, orgs.orgId));
|
|
|
|
|
|
|
|
if (resource.length === 0) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.NOT_FOUND,
|
2024-12-21 21:01:12 -05:00
|
|
|
`Resource with ID ${resourceId} not found`
|
|
|
|
)
|
2024-11-16 22:41:43 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!resource[0].orgs?.domain) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
2024-12-21 21:01:12 -05:00
|
|
|
"Resource does not have a domain"
|
|
|
|
)
|
2024-11-16 22:41:43 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-11-23 16:36:07 -05:00
|
|
|
const fullDomain = updateData.subdomain
|
|
|
|
? `${updateData.subdomain}.${resource[0].orgs.domain}`
|
|
|
|
: undefined;
|
|
|
|
|
|
|
|
const updatePayload = {
|
|
|
|
...updateData,
|
2024-12-21 21:01:12 -05:00
|
|
|
...(fullDomain && { fullDomain })
|
2024-11-23 16:36:07 -05:00
|
|
|
};
|
2024-11-16 22:41:43 -05:00
|
|
|
|
2024-11-03 13:57:51 -05:00
|
|
|
const updatedResource = await db
|
|
|
|
.update(resources)
|
2024-11-23 16:36:07 -05:00
|
|
|
.set(updatePayload)
|
2024-10-06 18:05:20 -04:00
|
|
|
.where(eq(resources.resourceId, resourceId))
|
|
|
|
.returning();
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
if (updatedResource.length === 0) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.NOT_FOUND,
|
2024-12-21 21:01:12 -05:00
|
|
|
`Resource with ID ${resourceId} not found`
|
|
|
|
)
|
2024-10-06 18:05:20 -04:00
|
|
|
);
|
|
|
|
}
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
return response(res, {
|
|
|
|
data: updatedResource[0],
|
|
|
|
success: true,
|
|
|
|
error: false,
|
|
|
|
message: "Resource updated successfully",
|
2024-12-21 21:01:12 -05:00
|
|
|
status: HttpCode.OK
|
2024-10-06 18:05:20 -04:00
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(error);
|
2024-11-03 13:57:51 -05:00
|
|
|
return next(
|
2024-12-21 21:01:12 -05:00
|
|
|
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
2024-11-03 13:57:51 -05:00
|
|
|
);
|
2024-10-06 18:05:20 -04:00
|
|
|
}
|
2024-10-02 00:04:40 -04:00
|
|
|
}
|