diff --git a/src/auth.ts b/src/auth.ts index 6f0255f..17d9a09 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -16,6 +16,7 @@ export type AuthenticatedUser = { type: string; userId?: string; orgId?: Array; + counties?: Array; role?: string; tenantId: string; claims: Array; diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index 248edaa..75fe1a0 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -36,6 +36,7 @@ export async function getSession( type: "user", userId: user.id, orgId: user.orgId ? user.orgId.map((item) => item.toString()) : [], + counties: user.counties ? user.counties.map((item) => item.toString()) : [], role: user.role, tenantId: user.tenantId, claims: rules[user.role].claims ?? [], diff --git a/src/notification/notification.service.ts b/src/notification/notification.service.ts index 965fde4..2cbc846 100644 --- a/src/notification/notification.service.ts +++ b/src/notification/notification.service.ts @@ -159,6 +159,14 @@ export async function listNotifications( }); } + if (user.counties && user.counties.length > 0) { + filterObj.push({ + "county.id": { + $in: user.counties.map((item) => new mongoose.Types.ObjectId(item)), + }, + }); + } + let { taggedFilter, taggedUserFilterIndex } = getTaggedUsersFilter( filterObj, sortObj diff --git a/src/payments/payment.schema.ts b/src/payments/payment.schema.ts index bcb043e..eac2229 100644 --- a/src/payments/payment.schema.ts +++ b/src/payments/payment.schema.ts @@ -15,7 +15,12 @@ const paymentSchema = new mongoose.Schema({ unique: true, }, permitPid: String, - county: Object, + county: { + id: mongoose.Types.ObjectId, + pid: String, + name: String, + avatar: String, + }, client: { type: mongoose.Types.ObjectId, ref: "organization", diff --git a/src/payments/payments.service.ts b/src/payments/payments.service.ts index f58cac4..16cc15b 100644 --- a/src/payments/payments.service.ts +++ b/src/payments/payments.service.ts @@ -41,6 +41,14 @@ export async function listPayments( }); } + if (user.counties && user.counties.length > 0) { + filterObj.push({ + "county.id": { + $in: user.counties.map((item) => new mongoose.Types.ObjectId(item)), + }, + }); + } + const pipeline: Array = [ { $match: { $and: [{ tenantId: user.tenantId }, ...filterObj] } }, ]; diff --git a/src/permit/permit.service.ts b/src/permit/permit.service.ts index e91d130..9e9a445 100644 --- a/src/permit/permit.service.ts +++ b/src/permit/permit.service.ts @@ -108,6 +108,7 @@ export async function getPermit(permitId: string, user: AuthenticatedUser) { .populate({ path: "assignedTo", select: "pid name avatar" }) .populate({ path: "createdBy", select: "pid name avatar" }); + // Don't return the record if the user doesn't have access to the org if ( permit && user.role == "client" && @@ -115,6 +116,15 @@ export async function getPermit(permitId: string, user: AuthenticatedUser) { ) return null; + // Don't return the record if the user doesn't have access to the org + if ( + permit && + user.counties && + user.counties.length > 0 && + !user.counties.includes(permit.county.id.toString()) + ) + return null; + return permit; } @@ -135,6 +145,14 @@ export async function listPermits( }); } + if (user.counties && user.counties.length > 0) { + filterObj.push({ + "county.id": { + $in: user.counties.map((item) => new mongoose.Types.ObjectId(item)), + }, + }); + } + let { taggedFilter, taggedUserFilterIndex } = getTaggedUsersFilter( filterObj, sortObj @@ -410,6 +428,14 @@ export async function searchPermit( }); } + if (user.counties && user.counties.length > 0) { + filterObj.push({ + "county.id": { + $in: user.counties.map((item) => new mongoose.Types.ObjectId(item)), + }, + }); + } + if (!params.searchToken) return { permits: [], metadata: { count: 0, page, pageSize } }; diff --git a/src/processed/processed.service.ts b/src/processed/processed.service.ts index c679f15..ae397c9 100644 --- a/src/processed/processed.service.ts +++ b/src/processed/processed.service.ts @@ -149,6 +149,14 @@ export async function listProcessedPermits( }); } + if (user.counties && user.counties.length > 0) { + filterObj.push({ + "county.id": { + $in: user.counties.map((item) => new mongoose.Types.ObjectId(item)), + }, + }); + } + let { taggedFilter, taggedUserFilterIndex } = getTaggedUsersFilter( filterObj, sortObj diff --git a/src/rts/rts.service.ts b/src/rts/rts.service.ts index 668b1b6..595f27e 100644 --- a/src/rts/rts.service.ts +++ b/src/rts/rts.service.ts @@ -90,6 +90,14 @@ export async function listRts( }); } + if (user.counties && user.counties.length > 0) { + filterObj.push({ + county: { + $in: user.counties.map((item) => new mongoose.Types.ObjectId(item)), + }, + }); + } + let { taggedFilter, taggedUserFilterIndex } = getTaggedUsersFilter( filterObj, sortObj diff --git a/src/user/user.schema.ts b/src/user/user.schema.ts index e5fcf69..3d5e4a9 100644 --- a/src/user/user.schema.ts +++ b/src/user/user.schema.ts @@ -14,6 +14,7 @@ const userSchema = new mongoose.Schema({ required: true, }, orgId: { type: [Schema.Types.ObjectId], ref: "organization" }, + counties: { type: [Schema.Types.ObjectId], ref: "organization" }, firstName: String, lastName: String, name: String, @@ -74,6 +75,7 @@ const userCore = { avatar: z.string().optional(), role: z.enum(roles), orgId: z.array(z.string()).optional(), + counties: z.array(z.string()).optional(), password: z.string().optional(), }; @@ -99,6 +101,7 @@ const updateUserInput = z.object({ avatar: z.string().url().optional(), role: z.enum(roles).optional(), orgId: z.array(z.string()).optional(), + counties: z.array(z.string()).optional(), }); const userResponse = z.object({ @@ -110,6 +113,17 @@ const userResponse = z.object({ _id: z.string().optional(), pid: z.string().optional(), name: z.string().optional(), + avatar: z.string().optional(), + }) + ) + .optional(), + counties: z + .array( + z.object({ + _id: z.string().optional(), + pid: z.string().optional(), + name: z.string().optional(), + avatar: z.string().optional(), }) ) .optional(), diff --git a/src/user/user.service.ts b/src/user/user.service.ts index dd3b860..0260252 100644 --- a/src/user/user.service.ts +++ b/src/user/user.service.ts @@ -74,7 +74,8 @@ export async function createUser( return userModel .findOne({ pid: newUser.pid }) - .populate({ path: "orgId", select: "pid name avatar" }); + .populate({ path: "orgId", select: "pid name avatar" }) + .populate({ path: "counties", select: "pid name avatar" }); } export async function resetUser(userId: string, user?: AuthenticatedUser) { @@ -120,14 +121,16 @@ export async function getUser(userId: string) { if (mongoose.Types.ObjectId.isValid(userId)) { return await userModel .findById(userId) - .populate({ path: "orgId", select: "_id pid name" }); + .populate({ path: "orgId", select: "_id pid name avatar" }) + .populate({ path: "counties", select: "_id pid name avatar" }); } return await userModel .findOne({ $and: [{ pid: userId }], }) - .populate({ path: "orgId", select: "_id pid name" }); + .populate({ path: "orgId", select: "_id pid name avatar" }) + .populate({ path: "counties", select: "_id pid name avatar" }); } export async function getUserWithoutPopulate(userId: string) { @@ -157,17 +160,19 @@ export async function listUsers(user: AuthenticatedUser) { { dev: { $ne: true } }, ], }) - .select("_id pid orgId firstName lastName name email avatar") + .select("_id pid orgId firstName lastName name email avatar counties") .populate({ path: "orgId", select: "_id pid name avatar" }) + .populate({ path: "counties", select: "_id pid name avatar" }) .populate({ path: "createdBy", select: "_id pid name avatar" }); } return await userModel .find({ $and: [{ tenantId: user.tenantId }, { dev: { $ne: true } }] }) .select( - "_id pid orgId firstName lastName name email role avatar status createdAt createdBy lastLogin" + "_id pid orgId counties firstName lastName name email role avatar status createdAt createdBy lastLogin" ) .populate({ path: "orgId", select: "_id pid name avatar" }) + .populate({ path: "counties", select: "_id pid name avatar" }) .populate({ path: "createdBy", select: "_id pid name avatar" }); } @@ -188,9 +193,10 @@ export async function updateUser( new: true, }) .select( - "_id pid orgId firstName lastName name email role avatar status createdAt createdBy lastLogin" + "_id pid orgId counties firstName lastName name email role avatar status createdAt createdBy lastLogin" ) - .populate({ path: "orgId", select: "_id pid name avatar" }); + .populate({ path: "orgId", select: "_id pid name avatar" }) + .populate({ path: "counties", select: "_id pid name avatar" }); if (!userInDb) return null; @@ -212,7 +218,7 @@ export async function updateUserInternal( new: true, }) .select( - "_id pid orgId firstName lastName name email role avatar status createdAt createdBy lastLogin" + "_id pid orgId counties firstName lastName name email role avatar status createdAt createdBy lastLogin" ); }