diff --git a/src/Workshop/workshop.schema.ts b/src/Workshop/workshop.schema.ts index 460ba30..ae724c7 100644 --- a/src/Workshop/workshop.schema.ts +++ b/src/Workshop/workshop.schema.ts @@ -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 }, }),