mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-16 18:06:39 +00:00
Fix return response and add list
This commit is contained in:
@@ -31,7 +31,7 @@ export async function createOrg(req: Request, res: Response, next: NextFunction)
|
||||
}).returning();
|
||||
|
||||
return res.status(HttpCode.CREATED).send(
|
||||
response({
|
||||
response(res, {
|
||||
data: newOrg[0],
|
||||
success: true,
|
||||
error: false,
|
||||
|
||||
@@ -39,7 +39,7 @@ export async function deleteOrg(req: Request, res: Response, next: NextFunction)
|
||||
}
|
||||
|
||||
return res.status(HttpCode.OK).send(
|
||||
response({
|
||||
response(res, {
|
||||
data: null,
|
||||
success: true,
|
||||
error: false,
|
||||
|
||||
@@ -40,7 +40,7 @@ export async function getOrg(req: Request, res: Response, next: NextFunction): P
|
||||
}
|
||||
|
||||
return res.status(HttpCode.OK).send(
|
||||
response({
|
||||
response(res, {
|
||||
data: org[0],
|
||||
success: true,
|
||||
error: false,
|
||||
|
||||
57
server/routers/org/listOrgs.ts
Normal file
57
server/routers/org/listOrgs.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { z } from 'zod';
|
||||
import { db } from '@server/db';
|
||||
import { orgs } from '@server/db/schema';
|
||||
import response from "@server/utils/response";
|
||||
import HttpCode from '@server/types/HttpCode';
|
||||
import createHttpError from 'http-errors';
|
||||
import { sql } from 'drizzle-orm';
|
||||
|
||||
const listOrgsSchema = 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)),
|
||||
});
|
||||
|
||||
export async function listOrgs(req: Request, res: Response, next: NextFunction): Promise<any> {
|
||||
try {
|
||||
const parsedQuery = listOrgsSchema.safeParse(req.query);
|
||||
if (!parsedQuery.success) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.BAD_REQUEST,
|
||||
parsedQuery.error.errors.map(e => e.message).join(', ')
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const { limit, offset } = parsedQuery.data;
|
||||
|
||||
const organizations = await db.select()
|
||||
.from(orgs)
|
||||
.limit(limit)
|
||||
.offset(offset);
|
||||
|
||||
const totalCountResult = await db.select({ count: sql<number>`cast(count(*) as integer)` })
|
||||
.from(orgs);
|
||||
const totalCount = totalCountResult[0].count;
|
||||
|
||||
return res.status(HttpCode.OK).send(
|
||||
response(res, {
|
||||
data: {
|
||||
organizations,
|
||||
pagination: {
|
||||
total: totalCount,
|
||||
limit,
|
||||
offset,
|
||||
},
|
||||
},
|
||||
success: true,
|
||||
error: false,
|
||||
message: "Organizations retrieved successfully",
|
||||
status: HttpCode.OK,
|
||||
})
|
||||
);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
@@ -58,7 +58,7 @@ export async function updateOrg(req: Request, res: Response, next: NextFunction)
|
||||
}
|
||||
|
||||
return res.status(HttpCode.OK).send(
|
||||
response({
|
||||
response(res, {
|
||||
data: updatedOrg[0],
|
||||
success: true,
|
||||
error: false,
|
||||
|
||||
Reference in New Issue
Block a user