diff --git a/packages/features/insights/server/trpc-router.ts b/packages/features/insights/server/trpc-router.ts index 8bb69a4664..58a687fcb2 100644 --- a/packages/features/insights/server/trpc-router.ts +++ b/packages/features/insights/server/trpc-router.ts @@ -53,9 +53,8 @@ const buildBaseWhereCondition = async ({ isEmptyResponse?: boolean; }> => { let whereCondition: Prisma.BookingTimeStatusWhereInput = {}; - const ANDConditions: Prisma.BookingTimeStatusWhereInput[] = []; // EventType Filter - if (eventTypeId) ANDConditions.push({ OR: [{ eventTypeId }, { eventParentId: eventTypeId }] }); + if (eventTypeId) whereCondition.OR = [{ eventTypeId }, { eventParentId: eventTypeId }]; // User/Member filter if (memberUserId) whereCondition.userId = memberUserId; if (userId) { @@ -77,9 +76,13 @@ const buildBaseWhereCondition = async ({ return { whereCondition: { ...whereCondition, - teamId: ctx.userOrganizationId, - isTeamBooking: true, - ...(ANDConditions.length > 0 ? { AND: [...ANDConditions] } : {}), + OR: [ + ...(whereCondition.OR ?? []), + { + teamId: ctx.userOrganizationId, + isTeamBooking: true, + }, + ], }, isEmptyResponse: true, }; @@ -102,23 +105,18 @@ const buildBaseWhereCondition = async ({ const userIdsFromOrg = usersFromOrg.map((u) => u.userId); whereCondition = { ...whereCondition, - AND: [ - ...ANDConditions, + OR: [ { - OR: [ - { - teamId: { - in: [ctx.userOrganizationId, ...teamsFromOrg.map((t) => t.id)], - }, - isTeamBooking: true, - }, - { - userId: { - in: userIdsFromOrg, - }, - isTeamBooking: false, - }, - ], + teamId: { + in: [ctx.userOrganizationId, ...teamsFromOrg.map((t) => t.id)], + }, + isTeamBooking: true, + }, + { + userId: { + in: userIdsFromOrg, + }, + isTeamBooking: false, }, ], }; @@ -137,21 +135,16 @@ const buildBaseWhereCondition = async ({ const userIdsFromTeam = usersFromTeam.map((u) => u.userId); whereCondition = { ...whereCondition, - AND: [ - ...ANDConditions, + OR: [ { - OR: [ - { - teamId, - isTeamBooking: true, - }, - { - userId: { - in: userIdsFromTeam, - }, - isTeamBooking: false, - }, - ], + teamId, + isTeamBooking: true, + }, + { + userId: { + in: userIdsFromTeam, + }, + isTeamBooking: false, }, ], };