Skip to content

Commit

Permalink
NERT-712 Issue with PI report resolved and also with Date ranges sele… (
Browse files Browse the repository at this point in the history
#262)

Co-authored-by: Oscar Inostroza <[email protected]>
  • Loading branch information
oscar-ip and Oscar Inostroza authored Oct 15, 2024
1 parent 20c5733 commit 933756b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 24 deletions.
29 changes: 18 additions & 11 deletions api/src/repositories/report-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ export class ReportRepository extends BaseRepository {
* @return {*} {Promise<IGetPIMgmtReport>}
* @memberof ReportRepository
*/
async getPIMgmtReportData(startDate: string, endDate: string): Promise<IGetPIMgmtReport[]> {
defaultLog.debug({ label: 'getPIMgmtReportData', message: 'params', startDate, endDate });
async getPIMgmtReportData(startDateTime: string, endDateTime: string): Promise<IGetPIMgmtReport[]> {
defaultLog.debug({ label: 'getPIMgmtReportData', message: 'params', startDateTime, endDateTime });

try {
const sqlStatement = SQL`
Expand All @@ -216,18 +216,22 @@ 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,
al.audit_log_id,
al.create_date,
al.operation
ORDER BY al.audit_log_id DESC;
`;
`);

const response = await this.connection.sql(sqlStatement);
return response.rows;
Expand All @@ -243,8 +247,8 @@ export class ReportRepository extends BaseRepository {
* @return {*} {Promise<IGetCustomReport>}
* @memberof ReportRepository
*/
async getCustomReportData(startDate: string, endDate: string): Promise<IGetCustomReport[]> {
defaultLog.debug({ label: 'getCustomReportData', message: 'params', startDate, endDate });
async getCustomReportData(startDateTime: string, endDateTime: string): Promise<IGetCustomReport[]> {
defaultLog.debug({ label: 'getCustomReportData', message: 'params', startDateTime, endDateTime });

try {
const sqlStatement = SQL`
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions app/src/components/fields/ReportsStartEndDateFields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -84,7 +84,7 @@ const ReportsStartEndDateFields: React.FC<IReportsStartEndDateFieldsProps> = (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') {
Expand Down Expand Up @@ -123,7 +123,7 @@ const ReportsStartEndDateFields: React.FC<IReportsStartEndDateFieldsProps> = (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') {
Expand Down
6 changes: 5 additions & 1 deletion app/src/features/admin/reports/AppCustomReportPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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);
};
Expand Down
5 changes: 4 additions & 1 deletion app/src/features/admin/reports/AppPiMgmtReportPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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));
};
Expand Down
31 changes: 23 additions & 8 deletions app/src/features/admin/reports/ReportsPage.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -179,20 +181,33 @@ const ReportsPage: React.FC = () => {
variant="contained"
color="primary"
size="large"
startIcon={<Icon path={mdiNewspaperVariant} size={1} />}
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: {
Expand Down

0 comments on commit 933756b

Please sign in to comment.