Add pagination, complete organization routes
This commit is contained in:
@@ -1,6 +1,13 @@
|
||||
import { FastifyRequest, FastifyReply } from "fastify";
|
||||
import { CreateOrgInput } from "./organization.schema";
|
||||
import { createOrg, getOrg } from "./organization.service";
|
||||
import { CreateOrgInput, UpdateOrgInput } from "./organization.schema";
|
||||
import {
|
||||
createOrg,
|
||||
deleteOrg,
|
||||
getOrg,
|
||||
listOrgs,
|
||||
updateOrg,
|
||||
} from "./organization.service";
|
||||
import { PageQueryParams } from "../pagination";
|
||||
|
||||
export async function createOrgHandler(req: FastifyRequest, res: FastifyReply) {
|
||||
const input = req.body as CreateOrgInput;
|
||||
@@ -28,3 +35,45 @@ export async function getOrgHandler(req: FastifyRequest, res: FastifyReply) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
export async function listOrgsHandler(req: FastifyRequest, res: FastifyReply) {
|
||||
const queryParams = req.query as PageQueryParams;
|
||||
|
||||
try {
|
||||
const authUser = req.user;
|
||||
const orgList = await listOrgs(queryParams, authUser.tenantId);
|
||||
return res.code(200).send(orgList);
|
||||
} catch (err) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateOrgHandler(req: FastifyRequest, res: FastifyReply) {
|
||||
const input = req.body as UpdateOrgInput;
|
||||
const { orgId } = req.params as { orgId: string };
|
||||
|
||||
try {
|
||||
const authUser = req.user;
|
||||
const updatedOrg = await updateOrg(input, orgId, authUser.tenantId);
|
||||
if (!updatedOrg) return res.code(404).send({ error: "resource not found" });
|
||||
|
||||
return res.code(200).send(updatedOrg);
|
||||
} catch (err) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteOrgHandler(req: FastifyRequest, res: FastifyReply) {
|
||||
const { orgId } = req.params as { orgId: string };
|
||||
|
||||
try {
|
||||
const authUser = req.user;
|
||||
const deleteResult = await deleteOrg(orgId, authUser.tenantId);
|
||||
if (deleteResult.deletedCount === 0)
|
||||
return res.code(404).send({ error: "resource not found" });
|
||||
|
||||
return res.code(204).send();
|
||||
} catch (err) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user