feat: add validation to prevent center owners and mentors from being invited to their own centers

- Implemented checks in CenterMentorSchema to block center owners from being invited as mentors.
- Added validation to prevent mentors from being invited to their own centers or centers they already mentor.
- Enhanced error handling to provide clear feedback when invitation conditions are not met.
This commit is contained in:
2024-12-03 16:18:26 +07:00
parent 7021aa7f20
commit 2b92f3bf5f

View File

@@ -147,6 +147,20 @@ export class CenterMentorSchema extends PothosSchema {
if (!userId) {
throw new Error('User ID is required')
}
// block invite center owner
const centerOwner = await prisma.center.findUnique({
where: { centerOwnerId: userId },
})
if (centerOwner) {
throw new Error('Center owner cannot be invited as a mentor')
}
// block invite mentor to own center and owner from another center
const centerMentor = await prisma.centerMentor.findUnique({
where: { mentorId: userId },
})
if (centerMentor) {
throw new Error('Mentor already has a center')
}
// get centerId by user id
const center = await prisma.center.findUnique({
where: { centerOwnerId: userId },