diff --git a/src/notification/notification.service.ts b/src/notification/notification.service.ts index a1d80e6..dcb5b82 100644 --- a/src/notification/notification.service.ts +++ b/src/notification/notification.service.ts @@ -112,6 +112,13 @@ export async function listNotifications( const sortObj = getSortObject(params, notificationFields); const filterObj = getFilterObject(params) || []; + filterObj.forEach((item) => { + if (item.changes) { + item[`changes.${item.changes.$eq}`] = { $exists: true }; + delete item["changes"]; + } + }); + if (user.role == "client") { filterObj.push({ client: new mongoose.Types.ObjectId(user.orgId) }); } diff --git a/src/unique.ts b/src/unique.ts index a06bd11..b6f139c 100644 --- a/src/unique.ts +++ b/src/unique.ts @@ -37,6 +37,16 @@ export async function getUniqueFields( const model = modelMap[collection]; if (!model) throw new Error("invalid collection"); + if (collection === "notifications" && field === "changes") { + const changeKeys = await model.aggregate([ + { $project: { changesKeys: { $objectToArray: "$changes" } } }, + { $unwind: "$changesKeys" }, + { $group: { _id: "$changesKeys.k" } }, + ]); + + return changeKeys.map((item) => item._id); + } + let values = await model.distinct(field, { tenantId: user.tenantId }); let matchedValues = [];