updated filtering code

This commit is contained in:
2025-05-02 16:27:52 +05:30
parent 4153bd2412
commit c84cd055d4
10 changed files with 160 additions and 211 deletions

View File

@@ -1,12 +1,12 @@
import { getFilterObject, getSortObject, PageQueryParams } from "../pagination";
import { ChangeEvent, dbEvents } from "../realtime";
import { generateId } from "../utils/id";
import { getFilterObject, getSortObject, PageQueryParams } from '../pagination';
import { ChangeEvent, dbEvents } from '../realtime';
import { generateId } from '../utils/id';
import {
CreateOrgInput,
orgFields,
orgModel,
UpdateOrgInput,
} from "./organization.schema";
} from './organization.schema';
export async function createOrg(input: CreateOrgInput, tenantId: string) {
const org = await orgModel.create({
@@ -17,13 +17,13 @@ export async function createOrg(input: CreateOrgInput, tenantId: string) {
});
dbEvents.emit(
"change",
'change',
{
type: "insert",
collection: "orgs",
type: 'insert',
collection: 'orgs',
document: org,
} as ChangeEvent,
["org:read"]
['org:read']
);
return org;
@@ -39,13 +39,13 @@ export async function listOrgs(params: PageQueryParams, tenantId: string) {
const page = params.page || 1;
const pageSize = params.pageSize || 10;
const sortObj = getSortObject(params, orgFields);
const filterObj = getFilterObject(params, orgFields);
const filterObj = getFilterObject(params);
const orgs = await orgModel.aggregate([
{ $match: { $and: [{ tenantId: tenantId }, ...filterObj] } },
{ $match: { $and: [{ tenantId: tenantId }, { ...filterObj }] } },
{
$facet: {
metadata: [{ $count: "count" }],
metadata: [{ $count: 'count' }],
data: [
{ $sort: sortObj },
{ $skip: (page - 1) * pageSize },
@@ -83,13 +83,13 @@ export async function updateOrg(
if (updateOrgResult) {
dbEvents.emit(
"change",
'change',
{
type: "update",
collection: "orgs",
type: 'update',
collection: 'orgs',
document: updateOrgResult,
} as ChangeEvent,
["org:read"]
['org:read']
);
}
@@ -103,15 +103,15 @@ export async function deleteOrg(orgId: string, tenantId: string) {
if (res.deletedCount > 0) {
dbEvents.emit(
"change",
'change',
{
type: "delete",
collection: "orgs",
type: 'delete',
collection: 'orgs',
document: {
pid: orgId,
},
} as ChangeEvent,
["org:read"]
['org:read']
);
}
@@ -122,15 +122,15 @@ export async function searchOrgs(params: PageQueryParams, tenantId: string) {
const page = params.page || 1;
const pageSize = params.pageSize || 10;
const sortObj = getSortObject(params, orgFields);
const filterObj = getFilterObject(params, orgFields);
const filterObj = getFilterObject(params);
if (!params.searchToken)
return { orgs: [], metadata: { count: 0, page, pageSize } };
const regex = new RegExp(params.searchToken, "i");
const regex = new RegExp(params.searchToken, 'i');
const orgs = await orgModel.aggregate([
{ $match: { $and: [{ tenantId: tenantId }, ...filterObj] } },
{ $match: { $and: [{ tenantId: tenantId }, { ...filterObj }] } },
{
$match: {
$or: [{ name: { $regex: regex } }, { domain: { $regex: regex } }],
@@ -138,7 +138,7 @@ export async function searchOrgs(params: PageQueryParams, tenantId: string) {
},
{
$facet: {
metadata: [{ $count: "count" }],
metadata: [{ $count: 'count' }],
data: [
{ $sort: sortObj },
{ $skip: (page - 1) * pageSize },