From f224d05a369c6cc6e1bfc613468822cb766beca3 Mon Sep 17 00:00:00 2001 From: Akhil Meka Date: Tue, 6 May 2025 10:04:06 +0530 Subject: [PATCH] add assignedTo field to notifications --- src/notification/notification.schema.ts | 10 ++++-- src/notification/notification.service.ts | 43 +++++++++++++++++++----- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/notification/notification.schema.ts b/src/notification/notification.schema.ts index 2703c15..9c639e8 100644 --- a/src/notification/notification.schema.ts +++ b/src/notification/notification.schema.ts @@ -1,6 +1,6 @@ import { buildJsonSchemas } from "fastify-zod"; import mongoose from "mongoose"; -import { TypeOf, z } from "zod"; +import { z } from "zod"; import { pageQueryParams } from "../pagination"; const notificationSchema = new mongoose.Schema({ @@ -20,6 +20,10 @@ const notificationSchema = new mongoose.Schema({ clientData: Object, createdAt: Date, updatedAt: Date, + assignedTo: { + type: mongoose.Types.ObjectId, + ref: "user", + }, }); export const notificationFields = Object.keys(notificationSchema.paths).filter( @@ -41,10 +45,12 @@ const createNotificationInput = z.object({ county: z.object({}).passthrough(), client: z.string(), clientData: z.object({}).passthrough(), + assignedTo: z.string().optional(), }); const updateNotificationInput = z.object({ - status: z.string(), + status: z.string().optional(), + assignedTo: z.string().optional(), }); export type CreateNotificationInput = z.infer; diff --git a/src/notification/notification.service.ts b/src/notification/notification.service.ts index 95570cc..241e0a5 100644 --- a/src/notification/notification.service.ts +++ b/src/notification/notification.service.ts @@ -13,12 +13,16 @@ export async function createNotification( input: CreateNotificationInput, tenantId: string ) { - return await notificationModel.create({ + const notification = await notificationModel.create({ ...input, pid: generateId(), tenantId: tenantId, createdAt: new Date(), }); + + return await notificationModel + .findOne({ pid: notification.pid }) + .populate({ path: "assignedTo", select: "pid name avatar" }); } export async function updateNotification( @@ -26,14 +30,16 @@ export async function updateNotification( input: UpdateNotificationInput, tenantId: string ) { - return await notificationModel.findOneAndUpdate( - { $and: [{ tenantId: tenantId }, { pid: notifId }] }, - { - ...input, - updatedAt: new Date(), - }, - { new: true } - ); + return await notificationModel + .findOneAndUpdate( + { $and: [{ tenantId: tenantId }, { pid: notifId }] }, + { + ...input, + updatedAt: new Date(), + }, + { new: true } + ) + .populate({ path: "assignedTo", select: "pid name avatar" }); } export async function listNotifications( @@ -62,6 +68,14 @@ export async function listNotifications( pipeline.push( ...[ + { + $lookup: { + from: "users", + localField: "assignedTo", + foreignField: "_id", + as: "assignedTo", + }, + }, { $project: { _id: 1, @@ -78,6 +92,17 @@ export async function listNotifications( clientData: 1, createdAt: 1, updatedAt: 1, + assignedTo: { + $let: { + vars: { assignedTo: { $arrayElemAt: ["$assignedTo", 0] } }, + in: { + _id: "$$assignedTo._id", + pid: "$$assignedTo.pid", + name: "$$assignedTo.name", + avatar: "$$assignedTo.avatar", + }, + }, + }, }, }, {