From 7eee1cad50366d465cd6a15ac3215ae404f209f7 Mon Sep 17 00:00:00 2001 From: Daniel Herrero Date: Tue, 6 Feb 2018 15:09:31 +0100 Subject: [PATCH] address observations crud created --- .../client/src/address-edit/address-edit.js | 24 +++++------ .../src/address-edit/address-edit.spec.js | 10 ++--- .../address/crudAddressObservations.js | 40 +++++++++++++++++++ .../common/models/address-observation.js | 3 ++ 4 files changed, 60 insertions(+), 17 deletions(-) create mode 100644 services/client/common/methods/address/crudAddressObservations.js create mode 100644 services/client/common/models/address-observation.js diff --git a/client/client/src/address-edit/address-edit.js b/client/client/src/address-edit/address-edit.js index 0ee19e2c6..8d7a56994 100644 --- a/client/client/src/address-edit/address-edit.js +++ b/client/client/src/address-edit/address-edit.js @@ -41,7 +41,7 @@ export default class Controller { } addObservation() { - this.observations.push({observationTypeFk: null, description: null, showAddIcon: true}); + this.observations.push({observationTypeFk: null, addressFk: this.address.id, description: null, showAddIcon: true}); this._setIconAdd(); } @@ -57,7 +57,7 @@ export default class Controller { } } _submitObservations(objectObservations) { - return this.$http.post('/client/api/crudAddressObservations', objectObservations); + return this.$http.post(`/client/api/AddressObservations/crudAddressObservations`, objectObservations); } submit() { @@ -66,9 +66,9 @@ export default class Controller { let submitObservations; let repeatedTypes = false; let observationsObj = { - remove: this.observationsRemoved, - news: [], - modified: [] + delete: this.observationsRemoved, + create: [], + update: [] }; for (let i = 0; i < this.observations.length; i++) { @@ -84,16 +84,16 @@ export default class Controller { break; } - this.observationsDictionary[observation.observationTypeFk] = observation; - if (!observation.id && observation.observationTypeFk && observation.description) { - observationsObj.news.push(observation); + observationsObj.create.push(observation); } else if (observation.id && this.observationsDictionary[observation.observationTypeFk].description !== observation.description) { - observationsObj.modified.push(observation); + observationsObj.update.push(observation); } + + this.observationsDictionary[observation.observationTypeFk] = observation; } - submitObservations = observationsObj.modified.length > 0 || observationsObj.news.length > 0 || observationsObj.remove.length > 0; + submitObservations = observationsObj.update.length > 0 || observationsObj.create.length > 0 || observationsObj.delete.length > 0; if (repeatedTypes) { this.vnApp.showMessage( @@ -125,8 +125,8 @@ export default class Controller { }; this.$http.get(`/client/api/AddressObservations?filter=${JSON.stringify(filter)}`).then(res => { this.observations = res.data; - this.observations.forEach(item => { - this.observationsDictionary[item.observationTypeFk] = {id: item.id, description: item.description}; + res.data.forEach(item => { + this.observationsDictionary[item.observationTypeFk] = Object.assign({}, item); }); }); } diff --git a/client/client/src/address-edit/address-edit.spec.js b/client/client/src/address-edit/address-edit.spec.js index 5d4ad71be..18fff508f 100644 --- a/client/client/src/address-edit/address-edit.spec.js +++ b/client/client/src/address-edit/address-edit.spec.js @@ -15,19 +15,19 @@ describe('Client', () => { $componentController = _$componentController_; $state = _$state_; $httpBackend = _$httpBackend_; - $state.params.addressId = '1234'; + $state.params.addressId = '1'; controller = $componentController('vnAddressEdit', {$state: $state}); })); it('should define and set address property', () => { - expect(controller.address.id).toBe(1234); + expect(controller.address.id).toBe(1); }); describe('$onInit()', () => { it('should perform a GET query to receive the address observations', () => { - let filter = {where: {addressFk: 1234}, include: [{relation: 'observationType'}]}; - let json = {data: 'some notes'}; - $httpBackend.when('GET', `/client/api/AddressObservations?filter=${JSON.stringify(filter)}`).respond(json); + let filter = {where: {addressFk: 1}, include: {relation: 'observationType'}}; + let res = ['some notes']; + $httpBackend.when('GET', `/client/api/AddressObservations?filter=${JSON.stringify(filter)}`).respond(res); $httpBackend.expectGET(`/client/api/AddressObservations?filter=${JSON.stringify(filter)}`); controller.$onInit(); $httpBackend.flush(); diff --git a/services/client/common/methods/address/crudAddressObservations.js b/services/client/common/methods/address/crudAddressObservations.js new file mode 100644 index 000000000..b158b0749 --- /dev/null +++ b/services/client/common/methods/address/crudAddressObservations.js @@ -0,0 +1,40 @@ +module.exports = Self => { + Self.remoteMethod('crudAddressObservations', { + description: 'create, delete or update address observations', + accessType: 'WRITE', + accepts: [ + { + arg: 'observations', + type: 'Object', + require: true, + description: 'object with observations to create, delete or update, Example: {delete: [], create: [], update: []}', + http: {source: 'body'} + } + ], + returns: { + arg: 'sumAmount' + }, + http: { + path: `/crudAddressObservations`, + verb: 'post' + } + }); + + Self.crudAddressObservations = observations => { + let promises = []; + + if (observations.delete.length) { + promises.push(Self.destroyAll({id: {inq: observations.delete}})); + } + if (observations.create.length) { + promises.push(Self.create(observations.create)); + } + if (observations.update.length) { + observations.update.forEach(observation => { + promises.push(Self.upsert(observation)); + }); + } + + return Promise.all(promises); + }; +}; diff --git a/services/client/common/models/address-observation.js b/services/client/common/models/address-observation.js new file mode 100644 index 000000000..579a47adf --- /dev/null +++ b/services/client/common/models/address-observation.js @@ -0,0 +1,3 @@ +module.exports = function(Self) { + require('../methods/address/crudAddressObservations.js')(Self); +};