feat: convert InsightsBookingService to use Prisma.sql raw queries#4
feat: convert InsightsBookingService to use Prisma.sql raw queries#4ShashankFC wants to merge 1 commit intoinsights-query-foundationfrom
Conversation
…22345) * fix: use raw query at InsightsBookingService * feat: convert InsightsBookingService to use Prisma.sql raw queries - Convert auth conditions from Prisma object notation to Prisma.sql - Convert filter conditions from Prisma object notation to Prisma.sql - Update return types from Prisma.BookingTimeStatusDenormalizedWhereInput to Prisma.Sql - Fix type error in isOrgOwnerOrAdmin method - Follow same pattern as InsightsRoutingService conversion Co-Authored-By: eunjae@cal.com <hey@eunjae.dev> * feat: convert InsightsBookingService to use Prisma.sql raw queries - Convert auth conditions from Prisma object notation to Prisma.sql - Convert filter conditions from Prisma object notation to Prisma.sql - Update return types from Prisma.BookingTimeStatusDenormalizedWhereInput to Prisma.Sql - Fix type error in isOrgOwnerOrAdmin method - Follow same pattern as InsightsRoutingService conversion Co-Authored-By: eunjae@cal.com <hey@eunjae.dev> * fix: update InsightsBookingService integration tests for Prisma.sql format - Replace Prisma object notation expectations with Prisma.sql template literals - Add NOTHING_CONDITION constant for consistency with InsightsRoutingService - Update all test cases to use direct Prisma.sql comparisons - Use $queryRaw for actual database integration testing - Follow same testing patterns as InsightsRoutingService Co-Authored-By: eunjae@cal.com <hey@eunjae.dev> * fix: exclude intentionally skipped jobs from required CI check failure - Remove 'skipped' from failure condition in pr.yml and all-checks.yml - Allow E2E jobs to be skipped without failing the required check - Only actual failures and cancelled jobs will cause required check to fail Co-Authored-By: eunjae@cal.com <hey@eunjae.dev> * fix tests * Revert "fix: exclude intentionally skipped jobs from required CI check failure" This reverts commit 6ff44fc9a8f14ad657f7bba7c2e454e192b66c8f. * clean up tests * address feedback --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
|
Claude finished @ShashankFC's task in 1m 59s —— View job Code Review CompleteTasks
SummaryThis PR successfully converts Strengths1. Security: SQL Injection Protection ✅ Prisma.sql`("userId" = ${this.options.userId}) AND ("teamId" IS NULL)`
Prisma.sql`("teamId" = ANY(${teamIds})) AND ("isTeamBooking" = true)`2. Code Simplification ✅ Before: return {
OR: [
{ teamId: { in: teamIds }, isTeamBooking: true },
{ userId: { in: userIds }, isTeamBooking: false }
]
};After: return Prisma.sql`(("teamId" = ANY(${teamIds})) AND ("isTeamBooking" = true)) OR (("userId" = ANY(${userIds})) AND ("isTeamBooking" = false))`;3. Consistent Architecture ✅ 4. Comprehensive Test Updates ✅ 5. Proper Caching ✅ Issues and Recommendations🔴 Critical: Missing Empty Array HandlingLocation: The code doesn't handle the case where Problem in const conditions: Prisma.Sql[] = [
Prisma.sql`("teamId" = ${options.teamId}) AND ("isTeamBooking" = true)`,
];
if (userIdsFromTeam.length > 0) {
conditions.push(Prisma.sql`("userId" = ANY(${userIdsFromTeam})) AND ("isTeamBooking" = false)`);
}If Problem in const conditions: Prisma.Sql[] = [Prisma.sql`("teamId" = ANY(${teamIds})) AND ("isTeamBooking" = true)`];If Recommendation: Add explicit checks: if (teamIds.length === 0 && userIdsFromOrg.length === 0) {
return NOTHING_CONDITION;
}🟡 Medium: Inconsistent Condition StructureLocation: In Current code: const conditions: Prisma.Sql[] = [
Prisma.sql`("teamId" = ${options.teamId}) AND ("isTeamBooking" = true)`,
];
if (userIdsFromTeam.length > 0) {
conditions.push(Prisma.sql`("userId" = ANY(${userIdsFromTeam})) AND ("isTeamBooking" = false)`);
}
return conditions.reduce((acc, condition, index) => {
if (index === 0) return condition;
return Prisma.sql`(${acc}) OR (${condition})`;
});Recommendation: For clarity and consistency, consider an early return when there are no users: const conditions: Prisma.Sql[] = [
Prisma.sql`("teamId" = ${options.teamId}) AND ("isTeamBooking" = true)`,
];
if (userIdsFromTeam.length === 0) {
return conditions[0];
}
conditions.push(Prisma.sql`("userId" = ANY(${userIdsFromTeam})) AND ("isTeamBooking" = false)`);
return Prisma.sql`(${conditions[0]}) OR (${conditions[1]})`;🟡 Medium: Test Coverage GapLocation: The tests don't cover several important edge cases:
Recommendation: Add test cases for these edge cases to ensure the service handles them gracefully. 🟢 Minor: Missing Type ExportLocation: The new Current code: export type InsightsBookingServicePublicOptions = {
scope: "user" | "org" | "team";
userId: number;
orgId: number;
teamId?: number; // Optional here
};But the schema at line 21-26 requires z.object({
scope: z.literal("team"),
userId: z.number(),
orgId: z.number(),
teamId: z.number(), // Required in schema
}),Recommendation: This is actually fine - the public type is more lenient for the constructor, and validation is handled internally. However, consider adding a JSDoc comment explaining this design choice. 🟢 Minor: Missing DocumentationLocation: The new Recommendation: Add documentation: /**
* Combines authorization and filter conditions into a single SQL condition.
* Used for building WHERE clauses in raw SQL queries.
* @returns A Prisma.Sql fragment representing the complete query conditions
*/
async getBaseConditions(): Promise<Prisma.Sql> {🟢 Minor: Removed MethodLocation: The The original implementation had a Impact: This is a breaking API change. Ensure all call sites have been updated accordingly. Performance ConsiderationsPositive:
Neutral:
Test QualityThe test suite is comprehensive:
Final VerdictApproval Status: ✅ Approve with minor fixes This is a solid refactoring that improves code clarity and maintains security. The critical issue with empty array handling should be addressed before merging, but it's a straightforward fix. The test coverage is good, though edge cases could be better tested. Priority Fixes:
|
|
This PR is being marked as stale due to inactivity. |
Test 5nn---nReplicated from ai-code-review-evaluation/cal.com-greptile#5