Implement watcher on ticket sales #1678
gitea/salix/dev This commit looks good Details

This commit is contained in:
Joan Sanchez 2019-09-05 09:00:55 +02:00
parent 0d21519523
commit 59c6952025
3 changed files with 40 additions and 2 deletions

View File

@ -3,6 +3,10 @@
url="/api/Tickets/{{$ctrl.$stateParams.id}}/getSales" url="/api/Tickets/{{$ctrl.$stateParams.id}}/getSales"
data="$ctrl.sales"> data="$ctrl.sales">
</vn-crud-model> </vn-crud-model>
<vn-watcher
vn-id="watcher"
data="$ctrl.sales">
</vn-watcher>
<vn-vertical> <vn-vertical>
<vn-card pad-large> <vn-card pad-large>
<vn-vertical> <vn-vertical>

View File

@ -116,6 +116,18 @@ class Controller {
}); });
} }
/**
* Returns new instances
*
* @return {Array} New instances
*/
newInstances() {
if (!this.sales) return;
return this.sales.filter(sale => {
return !sale.id;
});
}
/** /**
* Returns an array of indexes * Returns an array of indexes
@ -151,6 +163,7 @@ class Controller {
return checkedLines.length; return checkedLines.length;
} }
removeCheckedLines() { removeCheckedLines() {
const sales = this.checkedLines(); const sales = this.checkedLines();
@ -159,6 +172,9 @@ class Controller {
this.sales.splice(index, 1); this.sales.splice(index, 1);
}); });
if (this.newInstances().length === 0)
this.$scope.watcher.updateOriginalData();
this.refreshTotal(); this.refreshTotal();
} }
@ -295,8 +311,8 @@ class Controller {
updatePrice() { updatePrice() {
if (this.editedPrice != this.sale.price) { if (this.editedPrice != this.sale.price) {
this.$http.post(`/api/Sales/${this.edit.id}/updatePrice`, {newPrice: this.editedPrice}).then(res => { this.$http.post(`/api/Sales/${this.edit.id}/updatePrice`, {newPrice: this.editedPrice}).then(res => {
if (res.data) this.sale.price = res.data.price;
this.sale.price = res.data.price; this.$scope.watcher.updateOriginalData();
this.vnApp.showSuccess(this.$translate.instant('Data saved!')); this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
}); });
@ -434,6 +450,8 @@ class Controller {
const data = {quantity: parseInt(sale.quantity)}; const data = {quantity: parseInt(sale.quantity)};
const query = `/api/Sales/${sale.id}/updateQuantity`; const query = `/api/Sales/${sale.id}/updateQuantity`;
this.$http.post(query, data).then(() => { this.$http.post(query, data).then(() => {
this.$scope.watcher.updateOriginalData();
this.vnApp.showSuccess(this.$translate.instant('Data saved!')); this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
}).catch(e => { }).catch(e => {
this.$scope.model.refresh(); this.$scope.model.refresh();
@ -448,6 +466,8 @@ class Controller {
const data = {concept: sale.concept}; const data = {concept: sale.concept};
const query = `/api/Sales/${sale.id}`; const query = `/api/Sales/${sale.id}`;
this.$http.patch(query, data).then(() => { this.$http.patch(query, data).then(() => {
this.$scope.watcher.updateOriginalData();
this.vnApp.showSuccess(this.$translate.instant('Data saved!')); this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
}).catch(e => { }).catch(e => {
this.$scope.model.refresh(); this.$scope.model.refresh();
@ -478,6 +498,9 @@ class Controller {
sale.price = newSale.price; sale.price = newSale.price;
sale.item = newSale.item; sale.item = newSale.item;
if (this.newInstances().length === 0)
this.$scope.watcher.updateOriginalData();
this.vnApp.showSuccess(this.$translate.instant('Data saved!')); this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
}); });
} }

View File

@ -1,4 +1,5 @@
import '../index.js'; import '../index.js';
import watcher from 'core/mocks/watcher';
describe('Ticket', () => { describe('Ticket', () => {
describe('Component vnTicketSale', () => { describe('Component vnTicketSale', () => {
@ -42,6 +43,7 @@ describe('Ticket', () => {
$scope = $rootScope.$new(); $scope = $rootScope.$new();
$scope.ticket = ticket; $scope.ticket = ticket;
$scope.watcher = watcher;
$httpBackend = _$httpBackend_; $httpBackend = _$httpBackend_;
$httpBackend.whenGET(/api\/Tickets\/1\/getSales.*/).respond(sales); $httpBackend.whenGET(/api\/Tickets\/1\/getSales.*/).respond(sales);
@ -188,6 +190,7 @@ describe('Ticket', () => {
describe('updateQuantity()', () => { describe('updateQuantity()', () => {
it('should make a POST query saving sale quantity', () => { it('should make a POST query saving sale quantity', () => {
spyOn(controller.$scope.watcher, 'updateOriginalData');
const data = {quantity: 10}; const data = {quantity: 10};
const sale = sales[0]; const sale = sales[0];
sale.quantity = 10; sale.quantity = 10;
@ -196,11 +199,14 @@ describe('Ticket', () => {
$httpBackend.expect('POST', `/api/Sales/1/updateQuantity`, data).respond(); $httpBackend.expect('POST', `/api/Sales/1/updateQuantity`, data).respond();
controller.updateQuantity(sale); controller.updateQuantity(sale);
$httpBackend.flush(); $httpBackend.flush();
expect(controller.$scope.watcher.updateOriginalData).toHaveBeenCalledWith();
}); });
}); });
describe('updateConcept()', () => { describe('updateConcept()', () => {
it('should make a POST query saving sale concept', () => { it('should make a POST query saving sale concept', () => {
spyOn(controller.$scope.watcher, 'updateOriginalData');
const data = {concept: 'My new weapon'}; const data = {concept: 'My new weapon'};
const sale = sales[0]; const sale = sales[0];
sale.concept = 'My new weapon'; sale.concept = 'My new weapon';
@ -209,11 +215,14 @@ describe('Ticket', () => {
$httpBackend.expect('PATCH', `/api/Sales/1`, data).respond(); $httpBackend.expect('PATCH', `/api/Sales/1`, data).respond();
controller.updateConcept(sale); controller.updateConcept(sale);
$httpBackend.flush(); $httpBackend.flush();
expect(controller.$scope.watcher.updateOriginalData).toHaveBeenCalledWith();
}); });
}); });
describe('addSale()', () => { describe('addSale()', () => {
it('should make a POST query adding a new sale', () => { it('should make a POST query adding a new sale', () => {
spyOn(controller.$scope.watcher, 'updateOriginalData');
const newSale = {itemFk: 4, quantity: 10}; const newSale = {itemFk: 4, quantity: 10};
const params = {itemId: 4, quantity: 10}; const params = {itemId: 4, quantity: 10};
@ -233,6 +242,8 @@ describe('Ticket', () => {
$httpBackend.expect('POST', `/api/tickets/1/addSale`, params).respond(expectedResult); $httpBackend.expect('POST', `/api/tickets/1/addSale`, params).respond(expectedResult);
controller.addSale(newSale); controller.addSale(newSale);
$httpBackend.flush(); $httpBackend.flush();
expect(controller.$scope.watcher.updateOriginalData).toHaveBeenCalledWith();
}); });
}); });