Skip to content

Commit 9cd806f

Browse files
authored
fix: CSV generation from insights returns empty results (calcom#22775)
* fix: CSV generation from insights returns empty results * update * fix type error
1 parent 628b169 commit 9cd806f

2 files changed

Lines changed: 33 additions & 20 deletions

File tree

packages/lib/server/service/insightsBooking.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -261,16 +261,22 @@ export class InsightsBookingService {
261261
if (!this.options) {
262262
return NOTHING_CONDITION;
263263
}
264-
const isOwnerOrAdmin = await this.isOrgOwnerOrAdmin(this.options.userId, this.options.orgId);
265-
if (!isOwnerOrAdmin) {
266-
return NOTHING_CONDITION;
264+
const scope = this.options.scope;
265+
const targetId =
266+
scope === "org" ? this.options.orgId : scope === "team" ? this.options.teamId : undefined;
267+
268+
if (targetId && scope !== "user") {
269+
const isOwnerOrAdmin = await this.isOwnerOrAdmin(this.options.userId, targetId);
270+
if (!isOwnerOrAdmin) {
271+
return NOTHING_CONDITION;
272+
}
267273
}
268274

269-
if (this.options.scope === "user") {
275+
if (scope === "user") {
270276
return Prisma.sql`("userId" = ${this.options.userId}) AND ("teamId" IS NULL)`;
271-
} else if (this.options.scope === "org") {
277+
} else if (scope === "org") {
272278
return await this.buildOrgAuthorizationCondition(this.options);
273-
} else if (this.options.scope === "team") {
279+
} else if (scope === "team") {
274280
return await this.buildTeamAuthorizationCondition(this.options);
275281
} else {
276282
return NOTHING_CONDITION;
@@ -318,7 +324,7 @@ export class InsightsBookingService {
318324
parentId: options.orgId,
319325
select: { id: true },
320326
});
321-
if (!childTeamOfOrg) {
327+
if (options.orgId && !childTeamOfOrg) {
322328
return NOTHING_CONDITION;
323329
}
324330

@@ -659,9 +665,9 @@ export class InsightsBookingService {
659665
return result;
660666
}
661667

662-
private async isOrgOwnerOrAdmin(userId: number, orgId: number): Promise<boolean> {
663-
// Check if the user is an owner or admin of the organization
664-
const membership = await MembershipRepository.findUniqueByUserIdAndTeamId({ userId, teamId: orgId });
668+
private async isOwnerOrAdmin(userId: number, targetId: number): Promise<boolean> {
669+
// Check if the user is an owner or admin of the organization or team
670+
const membership = await MembershipRepository.findUniqueByUserIdAndTeamId({ userId, teamId: targetId });
665671
return Boolean(
666672
membership &&
667673
membership.accepted &&

packages/lib/server/service/insightsRoutingBase.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -306,16 +306,23 @@ export class InsightsRoutingBaseService {
306306
if (!this.options) {
307307
return NOTHING_CONDITION;
308308
}
309-
const isOwnerOrAdmin = await this.isOrgOwnerOrAdmin(this.options.userId, this.options.orgId);
310-
if (!isOwnerOrAdmin) {
311-
return NOTHING_CONDITION;
309+
310+
const scope = this.options.scope;
311+
const targetId =
312+
scope === "org" ? this.options.orgId : scope === "team" ? this.options.teamId : undefined;
313+
314+
if (targetId && scope !== "user") {
315+
const isOwnerOrAdmin = await this.isOwnerOrAdmin(this.options.userId, targetId);
316+
if (!isOwnerOrAdmin) {
317+
return NOTHING_CONDITION;
318+
}
312319
}
313320

314-
if (this.options.scope === "user") {
321+
if (scope === "user") {
315322
return Prisma.sql`"formUserId" = ${this.options.userId} AND "formTeamId" IS NULL`;
316-
} else if (this.options.scope === "org") {
323+
} else if (scope === "org") {
317324
return await this.buildOrgAuthorizationCondition(this.options);
318-
} else if (this.options.scope === "team") {
325+
} else if (scope === "team") {
319326
return await this.buildTeamAuthorizationCondition(this.options);
320327
} else {
321328
return NOTHING_CONDITION;
@@ -346,16 +353,16 @@ export class InsightsRoutingBaseService {
346353
parentId: options.orgId,
347354
select: { id: true },
348355
});
349-
if (!childTeamOfOrg) {
356+
if (options.orgId && !childTeamOfOrg) {
350357
return NOTHING_CONDITION;
351358
}
352359

353360
return Prisma.sql`"formTeamId" = ${options.teamId}`;
354361
}
355362

356-
private async isOrgOwnerOrAdmin(userId: number, orgId: number): Promise<boolean> {
357-
// Check if the user is an owner or admin of the organization
358-
const membership = await MembershipRepository.findUniqueByUserIdAndTeamId({ userId, teamId: orgId });
363+
private async isOwnerOrAdmin(userId: number, targetId: number): Promise<boolean> {
364+
// Check if the user is an owner or admin of the organization or team
365+
const membership = await MembershipRepository.findUniqueByUserIdAndTeamId({ userId, teamId: targetId });
359366
return Boolean(
360367
membership &&
361368
membership.accepted &&

0 commit comments

Comments
 (0)