Skip to content

Commit

Permalink
Merge pull request Bahmni#790 from Bahmni/BAH-3049_Reports
Browse files Browse the repository at this point in the history
BAH-3049 | Fix start date undefined value
  • Loading branch information
gsluthra authored Dec 4, 2023
2 parents c5beb59 + e58fa79 commit 8cb47d7
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 38 deletions.
3 changes: 2 additions & 1 deletion ui/app/i18n/reports/locale_en.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@
"CHOOSE_ANSWER_FROM_DROPDOWN_LABEL": "Choose Answer",
"DELETE_LABEL": "Delete",
"EDIT_LABEL": "Edit",
"REPORT_DATE_RANGE": "Select Date Range"
"REPORT_DATE_RANGE": "Select Date Range",
"START_DATE_CANNOT_LATER_THAN_STOP_DATE": "start date can not be later than stop date"
}
28 changes: 15 additions & 13 deletions ui/app/reports/controllers/reportsController.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
'use strict';

angular.module('bahmni.reports')
.controller('ReportsController', ['$scope', 'appService', 'reportService', 'FileUploader', 'messagingService', 'spinner', '$rootScope', 'auditLogService', function ($scope, appService, reportService, FileUploader, messagingService, spinner, $rootScope, auditLogService) {
.controller('ReportsController', ['$scope', 'appService', 'reportService', 'FileUploader', 'messagingService', 'spinner', '$rootScope', '$translate', 'auditLogService', function ($scope, appService, reportService, FileUploader, messagingService, spinner, $rootScope, $translate, auditLogService) {
const format = _.values(reportService.getAvailableFormats());
const dateRange = _.values(reportService.getAvailableDateRange());

var getTranslatedMessage = function (key) {
return $translate.instant(key);
};

$scope.uploader = new FileUploader({
url: Bahmni.Common.Constants.uploadReportTemplateUrl,
removeAfterUpload: true,
Expand All @@ -30,31 +34,26 @@ angular.module('bahmni.reports')
$scope.enableReportQueue = appService.getAppDescriptor().getConfigValue("enableReportQueue");
$scope.setDefault = function (item, header) {
var setToChange = header === 'reportsRequiringDateRange' ? $rootScope.reportsRequiringDateRange : $rootScope.reportsNotRequiringDateRange;
var isPreviousMonth = $rootScope.default[header][item] && $rootScope.default[header][item].getTime() === dateRange[2].getTime();
setToChange.forEach(function (report) {
if (item == 'dateRangeType') {
$rootScope.default.reportsRequiringDateRange.startDate = $rootScope.default[header][item];
$rootScope.default.reportsRequiringDateRange.stopDate = isPreviousMonth($rootScope.default[header][item]) ? getPreviousMonthEndDate() : dateRange[0];
$rootScope.default.reportsRequiringDateRange.stopDate = isPreviousMonth ? getPreviousMonthEndDate() : dateRange[0];
report['startDate'] = $rootScope.default[header][item];
report['stopDate'] = isPreviousMonth($rootScope.default[header][item]) ? getPreviousMonthEndDate() : dateRange[0];
}
else if ($rootScope.default[header][item] === undefined) {
report['stopDate'] = isPreviousMonth ? getPreviousMonthEndDate() : dateRange[0];
} else if ($rootScope.default[header][item] === undefined) {
$rootScope.default.reportsRequiringDateRange.startDate = dateRange[0];
$rootScope.reportsRequiringDateRange.forEach(function (report) {
report.startDate = dateRange[0];
report.stopDate = isPreviousMonth(dateRange[0]) ? getPreviousMonthEndDate() : dateRange[0];
report.stopDate = isPreviousMonth ? getPreviousMonthEndDate() : dateRange[0];
report.responseType = format[1];
});
}
else {
} else {
report[item] = $rootScope.default[header][item];
}
});
};

var isPreviousMonth = function (date) {
return (date.getMonth() === new Date().getMonth() - 1 && date.getFullYear() === new Date().getFullYear())
|| (date.getMonth() === 11 && date.getFullYear() === new Date().getFullYear() - 1);
};

var getPreviousMonthEndDate = function () {
return new Date(new Date().getFullYear(), new Date().getMonth(), 0);
};
Expand Down Expand Up @@ -85,6 +84,9 @@ angular.module('bahmni.reports')
if (!report.stopDate) {
msg.push("end date");
}
if ((report.startDate > report.stopDate)) {
msg.push(getTranslatedMessage("START_DATE_CANNOT_LATER_THAN_STOP_DATE"));
}
messagingService.showMessage("error", "Please select the " + msg.join(" and "));
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion ui/app/reports/services/reportService.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ angular.module('bahmni.reports')
"Previous Month": new Date(currentDate.getFullYear(), currentDate.getMonth() - 1, 1),
"This Quarter": new Date(currentDate.getFullYear(), Math.floor(currentDate.getMonth() / 3) * 3, 1),
"This Year": new Date(currentDate.getFullYear(), 0, 1),
"Last 7 days": new Date(new Date().setDate(currentDate.getDate() - 6)),
"Last 7 days": new Date(new Date().setDate(currentDate.getDate() - 7)),
"Last 30 days": new Date(new Date().setDate(currentDate.getDate() - 30))
};

Expand Down
8 changes: 3 additions & 5 deletions ui/app/reports/views/reports.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ <h2 class="section-title">{{::'REPORTS_TITLE_KEY' | translate}}</h2>
<th class="reports-stop-date">{{::'REPORTS_END_DATE_HEADER_KEY' |translate}}
<span class="asterick">*</span>
<input ng-model="default.reportsRequiringDateRange.stopDate" date-converter
class="form-field start-date" type="date" min="{{default.reportsRequiringDateRange.startDate | date:'yyyy-MM-dd'}}"
class="form-field stop-date" type="date" min="{{default.reportsRequiringDateRange.startDate | date:'yyyy-MM-dd'}}"
ng-change="setDefault('stopDate', 'reportsRequiringDateRange')">
</th>
<th class="reports-format">
Expand All @@ -45,12 +45,10 @@ <h2 class="section-title">{{::'REPORTS_TITLE_KEY' | translate}}</h2>
<tr ng-repeat="report in ::reportsRequiringDateRange" show-if-privilege="{{:: report.requiredPrivilege}}">
<td>{{:: report.name |translate }}</td>
<td class="reports-start-date">
<input date-converter class="form-field start-date" type="date" max="{{report.stopDate + 1 | date:'yyyy-MM-dd'}}"
ng-model="report.startDate">
<input date-converter class="form-field start-date" type="date" ng-model="report.startDate">
</td>
<td class="reports-stop-date">
<input date-converter class="form-field stop-date" type="date" min="{{report.startDate | date:'yyyy-MM-dd'}}"
ng-model="report.stopDate">
<input date-converter class="form-field stop-date" type="date" min="{{report.startDate | date:'yyyy-MM-dd'}}" ng-model="report.stopDate">
</td>
<td class="reports-format">
<select ng-model="report.responseType" ng-options="type as label for (label , type) in ::formats">
Expand Down
151 changes: 133 additions & 18 deletions ui/test/unit/reports/controllers/reportsController.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,18 @@ describe("ReportsController", function () {
});
reportServiceMock.getAvailableDateRange.and.returnValue({
"Today": new Date(),
"This Month": new Date(new Date().getFullYear(), new Date().getMonth(), 1)
"This Month": new Date(new Date().getFullYear(), new Date().getMonth(), 1),
"Previous Month": new Date(new Date().getFullYear(), new Date().getMonth() - 1, 1)
});
controller = $controller;
setupController();
}));

afterEach(function () {
window.Date = originalDate;
});

function setupController() {
controller('ReportsController', {
$scope: scope,
appService: appServiceMock,
Expand All @@ -72,11 +81,7 @@ describe("ReportsController", function () {
$rootScope: rootScope,
FileUploader: function () { }
});
}));

afterEach(function () {
window.Date = originalDate;
});
}

it("initializes report sets based on whether date range required or not", function () {
expect(mockAppDescriptor.getConfigForPage).toHaveBeenCalledWith("reports");
Expand All @@ -86,14 +91,7 @@ describe("ReportsController", function () {

it('should initialise formats based on the supportedFormats config', function () {
mockAppDescriptor.getConfigValue.and.returnValue(['csv', 'html']);
controller('ReportsController', {
$scope: scope,
appService: appServiceMock,
reportService: reportServiceMock,
messagingService: messagingServiceMock,
$rootScope: rootScope,
FileUploader: function () { }
});
setupController();

expect(_.keys(scope.formats).length).toBe(2);
expect(scope.formats['CSV']).toBe('text/csv');
Expand All @@ -111,6 +109,117 @@ describe("ReportsController", function () {
expect(rootScope.reportsRequiringDateRange[0].startDate.getDate()).toBe(new Date().getDate());
expect(rootScope.reportsRequiringDateRange[0].stopDate.getDate()).toBe(new Date().getDate());
});

it("should return the correct start and stop date when selected date range is last 30 days", function () {
var mockedDate = new Date('20-Dec-2023');
var expectedStartDate = new originalDate('20-Nov-2023');
spyOn(window, 'Date').and.callFake(function () {
return mockedDate;
});
reportServiceMock.getAvailableDateRange.and.returnValue({
"Today": mockedDate,
"This Month": new originalDate('1-Dec-2023'),
"Previous Month": new originalDate('1-Nov-2023')
});
setupController();

rootScope.default.reportsRequiringDateRange = {
dateRangeType: expectedStartDate,
};
scope.setDefault('dateRangeType', 'reportsRequiringDateRange');

expect(rootScope.reportsRequiringDateRange[0].startDate).toEqual(expectedStartDate);
expect(rootScope.reportsRequiringDateRange[0].stopDate).toEqual(mockedDate);
});

it("should return the correct start and stop date when selected date range is last 7 days", function () {
var mockedDate = new Date('20-Dec-2023');
var expectedStartDate = new originalDate('13-Dec-2023');
spyOn(window, 'Date').and.callFake(function () {
return mockedDate;
});
reportServiceMock.getAvailableDateRange.and.returnValue({
"Today": mockedDate,
"This Month": new originalDate('1-Dec-2023'),
"Previous Month": new originalDate('1-Nov-2023')
});
setupController();

rootScope.default.reportsRequiringDateRange = {
dateRangeType: expectedStartDate,
};
scope.setDefault('dateRangeType', 'reportsRequiringDateRange');

expect(rootScope.reportsRequiringDateRange[0].startDate).toEqual(expectedStartDate);
expect(rootScope.reportsRequiringDateRange[0].stopDate).toEqual(mockedDate);
});

it("should return the correct start and stop date when selected date range is this year", function () {
var mockedDate = new Date('20-Dec-2023');
var expectedStartDate = new originalDate('1-Jan-2023');
spyOn(window, 'Date').and.callFake(function () {
return mockedDate;
});
reportServiceMock.getAvailableDateRange.and.returnValue({
"Today": mockedDate,
"This Month": new originalDate('1-Dec-2023'),
"Previous Month": new originalDate('1-Nov-2023')
});
setupController();

rootScope.default.reportsRequiringDateRange = {
dateRangeType: expectedStartDate,
};
scope.setDefault('dateRangeType', 'reportsRequiringDateRange');

expect(rootScope.reportsRequiringDateRange[0].startDate).toEqual(expectedStartDate);
expect(rootScope.reportsRequiringDateRange[0].stopDate).toEqual(mockedDate);
});

it("should return the correct start and stop date when selected date range is this quarter", function () {
var mockedDate = new Date('20-Dec-2023');
var expectedStartDate = new originalDate('1-Oct-2023');
spyOn(window, 'Date').and.callFake(function () {
return mockedDate;
});
reportServiceMock.getAvailableDateRange.and.returnValue({
"Today": mockedDate,
"This Month": new originalDate('1-Dec-2023'),
"Previous Month": new originalDate('1-Nov-2023')
});
setupController();

rootScope.default.reportsRequiringDateRange = {
dateRangeType: expectedStartDate,
};
scope.setDefault('dateRangeType', 'reportsRequiringDateRange');

expect(rootScope.reportsRequiringDateRange[0].startDate).toEqual(expectedStartDate);
expect(rootScope.reportsRequiringDateRange[0].stopDate).toEqual(mockedDate);
});

it("should return the correct start and stop date when selected date range is this month", function () {
var mockedDate = new Date('20-Dec-2023');
var expectedStartDate = new originalDate('1-Dec-2023');
spyOn(window, 'Date').and.callFake(function () {
return mockedDate;
});
reportServiceMock.getAvailableDateRange.and.returnValue({
"Today": mockedDate,
"This Month": new originalDate('1-Dec-2023'),
"Previous Month": new originalDate('1-Nov-2023')
});
setupController();

rootScope.default.reportsRequiringDateRange = {
dateRangeType: expectedStartDate,
};
scope.setDefault('dateRangeType', 'reportsRequiringDateRange');

expect(rootScope.reportsRequiringDateRange[0].startDate).toEqual(expectedStartDate);
expect(rootScope.reportsRequiringDateRange[0].stopDate).toEqual(mockedDate);
});


const previousMonthTestCases = [
{ currentDate: '1-Dec-2022', expectedStartDate: '1-Nov-2022', expectedStopDate: '30-Nov-2022' },
Expand All @@ -129,14 +238,20 @@ describe("ReportsController", function () {
return mockedDate;
}
});

reportServiceMock.getAvailableDateRange.and.returnValue({
"Today": mockedDate,
"This Month": new originalDate('1-Jan-2023'),
"Previous Month": new originalDate(expectedStartDate)
});
setupController();

rootScope.default.reportsRequiringDateRange = {
dateRangeType: new originalDate(expectedStartDate),
};
scope.setDefault('dateRangeType', 'reportsRequiringDateRange');

expect(rootScope.reportsRequiringDateRange[0].startDate.getTime()).toBe(new originalDate(expectedStartDate).getTime());
expect(rootScope.reportsRequiringDateRange[0].stopDate.getTime()).toBe(new originalDate(expectedStopDate).getTime());
expect(rootScope.reportsRequiringDateRange[0].startDate.getTime()).toBe(new originalDate(expectedStartDate).getTime());
expect(rootScope.reportsRequiringDateRange[0].stopDate.getTime()).toBe(new originalDate(expectedStopDate).getTime());
});
});

Expand Down

0 comments on commit 8cb47d7

Please sign in to comment.