Create resource working

This commit is contained in:
Owen Schwartz 2024-10-19 16:19:47 -04:00
parent 0ff183796c
commit 091d649997
No known key found for this signature in database
GPG key ID: 8271FDFFD9E0CCBD
17 changed files with 721 additions and 130 deletions

View file

@ -1,7 +1,7 @@
import { Request, Response, NextFunction } from 'express';
import { z } from 'zod';
import { db } from '@server/db';
import { resources, roleResources, roles, userResources } from '@server/db/schema';
import { orgs, resources, roleResources, roles, userResources } from '@server/db/schema';
import response from "@server/utils/response";
import HttpCode from '@server/types/HttpCode';
import createHttpError from 'http-errors';
@ -10,7 +10,7 @@ import logger from '@server/logger';
import { eq, and } from 'drizzle-orm';
const createResourceParamsSchema = z.object({
siteId: z.number().int().positive(),
siteId: z.string().transform(Number).pipe(z.number().int().positive()),
orgId: z.string()
});
@ -58,8 +58,23 @@ export async function createResource(req: Request, res: Response, next: NextFunc
return next(createHttpError(HttpCode.FORBIDDEN, 'User does not have a role'));
}
// get the org
const org = await db.select()
.from(orgs)
.where(eq(orgs.orgId, orgId))
.limit(1);
if (org.length === 0) {
return next(
createHttpError(
HttpCode.NOT_FOUND,
`Organization with ID ${orgId} not found`
)
);
}
// Generate a unique resourceId
const resourceId = "subdomain" // TODO: create the subdomain here
const resourceId = `${subdomain}.${org[0].domain}`;
// Create new resource in the database
const newResource = await db.insert(resources).values({
@ -70,8 +85,6 @@ export async function createResource(req: Request, res: Response, next: NextFunc
subdomain,
}).returning();
// find the superuser roleId and also add the resource to the superuser role
const superuserRole = await db.select()
.from(roles)
@ -108,7 +121,7 @@ export async function createResource(req: Request, res: Response, next: NextFunc
status: HttpCode.CREATED,
});
} catch (error) {
logger.error(error);
throw error;
return next(createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred..."));
}
}

View file

@ -14,6 +14,13 @@ const getResourceSchema = z.object({
resourceId: z.string().uuid()
});
export type GetResourceResponse = {
resourceId: string;
siteId: number;
orgId: string;
name: string;
}
export async function getResource(req: Request, res: Response, next: NextFunction): Promise<any> {
try {
// Validate request parameters
@ -51,7 +58,12 @@ export async function getResource(req: Request, res: Response, next: NextFunctio
}
return response(res, {
data: resource[0],
data: {
resourceId: resource[0].resourceId,
siteId: resource[0].siteId,
orgId: resource[0].orgId,
name: resource[0].name
},
success: true,
error: false,
message: "Resource retrieved successfully",

View file

@ -16,7 +16,7 @@ import logger from "@server/logger";
const listResourcesParamsSchema = z
.object({
siteId: z.number().int().positive().optional(),
siteId: z.string().optional().transform(Number).pipe(z.number().int().positive()),
orgId: z.string().optional(),
})
.refine((data) => !!data.siteId !== !!data.orgId, {