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:
@@ -1,7 +1,9 @@
|
|||||||
import { Inject, Injectable } from '@nestjs/common'
|
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 { Pothos, PothosRef, PothosSchema, SchemaBuilderToken } from '@smatch-corp/nestjs-pothos'
|
||||||
import { MinioService } from 'src/Minio/minio.service'
|
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 { Builder } from '../Graphql/graphql.builder'
|
||||||
import { PrismaService } from '../Prisma/prisma.service'
|
import { PrismaService } from '../Prisma/prisma.service'
|
||||||
|
|
||||||
@@ -149,6 +151,9 @@ export class WorkshopSchema extends PothosSchema {
|
|||||||
// check if service is active
|
// check if service is active
|
||||||
const service = await this.prisma.service.findUnique({
|
const service = await this.prisma.service.findUnique({
|
||||||
where: { id: args.input.service.connect.id },
|
where: { id: args.input.service.connect.id },
|
||||||
|
include: {
|
||||||
|
center: true,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
if (!service || !service.isActive) {
|
if (!service || !service.isActive) {
|
||||||
throw new Error('Service is not active')
|
throw new Error('Service is not active')
|
||||||
@@ -164,6 +169,23 @@ export class WorkshopSchema extends PothosSchema {
|
|||||||
workshopId: workshop.id,
|
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
|
return workshop
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
|||||||
Reference in New Issue
Block a user