UNPKG

naga-audit-service

Version:

A comprehensive audit service library for NestJS applications with MongoDB support

121 lines 4.94 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PaginationService = void 0; const common_1 = require("@nestjs/common"); const momentTz = require("moment-timezone"); const service_common_constants_1 = require("../../constants/service-common.constants"); let PaginationService = class PaginationService { async findAndPaginate(collection, collectionName, options = {}) { const { filter = {}, skip = 0, limit = 10, projection = {}, sort = { updatedAt: -1 }, } = options; const validatedSkip = skip >= 0 ? skip : 0; const validatedLimit = limit > 0 ? limit : 10; const query = this.constructQuery(collectionName, filter); const [items, totalItems] = await Promise.all([ collection.find(query, projection, { lean: true, sort, skip: validatedSkip, limit: validatedLimit, }), collection.countDocuments(query), ]); return { totalItems, totalPages: Math.max(Math.ceil(totalItems / validatedLimit), 1), skip: validatedSkip, limit: validatedLimit, items, }; } constructQuery(collectionName, filter) { const query = {}; for (const key in filter) { if (filter.hasOwnProperty(key)) { this.applyLeadFilters(query, key, filter[key]); } } return query; } applyLeadFilters(query, key, value) { if (key === 'search' && typeof value === 'object') { const searchFields = value.fields; const searchValue = value.term; if (searchValue) { if (searchValue instanceof Array) { query.$or = searchFields.map((field) => ({ [field]: { $in: searchValue.map((value) => new RegExp(value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'i')) }, })); } else { query.$or = searchFields.map((field) => ({ [field]: new RegExp(searchValue.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'i'), })); } } } else if (Array.isArray(value)) { if (key.includes('$')) { query[key] = value; } else { const filteredValue = value.filter(item => item !== "N/A"); if (value.includes("N/A")) { if (filteredValue.length > 0) { query.$or = [ { [key]: { $in: filteredValue } }, { [key]: { $exists: false } } ]; } else { query[key] = { $exists: false }; } } else { query[key] = { $in: value }; } } } else if (typeof value === 'object') { if (value.from || value.to) { this.addDynamicDateFilter(query, key, value); } else { query[key] = value; } } else { if (value === 'NA') { query[key] = { $exists: false }; } else { query[key] = value; } } } addDynamicDateFilter(query, key, dateFilter) { const { from, to } = dateFilter; const fromDate = from ? momentTz.tz(new Date(from), service_common_constants_1.timeZone).startOf('day') : null; const toDate = to ? momentTz.tz(new Date(to), service_common_constants_1.timeZone).endOf('day') : fromDate; if (fromDate?.isValid() && toDate?.isValid()) { query[key] = { $gte: fromDate.toDate(), $lt: toDate.toDate(), }; console.log(`${key} query range:`, query[key]); } else { console.log('No valid from or to dates provided for dynamic filtering'); } } }; exports.PaginationService = PaginationService; exports.PaginationService = PaginationService = __decorate([ (0, common_1.Injectable)() ], PaginationService); //# sourceMappingURL=pagination.service.js.map