address observations crud created

This commit is contained in:
Daniel Herrero 2018-02-06 15:09:31 +01:00
parent fd4b427518
commit 7eee1cad50
4 changed files with 60 additions and 17 deletions

View File

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

View File

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

View File

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

View File

@ -0,0 +1,3 @@
module.exports = function(Self) {
require('../methods/address/crudAddressObservations.js')(Self);
};