Skip to content

Commit

Permalink
Merge pull request Bahmni#788 from Bahmni/BAH-3175
Browse files Browse the repository at this point in the history
BAH-3175 | Added Previous Month option in Bahmni Reports pre-filter
  • Loading branch information
gsluthra authored Nov 29, 2023
2 parents 8ebb31d + d76a116 commit c5beb59
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
15 changes: 12 additions & 3 deletions ui/app/reports/controllers/reportsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ angular.module('bahmni.reports')
setToChange.forEach(function (report) {
if (item == 'dateRangeType') {
$rootScope.default.reportsRequiringDateRange.startDate = $rootScope.default[header][item];
$rootScope.default.reportsRequiringDateRange.stopDate = dateRange[0];
$rootScope.default.reportsRequiringDateRange.stopDate = isPreviousMonth($rootScope.default[header][item]) ? getPreviousMonthEndDate() : dateRange[0];
report['startDate'] = $rootScope.default[header][item];
report['stopDate'] = dateRange[0];
report['stopDate'] = isPreviousMonth($rootScope.default[header][item]) ? getPreviousMonthEndDate() : dateRange[0];
}
else if ($rootScope.default[header][item] === undefined) {
$rootScope.reportsRequiringDateRange.forEach(function (report) {
report.startDate = dateRange[0];
report.stopDate = dateRange[0];
report.stopDate = isPreviousMonth(dateRange[0]) ? getPreviousMonthEndDate() : dateRange[0];
report.responseType = format[1];
});
}
Expand All @@ -50,6 +50,15 @@ angular.module('bahmni.reports')
});
};

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);
};

var isDateRangeRequiredFor = function (report) {
return _.find($rootScope.reportsRequiringDateRange, { name: report.name });
};
Expand Down
1 change: 1 addition & 0 deletions ui/app/reports/services/reportService.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ angular.module('bahmni.reports')
var avaialbleDateRange = {
"Today": currentDate,
"This Month": new Date(currentDate.getFullYear(), currentDate.getMonth(), 1),
"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)),
Expand Down
48 changes: 40 additions & 8 deletions ui/test/unit/reports/controllers/reportsController.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ describe("ReportsController", function () {
}
}
};

var originalDate;

beforeEach(module('bahmni.reports'));

beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
rootScope = $rootScope;
originalDate = window.Date;

messagingServiceMock = jasmine.createSpyObj('messagingService', ['showMessage']);
spinnerMock = jasmine.createSpyObj('spinner', ['forPromise']);
Expand Down Expand Up @@ -70,6 +74,10 @@ describe("ReportsController", function () {
});
}));

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

it("initializes report sets based on whether date range required or not", function () {
expect(mockAppDescriptor.getConfigForPage).toHaveBeenCalledWith("reports");
expect(rootScope.reportsRequiringDateRange.length).toBe(2);
Expand All @@ -92,20 +100,44 @@ describe("ReportsController", function () {
expect(scope.formats['HTML']).toBe('text/html');
});

it('should initialise date range with supportedDateRange config', function () {
it('should return the same start and stop date when selected date range is today', function () {
rootScope.default.reportsRequiringDateRange = {
dateRangeType: new Date(),
startDate: new Date(),
stopDate: new Date(),
dateRangeType: new Date()
};
scope.setDefault('dateRangeType', 'reportsRequiringDateRange');
scope.setDefault('startDate', 'reportsRequiringDateRange');
scope.setDefault('stopDate', 'reportsRequiringDateRange');

expect(_.keys(rootScope.default.reportsRequiringDateRange).length).toBe(3);
expect((rootScope.default.reportsRequiringDateRange.dateRangeType).getDate()).toBe(new Date().getDate());
expect(rootScope.reportsRequiringDateRange[0].startDate).toBe(rootScope.default.reportsRequiringDateRange.startDate);
expect(rootScope.reportsRequiringDateRange[0].stopDate).toBe(rootScope.default.reportsRequiringDateRange.stopDate);
expect(rootScope.reportsRequiringDateRange[0].startDate.getDate()).toBe(new Date().getDate());
expect(rootScope.reportsRequiringDateRange[0].stopDate.getDate()).toBe(new Date().getDate());
});

const previousMonthTestCases = [
{ currentDate: '1-Dec-2022', expectedStartDate: '1-Nov-2022', expectedStopDate: '30-Nov-2022' },
{ currentDate: '3-Jan-2023', expectedStartDate: '1-Dec-2022', expectedStopDate: '31-Dec-2022' },
{ currentDate: '5-Mar-2023', expectedStartDate: '1-Feb-2023', expectedStopDate: '28-Feb-2023' },
{ currentDate: '5-Mar-2024', expectedStartDate: '1-Feb-2024', expectedStopDate: '29-Feb-2024' }
];

previousMonthTestCases.forEach(({ currentDate, expectedStartDate, expectedStopDate }) => {
it(`should return previous month start and stop date when current date is ${currentDate} and selected date range is previous month`, function () {
var mockedDate = new Date(currentDate);
spyOn(window, 'Date').and.callFake(function (year, month, day) {
if (arguments.length === 3) {
return new originalDate(year, month, day);
} else {
return mockedDate;
}
});

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());
});
});

it('should initialise all available formats when supportedFormats config is not specified', function () {
Expand Down

0 comments on commit c5beb59

Please sign in to comment.