From fb03e8c265055e78e747e603c0d2c29345777a38 Mon Sep 17 00:00:00 2001 From: Akhil Meka Date: Thu, 27 Mar 2025 16:40:56 +0530 Subject: [PATCH] unique field route for notifications --- src/notification/notification.route.ts | 30 ++++++++++++++++++++++++ src/notification/notification.service.ts | 28 ++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/src/notification/notification.route.ts b/src/notification/notification.route.ts index 52425ba..c9d2e3d 100644 --- a/src/notification/notification.route.ts +++ b/src/notification/notification.route.ts @@ -5,6 +5,7 @@ import { listNotificationsHandler, updateNotificationHandler, } from "./notification.controller"; +import { getUniqueValuesNotification } from "./notification.service"; export async function notificationRoutes(fastify: FastifyInstance) { fastify.post( @@ -43,4 +44,33 @@ export async function notificationRoutes(fastify: FastifyInstance) { }, updateNotificationHandler ); + + fastify.get( + "/fields/:field", + { + schema: { + params: { + type: "object", + properties: { + field: { type: "string" }, + }, + }, + }, + config: { requiredClaims: ["notification:read"] }, + preHandler: [fastify.authorize], + }, + async (req, res) => { + const { field } = req.params as { field: string }; + + try { + const uniqueValues = await getUniqueValuesNotification( + field, + req.user.tenantId + ); + return res.code(200).send(uniqueValues); + } catch (err) { + return err; + } + } + ); } diff --git a/src/notification/notification.service.ts b/src/notification/notification.service.ts index 64c91ab..9ad5bdb 100644 --- a/src/notification/notification.service.ts +++ b/src/notification/notification.service.ts @@ -1,4 +1,6 @@ +import { orgModel } from "../organization/organization.schema"; import { getFilterObject, getSortObject, PageQueryParams } from "../pagination"; +import { userModel } from "../user/user.schema"; import { generateId } from "../utils/id"; import { CreateNotificationInput, @@ -87,3 +89,29 @@ export async function listNotifications( }, }; } + +export async function getUniqueValuesNotification( + field: string, + tenenatId: string +) { + let values = await notificationModel.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 (matchedValues.length > 0) { + const newValues = {}; + for (const item of matchedValues) { + newValues[item.id] = item.name; + } + return newValues; + } + + return values; +}