fix: put requests
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Vicent Llopis 2022-05-27 13:54:35 +02:00
parent 6359c9e5bf
commit da1e188983
4 changed files with 155 additions and 70 deletions

View File

@ -8,6 +8,7 @@
}, },
"properties": { "properties": {
"zoneExclusionFk": { "zoneExclusionFk": {
"id": true,
"type": "number", "type": "number",
"required": true "required": true
}, },

View File

@ -15,6 +15,7 @@ describe('component vnZoneCalendar', () => {
controller.zone = {id: 1}; controller.zone = {id: 1};
controller.days = []; controller.days = [];
controller.exclusions = []; controller.exclusions = [];
controller.geoExclusions = [];
})); }));
describe('date() setter', () => { describe('date() setter', () => {
@ -122,7 +123,8 @@ describe('component vnZoneCalendar', () => {
$events: [], $events: [],
$exclusions: [], $exclusions: [],
$type: 'day', $type: 'day',
$weekday: 1 $weekday: 1,
$geoExclusions: [],
} }
); );
}); });

View File

@ -6,9 +6,9 @@ class Controller extends Section {
constructor($element, $, vnWeekDays) { constructor($element, $, vnWeekDays) {
super($element, $); super($element, $);
this.vnWeekDays = vnWeekDays; this.vnWeekDays = vnWeekDays;
this.editMode = 'include'; this.editMode = 'exclude';
this.exclusions; this.exclusions;
this.days; this.geoExclusions;
} }
$onInit() { $onInit() {
@ -23,6 +23,10 @@ class Controller extends Section {
return `Zones/${this.$params.id}/exclusions`; return `Zones/${this.$params.id}/exclusions`;
} }
get geoExclusionsPath() {
return `Zones/exclusionGeo`;
}
get checked() { get checked() {
const geos = this.$.model.data || []; const geos = this.$.model.data || [];
const checkedLines = []; const checkedLines = [];
@ -64,57 +68,57 @@ class Controller extends Section {
onSelection(days, type, weekday, events, exclusions, geoExclusions) { onSelection(days, type, weekday, events, exclusions, geoExclusions) {
if (this.editMode == 'include') { if (this.editMode == 'include') {
if (events.length) if (events.length)
this.edit(events[0]); this.editInclusion(events[0]);
else else
this.create(type, days, weekday); this.createInclusion(type, days, weekday);
} else { } else {
if (geoExclusions.length) if (geoExclusions.length)
this.editExclusion(geoExclusions); this.editExclusion(geoExclusions);
else if (exclusions.length) else if (exclusions.length)
this.editExclusion(exclusions); this.editExclusion(exclusions);
else else
this.createExclusion(exclusions, days); this.createExclusion(days);
} }
} }
editExclusion(exclusions) { editExclusion(allTypeExclusions) {
this.isNew = false; this.isNew = false;
this.excludeSelected = angular.copy(exclusions[0]); this.excludeSelected = angular.copy(allTypeExclusions[0]);
this.exclusions = exclusions;
if (this.excludeSelected.geoFk) if (this.excludeSelected.geoFk) {
this.excludeSelected.type = 'specificLocations'; this.excludeSelected.type = 'specificLocations';
else this.geoExclusions = allTypeExclusions;
} else {
this.excludeSelected.type = 'all'; this.excludeSelected.type = 'all';
this.exclusions = allTypeExclusions;
}
this.$.excludeDialog.show(); this.$.excludeDialog.show();
} }
createExclusion(exclusions, days) { createExclusion(days) {
this.isNew = true; this.isNew = true;
this.excludeSelected = { this.excludeSelected = {
type: 'all', type: 'all',
dated: days[0] dated: days[0]
}; };
this.exclusions = exclusions; this.geoExclusions = [];
this.days = days;
this.$.excludeDialog.show(); this.$.excludeDialog.show();
} }
onEditClick(row, event) { onEditClick(row, event) {
if (event.defaultPrevented) return; if (event.defaultPrevented) return;
this.edit(row); this.editInclusion(row);
} }
edit(row) { editInclusion(row) {
this.isNew = false; this.isNew = false;
this.selected = angular.copy(row); this.selected = angular.copy(row);
this.selected.wdays = this.vnWeekDays.fromSet(row.weekDays); this.selected.wdays = this.vnWeekDays.fromSet(row.weekDays);
this.$.includeDialog.show(); this.$.includeDialog.show();
} }
create(type, days, weekday) { createInclusion(type, days, weekday) {
this.isNew = true; this.isNew = true;
if (type == 'weekday') { if (type == 'weekday') {
@ -173,31 +177,19 @@ class Controller extends Section {
} }
onExcludeResponse(response) { onExcludeResponse(response) {
const type = this.excludeSelected.type;
switch (response) { switch (response) {
case 'accept': { case 'accept': {
let excludeSelected = this.excludeSelected;
let type = excludeSelected.type;
if (type == 'all') if (type == 'all')
return this.exclusionCreate(this.days); return this.exclusionCreate();
else { else
const geoIds = []; return this.exclusionGeoCreate();
for (let zoneGeo of this.checked) {
geoIds.push({
id: zoneGeo.id
});
}
const params = {
zoneFk: parseInt(this.$params.id),
date: this.days[0],
geoIds: geoIds
};
return this.$http.post(`Zones/exclusionGeo`, params)
.then(() => this.refresh());
}
} }
case 'delete': case 'delete':
if (type == 'all')
return this.exclusionDelete(this.exclusions); return this.exclusionDelete(this.exclusions);
if (type == 'specificLocations')
return this.exclusionDelete(this.geoExclusions);
} }
} }
@ -218,13 +210,51 @@ class Controller extends Section {
.then(() => this.refresh()); .then(() => this.refresh());
} }
exclusionCreate(days) { exclusionCreate() {
let exclusions = days.map(dated => { const excludeSelected = this.excludeSelected;
return {dated}; const dated = excludeSelected.dated;
}); let req;
this.$http.post(this.exclusionsPath, exclusions) if (this.isNew)
.then(() => this.refresh()); req = this.$http.post(this.exclusionsPath, [{dated}]);
else
req = this.$http.put(`${this.exclusionsPath}/${excludeSelected.id}`, {dated});
return req.then(() => {
this.excludeSelected = null;
this.isNew = null;
this.refresh();
});
}
exclusionGeoCreate() {
const excludeSelected = this.excludeSelected;
let req;
const geoIds = [];
for (let zoneGeo of this.checked) {
geoIds.push({
id: zoneGeo.id
});
}
const params = {
zoneFk: parseInt(this.$params.id),
date: excludeSelected.dated,
geoIds: geoIds
};
console.log(excludeSelected);
if (this.isNew)
req = this.$http.post(this.geoExclusionsPath, params);
else
req = this.$http.put(`${this.geoExclusionsPath}/${excludeSelected.zoneExclusionFk}`, params);
return req.then(() => {
this.excludeSelected = null;
this.isNew = null;
this.refresh();
});
// return this.$http.post(`Zones/exclusionGeo`, params)
// .then(() => this.refresh());
} }
exclusionDelete(exclusions) { exclusionDelete(exclusions) {
@ -263,8 +293,8 @@ class Controller extends Section {
const data = this.$.model.data; const data = this.$.model.data;
for (let geo of data) { for (let geo of data) {
for (let exclusion of this.exclusions) { for (let geoExclusion of this.geoExclusions) {
if (geo.id == exclusion.geoFk) if (geo.id == geoExclusion.geoFk)
geo.checked = true; geo.checked = true;
} }
} }

View File

@ -34,7 +34,8 @@ describe('component vnZoneEvents', () => {
const query = `Zones/getEventsFiltered?ended=${date}&started=${date}&zoneFk=${params.zoneFk}`; const query = `Zones/getEventsFiltered?ended=${date}&started=${date}&zoneFk=${params.zoneFk}`;
const response = { const response = {
events: 'myEvents', events: 'myEvents',
exclusions: 'myExclusions' exclusions: 'myExclusions',
geoExclusions: 'myGeoExclusions',
}; };
$httpBackend.whenGET(query).respond(response); $httpBackend.whenGET(query).respond(response);
controller.refresh(); controller.refresh();
@ -76,38 +77,50 @@ describe('component vnZoneEvents', () => {
expect(controller.create).toHaveBeenCalledWith(type, days, weekday); expect(controller.create).toHaveBeenCalledWith(type, days, weekday);
}); });
it('should call the exclusionDelete() method', () => { // it('should call the exclusionDelete() method', () => {
jest.spyOn(controller, 'exclusionDelete').mockReturnThis(); // jest.spyOn(controller, 'exclusionDelete').mockReturnThis();
const weekday = {}; // const weekday = {};
const days = []; // const days = [];
const type = 'EventType'; // const type = 'EventType';
const events = []; // const events = [];
const exclusions = [{id: 1}]; // const exclusions = [{id: 1}];
controller.editMode = 'delete'; // const geoExclusions = [];
controller.onSelection(days, type, weekday, events, exclusions); // controller.editMode = 'delete';
// controller.onSelection(days, type, weekday, events, exclusions, geoExclusions);
expect(controller.exclusionDelete).toHaveBeenCalledWith(exclusions); // expect(controller.exclusionDelete).toHaveBeenCalledWith(exclusions);
}); // });
it('should call the exclusionCreate() method', () => { it('should call the exclusionCreate() method', () => {
jest.spyOn(controller, 'exclusionCreate').mockReturnThis(); jest.spyOn(controller, 'exclusionCreate').mockReturnThis();
const weekday = {};
const days = [{dated: new Date()}]; const days = [{dated: new Date()}];
const type = 'EventType'; controller.days = days;
const events = []; controller.excludeSelected = {type: 'all'};
const exclusions = []; controller.onExcludeResponse('accept');
controller.editMode = 'delete';
controller.onSelection(days, type, weekday, events, exclusions);
expect(controller.exclusionCreate).toHaveBeenCalledWith(days); expect(controller.exclusionCreate).toHaveBeenCalledWith(days);
}); });
// it('should call the exclusionCreate() method', () => {
// jest.spyOn(controller, 'exclusionCreate').mockReturnThis();
// const weekday = {};
// const days = [{dated: new Date()}];
// const type = 'EventType';
// const events = [];
// const exclusions = [];
// controller.editMode = 'delete';
// controller.onSelection(days, type, weekday, events, exclusions);
// expect(controller.exclusionCreate).toHaveBeenCalledWith(days);
// });
}); });
describe('create()', () => { describe('create()', () => {
it('shoud set the selected property and then call the dialog show() method', () => { it('shoud set the selected property and then call the includeDialog show() method', () => {
controller.$.dialog = {show: jest.fn()}; controller.$.includeDialog = {show: jest.fn()};
const type = 'weekday'; const type = 'weekday';
const days = [new Date()]; const days = [new Date()];
@ -120,11 +133,11 @@ describe('component vnZoneEvents', () => {
expect(selection.type).toEqual('indefinitely'); expect(selection.type).toEqual('indefinitely');
expect(firstWeekday).toBeTruthy(); expect(firstWeekday).toBeTruthy();
expect(controller.isNew).toBeTruthy(); expect(controller.isNew).toBeTruthy();
expect(controller.$.dialog.show).toHaveBeenCalledWith(); expect(controller.$.includeDialog.show).toHaveBeenCalledWith();
}); });
it('shoud set the selected property with the first day and then call the dialog show() method', () => { it('shoud set the selected property with the first day and then call the includeDialog show() method', () => {
controller.$.dialog = {show: jest.fn()}; controller.$.includeDialog = {show: jest.fn()};
const type = 'nonListedType'; const type = 'nonListedType';
const days = [new Date()]; const days = [new Date()];
@ -136,7 +149,7 @@ describe('component vnZoneEvents', () => {
expect(selection.type).toEqual('day'); expect(selection.type).toEqual('day');
expect(selection.dated).toEqual(days[0]); expect(selection.dated).toEqual(days[0]);
expect(controller.isNew).toBeTruthy(); expect(controller.isNew).toBeTruthy();
expect(controller.$.dialog.show).toHaveBeenCalledWith(); expect(controller.$.includeDialog.show).toHaveBeenCalledWith();
}); });
}); });
@ -180,6 +193,45 @@ describe('component vnZoneEvents', () => {
}); });
}); });
describe('onExcludeResponse()', () => {
it('should call the exclusionDelete() method', () => {
jest.spyOn(controller, 'exclusionDelete').mockReturnThis();
const exclusions = [{id: 1}];
controller.exclusions = exclusions;
controller.onExcludeResponse('delete');
expect(controller.exclusionDelete).toHaveBeenCalledWith(exclusions);
});
it('shoud make an HTTP POST query to create a new one and then call the refresh() method', () => {
jest.spyOn(controller, 'refresh').mockReturnThis();
controller.selected = {id: 1};
controller.isNew = true;
$httpBackend.when('POST', `Zones/1/events`).respond(200);
controller.onExcludeResponse('accept');
$httpBackend.flush();
expect(controller.refresh).toHaveBeenCalledWith();
});
it('shoud make an HTTP PUT query and then call the refresh() method', () => {
jest.spyOn(controller, 'refresh').mockReturnThis();
controller.selected = {id: 1};
controller.isNew = false;
const eventId = 1;
$httpBackend.when('PUT', `Zones/1/events/${eventId}`).respond(200);
controller.onExcludeResponse('accept');
$httpBackend.flush();
expect(controller.refresh).toHaveBeenCalledWith();
});
});
describe('onDeleteResponse()', () => { describe('onDeleteResponse()', () => {
it('shoud make an HTTP DELETE query and then call the refresh() method', () => { it('shoud make an HTTP DELETE query and then call the refresh() method', () => {
jest.spyOn(controller, 'refresh').mockReturnThis(); jest.spyOn(controller, 'refresh').mockReturnThis();