From 12ec345f5b4b41a6e19c3fe99aebe2fb353fb36c Mon Sep 17 00:00:00 2001 From: Ly Tuan Kiet Date: Wed, 27 Nov 2024 01:32:18 +0700 Subject: [PATCH] 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. --- src/CenterMentor/centermentor.schema.ts | 44 +++++++++++-------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/src/CenterMentor/centermentor.schema.ts b/src/CenterMentor/centermentor.schema.ts index 2d5c9cf..9b55cc0 100644 --- a/src/CenterMentor/centermentor.schema.ts +++ b/src/CenterMentor/centermentor.schema.ts @@ -1,14 +1,11 @@ import { Inject, Injectable } from '@nestjs/common' -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 { PrismaService } from '../Prisma/prisma.service' import { MailService } from '../Mail/mail.service' import { JwtUtils } from '../common/utils/jwt.utils' +import { clerkClient } from '@clerk/express' +import { RedisService } from 'src/Redis/redis.service' @Injectable() export class CenterMentorSchema extends PothosSchema { constructor( @@ -16,6 +13,7 @@ export class CenterMentorSchema extends PothosSchema { private readonly prisma: PrismaService, private readonly mailService: MailService, private readonly jwtUtils: JwtUtils, + private readonly redisService: RedisService, ) { super() } @@ -57,8 +55,7 @@ export class CenterMentorSchema extends PothosSchema { init(): void { this.builder.queryFields((t) => ({ centerMentors: t.prismaField({ - description: - 'Retrieve a list of center mentors with optional filtering, ordering, and pagination.', + description: 'Retrieve a list of center mentors with optional filtering, ordering, and pagination.', type: [this.centerMentor()], args: this.builder.generator.findManyArgs('CenterMentor'), resolve: async (query, _root, args) => { @@ -154,10 +151,7 @@ export class CenterMentorSchema extends PothosSchema { throw new Error('Center not found') } // build signature - const token = this.jwtUtils.signTokenRS256( - { centerId: center.id, email: args.email }, - '1d', - ) + const token = this.jwtUtils.signTokenRS256({ centerId: center.id, email: args.email }, '1d') // build invite url const inviteUrl = `${process.env.CENTER_BASE_URL}/invite?token=${token}` // mail to user with params centerId, email @@ -184,22 +178,14 @@ export class CenterMentorSchema extends PothosSchema { resolve: async (_query, _root, args) => { return this.prisma.$transaction(async () => { // sign token - const token = this.jwtUtils.signTokenRS256( - { centerId: args.centerId, email: args.email }, - '1d', - ) + const token = this.jwtUtils.signTokenRS256({ centerId: args.centerId, email: args.email }, '1d') // build invite url const inviteUrl = `${process.env.CENTER_BASE_URL}/invite?token=${token}` // mail to user with params centerId, email - await this.mailService.sendTemplateEmail( - [args.email], - 'Invite to center', - 'MentorInvitation', - { - center_name: args.centerId, - invite_url: inviteUrl, - }, - ) + await this.mailService.sendTemplateEmail([args.email], 'Invite to center', 'MentorInvitation', { + center_name: args.centerId, + invite_url: inviteUrl, + }) return null }) }, @@ -277,6 +263,14 @@ export class CenterMentorSchema extends PothosSchema { 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 const updatedCenterMentor = await prisma.centerMentor.update({ where: {