Refactor CenterMentorSchema to streamline code and improve clarity. Consolidate import statements, enhance query field descriptions, and optimize token generation logic. Integrate Redis service for session management, ensuring cache invalidation for active Clerk sessions. Update email sending logic for mentor invitations to improve readability.
This commit is contained in:
@@ -1,14 +1,11 @@
|
|||||||
import { Inject, Injectable } from '@nestjs/common'
|
import { Inject, Injectable } from '@nestjs/common'
|
||||||
import {
|
import { Pothos, PothosRef, PothosSchema, SchemaBuilderToken } from '@smatch-corp/nestjs-pothos'
|
||||||
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'
|
||||||
import { MailService } from '../Mail/mail.service'
|
import { MailService } from '../Mail/mail.service'
|
||||||
import { JwtUtils } from '../common/utils/jwt.utils'
|
import { JwtUtils } from '../common/utils/jwt.utils'
|
||||||
|
import { clerkClient } from '@clerk/express'
|
||||||
|
import { RedisService } from 'src/Redis/redis.service'
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class CenterMentorSchema extends PothosSchema {
|
export class CenterMentorSchema extends PothosSchema {
|
||||||
constructor(
|
constructor(
|
||||||
@@ -16,6 +13,7 @@ export class CenterMentorSchema extends PothosSchema {
|
|||||||
private readonly prisma: PrismaService,
|
private readonly prisma: PrismaService,
|
||||||
private readonly mailService: MailService,
|
private readonly mailService: MailService,
|
||||||
private readonly jwtUtils: JwtUtils,
|
private readonly jwtUtils: JwtUtils,
|
||||||
|
private readonly redisService: RedisService,
|
||||||
) {
|
) {
|
||||||
super()
|
super()
|
||||||
}
|
}
|
||||||
@@ -57,8 +55,7 @@ export class CenterMentorSchema extends PothosSchema {
|
|||||||
init(): void {
|
init(): void {
|
||||||
this.builder.queryFields((t) => ({
|
this.builder.queryFields((t) => ({
|
||||||
centerMentors: t.prismaField({
|
centerMentors: t.prismaField({
|
||||||
description:
|
description: 'Retrieve a list of center mentors with optional filtering, ordering, and pagination.',
|
||||||
'Retrieve a list of center mentors with optional filtering, ordering, and pagination.',
|
|
||||||
type: [this.centerMentor()],
|
type: [this.centerMentor()],
|
||||||
args: this.builder.generator.findManyArgs('CenterMentor'),
|
args: this.builder.generator.findManyArgs('CenterMentor'),
|
||||||
resolve: async (query, _root, args) => {
|
resolve: async (query, _root, args) => {
|
||||||
@@ -154,10 +151,7 @@ export class CenterMentorSchema extends PothosSchema {
|
|||||||
throw new Error('Center not found')
|
throw new Error('Center not found')
|
||||||
}
|
}
|
||||||
// build signature
|
// build signature
|
||||||
const token = this.jwtUtils.signTokenRS256(
|
const token = this.jwtUtils.signTokenRS256({ centerId: center.id, email: args.email }, '1d')
|
||||||
{ centerId: center.id, email: args.email },
|
|
||||||
'1d',
|
|
||||||
)
|
|
||||||
// build invite url
|
// build invite url
|
||||||
const inviteUrl = `${process.env.CENTER_BASE_URL}/invite?token=${token}`
|
const inviteUrl = `${process.env.CENTER_BASE_URL}/invite?token=${token}`
|
||||||
// mail to user with params centerId, email
|
// mail to user with params centerId, email
|
||||||
@@ -184,22 +178,14 @@ export class CenterMentorSchema extends PothosSchema {
|
|||||||
resolve: async (_query, _root, args) => {
|
resolve: async (_query, _root, args) => {
|
||||||
return this.prisma.$transaction(async () => {
|
return this.prisma.$transaction(async () => {
|
||||||
// sign token
|
// sign token
|
||||||
const token = this.jwtUtils.signTokenRS256(
|
const token = this.jwtUtils.signTokenRS256({ centerId: args.centerId, email: args.email }, '1d')
|
||||||
{ centerId: args.centerId, email: args.email },
|
|
||||||
'1d',
|
|
||||||
)
|
|
||||||
// build invite url
|
// build invite url
|
||||||
const inviteUrl = `${process.env.CENTER_BASE_URL}/invite?token=${token}`
|
const inviteUrl = `${process.env.CENTER_BASE_URL}/invite?token=${token}`
|
||||||
// mail to user with params centerId, email
|
// mail to user with params centerId, email
|
||||||
await this.mailService.sendTemplateEmail(
|
await this.mailService.sendTemplateEmail([args.email], 'Invite to center', 'MentorInvitation', {
|
||||||
[args.email],
|
center_name: args.centerId,
|
||||||
'Invite to center',
|
invite_url: inviteUrl,
|
||||||
'MentorInvitation',
|
})
|
||||||
{
|
|
||||||
center_name: args.centerId,
|
|
||||||
invite_url: inviteUrl,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
return null
|
return null
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
@@ -277,6 +263,14 @@ export class CenterMentorSchema extends PothosSchema {
|
|||||||
updatedAt: new Date(),
|
updatedAt: new Date(),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
// get active clerk session and invalidate cache
|
||||||
|
const sessionList = await clerkClient.sessions.getSessionList({
|
||||||
|
userId: mentor.id,
|
||||||
|
})
|
||||||
|
// clear all session cache in redis
|
||||||
|
sessionList.data.forEach(async (session) => {
|
||||||
|
await this.redisService.del(session.id)
|
||||||
|
})
|
||||||
// update centerMentor
|
// update centerMentor
|
||||||
const updatedCenterMentor = await prisma.centerMentor.update({
|
const updatedCenterMentor = await prisma.centerMentor.update({
|
||||||
where: {
|
where: {
|
||||||
|
|||||||
Reference in New Issue
Block a user