Add pagination, complete organization routes

This commit is contained in:
2024-12-20 23:14:32 +05:30
parent a584fc91b5
commit cc2665544b
6 changed files with 190 additions and 6 deletions

View File

@@ -1,5 +1,10 @@
import { PageQueryParams } from "../pagination";
import { generateId } from "../utils/id";
import { CreateOrgInput, orgModel } from "./organization.schema";
import {
CreateOrgInput,
orgModel,
UpdateOrgInput,
} from "./organization.schema";
export async function createOrg(input: CreateOrgInput, tenantId: string) {
const org = await orgModel.create({
@@ -17,3 +22,49 @@ export async function getOrg(orgId: string, tenantId: string) {
$and: [{ tenantId: tenantId }, { pid: orgId }],
});
}
export async function listOrgs(params: PageQueryParams, tenantId: string) {
const page = params.page || 1;
const pageSize = params.pageSize || 10;
const orgs = await orgModel.aggregate([
{ $match: { $and: [{ tenantId: tenantId }] } },
{
$facet: {
metadata: [{ $count: "count" }],
data: [{ $skip: (page - 1) * pageSize }, { $limit: pageSize }],
},
},
]);
return {
orgs: orgs[0].data,
metadata: {
count: orgs[0].metadata[0].count,
page,
pageSize,
},
};
}
export async function updateOrg(
input: UpdateOrgInput,
orgId: string,
tenantId: string
) {
const updateOrgResult = await orgModel.findOneAndUpdate(
{
$and: [{ tenantId: tenantId }, { pid: orgId }],
},
{ ...input, updatedAt: new Date() },
{ new: true }
);
return updateOrgResult;
}
export async function deleteOrg(orgId: string, tenantId: string) {
return await orgModel.deleteOne({
$and: [{ tenantId: tenantId }, { pid: orgId }],
});
}