feat: enhance notification system and role-based access across schemas

- Updated MessageSchema to include context-aware filtering for notifications, allowing retrieval based on recipientId for NOTIFICATION and SYSTEM contexts.
- Enhanced RefundTicketSchema to notify moderators upon refund requests, improving communication and response times.
- Modified ResumeSchema to send notifications to mentors and center owners when a new resume is submitted, ensuring timely updates.
- Improved ServiceSchema to notify center owners and mentors about service approvals, enhancing user engagement and awareness.
- Implemented role-based access control checks across schemas to ensure only authorized users can perform specific actions, enhancing security and user experience.
This commit is contained in:
2024-12-09 19:38:36 +07:00
parent 9732d7b904
commit 092a15753b
4 changed files with 115 additions and 12 deletions

View File

@@ -1,6 +1,7 @@
import { Inject, Injectable } from '@nestjs/common'
import { OrderStatus, PaymentStatus, RefundTicketStatus, Role } from '@prisma/client'
import { MessageContextType, MessageType, OrderStatus, PaymentStatus, RefundTicketStatus, Role } from '@prisma/client'
import { Pothos, PothosRef, PothosSchema, SchemaBuilderToken } from '@smatch-corp/nestjs-pothos'
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'
@@ -122,7 +123,7 @@ export class RefundTicketSchema extends PothosSchema {
if (ctx.isSubscription) {
throw new Error('Subscription is not allowed')
}
// Check if the user is a customer or a center mentor
if (ctx.http.me?.role !== Role.CUSTOMER && ctx.http.me?.role !== Role.CENTER_MENTOR) {
throw new Error('Only customers and center mentors can request refund')
@@ -165,9 +166,9 @@ export class RefundTicketSchema extends PothosSchema {
const orderDate = DateTimeUtils.fromDate(order.createdAt)
const diffTime = Math.abs(now.diff(orderDate).toMillis())
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24))
let refundAmount = 0
// Special handling for center mentors - full refund always allowed
if (ctx.http.me?.role === Role.CENTER_MENTOR) {
refundAmount = order.total
@@ -217,7 +218,23 @@ export class RefundTicketSchema extends PothosSchema {
bankName: bankName,
},
})
// notify all Moderator
const moderators = await this.prisma.user.findMany({
where: { role: Role.MODERATOR },
})
for (const moderator of moderators) {
const message = await this.prisma.message.create({
data: {
senderId: ctx.http.me?.id ?? '',
recipientId: moderator.id,
type: MessageType.TEXT,
content: `Có yêu cầu hoàn tiền mới từ ${ctx.http.me?.name}`,
sentAt: DateTimeUtils.nowAsJSDate(),
context: MessageContextType.NOTIFICATION,
},
})
ctx.http.pubSub.publish(`${PubSubEvent.NOTIFICATION}.${moderator.id}`, message)
}
return refundTicket
},
}),