Scheduling infrastructure for absolutely everyone.
fix: CSV generation from insights returns empty results (#22775)
Anik Dhabal Babu9cd806ff61de
diff --git a/packages/lib/server/service/insightsBooking.ts b/packages/lib/server/service/insightsBooking.tsindex 08a3a7e7468..aa76df25b39 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<boolean> {- // 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<boolean> {+ // 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.tsindex e1bc80ad5bc..a8137a9d832 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<boolean> {- // 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<boolean> {+ // 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 &&