updated filtering code
This commit is contained in:
@@ -4,13 +4,13 @@ import {
|
||||
rtsModel,
|
||||
UpdateRtsInput,
|
||||
UploadRtsInput,
|
||||
} from "./rts.schema";
|
||||
import { AuthenticatedUser } from "../auth";
|
||||
import { generateId } from "../utils/id";
|
||||
import { getFilterObject, getSortObject, PageQueryParams } from "../pagination";
|
||||
import { getUser } from "../user/user.service";
|
||||
import { orgModel } from "../organization/organization.schema";
|
||||
import { userModel } from "../user/user.schema";
|
||||
} from './rts.schema';
|
||||
import { AuthenticatedUser } from '../auth';
|
||||
import { generateId } from '../utils/id';
|
||||
import { getFilterObject, getSortObject, PageQueryParams } from '../pagination';
|
||||
import { getUser } from '../user/user.service';
|
||||
import { orgModel } from '../organization/organization.schema';
|
||||
import { userModel } from '../user/user.schema';
|
||||
|
||||
export async function createRts(
|
||||
input: CreateRtsInput,
|
||||
@@ -53,43 +53,43 @@ export async function createRts(
|
||||
export async function getRts(id: string, tenantId: string) {
|
||||
return await rtsModel
|
||||
.findOne({ pid: id, tenantId: tenantId })
|
||||
.populate({ path: "county", select: "pid name avatar" })
|
||||
.populate({ path: "client", select: "pid name avatar" })
|
||||
.populate({ path: "createdBy", select: "pid name avatar" });
|
||||
.populate({ path: 'county', select: 'pid name avatar' })
|
||||
.populate({ path: 'client', select: 'pid name avatar' })
|
||||
.populate({ path: 'createdBy', select: 'pid name avatar' });
|
||||
}
|
||||
|
||||
export async function listRts(params: PageQueryParams, tenantId: string) {
|
||||
const page = params.page || 1;
|
||||
const pageSize = params.pageSize || 10;
|
||||
const sortObj = getSortObject(params, rtsFields);
|
||||
const filterObj = getFilterObject(params, rtsFields);
|
||||
const filterObj = getFilterObject(params);
|
||||
|
||||
const rtsList = await rtsModel.aggregate([
|
||||
{
|
||||
$match: { $and: [{ tenantId: tenantId }, ...filterObj] },
|
||||
$match: { $and: [{ tenantId: tenantId }, { ...filterObj }] },
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "organizations",
|
||||
localField: "county",
|
||||
foreignField: "_id",
|
||||
as: "countyRec",
|
||||
from: 'organizations',
|
||||
localField: 'county',
|
||||
foreignField: '_id',
|
||||
as: 'countyRec',
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "organizations",
|
||||
localField: "client",
|
||||
foreignField: "_id",
|
||||
as: "clientRec",
|
||||
from: 'organizations',
|
||||
localField: 'client',
|
||||
foreignField: '_id',
|
||||
as: 'clientRec',
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "users",
|
||||
localField: "createdBy",
|
||||
foreignField: "_id",
|
||||
as: "createdRec",
|
||||
from: 'users',
|
||||
localField: 'createdBy',
|
||||
foreignField: '_id',
|
||||
as: 'createdRec',
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -101,36 +101,36 @@ export async function listRts(params: PageQueryParams, tenantId: string) {
|
||||
createdAt: 1,
|
||||
county: {
|
||||
$let: {
|
||||
vars: { county: { $arrayElemAt: ["$countyRec", 0] } },
|
||||
vars: { county: { $arrayElemAt: ['$countyRec', 0] } },
|
||||
in: {
|
||||
_id: "$$county._id",
|
||||
pid: "$$county.pid",
|
||||
name: "$$county.name",
|
||||
type: "$$county.type",
|
||||
avatar: "$$county.avatar",
|
||||
_id: '$$county._id',
|
||||
pid: '$$county.pid',
|
||||
name: '$$county.name',
|
||||
type: '$$county.type',
|
||||
avatar: '$$county.avatar',
|
||||
},
|
||||
},
|
||||
},
|
||||
client: {
|
||||
$let: {
|
||||
vars: { client: { $arrayElemAt: ["$clientRec", 0] } },
|
||||
vars: { client: { $arrayElemAt: ['$clientRec', 0] } },
|
||||
in: {
|
||||
_id: "$$client._id",
|
||||
pid: "$$client.pid",
|
||||
name: "$$client.name",
|
||||
type: "$$client.type",
|
||||
avatar: "$$client.avatar",
|
||||
_id: '$$client._id',
|
||||
pid: '$$client.pid',
|
||||
name: '$$client.name',
|
||||
type: '$$client.type',
|
||||
avatar: '$$client.avatar',
|
||||
},
|
||||
},
|
||||
},
|
||||
createdBy: {
|
||||
$let: {
|
||||
vars: { created: { $arrayElemAt: ["$createdRec", 0] } },
|
||||
vars: { created: { $arrayElemAt: ['$createdRec', 0] } },
|
||||
in: {
|
||||
_id: "$$created._id",
|
||||
pid: "$$created.pid",
|
||||
name: "$$created.name",
|
||||
avatar: "$$created.avatar",
|
||||
_id: '$$created._id',
|
||||
pid: '$$created.pid',
|
||||
name: '$$created.name',
|
||||
avatar: '$$created.avatar',
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -138,7 +138,7 @@ export async function listRts(params: PageQueryParams, tenantId: string) {
|
||||
},
|
||||
{
|
||||
$facet: {
|
||||
metadata: [{ $count: "count" }],
|
||||
metadata: [{ $count: 'count' }],
|
||||
data: [
|
||||
{ $sort: sortObj },
|
||||
{ $skip: (page - 1) * pageSize },
|
||||
@@ -168,9 +168,9 @@ export async function updateRts(
|
||||
) {
|
||||
const updatedRts = await rtsModel
|
||||
.findOneAndUpdate({ pid: id, tenantId: tenantId }, input, { new: true })
|
||||
.populate({ path: "createdBy", select: "pid name avatar" })
|
||||
.populate({ path: "county", select: "pid name avatar" })
|
||||
.populate({ path: "client", select: "pid name avatar" });
|
||||
.populate({ path: 'createdBy', select: 'pid name avatar' })
|
||||
.populate({ path: 'county', select: 'pid name avatar' })
|
||||
.populate({ path: 'client', select: 'pid name avatar' });
|
||||
|
||||
return updatedRts;
|
||||
}
|
||||
@@ -202,12 +202,12 @@ export async function getUniqueValuesRTS(field: string, tenenatId: string) {
|
||||
let values = await rtsModel.distinct(field, { tenantId: tenenatId });
|
||||
|
||||
let matchedValues = [];
|
||||
if (field === "county") {
|
||||
matchedValues = await orgModel.find().where("_id").in(values).exec();
|
||||
} else if (field === "client") {
|
||||
matchedValues = await orgModel.find().where("_id").in(values).exec();
|
||||
} else if (field === "createdBy") {
|
||||
matchedValues = await userModel.find().where("_id").in(values).exec();
|
||||
if (field === 'county') {
|
||||
matchedValues = await orgModel.find().where('_id').in(values).exec();
|
||||
} else if (field === 'client') {
|
||||
matchedValues = await orgModel.find().where('_id').in(values).exec();
|
||||
} else if (field === 'createdBy') {
|
||||
matchedValues = await userModel.find().where('_id').in(values).exec();
|
||||
}
|
||||
|
||||
if (matchedValues.length > 0) {
|
||||
|
||||
Reference in New Issue
Block a user