From 933756ba97db92a08f372c45456e90411bc2c966 Mon Sep 17 00:00:00 2001 From: oscar-ip <136661790+oscar-ip@users.noreply.github.com> Date: Tue, 15 Oct 2024 10:12:06 -0600 Subject: [PATCH] =?UTF-8?q?NERT-712=20Issue=20with=20PI=20report=20resolve?= =?UTF-8?q?d=20and=20also=20with=20Date=20ranges=20sele=E2=80=A6=20(#262)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Oscar Inostroza --- api/src/repositories/report-repository.ts | 29 ++++++++++------- .../fields/ReportsStartEndDateFields.tsx | 6 ++-- .../admin/reports/AppCustomReportPage.tsx | 6 +++- .../admin/reports/AppPiMgmtReportPage.tsx | 5 ++- .../features/admin/reports/ReportsPage.tsx | 31 ++++++++++++++----- 5 files changed, 53 insertions(+), 24 deletions(-) diff --git a/api/src/repositories/report-repository.ts b/api/src/repositories/report-repository.ts index 72214f60..3d0cb8e9 100644 --- a/api/src/repositories/report-repository.ts +++ b/api/src/repositories/report-repository.ts @@ -198,8 +198,8 @@ export class ReportRepository extends BaseRepository { * @return {*} {Promise} * @memberof ReportRepository */ - async getPIMgmtReportData(startDate: string, endDate: string): Promise { - defaultLog.debug({ label: 'getPIMgmtReportData', message: 'params', startDate, endDate }); + async getPIMgmtReportData(startDateTime: string, endDateTime: string): Promise { + defaultLog.debug({ label: 'getPIMgmtReportData', message: 'params', startDateTime, endDateTime }); try { const sqlStatement = SQL` @@ -216,10 +216,14 @@ export class ReportRepository extends BaseRepository { LEFT JOIN system_user su ON al.system_user_id = su.system_user_id LEFT JOIN project prj ON (al.after_value -> 'project_id')::TEXT::int = prj.project_id WHERE - al.create_date >= DATE(${startDate}) AND - al.create_date <= DATE(${endDate}) AND + al.create_date >= DATE(`; + sqlStatement.append("'" + startDateTime + "'"); + sqlStatement.append(SQL`) AND al.create_date <= DATE(`); + sqlStatement.append("'" + endDateTime + "'"); + sqlStatement.append(SQL`) AND al.operation IN ('INSERT', 'UPDATE') AND - al.table_name IN ('restoration.project_attachment', 'restoration.project') + al.table_name IN ('restoration.project_attachment', 'restoration.project') AND + prj.name IS NOT NULL GROUP BY prj.name, su.user_identifier, @@ -227,7 +231,7 @@ export class ReportRepository extends BaseRepository { al.create_date, al.operation ORDER BY al.audit_log_id DESC; - `; + `); const response = await this.connection.sql(sqlStatement); return response.rows; @@ -243,8 +247,8 @@ export class ReportRepository extends BaseRepository { * @return {*} {Promise} * @memberof ReportRepository */ - async getCustomReportData(startDate: string, endDate: string): Promise { - defaultLog.debug({ label: 'getCustomReportData', message: 'params', startDate, endDate }); + async getCustomReportData(startDateTime: string, endDateTime: string): Promise { + defaultLog.debug({ label: 'getCustomReportData', message: 'params', startDateTime, endDateTime }); try { const sqlStatement = SQL` @@ -319,8 +323,11 @@ export class ReportRepository extends BaseRepository { FROM project_species ps) t7 ON prj.project_id = t7.project_id WHERE - prj.create_date >= DATE(${startDate}) AND - prj.create_date <= DATE(${endDate}) + prj.create_date >= DATE(`; + sqlStatement.append("'" + startDateTime + "'"); + sqlStatement.append(SQL`) AND prj.create_date <= DATE(`); + sqlStatement.append("'" + endDateTime + "'"); + sqlStatement.append(SQL`) GROUP BY prj.project_id, prj.is_project, @@ -347,7 +354,7 @@ export class ReportRepository extends BaseRepository { sc.size_ha, sc.create_date ORDER BY prj.project_id ASC; - `; + `); const response = await this.connection.sql(sqlStatement); return response.rows; diff --git a/app/src/components/fields/ReportsStartEndDateFields.tsx b/app/src/components/fields/ReportsStartEndDateFields.tsx index 0ae9ecc1..09b0c3cf 100644 --- a/app/src/components/fields/ReportsStartEndDateFields.tsx +++ b/app/src/components/fields/ReportsStartEndDateFields.tsx @@ -5,7 +5,7 @@ import { DatePicker } from '@mui/x-date-pickers'; import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs'; import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider'; import { DateValidationError } from '@mui/x-date-pickers/models'; -import { DATE_FORMAT, DATE_LIMIT } from 'constants/dateTimeFormats'; +import { DATE_FORMAT } from 'constants/dateTimeFormats'; import { default as dayjs } from 'dayjs'; import React, { useMemo } from 'react'; @@ -84,7 +84,7 @@ const ReportsStartEndDateFields: React.FC = (pr label="Start Date" format={DATE_FORMAT.ShortDateFormat} minDate={dayjs('2024-09-01')} - maxDate={dayjs(DATE_LIMIT.max)} + maxDate={dayjs()} value={formattedStartDateValue} onChange={(value) => { if (!value || String(value) === 'Invalid Date') { @@ -123,7 +123,7 @@ const ReportsStartEndDateFields: React.FC = (pr label="End Date" format={DATE_FORMAT.ShortDateFormat} minDate={dayjs(startDate)} - maxDate={dayjs(DATE_LIMIT.max)} + maxDate={dayjs()} value={formattedEndDateValue} onChange={(value: dayjs.Dayjs | null) => { if (!value || String(value) === 'Invalid Date') { diff --git a/app/src/features/admin/reports/AppCustomReportPage.tsx b/app/src/features/admin/reports/AppCustomReportPage.tsx index d7f50547..39b99cad 100644 --- a/app/src/features/admin/reports/AppCustomReportPage.tsx +++ b/app/src/features/admin/reports/AppCustomReportPage.tsx @@ -6,6 +6,7 @@ import { useLocation, useNavigate } from 'react-router'; import { CustomReportI18N } from 'constants/i18n'; import { IGetCustomReportData } from 'interfaces/useAdminApi.interface'; import { csvDownload } from 'utils/cvsUtils'; +import dayjs from 'dayjs'; const pageStyles = { breadCrumbLink: { @@ -40,8 +41,11 @@ const AppCustomReportPage: React.FC = () => { const location = useLocation(); const { startDate, endDate } = location.state; + const startDateTime = dayjs(startDate).startOf('day').toISOString(); + const endDateTime = dayjs(endDate).endOf('day').toISOString(); + const getCustomReportData = async () => { - const data = await restorationTrackerApi.admin.getCustomReport(startDate, endDate); + const data = await restorationTrackerApi.admin.getCustomReport(startDateTime, endDateTime); setIsLoading(false); setCustomReportData(data); }; diff --git a/app/src/features/admin/reports/AppPiMgmtReportPage.tsx b/app/src/features/admin/reports/AppPiMgmtReportPage.tsx index 31c9e748..ecaf0792 100644 --- a/app/src/features/admin/reports/AppPiMgmtReportPage.tsx +++ b/app/src/features/admin/reports/AppPiMgmtReportPage.tsx @@ -90,8 +90,11 @@ const AppPiMgmtReportPage: React.FC = () => { return rowsPiMgmtReport; } + const startDateTime = dayjs(startDate).startOf('day').toISOString(); + const endDateTime = dayjs(endDate).endOf('day').toISOString(); + const getPiMgmtReportData = async () => { - const data = await restorationTrackerApi.admin.getPiMgmtReport(startDate, endDate); + const data = await restorationTrackerApi.admin.getPiMgmtReport(startDateTime, endDateTime); setIsLoading(false); setPiMgmtReportData(mapToTableData(data)); }; diff --git a/app/src/features/admin/reports/ReportsPage.tsx b/app/src/features/admin/reports/ReportsPage.tsx index 5d3ff3c1..e047faa3 100644 --- a/app/src/features/admin/reports/ReportsPage.tsx +++ b/app/src/features/admin/reports/ReportsPage.tsx @@ -1,4 +1,6 @@ import { Box, Container, Paper, Typography, Button, Divider, Stack } from '@mui/material'; +import { mdiNewspaperVariant } from '@mdi/js'; +import { Icon } from '@mdi/react'; import { useNertApi } from 'hooks/useNertApi'; import React, { useContext, useState } from 'react'; import DateRangeSelection from './DateRangeSelection'; @@ -179,20 +181,33 @@ const ReportsPage: React.FC = () => { variant="contained" color="primary" size="large" + startIcon={} onClick={() => { if ('appUserReport' === selectedReport) { history('/admin/reports/user'); return; } - if ('customRange' === selectedRange && (!startDate || !endDate)) { - dialogContext.setErrorDialog({ - dialogTitle: 'Dates Validation Failed', - dialogText: 'Please enter the required start and end dates.', - ...defaultErrorDialogProps, - open: true - }); - return; + if ('customRange' === selectedRange) { + if (!startDate || !endDate) { + dialogContext.setErrorDialog({ + dialogTitle: 'Dates Validation Failed', + dialogText: 'Please enter the required start and end dates.', + ...defaultErrorDialogProps, + open: true + }); + return; + } + if (dayjs(startDate) > dayjs(endDate)) { + dialogContext.setErrorDialog({ + dialogTitle: 'Dates Validation Failed', + dialogText: 'Start date cannot be after end date.', + ...defaultErrorDialogProps, + open: true + }); + return; + } } + if ('customReport' === selectedReport) { history('/admin/reports/custom', { state: {