feat: add notification messaging for service approval and rejection in ServiceSchema

- Implemented user notification system by creating messages in the database when a service is approved or rejected.
- Utilized the new Message and PubSubEvent classes to handle notifications effectively.
- Enhanced user experience by informing users of service status changes through direct messages.
This commit is contained in:
2024-12-05 22:41:25 +07:00
parent 37b0086b4d
commit de46499288

View File

@@ -3,8 +3,10 @@ import { Pothos, PothosRef, PothosSchema, SchemaBuilderToken } from '@smatch-cor
import { Builder } from '../Graphql/graphql.builder' import { Builder } from '../Graphql/graphql.builder'
import { PrismaService } from '../Prisma/prisma.service' import { PrismaService } from '../Prisma/prisma.service'
import { MinioService } from '../Minio/minio.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 { MailService } from '../Mail/mail.service'
import { PubSubEvent } from 'src/common/pubsub/pubsub-event'
import { DateTimeUtils } from 'src/common/utils/datetime.utils'
@Injectable() @Injectable()
export class ServiceSchema extends PothosSchema { export class ServiceSchema extends PothosSchema {
constructor( constructor(
@@ -385,12 +387,52 @@ export class ServiceSchema extends PothosSchema {
SERVICE_NAME: service.name, SERVICE_NAME: service.name,
CENTER_NAME: center.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 { } else {
await this.mailService.sendTemplateEmail(emails, 'Thông báo về trạng thái dịch vụ', 'ServiceRejected', { await this.mailService.sendTemplateEmail(emails, 'Thông báo về trạng thái dịch vụ', 'ServiceRejected', {
SERVICE_NAME: service.name, SERVICE_NAME: service.name,
CENTER_NAME: center.name, CENTER_NAME: center.name,
ADMIN_NOTE: args.adminNote ?? 'Không có lý do', 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 return updatedService
}) })