From ffdf6f1204cf68fdd2647cb83c07d296b7308aad Mon Sep 17 00:00:00 2001 From: Rohit Sah Date: Tue, 19 Sep 2023 21:33:40 +0545 Subject: [PATCH] refactor(api): :recycle: added tenantId in session --- apps/api/src/resolvers/admin.ts | 10 ++++++++-- apps/api/src/types.ts | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/apps/api/src/resolvers/admin.ts b/apps/api/src/resolvers/admin.ts index 342f717..212fc8e 100644 --- a/apps/api/src/resolvers/admin.ts +++ b/apps/api/src/resolvers/admin.ts @@ -17,7 +17,7 @@ import { Tenant } from "../entities/Tenant"; export class AdminResolver { @Query(() => User, { nullable: true }) async meStaff(@Ctx() { req }: MyContext) { - if (!req.session?.userId) { + if (!req.session?.userId || !req.session?.tenantId) { return null; } const user = await User.findOne({ @@ -26,7 +26,12 @@ export class AdminResolver { tenant: true, }, }, - where: { id: req.session?.userId }, + where: { + id: req.session?.userId, + staff: { + tenantId: req.session?.tenantId, + }, + }, }); if (!user?.staff.tenantId) { return null; @@ -84,6 +89,7 @@ export class AdminResolver { }; } req.session.userId = user.id; + req.session.tenantId = user.staff.tenantId; return { user }; } diff --git a/apps/api/src/types.ts b/apps/api/src/types.ts index d8d35da..aae4915 100644 --- a/apps/api/src/types.ts +++ b/apps/api/src/types.ts @@ -3,7 +3,7 @@ import { Session } from "express-session"; import { type Redis } from "ioredis"; export type MyContext = { - req: Request & { session?: Session & { userId?: number } }; + req: Request & { session?: Session & { userId?: number; tenantId?: number } }; res: Response; redis: Redis; };