From 9cd806ff61de5a919b552e4606a18e307bc2e3ae Mon Sep 17 00:00:00 2001 From: Anik Dhabal Babu <81948346+anikdhabal@users.noreply.github.com> Date: Mon, 28 Jul 2025 18:47:14 +0530 Subject: [PATCH] fix: CSV generation from insights returns empty results (#22775) * fix: CSV generation from insights returns empty results * update * fix type error --- .../lib/server/service/insightsBooking.ts | 26 +++++++++++------- .../lib/server/service/insightsRoutingBase.ts | 27 ++++++++++++------- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/packages/lib/server/service/insightsBooking.ts b/packages/lib/server/service/insightsBooking.ts index 08a3a7e746..aa76df25b3 100644 --- a/packages/lib/server/service/insightsBooking.ts +++ b/packages/lib/server/service/insightsBooking.ts @@ -261,16 +261,22 @@ export class InsightsBookingService { if (!this.options) { return NOTHING_CONDITION; } - const isOwnerOrAdmin = await this.isOrgOwnerOrAdmin(this.options.userId, this.options.orgId); - if (!isOwnerOrAdmin) { - return NOTHING_CONDITION; + const scope = this.options.scope; + const targetId = + scope === "org" ? this.options.orgId : scope === "team" ? this.options.teamId : undefined; + + if (targetId && scope !== "user") { + const isOwnerOrAdmin = await this.isOwnerOrAdmin(this.options.userId, targetId); + if (!isOwnerOrAdmin) { + return NOTHING_CONDITION; + } } - if (this.options.scope === "user") { + if (scope === "user") { return Prisma.sql`("userId" = ${this.options.userId}) AND ("teamId" IS NULL)`; - } else if (this.options.scope === "org") { + } else if (scope === "org") { return await this.buildOrgAuthorizationCondition(this.options); - } else if (this.options.scope === "team") { + } else if (scope === "team") { return await this.buildTeamAuthorizationCondition(this.options); } else { return NOTHING_CONDITION; @@ -318,7 +324,7 @@ export class InsightsBookingService { parentId: options.orgId, select: { id: true }, }); - if (!childTeamOfOrg) { + if (options.orgId && !childTeamOfOrg) { return NOTHING_CONDITION; } @@ -659,9 +665,9 @@ export class InsightsBookingService { return result; } - private async isOrgOwnerOrAdmin(userId: number, orgId: number): Promise { - // Check if the user is an owner or admin of the organization - const membership = await MembershipRepository.findUniqueByUserIdAndTeamId({ userId, teamId: orgId }); + private async isOwnerOrAdmin(userId: number, targetId: number): Promise { + // Check if the user is an owner or admin of the organization or team + const membership = await MembershipRepository.findUniqueByUserIdAndTeamId({ userId, teamId: targetId }); return Boolean( membership && membership.accepted && diff --git a/packages/lib/server/service/insightsRoutingBase.ts b/packages/lib/server/service/insightsRoutingBase.ts index e1bc80ad5b..a8137a9d83 100644 --- a/packages/lib/server/service/insightsRoutingBase.ts +++ b/packages/lib/server/service/insightsRoutingBase.ts @@ -306,16 +306,23 @@ export class InsightsRoutingBaseService { if (!this.options) { return NOTHING_CONDITION; } - const isOwnerOrAdmin = await this.isOrgOwnerOrAdmin(this.options.userId, this.options.orgId); - if (!isOwnerOrAdmin) { - return NOTHING_CONDITION; + + const scope = this.options.scope; + const targetId = + scope === "org" ? this.options.orgId : scope === "team" ? this.options.teamId : undefined; + + if (targetId && scope !== "user") { + const isOwnerOrAdmin = await this.isOwnerOrAdmin(this.options.userId, targetId); + if (!isOwnerOrAdmin) { + return NOTHING_CONDITION; + } } - if (this.options.scope === "user") { + if (scope === "user") { return Prisma.sql`"formUserId" = ${this.options.userId} AND "formTeamId" IS NULL`; - } else if (this.options.scope === "org") { + } else if (scope === "org") { return await this.buildOrgAuthorizationCondition(this.options); - } else if (this.options.scope === "team") { + } else if (scope === "team") { return await this.buildTeamAuthorizationCondition(this.options); } else { return NOTHING_CONDITION; @@ -346,16 +353,16 @@ export class InsightsRoutingBaseService { parentId: options.orgId, select: { id: true }, }); - if (!childTeamOfOrg) { + if (options.orgId && !childTeamOfOrg) { return NOTHING_CONDITION; } return Prisma.sql`"formTeamId" = ${options.teamId}`; } - private async isOrgOwnerOrAdmin(userId: number, orgId: number): Promise { - // Check if the user is an owner or admin of the organization - const membership = await MembershipRepository.findUniqueByUserIdAndTeamId({ userId, teamId: orgId }); + private async isOwnerOrAdmin(userId: number, targetId: number): Promise { + // Check if the user is an owner or admin of the organization or team + const membership = await MembershipRepository.findUniqueByUserIdAndTeamId({ userId, teamId: targetId }); return Boolean( membership && membership.accepted &&