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.
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { Inject, Injectable, Logger } from '@nestjs/common'
|
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 { Pothos, PothosRef, PothosSchema, SchemaBuilderToken } from '@smatch-corp/nestjs-pothos'
|
||||||
import { Builder } from '../Graphql/graphql.builder'
|
import { Builder } from '../Graphql/graphql.builder'
|
||||||
import { PrismaService } from '../Prisma/prisma.service'
|
import { PrismaService } from '../Prisma/prisma.service'
|
||||||
@@ -126,7 +126,7 @@ export class PersonalMilestoneSchema extends PothosSchema {
|
|||||||
throw new Error('Cannot get your info')
|
throw new Error('Cannot get your info')
|
||||||
}
|
}
|
||||||
const userId = ctx.http.me.id
|
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: args.data.map((data) => ({
|
||||||
...data,
|
...data,
|
||||||
userId,
|
userId,
|
||||||
@@ -134,6 +134,44 @@ export class PersonalMilestoneSchema extends PothosSchema {
|
|||||||
})),
|
})),
|
||||||
skipDuplicates: true,
|
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,
|
||||||
|
})
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
}))
|
}))
|
||||||
|
|||||||
@@ -261,30 +261,35 @@ export class ServiceSchema extends PothosSchema {
|
|||||||
if (!centerOwner) {
|
if (!centerOwner) {
|
||||||
throw new Error('Center owner not found')
|
throw new Error('Center owner not found')
|
||||||
}
|
}
|
||||||
const centerMentor = await this.prisma.centerMentor.findMany({
|
// const centerMentor = await this.prisma.centerMentor.findMany({
|
||||||
where: { centerId: service.centerId },
|
// 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 messages = await this.prisma.message.createMany({
|
||||||
const mentorEmails = await this.prisma.user.findMany({
|
data: moderatorIds.map((moderator) => ({
|
||||||
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: {
|
|
||||||
senderId: ctx.http.me?.id ?? '',
|
senderId: ctx.http.me?.id ?? '',
|
||||||
recipientId: centerOwner.id,
|
recipientId: moderator.id,
|
||||||
type: MessageType.TEXT,
|
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(),
|
sentAt: DateTimeUtils.nowAsJSDate(),
|
||||||
context: MessageContextType.NOTIFICATION,
|
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
|
return service
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
|||||||
Reference in New Issue
Block a user