added client role and related code
This commit is contained in:
@@ -1,29 +1,29 @@
|
||||
import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
|
||||
import { PageQueryParams } from '../pagination';
|
||||
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
||||
import { PageQueryParams } from "../pagination";
|
||||
import {
|
||||
getProcessedPermit,
|
||||
getUniqueValuesProcessed,
|
||||
listProcessedPermits,
|
||||
updateProcessed,
|
||||
} from './processed.service';
|
||||
import { $processed, UpdateProcessedInput } from './processed.schema';
|
||||
import { noteRoutes } from '../note/note.route';
|
||||
} from "./processed.service";
|
||||
import { $processed, UpdateProcessedInput } from "./processed.schema";
|
||||
import { noteRoutes } from "../note/note.route";
|
||||
|
||||
export async function processedRoutes(fastify: FastifyInstance) {
|
||||
fastify.get(
|
||||
'/',
|
||||
"/",
|
||||
{
|
||||
schema: {
|
||||
querystring: $processed('pageQueryParams'),
|
||||
querystring: $processed("pageQueryParams"),
|
||||
},
|
||||
config: { requiredClaims: ['permit:read'] },
|
||||
config: { requiredClaims: ["permit:read"] },
|
||||
preHandler: [fastify.authorize],
|
||||
},
|
||||
async (req: FastifyRequest, res: FastifyReply) => {
|
||||
const params = req.query as PageQueryParams;
|
||||
|
||||
try {
|
||||
const permits = await listProcessedPermits(params, req.user.tenantId);
|
||||
const permits = await listProcessedPermits(params, req.user);
|
||||
return res.code(200).send(permits);
|
||||
} catch (err) {
|
||||
return err;
|
||||
@@ -32,19 +32,19 @@ export async function processedRoutes(fastify: FastifyInstance) {
|
||||
);
|
||||
|
||||
fastify.get(
|
||||
'/search',
|
||||
"/search",
|
||||
{
|
||||
schema: {
|
||||
querystring: $processed('pageQueryParams'),
|
||||
querystring: $processed("pageQueryParams"),
|
||||
},
|
||||
config: { requiredClaims: ['permit:read'] },
|
||||
config: { requiredClaims: ["permit:read"] },
|
||||
preHandler: [fastify.authorize],
|
||||
},
|
||||
async (req: FastifyRequest, res: FastifyReply) => {
|
||||
const params = req.query as PageQueryParams;
|
||||
|
||||
try {
|
||||
const permits = await listProcessedPermits(params, req.user.tenantId);
|
||||
const permits = await listProcessedPermits(params, req.user);
|
||||
return res.code(200).send(permits);
|
||||
} catch (err) {
|
||||
return err;
|
||||
@@ -53,15 +53,15 @@ export async function processedRoutes(fastify: FastifyInstance) {
|
||||
);
|
||||
|
||||
fastify.get(
|
||||
'/:permitId',
|
||||
"/:permitId",
|
||||
{
|
||||
schema: {
|
||||
params: {
|
||||
type: 'object',
|
||||
properties: { permitId: { type: 'string' } },
|
||||
type: "object",
|
||||
properties: { permitId: { type: "string" } },
|
||||
},
|
||||
},
|
||||
config: { requiredClaims: ['permit:read'] },
|
||||
config: { requiredClaims: ["permit:read"] },
|
||||
preHandler: [fastify.authorize],
|
||||
},
|
||||
async (req: FastifyRequest, res: FastifyReply) => {
|
||||
@@ -77,16 +77,16 @@ export async function processedRoutes(fastify: FastifyInstance) {
|
||||
);
|
||||
|
||||
fastify.patch(
|
||||
'/:permitId',
|
||||
"/:permitId",
|
||||
{
|
||||
schema: {
|
||||
params: {
|
||||
type: 'object',
|
||||
properties: { permitId: { type: 'string' } },
|
||||
type: "object",
|
||||
properties: { permitId: { type: "string" } },
|
||||
},
|
||||
body: $processed('updateProcessedInput'),
|
||||
body: $processed("updateProcessedInput"),
|
||||
},
|
||||
config: { requiredClaims: ['permit:write'] },
|
||||
config: { requiredClaims: ["permit:write"] },
|
||||
preHandler: [fastify.authorize],
|
||||
},
|
||||
async (req: FastifyRequest, res: FastifyReply) => {
|
||||
@@ -103,17 +103,17 @@ export async function processedRoutes(fastify: FastifyInstance) {
|
||||
);
|
||||
|
||||
fastify.get(
|
||||
'/fields/:field',
|
||||
"/fields/:field",
|
||||
{
|
||||
schema: {
|
||||
params: {
|
||||
type: 'object',
|
||||
type: "object",
|
||||
properties: {
|
||||
field: { type: 'string' },
|
||||
field: { type: "string" },
|
||||
},
|
||||
},
|
||||
},
|
||||
config: { requiredClaims: ['permit:read'] },
|
||||
config: { requiredClaims: ["permit:read"] },
|
||||
preHandler: [fastify.authorize],
|
||||
},
|
||||
async (req: FastifyRequest, res: FastifyReply) => {
|
||||
@@ -132,8 +132,8 @@ export async function processedRoutes(fastify: FastifyInstance) {
|
||||
);
|
||||
|
||||
await noteRoutes(fastify, {
|
||||
read: 'permit:read',
|
||||
write: 'permit:write',
|
||||
delete: 'permit:delete',
|
||||
read: "permit:read",
|
||||
write: "permit:write",
|
||||
delete: "permit:delete",
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import { AuthenticatedUser } from '../auth';
|
||||
import { orgModel } from '../organization/organization.schema';
|
||||
import { getFilterObject, getSortObject, PageQueryParams } from '../pagination';
|
||||
import { userModel } from '../user/user.schema';
|
||||
import mongoose from "mongoose";
|
||||
import { AuthenticatedUser } from "../auth";
|
||||
import { orgModel } from "../organization/organization.schema";
|
||||
import { getFilterObject, getSortObject, PageQueryParams } from "../pagination";
|
||||
import { userModel } from "../user/user.schema";
|
||||
import {
|
||||
processedFields,
|
||||
processedModel,
|
||||
UpdateProcessedInput,
|
||||
} from './processed.schema';
|
||||
} from "./processed.schema";
|
||||
|
||||
export async function getProcessedPermit(permitId: String, tenantId: String) {
|
||||
return await processedModel.findOne({
|
||||
@@ -27,35 +28,39 @@ export async function updateProcessed(
|
||||
{ ...input, lastUpdateDate: new Date() },
|
||||
{ new: true }
|
||||
)
|
||||
.populate({ path: 'county', select: 'pid name avatar' })
|
||||
.populate({ path: 'client', select: 'pid name avatar' })
|
||||
.populate({ path: 'assignedTo', 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: "assignedTo", select: "pid name avatar" })
|
||||
.populate({ path: "createdBy", select: "pid name avatar" });
|
||||
}
|
||||
|
||||
export async function listProcessedPermits(
|
||||
params: PageQueryParams,
|
||||
tenantId: string
|
||||
user: AuthenticatedUser
|
||||
) {
|
||||
const page = params.page || 1;
|
||||
const pageSize = params.pageSize || 10;
|
||||
const sortObj = getSortObject(params, processedFields);
|
||||
const filterObj = getFilterObject(params);
|
||||
const filterObj = getFilterObject(params) || {};
|
||||
|
||||
if (user.role == "client") {
|
||||
filterObj["client"] = new mongoose.Types.ObjectId(user.orgId);
|
||||
}
|
||||
|
||||
const pipeline: any = [
|
||||
{
|
||||
$match: { $and: [{ tenantId: tenantId }, { ...filterObj }] },
|
||||
$match: { $and: [{ tenantId: user.tenantId }, { ...filterObj }] },
|
||||
},
|
||||
];
|
||||
|
||||
if (params.searchToken && params.searchToken != '') {
|
||||
const regex = new RegExp(params.searchToken, 'i');
|
||||
if (params.searchToken && params.searchToken != "") {
|
||||
const regex = new RegExp(params.searchToken, "i");
|
||||
pipeline.push({
|
||||
$match: {
|
||||
$or: [
|
||||
{ permitNumber: { $regex: regex } },
|
||||
{ link: { $regex: regex } },
|
||||
{ 'address.full_address': { $regex: regex } },
|
||||
{ "address.full_address": { $regex: regex } },
|
||||
],
|
||||
},
|
||||
});
|
||||
@@ -65,10 +70,10 @@ export async function listProcessedPermits(
|
||||
...[
|
||||
{
|
||||
$lookup: {
|
||||
from: 'users',
|
||||
localField: 'assignedTo',
|
||||
foreignField: '_id',
|
||||
as: 'assignedRec',
|
||||
from: "users",
|
||||
localField: "assignedTo",
|
||||
foreignField: "_id",
|
||||
as: "assignedRec",
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -111,12 +116,12 @@ export async function listProcessedPermits(
|
||||
transferDate: 1,
|
||||
assignedTo: {
|
||||
$let: {
|
||||
vars: { assigned: { $arrayElemAt: ['$assignedRec', 0] } },
|
||||
vars: { assigned: { $arrayElemAt: ["$assignedRec", 0] } },
|
||||
in: {
|
||||
_id: '$$assigned._id',
|
||||
pid: '$$assigned.pid',
|
||||
name: '$$assigned.name',
|
||||
avatar: '$$assigned.avatar',
|
||||
_id: "$$assigned._id",
|
||||
pid: "$$assigned.pid",
|
||||
name: "$$assigned.name",
|
||||
avatar: "$$assigned.avatar",
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -124,7 +129,7 @@ export async function listProcessedPermits(
|
||||
},
|
||||
{
|
||||
$facet: {
|
||||
metadata: [{ $count: 'count' }],
|
||||
metadata: [{ $count: "count" }],
|
||||
data: [
|
||||
{ $sort: sortObj },
|
||||
{ $skip: (page - 1) * pageSize },
|
||||
@@ -157,12 +162,12 @@ export async function getUniqueValuesProcessed(
|
||||
let values = await processedModel.distinct(field, { tenantId: tenenatId });
|
||||
|
||||
let matchedValues = [];
|
||||
if (field === 'county.name') {
|
||||
matchedValues = await orgModel.find().where('name').in(values).exec();
|
||||
} else if (field === 'client') {
|
||||
matchedValues = await orgModel.find().where('_id').in(values).exec();
|
||||
} else if (field === 'assignedTo') {
|
||||
matchedValues = await userModel.find().where('name').in(values).exec();
|
||||
if (field === "county.name") {
|
||||
matchedValues = await orgModel.find().where("name").in(values).exec();
|
||||
} else if (field === "client") {
|
||||
matchedValues = await orgModel.find().where("_id").in(values).exec();
|
||||
} else if (field === "assignedTo") {
|
||||
matchedValues = await userModel.find().where("name").in(values).exec();
|
||||
}
|
||||
|
||||
if (matchedValues.length > 0) {
|
||||
|
||||
Reference in New Issue
Block a user