Ticket packaging refactor + front test updated

This commit is contained in:
Joan Sanchez 2018-08-17 14:53:25 +02:00
parent d0fff2135e
commit a60720d1ed
8 changed files with 66 additions and 49 deletions

View File

@ -1,11 +1,13 @@
<mg-ajax
path="/ticket/api/Tickets/{{index.params.id}}/packages"
options="mgIndex" actions="$ctrl.getPackages()">
</mg-ajax>
<vn-crud-model
vn-id="model"
url="/ticket/api/TicketPackagings"
link="{ticketFk: $ctrl.$stateParams.id}"
data="packages" on-data-change="$ctrl.getPackages()">
</vn-crud-model>
<vn-watcher
vn-id="watcher"
data="$ctrl.packages"
data="packages"
form="form">
</vn-watcher>
@ -13,28 +15,25 @@
<vn-card pad-large>
<vn-title>Packages</vn-title>
<vn-one>
<vn-horizontal ng-repeat="package in index.model track by $index">
<vn-horizontal ng-repeat="package in packages track by $index">
<vn-autocomplete vn-one
margin-large-right
vn-focus
url="/ticket/api/Packagings/listPackaging"
label="Package"
show-field="name"
value-field="packagingFk"
where="{or: [{id: {like: '%search%'}}, {name: {like: '%search%'}}]}"
where="{or: [{'itemFk': {like: '%search%'}}, {'name': {like: '%search%'}}]}"
field="package.packagingFk">
<tpl-item>{{id}} : {{name}}</tpl-item>
<tpl-item>{{itemFk}} : {{name}}</tpl-item>
</vn-autocomplete>
<vn-textfield
vn-one
margin-large-right
label="Quantity"
model="package.quantity"
rule="TicketPackaging.quantity">
</vn-textfield>
<vn-textfield
vn-one
margin-large-right
label="Added"
model="package.created | date: 'dd/MM/yyyy'"
disabled="true"

View File

@ -1,16 +1,17 @@
import ngModule from '../module';
class Controller {
constructor($http, $scope, $translate, vnApp) {
constructor($http, $scope, $stateParams, $translate, vnApp) {
this.$http = $http;
this.$ = $scope;
this.$scope = $scope;
this.$stateParams = $stateParams;
this.$translate = $translate;
this.vnApp = vnApp;
this.removedPackages = [];
}
submit() {
let query = `/ticket/api/TicketPackagings/crudTicketPackaging`;
let query = `/ticket/api/TicketPackagings/crud`;
let packagesObj = {
delete: this.removedPackages,
create: [],
@ -25,14 +26,11 @@ class Controller {
packagesObj.update.push(item);
});
if (this.$.form.$invalid)
return this.vnApp.showError(this.$translate.instant('Some fields are invalid'));
if (!this.hasChanges(packagesObj))
return this.vnApp.showError(this.$translate.instant('No changes to save'));
this.$scope.watcher.check();
this.$http.post(query, packagesObj).then(res => {
this.$.index.accept();
this.$scope.watcher.notifySaved();
this.$scope.model.refresh();
});
}
@ -54,14 +52,11 @@ class Controller {
}
getPackages() {
this.packages = this.$.index.model;
this.packages = this.$scope.model.data;
this.setOldPackages();
}
setOldPackages() {
if (this.oldPackages && !this.$.watcher.dataChanged())
return;
this.oldPackages = [];
this.removedPackages = [];
this.packages.forEach(item => {
@ -72,16 +67,9 @@ class Controller {
packageEquals(newPackage, oldPackage) {
return newPackage.packagingFk === oldPackage.packagingFk && newPackage.quantity == oldPackage.quantity;
}
hasChanges(packagesObj) {
if (packagesObj.create.length || packagesObj.update.length || packagesObj.delete.length)
return true;
return false;
}
}
Controller.$inject = ['$http', '$scope', '$translate', 'vnApp'];
Controller.$inject = ['$http', '$scope', '$stateParams', '$translate', 'vnApp'];
ngModule.component('vnTicketPackageIndex', {
template: require('./index.html'),

View File

@ -16,10 +16,13 @@ describe('Ticket', () => {
$httpBackend = _$httpBackend_;
$httpBackend.when('GET', /\/locale\/\w+\/[a-z]{2}\.json/).respond({});
$scope = {
index: {
accept: function() {}
model: {
refresh: () => {}
},
form: {}
watcher: {
check: () => {},
notifySaved: () => {}
}
};
controller = $componentController('vnTicketPackageIndex', {$scope: $scope});
}));
@ -35,8 +38,9 @@ describe('Ticket', () => {
describe('submit()', () => {
it('should perform a post', () => {
spyOn(controller.$.index, 'accept');
let query = '/ticket/api/TicketPackagings/crudTicketPackaging';
spyOn(controller.$scope.watcher, 'notifySaved');
spyOn(controller.$scope.model, 'refresh');
controller.removedPackages = [];
controller.oldPackages = [];
controller.oldPackages[1] = {id: 1, packagingFk: 1, quantity: 5, ticketFk: 1};
@ -45,9 +49,39 @@ describe('Ticket', () => {
{id: 1, packagingFk: 1, quantity: 25, ticketFk: 1}
];
$httpBackend.expectPOST(query, {delete: [], create: [{quantity: 5, packagingFk: 2, ticketFk: 1}], update: [{id: 1, packagingFk: 1, quantity: 25, ticketFk: 1}]}).respond('ok');
let data = {
delete: [],
create: [{quantity: 5, packagingFk: 2, ticketFk: 1}],
update: [{id: 1, packagingFk: 1, quantity: 25, ticketFk: 1}]
};
$httpBackend.when('POST', '/ticket/api/TicketPackagings/crud', data).respond(200);
$httpBackend.expect('POST', '/ticket/api/TicketPackagings/crud', data).respond('ok');
controller.submit();
$httpBackend.flush();
expect(controller.$scope.model.refresh).toHaveBeenCalledWith();
expect(controller.$scope.watcher.notifySaved).toHaveBeenCalledWith();
});
});
describe('packageEquals()', () => {
it('should return true if the old package and the new one matches', () => {
let oldPackage = {quantity: 5, packagingFk: 2};
let newPackage = {quantity: 5, packagingFk: 2};
let result = controller.packageEquals(oldPackage, newPackage);
expect(result).toBeTruthy();
});
it(`should return false if the old package and the new one doesn't match`, () => {
let oldPackage = {quantity: 5, packagingFk: 2};
let newPackage = {quantity: 6, packagingFk: 2};
let result = controller.packageEquals(oldPackage, newPackage);
expect(result).toBeFalsy();
});
});
});

View File

@ -11,7 +11,6 @@
"Is invalid": "Is invalid",
"Quantity cannot be zero": "Quantity cannot be zero",
"Enter an integer different to zero": "Enter an integer different to zero",
"Package cannot be blank": "Package cannot be blank",
"The company name must be unique": "The company name must be unique",
"Invalid email": "Invalid email",
"The IBAN does not have the correct format": "The IBAN does not have the correct format",

View File

@ -11,7 +11,7 @@
"Is invalid": "Is invalid",
"Quantity cannot be zero": "La cantidad no puede ser cero",
"Enter an integer different to zero": "Introduce un entero distinto de cero",
"Package cannot be blank": "Embalaje no puede estar en blanco",
"The package cannot be blank": "El embalaje no puede estar en blanco",
"The company name must be unique": "La razón social debe ser única",
"Invalid email": "Correo electrónico inválido",
"The IBAN does not have the correct format": "El IBAN no tiene el formato correcto",

View File

@ -1,3 +0,0 @@
module.exports = Self => {
Self.installCrudModel('crudTicketPackaging');
};

View File

@ -24,11 +24,13 @@ module.exports = Self => {
Self.listPackaging = async filter => {
let stmt = new ParameterizedSQL(
`SELECT i.name, i.id, p.id packagingFk
FROM item i JOIN packaging p ON i.id = p.itemFk`
`SELECT name, itemFk, packagingFk
FROM (SELECT i.name, i.id itemFk, p.id packagingFk
FROM item i
JOIN packaging p ON i.id = p.itemFk) p`
);
stmt.merge(Self.buildSuffix(filter, 'i'));
stmt.merge(Self.buildSuffix(filter));
return Self.rawStmt(stmt);
};
};

View File

@ -1,6 +1,4 @@
module.exports = function(Self) {
require('../methods/packaging/crudTicketPackaging')(Self);
Self.validateBinded('quantity', validateQuantity, {
message: 'Enter an integer different to zero',
allowNull: false,
@ -11,5 +9,5 @@ module.exports = function(Self) {
return !isNaN(quantity) && quantity != 0;
}
Self.validatesPresenceOf('packagingFk', {message: 'Package cannot be blank'});
Self.validatesPresenceOf('packagingFk', {message: 'The package cannot be blank'});
};