Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BAH 3049 | Samridhi | Fix start date undefined value #716

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
9 changes: 8 additions & 1 deletion ui/app/reports/controllers/reportsController.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
'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,
Expand Down Expand Up @@ -38,6 +41,7 @@ angular.module('bahmni.reports')
report['stopDate'] = 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 = dateRange[0];
Expand Down Expand Up @@ -76,6 +80,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
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
76 changes: 76 additions & 0 deletions ui/test/unit/reports/controllers/reportsController.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,82 @@ describe("ReportsController", function () {
expect(rootScope.reportsRequiringDateRange[0].stopDate).toBe(rootScope.default.reportsRequiringDateRange.stopDate);
});

it("should return date and month for the this month", function () {
var currentDate = new Date();
rootScope.default.reportsRequiringDateRange = {
dateRangeType: new Date(currentDate.getFullYear(), currentDate.getMonth(), 1)
};

scope.setDefault('dateRangeType', 'reportsRequiringDateRange');

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

it("should return date and month for the this quarter", function () {
var currentDate = new Date();
rootScope.default.reportsRequiringDateRange = {
dateRangeType: new Date(currentDate.getFullYear(), Math.floor(currentDate.getMonth() / 3) * 3, 1)
};

scope.setDefault('dateRangeType', 'reportsRequiringDateRange');

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

it("should return date and month for the this year", function () {
var currentDate = new Date();
rootScope.default.reportsRequiringDateRange = {
dateRangeType: new Date(currentDate.getFullYear(), 0, 1)
};

scope.setDefault('dateRangeType', 'reportsRequiringDateRange');

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

it("should return date and month for last 7 days", function () {
var currentDate = new Date();
rootScope.default.reportsRequiringDateRange = {
dateRangeType: new Date(new Date().setDate(currentDate.getDate() - 6))
};

scope.setDefault('dateRangeType', 'reportsRequiringDateRange');

expect(_.keys(rootScope.default.reportsRequiringDateRange).length).toBe(3);
expect(rootScope.reportsRequiringDateRange[0].startDate.getDate()).toBe(rootScope.default.reportsRequiringDateRange.startDate.getDate());
expect(rootScope.reportsRequiringDateRange[0].startDate.getMonth()).toBe(currentDate.getMonth());
expect(rootScope.reportsRequiringDateRange[0].stopDate.getDate()).toBe(currentDate.getDate());
expect(rootScope.reportsRequiringDateRange[0].stopDate.getMonth()).toBe(currentDate.getMonth());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the currentDate is 2-Oct-2023 (Mon), then for Menu option Last 7 days, the dates selected automatically should be: 26-Sep-2023 (Tue) to 2-Oct-2023 (Mon). In this case the startDate month = Sep (and not Oct). But I think your test is checking startDate.getMonth() to be currentDate.getMonth(). That isn't right I think. This test will break on certain days I think. Please reconfirm.

It would have been best if somehow tests could be written independent of actual date (today), and instead could be passed something like: If Current Date is X, then option Last 7 Days should show DateA and DateB, and so on. I am guessing that will require a little refactoring if your code, to take currentDate as a parameter, and then do calculations.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can otherwise try and use Jasmine clock to mock the current Date. See this ChatGPT discussion I just did regarding this code block (scroll to end for Jasmine clock): https://chat.openai.com/share/42d88843-c43c-4dfc-81bc-6ef80438efbf

});

it("should return date and month for last 30 days", function () {
var currentDate = new Date();
rootScope.default.reportsRequiringDateRange = {
dateRangeType: new Date(new Date().setDate(currentDate.getDate() - 30))
};

scope.setDefault('dateRangeType', 'reportsRequiringDateRange');

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

it('should initialise all available formats when supportedFormats config is not specified', function () {
mockAppDescriptor.getConfigValue.and.returnValue(undefined);

Expand Down
Loading