From 2eb291996511b3b3cf36f0ffc61f265a3cb7c3dc Mon Sep 17 00:00:00 2001 From: Ly Tuan Kiet Date: Fri, 13 Dec 2024 19:08:15 +0700 Subject: [PATCH] feat: enhance PersonalMilestone and Service schemas with new functionalities - Added a new mutation to update personal milestones and change schedule status to IN_PROGRESS in PersonalMilestoneSchema. - Refactored service notification logic to send messages to moderators upon service approval in ServiceSchema, improving communication flow. - Cleaned up commented-out code for better readability and maintainability. These changes enhance the GraphQL API by improving milestone management and service notification processes, ensuring better user experience and state management. --- .../personalmilestone.schema.ts | 42 ++++++++++++++++++- src/Service/service.schema.ts | 41 ++++++++++-------- 2 files changed, 63 insertions(+), 20 deletions(-) diff --git a/src/PersonalMilestone/personalmilestone.schema.ts b/src/PersonalMilestone/personalmilestone.schema.ts index 3d6aaca..859ec4a 100644 --- a/src/PersonalMilestone/personalmilestone.schema.ts +++ b/src/PersonalMilestone/personalmilestone.schema.ts @@ -1,5 +1,5 @@ import { Inject, Injectable, Logger } from '@nestjs/common' -import { PersonalMilestoneStatus } from '@prisma/client' +import { PersonalMilestoneStatus, ScheduleStatus } from '@prisma/client' import { Pothos, PothosRef, PothosSchema, SchemaBuilderToken } from '@smatch-corp/nestjs-pothos' import { Builder } from '../Graphql/graphql.builder' import { PrismaService } from '../Prisma/prisma.service' @@ -126,7 +126,7 @@ export class PersonalMilestoneSchema extends PothosSchema { throw new Error('Cannot get your info') } const userId = ctx.http.me.id - return await this.prisma.personalMilestone.createManyAndReturn({ + const result = await this.prisma.personalMilestone.createManyAndReturn({ data: args.data.map((data) => ({ ...data, userId, @@ -134,6 +134,44 @@ export class PersonalMilestoneSchema extends PothosSchema { })), skipDuplicates: true, }) + // update schedule status to IN_PROGRESS + await this.prisma.schedule.update({ + where: { id: args.scheduleId }, + data: { status: ScheduleStatus.IN_PROGRESS }, + }) + return result + }, + }), + updatePersonalMilestone: t.prismaField({ + type: this.personalMilestone(), + args: { + where: t.arg({ + type: this.builder.generator.getWhereUnique('PersonalMilestone'), + description: 'The where clause for the personal milestone.', + required: true, + }), + data: t.arg({ + type: this.builder.generator.getUpdateInput('PersonalMilestone'), + description: 'The data for the personal milestone.', + required: true, + }), + }, + description: 'Update a personal milestone.', + resolve: async (_query, _root, args, ctx, _info) => { + if (ctx.isSubscription) { + throw new Error('Not allowed') + } + if (!ctx.http.me) { + throw new Error('Cannot get your info') + } + const userId = ctx.http.me.id + return this.prisma.personalMilestone.update({ + where: { + ...args.where, + userId, + }, + data: args.data, + }) }, }), })) diff --git a/src/Service/service.schema.ts b/src/Service/service.schema.ts index 99005bc..e378511 100644 --- a/src/Service/service.schema.ts +++ b/src/Service/service.schema.ts @@ -261,30 +261,35 @@ export class ServiceSchema extends PothosSchema { if (!centerOwner) { throw new Error('Center owner not found') } - const centerMentor = await this.prisma.centerMentor.findMany({ - where: { centerId: service.centerId }, + // const centerMentor = await this.prisma.centerMentor.findMany({ + // where: { centerId: service.centerId }, + // }) + // const mentorIds = centerMentor.map((mentor) => mentor.mentorId) + // const mentorEmails = await this.prisma.user.findMany({ + // where: { id: { in: mentorIds } }, + // }) + // const emails = [centerOwner.email, ...mentorEmails.map((mentor) => mentor.email)] + // await this.mailService.sendTemplateEmail(emails, 'Thông báo về trạng thái dịch vụ', 'ServiceApproved', { + // SERVICE_NAME: service.name, + // CENTER_NAME: center.name, + // }) + // send notification to all moderator using context + const moderatorIds = await this.prisma.user.findMany({ + where: { role: Role.MODERATOR }, }) - const mentorIds = centerMentor.map((mentor) => mentor.mentorId) - const mentorEmails = await this.prisma.user.findMany({ - where: { id: { in: mentorIds } }, - }) - const emails = [centerOwner.email, ...mentorEmails.map((mentor) => mentor.email)] - await this.mailService.sendTemplateEmail(emails, 'Thông báo về trạng thái dịch vụ', 'ServiceApproved', { - SERVICE_NAME: service.name, - CENTER_NAME: center.name, - }) - // send notification to all mentor or center owner for the center using context - const message = await this.prisma.message.create({ - data: { + const messages = await this.prisma.message.createMany({ + data: moderatorIds.map((moderator) => ({ senderId: ctx.http.me?.id ?? '', - recipientId: centerOwner.id, + recipientId: moderator.id, type: MessageType.TEXT, - content: `Dịch vụ ${service.name} của bạn đã được chấp thuận`, + content: `Có một dịch vụ mới với tên ${service.name} được đăng tải bởi ${center.name}`, sentAt: DateTimeUtils.nowAsJSDate(), context: MessageContextType.NOTIFICATION, - }, + })), + }) + moderatorIds.forEach((moderator) => { + ctx.http.pubSub.publish(`${PubSubEvent.NOTIFICATION}.${moderator.id}`, messages) }) - ctx.http.pubSub.publish(`${PubSubEvent.NOTIFICATION}.${centerOwner.id}`, message) return service }, }),