Merge branch 'dev' of https://git.verdnatura.es/salix into dev

This commit is contained in:
gerard 2018-08-27 11:40:14 +02:00
commit 0566dd5328
4 changed files with 76 additions and 108 deletions

View File

@ -3,4 +3,5 @@ Is equalizated: Recargo de equivalencia
Observation type: Tipo de observación
Description: Descripción
The observation type must be unique: El tipo de observación ha de ser único
Remove note: Quitar nota
Remove note: Quitar nota
Add note: Añadir nota

View File

@ -10,7 +10,7 @@
data="contacts"
form="form">
</vn-watcher>
<form name="form" ng-submit="$ctrl.submit()">
<form name="form" ng-submit="$ctrl.onSubmit()">
<vn-card pad-large>
<vn-title>Contacts</vn-title>
<vn-horizontal ng-repeat="contact in contacts">

View File

@ -7,33 +7,31 @@ class Controller {
this.$stateParams = $stateParams;
this.$translate = $translate;
this.vnApp = vnApp;
this.removedContacts = [];
}
onDataChange() {
this.contacts = this.$scope.model.data;
this.oldContacts = this.$scope.model.data;
}
/**
* Saves the original contacts
* @param {Object} value - Default values
*/
set oldContacts(value) {
this._oldContacts = [];
this.oldInstances = Object.assign([], this.contacts);
this.removedContacts = [];
value.forEach(item => {
this._oldContacts[item.id] = Object.assign({}, item);
});
}
get oldContacts() {
return this._oldContacts;
add() {
let data = {
clientFk: this.client.id,
name: this.$translate.instant('Phone'),
phone: null
};
this.contacts.push(data);
}
submit() {
let query = `/client/api/ClientContacts/crud`;
remove(index) {
if (this.contacts[index] && this.contacts[index].id)
this.removedContacts.push(this.contacts[index].id);
this.contacts.splice(index, 1);
}
onSubmit() {
let data = {
clientFk: this.client.id,
delete: this.removedContacts,
@ -45,66 +43,23 @@ class Controller {
if (typeof item.id === 'undefined')
data.create.push(item);
if (typeof item.id !== 'undefined' && !this.isEqual(item, this.oldContacts[item.id]))
if (typeof item.id !== 'undefined' && this.hasChanges(item))
data.update.push(item);
});
if (!this.hasChanges(data))
return this.vnApp.showError(this.$translate.instant('No changes to save'));
this.$scope.watcher.check();
if (this.$scope.form.$invalid)
return this.vnApp.showError(this.$translate.instant('Some fields are invalid'));
this.$http.post(query, data).then(() => {
this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
this.$http.post(`/client/api/ClientContacts/crud`, data).then(() => {
this.$scope.watcher.notifySaved();
this.$scope.model.refresh();
});
}
/**
* Remove contact from list
* @param {Int} index - Contact array position
*/
remove(index) {
if (this.contacts[index] && this.contacts[index].id)
this.removedContacts.push(this.contacts[index].id);
this.contacts.splice(index, 1);
}
/**
* Add contact to list
*/
add() {
let data = {
clientFk: this.client.id,
name: this.$translate.instant('Phone'),
phone: null
};
this.contacts.push(data);
}
/**
* Returns true/false if the new
* contact equals the old one
* @param {Object} newContact - New contact
* @param {Object} oldContact - Old contact
* @return {Boolean} - True if are equals
*/
isEqual(newContact, oldContact) {
return newContact.name === oldContact.name && newContact.phone == oldContact.phone;
}
/**
* Checks if there's any changes to do
* @param {Object} data - Crud data
* @return {Boolean} - Returns true if there's any changes to do
*/
hasChanges(data) {
if (data.create.length || data.update.length || data.delete.length)
return true;
return false;
hasChanges(instance) {
let oldInstance = this.oldInstances.find(item => {
return item.id == instance.id;
});
return instance.name != oldInstance.name || instance.phone != oldInstance.phone;
}
}

View File

@ -19,13 +19,32 @@ describe('Client', () => {
$httpBackend.when('GET', /\/locale\/\w+\/[a-z]{2}\.json/).respond({});
$scope = $rootScope.$new();
$scope.form = {$invalid: false};
$scope.model = {refresh: () => {}};
$scope.model = {
refresh: () => {},
data: [
{id: 1, name: 'My contact 1', phone: '123456789'},
{id: 2, name: 'My contact 2', phone: '123456789'},
{id: 3, name: 'My contact 3', phone: '123456789'}
]
};
$scope.watcher = {check: () => {}, notifySaved: () => {}};
controller = $componentController('vnClientContact', {$scope: $scope}, {$state: $state});
controller.client = {
id: 101
};
}));
describe('onDataChange()', () => {
it('should define contacts and oldInstances properties into controller', () => {
controller.onDataChange();
expect(controller.contacts).toBeDefined();
expect(controller.contacts.length).toEqual(3);
expect(controller.oldInstances).toBeDefined();
expect(controller.oldInstances.length).toEqual(3);
});
});
describe('add / remove tags', () => {
it('should add one empty contact into controller contacts collection', () => {
controller.contacts = [];
@ -37,6 +56,7 @@ describe('Client', () => {
it('should remove a contact that occupies the position in the index', () => {
let index = 2;
controller.removedContacts = [];
controller.contacts = [
{id: 1, name: 'My contact 1', phone: '123456789'},
{id: 2, name: 'My contact 2', phone: '123456789'},
@ -50,29 +70,30 @@ describe('Client', () => {
});
});
describe('isEqual()', () => {
it('should return true if two contacts are equals', () => {
let contact1 = {id: 1, name: 'My contact 1', phone: '123456789'};
let contact2 = {id: 1, name: 'My contact 1', phone: '123456789'};
let equals = controller.isEqual(contact1, contact2);
describe('hasChanges()', () => {
it('should return true if the instance has changes', () => {
controller.oldInstances = [{id: 1, name: 'My contact 1', phone: '123456789'}];
let contact = {id: 1, name: 'My renamed contact', phone: '123456789'};
let hasChanges = controller.hasChanges(contact);
expect(equals).toBeTruthy();
expect(hasChanges).toBeTruthy();
});
it('should return false if two contacts aint equal', () => {
let contact1 = {id: 1, name: 'My contact 1', phone: '123456789'};
let contact2 = {id: 2, name: 'My contact 2', phone: '123456789'};
let equals = controller.isEqual(contact1, contact2);
it(`should return false if the instance hasn't changes`, () => {
controller.oldInstances = [{id: 1, name: 'My contact 1', phone: '123456789'}];
let contact = {id: 1, name: 'My contact 1', phone: '123456789'};
let hasChanges = controller.hasChanges(contact);
expect(equals).toBeFalsy();
expect(hasChanges).toBeFalsy();
});
});
describe('submit()', () => {
describe('onSubmit()', () => {
it("should perfom a query to delete contacts", () => {
controller.oldContacts = [];
controller.oldContacts[1] = {id: 1, clientFk: 101, name: 'My contact 1', phone: '123456789'};
controller.oldContacts[2] = {id: 2, clientFk: 101, name: 'My contact 2', phone: '123456789'};
controller.oldInstances = [
{id: 1, clientFk: 101, name: 'My contact 1', phone: '123456789'},
{id: 2, clientFk: 101, name: 'My contact 2', phone: '123456789'}
];
controller.contacts = [
{id: 2, name: 'My contact 2', phone: '123456789'}
@ -88,14 +109,15 @@ describe('Client', () => {
$httpBackend.whenPOST(`/client/api/ClientContacts/crud`, newData).respond(200, true);
$httpBackend.expectPOST(`/client/api/ClientContacts/crud`, newData);
controller.submit();
controller.onSubmit();
$httpBackend.flush();
});
it("should perfom a query to update contacts", () => {
controller.oldContacts = [];
controller.oldContacts[1] = {id: 1, clientFk: 101, name: 'My contact 1', phone: '123456789'};
controller.oldContacts[2] = {id: 2, clientFk: 101, name: 'My contact 2', phone: '123456789'};
controller.oldInstances = [
{id: 1, clientFk: 101, name: 'My contact 1', phone: '123456789'},
{id: 2, clientFk: 101, name: 'My contact 2', phone: '123456789'}
];
controller.contacts = [
{id: 1, clientFk: 101, name: 'My contact 1', phone: '123456789'},
@ -114,14 +136,15 @@ describe('Client', () => {
$httpBackend.whenPOST(`/client/api/ClientContacts/crud`, newData).respond(200, true);
$httpBackend.expectPOST(`/client/api/ClientContacts/crud`, newData);
controller.submit();
controller.onSubmit();
$httpBackend.flush();
});
it("should perfom a query to create new contact", () => {
controller.oldContacts = [];
controller.oldContacts[1] = {id: 1, name: 'My contact 1', phone: '123456789'};
controller.oldContacts[2] = {id: 2, name: 'My contact 2', phone: '123456789'};
controller.oldInstances = [
{id: 1, clientFk: 101, name: 'My contact 1', phone: '123456789'},
{id: 2, clientFk: 101, name: 'My contact 2', phone: '123456789'}
];
controller.contacts = [
{id: 1, name: 'My contact 1', phone: '123456789'},
@ -139,20 +162,9 @@ describe('Client', () => {
$httpBackend.whenPOST(`/client/api/ClientContacts/crud`, newData).respond(200, true);
$httpBackend.expectPOST(`/client/api/ClientContacts/crud`, newData);
controller.submit();
controller.onSubmit();
$httpBackend.flush();
});
it("should throw 'No changes to save' error when there are no changes to apply", () => {
let newData = {
delete: [],
create: [],
update: []
};
let hasChanges = controller.hasChanges(newData);
expect(hasChanges).toBeFalsy();
});
});
});
});