Skip to content

Commit

Permalink
OAM-45: add code generation, sort by default
Browse files Browse the repository at this point in the history
  • Loading branch information
olewandowski1 committed Apr 30, 2024
1 parent 986c76c commit 06d7f1f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 72 deletions.
9 changes: 6 additions & 3 deletions src/admin-facility-view/admin-facility-view.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@
return programService.getAll();
},
wards: function(wardService, facility) {
return wardService.getWardsByFacility({
facilityId: facility.id
}).then(function(response) {
var searchParams = {
facilityId: facility.id,
sort: 'code,asc'
};

return wardService.getWardsByFacility(searchParams).then(function(response) {
return response.content;
});
}
Expand Down
55 changes: 34 additions & 21 deletions src/admin-facility-view/facility-view.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
vm.wards = wards;
vm.selectedTab = 0;
vm.managedExternally = facility.isManagedExternally();
vm.generateWardCode = generateWardCode;

if (!vm.facilityWithPrograms.supportedPrograms) {
vm.facilityWithPrograms.supportedPrograms = [];
Expand Down Expand Up @@ -286,36 +287,48 @@
*/
function addWard() {
var newWard = angular.copy(vm.newWard);

newWard.code = vm.generateWardCode(vm.facility.code);
newWard.facility = {
id: vm.facility.id
};

return wardService.getAllWards().then(function(dbWards) {
var wardExistsInDb = wardExists(dbWards.content, newWard);
var wardExistsInLocalState = wardExists(vm.wards, newWard);
vm.wards.push(newWard);

if (wardExistsInDb || wardExistsInLocalState) {
return notifyAndReturn('adminFacilityView.wardExists');
}
vm.newWard = {
disabled: false
};

vm.wards.push(newWard);
vm.newWard = {
disabled: false
};
return $q.when();
}

return $q.when();
});
/**
* @ngdoc method
* @methodOf admin-facility-view.controller:FacilityViewController
* @name generateWardCode
*
* @description
* Generates ward code based on the facility code.
*/
function generateWardCode(facilityCode) {
var serialNumber = padNumber(vm.wards.length + 1, 4);

function wardExists(wards, wardToCheck) {
return wards.some(function(ward) {
return wardToCheck.code === ward.code;
});
}
return facilityCode + '.' + serialNumber;
}

function notifyAndReturn(message) {
notificationService.error(message);
return $q.when();
}
/**
* @ngdoc method
* @methodOf admin-facility-view.controller:FacilityViewController
* @name padNumber
*
* @description
* Pads a number with leading zeros.
*/
function padNumber(number, length) {
var numberString = number.toString();
var padding = length - numberString.length;

return '0'.repeat(padding) + numberString;
}

/**
Expand Down
44 changes: 9 additions & 35 deletions src/admin-facility-view/facility-view.controller.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,16 +261,24 @@ describe('FacilityViewController', function() {
id: 'facility-id'
};
this.vm.newWard = {
code: 'ward-code',
disabled: false
};
spyOn(this.vm, 'generateWardCode').andReturn('generated-code');
});

it('should generate ward code', function() {
this.vm.addWard();

expect(this.vm.generateWardCode).toHaveBeenCalled();
expect(this.vm.wards[0].code).toEqual('generated-code');
});

it('should add new ward to the list', function() {
var newWard = angular.copy(this.vm.newWard);
newWard.facility = {
id: this.vm.facility.id
};
newWard.code = 'generated-code';

spyOn(this.wardService, 'getAllWards').andReturn(this.$q.when({
content: []
Expand All @@ -294,40 +302,6 @@ describe('FacilityViewController', function() {
disabled: false
});
});

it('should not add new ward if it exists in db', function() {
var existingWard = angular.copy(this.vm.newWard);
existingWard.facility = {
id: this.vm.facility.id
};

spyOn(this.wardService, 'getAllWards').andReturn(this.$q.when({
content: [existingWard]
}));

this.vm.addWard();
this.$rootScope.$apply();

expect(this.vm.wards.length).toBe(0);
});

it('should not add new ward if it exists in local state', function() {
var existingWard = angular.copy(this.vm.newWard);
existingWard.facility = {
id: this.vm.facility.id
};

this.vm.wards.push(existingWard);

spyOn(this.wardService, 'getAllWards').andReturn(this.$q.when({
content: []
}));

this.vm.addWard();
this.$rootScope.$apply();

expect(this.vm.wards.length).toBe(1);
});
});

describe('saveFacilityWards', function() {
Expand Down
3 changes: 1 addition & 2 deletions src/admin-facility-view/messages_en.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@
"adminFacilityView.name": "Name",
"adminFacilityView.description": "Description",
"adminFacilityView.code": "Code",
"adminFacilityView.wardExists": "Ward with the same code already exists and might be attached to different facility. Ensure code uniqueness.",
"adminFacilityView.wardIsDisabled.note": "NOTE: Once a Service/Ward is disabled, it is not available for issuing stock.",
"adminFacilityView.uniqueCodePopover": "Must be unique and is not editable once saved.",
"adminFacilityView.codePopover": "Generated by system, ensuring code uniqueness.",
"adminFacilityView.statusPopover": "If checked, the Service/Ward won't be available for issuing stock.",
"adminFacilityView.saveWards.success": "Services & Wards saved successfully!",
"adminFacilityView.saveWards.fail": "Failed to save Services & Wards."
Expand Down
12 changes: 1 addition & 11 deletions src/admin-facility-view/services-and-wards.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
<form id="add-ward-form" ng-submit="vm.addWard()" reload-form>
<div class="form-group">
<label for="wardCode">{{'adminFacilityView.code' | message}}</label>
<input
id="wardCode"
type="text"
ng-model="vm.newWard.code"
ng-disabled="vm.managedExternally"
required
/>
</div>
<div class="form-group">
<label for="wardName">{{'adminFacilityView.name' | message}}</label>
<input
Expand Down Expand Up @@ -60,7 +50,7 @@
<thead>
<tr>
<th
openlmis-popover="{{'adminFacilityView.uniqueCodePopover' | message}}"
openlmis-popover="{{'adminFacilityView.codePopover' | message}}"
>
{{'adminFacilityView.code' | message}}
</th>
Expand Down

0 comments on commit 06d7f1f

Please sign in to comment.