add assignedTo field to notifications
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { buildJsonSchemas } from "fastify-zod";
|
import { buildJsonSchemas } from "fastify-zod";
|
||||||
import mongoose from "mongoose";
|
import mongoose from "mongoose";
|
||||||
import { TypeOf, z } from "zod";
|
import { z } from "zod";
|
||||||
import { pageQueryParams } from "../pagination";
|
import { pageQueryParams } from "../pagination";
|
||||||
|
|
||||||
const notificationSchema = new mongoose.Schema({
|
const notificationSchema = new mongoose.Schema({
|
||||||
@@ -20,6 +20,10 @@ const notificationSchema = new mongoose.Schema({
|
|||||||
clientData: Object,
|
clientData: Object,
|
||||||
createdAt: Date,
|
createdAt: Date,
|
||||||
updatedAt: Date,
|
updatedAt: Date,
|
||||||
|
assignedTo: {
|
||||||
|
type: mongoose.Types.ObjectId,
|
||||||
|
ref: "user",
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
export const notificationFields = Object.keys(notificationSchema.paths).filter(
|
export const notificationFields = Object.keys(notificationSchema.paths).filter(
|
||||||
@@ -41,10 +45,12 @@ const createNotificationInput = z.object({
|
|||||||
county: z.object({}).passthrough(),
|
county: z.object({}).passthrough(),
|
||||||
client: z.string(),
|
client: z.string(),
|
||||||
clientData: z.object({}).passthrough(),
|
clientData: z.object({}).passthrough(),
|
||||||
|
assignedTo: z.string().optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
const updateNotificationInput = z.object({
|
const updateNotificationInput = z.object({
|
||||||
status: z.string(),
|
status: z.string().optional(),
|
||||||
|
assignedTo: z.string().optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export type CreateNotificationInput = z.infer<typeof createNotificationInput>;
|
export type CreateNotificationInput = z.infer<typeof createNotificationInput>;
|
||||||
|
|||||||
@@ -13,12 +13,16 @@ export async function createNotification(
|
|||||||
input: CreateNotificationInput,
|
input: CreateNotificationInput,
|
||||||
tenantId: string
|
tenantId: string
|
||||||
) {
|
) {
|
||||||
return await notificationModel.create({
|
const notification = await notificationModel.create({
|
||||||
...input,
|
...input,
|
||||||
pid: generateId(),
|
pid: generateId(),
|
||||||
tenantId: tenantId,
|
tenantId: tenantId,
|
||||||
createdAt: new Date(),
|
createdAt: new Date(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return await notificationModel
|
||||||
|
.findOne({ pid: notification.pid })
|
||||||
|
.populate({ path: "assignedTo", select: "pid name avatar" });
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function updateNotification(
|
export async function updateNotification(
|
||||||
@@ -26,14 +30,16 @@ export async function updateNotification(
|
|||||||
input: UpdateNotificationInput,
|
input: UpdateNotificationInput,
|
||||||
tenantId: string
|
tenantId: string
|
||||||
) {
|
) {
|
||||||
return await notificationModel.findOneAndUpdate(
|
return await notificationModel
|
||||||
|
.findOneAndUpdate(
|
||||||
{ $and: [{ tenantId: tenantId }, { pid: notifId }] },
|
{ $and: [{ tenantId: tenantId }, { pid: notifId }] },
|
||||||
{
|
{
|
||||||
...input,
|
...input,
|
||||||
updatedAt: new Date(),
|
updatedAt: new Date(),
|
||||||
},
|
},
|
||||||
{ new: true }
|
{ new: true }
|
||||||
);
|
)
|
||||||
|
.populate({ path: "assignedTo", select: "pid name avatar" });
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function listNotifications(
|
export async function listNotifications(
|
||||||
@@ -62,6 +68,14 @@ export async function listNotifications(
|
|||||||
|
|
||||||
pipeline.push(
|
pipeline.push(
|
||||||
...[
|
...[
|
||||||
|
{
|
||||||
|
$lookup: {
|
||||||
|
from: "users",
|
||||||
|
localField: "assignedTo",
|
||||||
|
foreignField: "_id",
|
||||||
|
as: "assignedTo",
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
$project: {
|
$project: {
|
||||||
_id: 1,
|
_id: 1,
|
||||||
@@ -78,6 +92,17 @@ export async function listNotifications(
|
|||||||
clientData: 1,
|
clientData: 1,
|
||||||
createdAt: 1,
|
createdAt: 1,
|
||||||
updatedAt: 1,
|
updatedAt: 1,
|
||||||
|
assignedTo: {
|
||||||
|
$let: {
|
||||||
|
vars: { assignedTo: { $arrayElemAt: ["$assignedTo", 0] } },
|
||||||
|
in: {
|
||||||
|
_id: "$$assignedTo._id",
|
||||||
|
pid: "$$assignedTo.pid",
|
||||||
|
name: "$$assignedTo.name",
|
||||||
|
avatar: "$$assignedTo.avatar",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user