mirror of
https://github.com/fosrl/pangolin.git
synced 2026-03-05 18:26:40 +00:00
Move nessicary info to url params
This commit is contained in:
@@ -1,19 +1,70 @@
|
||||
import { Request, Response, NextFunction } from "express";
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { z } from 'zod';
|
||||
import { db } from '@server/db';
|
||||
import { sites } from '@server/db/schema';
|
||||
import response from "@server/utils/response";
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
import HttpCode from '@server/types/HttpCode';
|
||||
import createHttpError from 'http-errors';
|
||||
|
||||
// define zod type here
|
||||
const createSiteParamsSchema = z.object({
|
||||
orgId: z.number().int().positive(),
|
||||
});
|
||||
|
||||
export async function createSite(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction,
|
||||
): Promise<any> {
|
||||
return response<null>(res, {
|
||||
data: null,
|
||||
// Define Zod schema for request body validation
|
||||
const createSiteSchema = z.object({
|
||||
name: z.string().min(1).max(255),
|
||||
subdomain: z.string().min(1).max(255).optional(),
|
||||
pubKey: z.string().optional(),
|
||||
subnet: z.string().optional(),
|
||||
});
|
||||
|
||||
export async function createSite(req: Request, res: Response, next: NextFunction): Promise<any> {
|
||||
try {
|
||||
// Validate request body
|
||||
const parsedBody = createSiteSchema.safeParse(req.body);
|
||||
if (!parsedBody.success) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.BAD_REQUEST,
|
||||
parsedBody.error.errors.map(e => e.message).join(', ')
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const { name, subdomain, pubKey, subnet } = parsedBody.data;
|
||||
|
||||
// Validate request params
|
||||
const parsedParams = createSiteParamsSchema.safeParse(req.params);
|
||||
if (!parsedParams.success) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.BAD_REQUEST,
|
||||
parsedParams.error.errors.map(e => e.message).join(', ')
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const { orgId } = parsedParams.data;
|
||||
|
||||
// Create new site in the database
|
||||
const newSite = await db.insert(sites).values({
|
||||
orgId,
|
||||
name,
|
||||
subdomain,
|
||||
pubKey,
|
||||
subnet,
|
||||
}).returning();
|
||||
|
||||
return res.status(HttpCode.CREATED).send(
|
||||
response(res, {
|
||||
data: newSite[0],
|
||||
success: true,
|
||||
error: false,
|
||||
message: "Logged in successfully",
|
||||
status: HttpCode.OK,
|
||||
});
|
||||
}
|
||||
message: "Site created successfully",
|
||||
status: HttpCode.CREATED,
|
||||
})
|
||||
);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
@@ -7,10 +7,13 @@ import HttpCode from '@server/types/HttpCode';
|
||||
import createHttpError from 'http-errors';
|
||||
import { sql, eq } from 'drizzle-orm';
|
||||
|
||||
const listSitesParamsSchema = z.object({
|
||||
orgId: z.string().optional().transform(Number).pipe(z.number().int().positive()),
|
||||
});
|
||||
|
||||
const listSitesSchema = z.object({
|
||||
limit: z.string().optional().transform(Number).pipe(z.number().int().positive().default(10)),
|
||||
offset: z.string().optional().transform(Number).pipe(z.number().int().nonnegative().default(0)),
|
||||
orgId: z.string().optional().transform(Number).pipe(z.number().int().positive()),
|
||||
});
|
||||
|
||||
export async function listSites(req: Request, res: Response, next: NextFunction): Promise<any> {
|
||||
@@ -25,7 +28,19 @@ export async function listSites(req: Request, res: Response, next: NextFunction)
|
||||
);
|
||||
}
|
||||
|
||||
const { limit, offset, orgId } = parsedQuery.data;
|
||||
const { limit, offset } = parsedQuery.data;
|
||||
|
||||
const parsedParams = listSitesParamsSchema.safeParse(req.params);
|
||||
if (!parsedParams.success) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.BAD_REQUEST,
|
||||
parsedParams.error.errors.map(e => e.message).join(', ')
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const { orgId } = parsedParams.data;
|
||||
|
||||
let baseQuery: any = db
|
||||
.select({
|
||||
|
||||
Reference in New Issue
Block a user