enabled context and fix some api
This commit is contained in:
@@ -9,7 +9,6 @@ import { Builder } from '../Graphql/graphql.builder';
|
||||
import { PrismaService } from '../Prisma/prisma.service';
|
||||
import { MailService } from '../Mail/mail.service';
|
||||
import { JwtUtils } from '../common/utils/jwt.utils';
|
||||
import { UserSchema } from 'src/User/user.schema';
|
||||
@Injectable()
|
||||
export class CenterMentorSchema extends PothosSchema {
|
||||
constructor(
|
||||
@@ -17,7 +16,6 @@ export class CenterMentorSchema extends PothosSchema {
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly mailService: MailService,
|
||||
private readonly jwtUtils: JwtUtils,
|
||||
private readonly userSchema: UserSchema,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
@@ -48,7 +46,7 @@ export class CenterMentorSchema extends PothosSchema {
|
||||
managedService: t.relation('managedService', {
|
||||
description: 'The managed services of the center mentor.',
|
||||
}),
|
||||
adminNote: t.relation('AdminNote', {
|
||||
adminNote: t.relation('adminNote', {
|
||||
description: 'The admin note of the center mentor.',
|
||||
}),
|
||||
}),
|
||||
@@ -138,7 +136,7 @@ export class CenterMentorSchema extends PothosSchema {
|
||||
args: {
|
||||
email: t.arg({ type: 'String', required: true }),
|
||||
},
|
||||
resolve: async (query, root, args, ctx, info) => {
|
||||
resolve: async (query, root, args, ctx) => {
|
||||
return this.prisma.$transaction(async (prisma) => {
|
||||
// get centerId by user id from context
|
||||
const userId = ctx.me.id;
|
||||
@@ -180,8 +178,8 @@ export class CenterMentorSchema extends PothosSchema {
|
||||
centerId: t.arg({ type: 'String', required: true }),
|
||||
},
|
||||
description: 'Test invite center mentor.',
|
||||
resolve: async (query, root, args, ctx, info) => {
|
||||
return this.prisma.$transaction(async (prisma) => {
|
||||
resolve: async (query, root, args) => {
|
||||
return this.prisma.$transaction(async () => {
|
||||
// sign token
|
||||
const token = this.jwtUtils.signTokenRS256(
|
||||
{ centerId: args.centerId, email: args.email },
|
||||
@@ -204,7 +202,7 @@ export class CenterMentorSchema extends PothosSchema {
|
||||
},
|
||||
}),
|
||||
approveOrRejectCenterMentor: t.prismaField({
|
||||
type: this.userSchema.user(),
|
||||
type: this.centerMentor(),
|
||||
description: 'Approve or reject a center mentor.',
|
||||
args: {
|
||||
where: t.arg({
|
||||
@@ -216,27 +214,104 @@ export class CenterMentorSchema extends PothosSchema {
|
||||
},
|
||||
resolve: async (query, root, args, ctx, info) => {
|
||||
return this.prisma.$transaction(async (prisma) => {
|
||||
// validate input
|
||||
if (args.approved && !args.adminNote) {
|
||||
throw new Error('Admin note is required');
|
||||
}
|
||||
// get mentor info
|
||||
const mentor = await prisma.user.findUnique({
|
||||
where: args.where,
|
||||
});
|
||||
if (!mentor) {
|
||||
throw new Error('Mentor not found');
|
||||
}
|
||||
// get centerMentor
|
||||
const centerMentor = await prisma.centerMentor.findUnique({
|
||||
where: { mentorId: mentor.id },
|
||||
});
|
||||
if (!centerMentor) {
|
||||
throw new Error('Center mentor not found');
|
||||
}
|
||||
// get center
|
||||
const center = await prisma.center.findUnique({
|
||||
where: { id: centerMentor.centerId },
|
||||
});
|
||||
if (!center) {
|
||||
throw new Error('Center not found');
|
||||
}
|
||||
// get email
|
||||
const email = await prisma.user.findUnique({
|
||||
where: args.where,
|
||||
select: { email: true },
|
||||
});
|
||||
if (!email) {
|
||||
throw new Error('Email is required');
|
||||
}
|
||||
// if approved, update role to mentor
|
||||
if (args.approved) {
|
||||
return await prisma.user.update({
|
||||
// send mail to user
|
||||
await this.mailService.sendTemplateEmail(
|
||||
email.email,
|
||||
'Thông báo về việc được chấp nhận làm mentor',
|
||||
'MentorApproved',
|
||||
{
|
||||
CENTER_NAME: center.name,
|
||||
USER_NAME: mentor.name,
|
||||
},
|
||||
);
|
||||
// create adminNote
|
||||
const adminNote = await prisma.adminNote.create({
|
||||
data: {
|
||||
content: args.adminNote ?? '',
|
||||
mentorId: mentor.id,
|
||||
notedByUserId: ctx.me.id,
|
||||
},
|
||||
});
|
||||
const updatedUser = await prisma.user.update({
|
||||
where: args.where,
|
||||
data: {
|
||||
role: 'CENTER_MENTOR',
|
||||
updatedAt: new Date(),
|
||||
adminNote: {
|
||||
create: {
|
||||
content: args.adminNote ?? '',
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
// update centerMentor
|
||||
const updatedCenterMentor = await prisma.centerMentor.update({
|
||||
where: {
|
||||
mentorId_centerId: {
|
||||
mentorId: mentor.id,
|
||||
centerId: centerMentor.centerId,
|
||||
},
|
||||
},
|
||||
data: {
|
||||
adminNote: { connect: { id: adminNote.id } },
|
||||
},
|
||||
});
|
||||
return updatedCenterMentor;
|
||||
}
|
||||
// if rejected, update adminNote
|
||||
return await prisma.user.update({
|
||||
where: args.where,
|
||||
await this.mailService.sendTemplateEmail(
|
||||
email.email,
|
||||
'Thông báo về việc không được chấp nhận làm mentor',
|
||||
'MentorRejected',
|
||||
{
|
||||
CENTER_NAME: center.name,
|
||||
USER_NAME: mentor.name,
|
||||
},
|
||||
);
|
||||
return await prisma.centerMentor.update({
|
||||
where: {
|
||||
mentorId_centerId: {
|
||||
mentorId: mentor.id,
|
||||
centerId: centerMentor.centerId,
|
||||
},
|
||||
},
|
||||
data: {
|
||||
adminNote: {
|
||||
create: { content: args.adminNote ?? '' },
|
||||
create: {
|
||||
content: args.adminNote ?? '',
|
||||
notedByUserId: ctx.me.id,
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user