diff --git a/src/Graphql/graphql.module.ts b/src/Graphql/graphql.module.ts index 17d3c1a..fe968ca 100644 --- a/src/Graphql/graphql.module.ts +++ b/src/Graphql/graphql.module.ts @@ -1,4 +1,4 @@ -import { Global, MiddlewareConsumer, Module } from '@nestjs/common'; +import { Global, Logger, MiddlewareConsumer, Module } from '@nestjs/common'; import { ApolloDriverConfig } from '@nestjs/apollo'; import { Builder } from './graphql.builder'; @@ -9,6 +9,7 @@ import { ChatroomModule } from '../ChatRoom/chatroom.module'; import { CommonModule } from '../common/common.module'; import { GraphQLModule } from '@nestjs/graphql'; import { GraphQLValidationMiddleware } from '../middlewares/graphql.middleware'; +import { GraphqlService } from './graphql.service'; import { ManagedServiceModule } from '../ManagedService/managedservice.module'; import { MessageModule } from '../Message/message.module'; import { MilestoneModule } from '../Milestone/milestone.module'; @@ -20,6 +21,7 @@ import { PrismaCrudGenerator } from './graphql.generator'; import { PrismaModule } from '../Prisma/prisma.module'; import { PrismaService } from '../Prisma/prisma.service'; import { RefundTicketModule } from '../RefundTicket/refundticket.module'; +import { Request } from 'express'; import { ResumeModule } from '../Resume/resume.module'; import { ScheduleModule } from '../Schedule/schedule.module'; import { ServiceAndCategoryModule } from '../ServiceAndCategory/serviceandcategory.module'; @@ -77,12 +79,18 @@ import { initContextCache } from '@pothos/core'; subscriptions: { 'graphql-ws': true, }, - context: async () => ({ + context: async (req: Request) => ({ ...initContextCache(), + me: await new GraphqlService(new PrismaService()).acquireContext(req), }), }), ], providers: [ + { + provide: GraphqlService, + useFactory: (prisma: PrismaService) => new GraphqlService(prisma), + inject: [PrismaService], + }, { provide: Builder, useFactory: (prisma: PrismaService) => new Builder(prisma), diff --git a/src/Graphql/graphql.service.ts b/src/Graphql/graphql.service.ts index e6ea6b4..0132e17 100644 --- a/src/Graphql/graphql.service.ts +++ b/src/Graphql/graphql.service.ts @@ -1,4 +1,41 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, UnauthorizedException } from '@nestjs/common'; + +import { PrismaService } from '../Prisma/prisma.service'; +import { Request } from 'express'; +import { clerkClient } from '@clerk/express'; @Injectable() -export class GraphqlService {} +export class GraphqlService { + constructor(private readonly prisma: PrismaService) {} + + async acquireContext(req: Request) { + // development flag + const isDevelopment = process.env.NODE_ENV === 'development'; + if (isDevelopment) { + return null; + } + // get x-session-id from headers + let sessionId; + try { + sessionId = req.headers['x-session-id']; + //eslint-disable-next-line @typescript-eslint/no-unused-vars + } catch (error) { + throw new UnauthorizedException('Must provide a session ID'); + } + if (!sessionId) { + throw new UnauthorizedException('Session ID is required'); + } + // check if the token is valid + const session = await clerkClient.sessions.getSession(sessionId as string); + if (!session) { + throw new UnauthorizedException('Invalid session'); + } + const user = await this.prisma.user.findUnique({ + where: { id: session.userId }, + }); + if (!user) { + throw new UnauthorizedException('User not found'); + } + return user; + } +} diff --git a/src/Mail/mail.service.ts b/src/Mail/mail.service.ts index ad66c34..a569b3f 100644 --- a/src/Mail/mail.service.ts +++ b/src/Mail/mail.service.ts @@ -2,6 +2,7 @@ import { Injectable, Logger } from '@nestjs/common'; import { MailerService } from '@nestjs-modules/mailer'; import { OpenaiService } from '../OpenAI/openai.service'; +import { User } from '@prisma/client'; @Injectable() export class MailService { @@ -34,7 +35,7 @@ export class MailService { to: string, subject: string, template: string, - context: any, + context: User, ) { try { const result = await this.mailerService.sendMail({ diff --git a/src/Service/service.schema.ts b/src/Service/service.schema.ts index 8b10f3b..4864c5c 100644 --- a/src/Service/service.schema.ts +++ b/src/Service/service.schema.ts @@ -60,6 +60,9 @@ export class ServiceSchema extends PothosSchema { type: ServiceStatus, description: 'The status of the service.', }), + isActive: t.exposeBoolean('isActive', { + description: 'Whether the service is active.', + }), createdAt: t.expose('createdAt', { type: 'DateTime', description: 'The date and time the service was created.',