diff --git a/src/Service/service.schema.ts b/src/Service/service.schema.ts index 1bef4f1..0936bbd 100644 --- a/src/Service/service.schema.ts +++ b/src/Service/service.schema.ts @@ -3,8 +3,10 @@ import { Pothos, PothosRef, PothosSchema, SchemaBuilderToken } from '@smatch-cor import { Builder } from '../Graphql/graphql.builder' import { PrismaService } from '../Prisma/prisma.service' import { MinioService } from '../Minio/minio.service' -import { Role, ServiceStatus } from '@prisma/client' +import { Role, ServiceStatus, Message, MessageContextType, MessageType } from '@prisma/client' import { MailService } from '../Mail/mail.service' +import { PubSubEvent } from 'src/common/pubsub/pubsub-event' +import { DateTimeUtils } from 'src/common/utils/datetime.utils' @Injectable() export class ServiceSchema extends PothosSchema { constructor( @@ -385,12 +387,52 @@ export class ServiceSchema extends PothosSchema { SERVICE_NAME: service.name, CENTER_NAME: center.name, }) + // get user ids from mentorIds + const userIds = mentorIds.map((id) => id) + // send notification to user using context + userIds.forEach(async (id) => { + // add message to database + const message = await this.prisma.message.create({ + data: { + senderId: ctx.http.me?.id ?? '', + recipientId: id, + type: MessageType.TEXT, + content: `Dịch vụ ${service.name} của bạn đã được chấp thuận`, + sentAt: DateTimeUtils.nowAsJSDate(), + context: MessageContextType.NOTIFICATION, + metadata: { + serviceId: service.id, + }, + }, + }) + ctx.http.pubSub.publish(`${PubSubEvent.NOTIFICATION}.${id}`, message) + }) } else { await this.mailService.sendTemplateEmail(emails, 'Thông báo về trạng thái dịch vụ', 'ServiceRejected', { SERVICE_NAME: service.name, CENTER_NAME: center.name, ADMIN_NOTE: args.adminNote ?? 'Không có lý do', }) + // send notification to user using context + // get user ids from mentorIds + const userIds = mentorIds.map((id) => id) + userIds.forEach(async (id) => { + // add message to database + const message = await this.prisma.message.create({ + data: { + senderId: ctx.http.me?.id ?? '', + recipientId: id, + type: MessageType.TEXT, + content: `Dịch vụ ${service.name} của bạn đã bị từ chối`, + sentAt: DateTimeUtils.nowAsJSDate(), + context: MessageContextType.NOTIFICATION, + metadata: { + serviceId: service.id, + }, + }, + }) + ctx.http.pubSub.publish(`${PubSubEvent.NOTIFICATION}.${id}`, message) + }) } return updatedService })