feat: enhance workshop notification system for customers

- Updated WorkshopSchema to notify customers when a workshop is scheduled, improving engagement and communication.
- Added logic to fetch active services and include center information in the notification process.
- Implemented message creation for customers with the role of CUSTOMER, ensuring they receive timely updates about workshops.
- Utilized PubSub for real-time notifications, enhancing user experience and interaction with the platform.
This commit is contained in:
2024-12-09 19:45:49 +07:00
parent f5ee243dc7
commit 13e525e92f

View File

@@ -1,7 +1,9 @@
import { Inject, Injectable } from '@nestjs/common'
import { Role } from '@prisma/client'
import { MessageContextType, MessageType, Role } from '@prisma/client'
import { Pothos, PothosRef, PothosSchema, SchemaBuilderToken } from '@smatch-corp/nestjs-pothos'
import { MinioService } from 'src/Minio/minio.service'
import { PubSubEvent } from 'src/common/pubsub/pubsub-event'
import { DateTimeUtils } from 'src/common/utils/datetime.utils'
import { Builder } from '../Graphql/graphql.builder'
import { PrismaService } from '../Prisma/prisma.service'
@@ -149,6 +151,9 @@ export class WorkshopSchema extends PothosSchema {
// check if service is active
const service = await this.prisma.service.findUnique({
where: { id: args.input.service.connect.id },
include: {
center: true,
},
})
if (!service || !service.isActive) {
throw new Error('Service is not active')
@@ -164,6 +169,23 @@ export class WorkshopSchema extends PothosSchema {
workshopId: workshop.id,
},
})
// notify all user has role CUSTOMER
const customers = await this.prisma.user.findMany({
where: { role: Role.CUSTOMER },
})
for (const customer of customers) {
const message = await this.prisma.message.create({
data: {
senderId: ctx.http.me?.id ?? '',
recipientId: customer.id,
type: MessageType.TEXT,
content: `Workshop ${workshop.title} đã được lên lịch do ${service.center.name} tổ chức. Nhanh tay đăng kí ngay!`,
sentAt: DateTimeUtils.nowAsJSDate(),
context: MessageContextType.NOTIFICATION,
},
})
ctx.http.pubSub.publish(`${PubSubEvent.NOTIFICATION}.${customer.id}`, message)
}
return workshop
},
}),