Add sorting

This commit is contained in:
2025-01-02 11:19:44 +05:30
parent 40b2ab54f0
commit 83786e2994
5 changed files with 109 additions and 70 deletions

View File

@@ -1,7 +1,8 @@
import { PageQueryParams } from "../pagination";
import { getSortObject, PageQueryParams } from "../pagination";
import { generateId } from "../utils/id";
import {
CreateOrgInput,
orgFields,
orgModel,
UpdateOrgInput,
} from "./organization.schema";
@@ -26,13 +27,18 @@ export async function getOrg(orgId: string, tenantId: string) {
export async function listOrgs(params: PageQueryParams, tenantId: string) {
const page = params.page || 1;
const pageSize = params.pageSize || 10;
const sortObj = getSortObject(params, orgFields);
const orgs = await orgModel.aggregate([
{ $match: { $and: [{ tenantId: tenantId }] } },
{
$facet: {
metadata: [{ $count: "count" }],
data: [{ $skip: (page - 1) * pageSize }, { $limit: pageSize }],
data: [
{ $skip: (page - 1) * pageSize },
{ $limit: pageSize },
{ $sort: sortObj },
],
},
},
]);