From 2bde793d44689c5616c9705f9ecb9f0517ea7a55 Mon Sep 17 00:00:00 2001 From: chamiakJ Date: Tue, 29 Jul 2025 19:21:11 +0530 Subject: [PATCH] fix(reporting): add table alias parameter to date range clause method --- .../reporting/reporting-controller-base-with-timezone.ts | 9 +++++---- .../reporting/reporting-members-controller.ts | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/worklenz-backend/src/controllers/reporting/reporting-controller-base-with-timezone.ts b/worklenz-backend/src/controllers/reporting/reporting-controller-base-with-timezone.ts index 59fc9a50..4fa08360 100644 --- a/worklenz-backend/src/controllers/reporting/reporting-controller-base-with-timezone.ts +++ b/worklenz-backend/src/controllers/reporting/reporting-controller-base-with-timezone.ts @@ -25,9 +25,10 @@ export default abstract class ReportingControllerBaseWithTimezone extends Workle * @param key - Date range key (e.g., YESTERDAY, LAST_WEEK) * @param dateRange - Array of date strings * @param userTimezone - User's timezone (e.g., 'America/New_York') + * @param tableAlias - Table alias to use (e.g., 'twl', 'task_work_log') * @returns SQL clause for date filtering */ - protected static getDateRangeClauseWithTimezone(key: string, dateRange: string[], userTimezone: string) { + protected static getDateRangeClauseWithTimezone(key: string, dateRange: string[], userTimezone: string, tableAlias: string = 'task_work_log') { // For custom date ranges if (dateRange.length === 2) { // Convert dates to user's timezone start/end of day @@ -40,10 +41,10 @@ export default abstract class ReportingControllerBaseWithTimezone extends Workle if (start.isSame(end, 'day')) { // Single day selection - return `AND task_work_log.created_at >= '${startUtc}'::TIMESTAMP AND task_work_log.created_at <= '${endUtc}'::TIMESTAMP`; + return `AND ${tableAlias}.created_at >= '${startUtc}'::TIMESTAMP AND ${tableAlias}.created_at <= '${endUtc}'::TIMESTAMP`; } - return `AND task_work_log.created_at >= '${startUtc}'::TIMESTAMP AND task_work_log.created_at <= '${endUtc}'::TIMESTAMP`; + return `AND ${tableAlias}.created_at >= '${startUtc}'::TIMESTAMP AND ${tableAlias}.created_at <= '${endUtc}'::TIMESTAMP`; } // For predefined ranges, calculate based on user's timezone @@ -74,7 +75,7 @@ export default abstract class ReportingControllerBaseWithTimezone extends Workle if (startDate && endDate) { const startUtc = startDate.utc().format("YYYY-MM-DD HH:mm:ss"); const endUtc = endDate.utc().format("YYYY-MM-DD HH:mm:ss"); - return `AND task_work_log.created_at >= '${startUtc}'::TIMESTAMP AND task_work_log.created_at <= '${endUtc}'::TIMESTAMP`; + return `AND ${tableAlias}.created_at >= '${startUtc}'::TIMESTAMP AND ${tableAlias}.created_at <= '${endUtc}'::TIMESTAMP`; } return ""; diff --git a/worklenz-backend/src/controllers/reporting/reporting-members-controller.ts b/worklenz-backend/src/controllers/reporting/reporting-members-controller.ts index 60a3da76..d900347c 100644 --- a/worklenz-backend/src/controllers/reporting/reporting-members-controller.ts +++ b/worklenz-backend/src/controllers/reporting/reporting-members-controller.ts @@ -548,7 +548,7 @@ export default class ReportingMembersController extends ReportingControllerBaseW // Get user timezone for proper date filtering const userTimezone = await this.getUserTimezone(req.user?.id as string); - const durationClause = this.getDateRangeClauseWithTimezone(duration as string || DATE_RANGES.LAST_WEEK, dateRange, userTimezone); + const durationClause = this.getDateRangeClauseWithTimezone(duration as string || DATE_RANGES.LAST_WEEK, dateRange, userTimezone, 'twl'); const minMaxDateClause = this.getMinMaxDates(duration as string || DATE_RANGES.LAST_WEEK, dateRange, "task_work_log"); const memberName = (req.query.member_name as string)?.trim() || null; @@ -1101,7 +1101,7 @@ export default class ReportingMembersController extends ReportingControllerBaseW // Get user timezone for proper date filtering const userTimezone = await this.getUserTimezone(req.user?.id as string); - const durationClause = this.getDateRangeClauseWithTimezone(duration || DATE_RANGES.LAST_WEEK, date_range, userTimezone); + const durationClause = this.getDateRangeClauseWithTimezone(duration || DATE_RANGES.LAST_WEEK, date_range, userTimezone, 'twl'); const minMaxDateClause = this.getMinMaxDates(duration || DATE_RANGES.LAST_WEEK, date_range, "task_work_log"); const billableQuery = this.buildBillableQuery(billable);