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"
data="$ctrl.sales">
</vn-crud-model>
<vn-watcher
vn-id="watcher"
data="$ctrl.sales">
</vn-watcher>
<vn-vertical>
<vn-card pad-large>
<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
@ -151,6 +163,7 @@ class Controller {
return checkedLines.length;
}
removeCheckedLines() {
const sales = this.checkedLines();
@ -159,6 +172,9 @@ class Controller {
this.sales.splice(index, 1);
});
if (this.newInstances().length === 0)
this.$scope.watcher.updateOriginalData();
this.refreshTotal();
}
@ -295,8 +311,8 @@ class Controller {
updatePrice() {
if (this.editedPrice != this.sale.price) {
this.$http.post(`/api/Sales/${this.edit.id}/updatePrice`, {newPrice: this.editedPrice}).then(res => {
if (res.data)
this.sale.price = res.data.price;
this.$scope.watcher.updateOriginalData();
this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
});
@ -434,6 +450,8 @@ class Controller {
const data = {quantity: parseInt(sale.quantity)};
const query = `/api/Sales/${sale.id}/updateQuantity`;
this.$http.post(query, data).then(() => {
this.$scope.watcher.updateOriginalData();
this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
}).catch(e => {
this.$scope.model.refresh();
@ -448,6 +466,8 @@ class Controller {
const data = {concept: sale.concept};
const query = `/api/Sales/${sale.id}`;
this.$http.patch(query, data).then(() => {
this.$scope.watcher.updateOriginalData();
this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
}).catch(e => {
this.$scope.model.refresh();
@ -478,6 +498,9 @@ class Controller {
sale.price = newSale.price;
sale.item = newSale.item;
if (this.newInstances().length === 0)
this.$scope.watcher.updateOriginalData();
this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
});
}

View File

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