Merge branch '1878-print_with_auth' of verdnatura/salix into dev
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Joan Sanchez 2020-03-06 10:34:38 +00:00 committed by Gitea
commit 97671fd4a1
19 changed files with 157 additions and 70 deletions

View File

@ -108,7 +108,10 @@ function runFn(
$filter, $filter,
$interpolate, $interpolate,
$window, $window,
vnApp) { vnApp,
vnToken,
vnConfig,
aclService) {
Object.assign(Component.prototype, { Object.assign(Component.prototype, {
$translate, $translate,
$q, $q,
@ -121,7 +124,10 @@ function runFn(
$filter, $filter,
$interpolate, $interpolate,
$window, $window,
vnApp vnApp,
vnToken,
vnConfig,
aclService
}); });
} }
runFn.$inject = [ runFn.$inject = [
@ -136,7 +142,10 @@ runFn.$inject = [
'$filter', '$filter',
'$interpolate', '$interpolate',
'$window', '$window',
'vnApp' 'vnApp',
'vnToken',
'vnConfig',
'aclService'
]; ];
ngModule.run(runFn); ngModule.run(runFn);

View File

@ -155,7 +155,6 @@ class Controller {
}); });
} }
onUpdateGreugeResponse(response) { onUpdateGreugeResponse(response) {
if (response == 'accept') { if (response == 'accept') {
const promises = []; const promises = [];

View File

@ -1,14 +1,11 @@
import ngModule from '../module'; import ngModule from '../module';
import Component from 'core/lib/component';
class Controller { class Controller extends Component {
constructor($scope, $state, $http, $translate, vnApp, aclService, $httpParamSerializer) { constructor($element, $scope, $httpParamSerializer) {
this.$scope = $scope; super($element, $scope);
this.$state = $state;
this.$http = $http;
this.$translate = $translate;
this.vnApp = vnApp;
this.aclService = aclService;
this.$httpParamSerializer = $httpParamSerializer; this.$httpParamSerializer = $httpParamSerializer;
this.moreOptions = [ this.moreOptions = [
{callback: this.showPickupOrder, name: 'Show Pickup order'}, {callback: this.showPickupOrder, name: 'Show Pickup order'},
{callback: this.confirmPickupOrder, name: 'Send Pickup order'}, {callback: this.confirmPickupOrder, name: 'Send Pickup order'},
@ -22,7 +19,7 @@ class Controller {
return !hasAclProperty || (hasAclProperty && this.aclService.hasAny([option.acl])); return !hasAclProperty || (hasAclProperty && this.aclService.hasAny([option.acl]));
}); });
this.$scope.moreButton.data = options; this.$.moreButton.data = options;
} }
onMoreChange(callback) { onMoreChange(callback) {
@ -63,7 +60,8 @@ class Controller {
showPickupOrder() { showPickupOrder() {
const params = { const params = {
clientId: this.claim.clientFk, clientId: this.claim.clientFk,
claimId: this.claim.id claimId: this.claim.id,
authorization: this.vnToken.token
}; };
const serializedParams = this.$httpParamSerializer(params); const serializedParams = this.$httpParamSerializer(params);
let url = `api/report/claim-pickup-order?${serializedParams}`; let url = `api/report/claim-pickup-order?${serializedParams}`;
@ -71,7 +69,7 @@ class Controller {
} }
confirmPickupOrder() { confirmPickupOrder() {
this.$scope.confirmPickupOrder.show(); this.$.confirmPickupOrder.show();
} }
sendPickupOrder(response) { sendPickupOrder(response) {
@ -81,16 +79,14 @@ class Controller {
clientId: this.claim.clientFk, clientId: this.claim.clientFk,
claimId: this.claim.id claimId: this.claim.id
}; };
const serializedParams = this.$httpParamSerializer(params); this.$http.get(`email/claim-pickup-order`, {params}).then(
const url = `email/claim-pickup-order?${serializedParams}`;
this.$http.get(url).then(
() => this.vnApp.showMessage(this.$translate.instant('Notification sent!')) () => this.vnApp.showMessage(this.$translate.instant('Notification sent!'))
); );
} }
} }
confirmDeleteClaim() { confirmDeleteClaim() {
this.$scope.confirmDeleteClaim.show(); this.$.confirmDeleteClaim.show();
} }
deleteClaim(response) { deleteClaim(response) {
@ -103,7 +99,7 @@ class Controller {
} }
} }
Controller.$inject = ['$scope', '$state', '$http', '$translate', 'vnApp', 'aclService', '$httpParamSerializer']; Controller.$inject = ['$element', '$scope', '$httpParamSerializer'];
ngModule.component('vnClaimDescriptor', { ngModule.component('vnClaimDescriptor', {
template: require('./index.html'), template: require('./index.html'),

View File

@ -3,14 +3,19 @@ import './index.js';
describe('Item Component vnClaimDescriptor', () => { describe('Item Component vnClaimDescriptor', () => {
let $httpParamSerializer; let $httpParamSerializer;
let $httpBackend; let $httpBackend;
let $element;
let $scope;
let controller; let controller;
beforeEach(ngModule('claim')); beforeEach(ngModule('claim'));
beforeEach(angular.mock.inject(($componentController, _$httpBackend_, _$httpParamSerializer_) => { beforeEach(angular.mock.inject(($componentController, $rootScope, _$httpBackend_, _$httpParamSerializer_) => {
$httpBackend = _$httpBackend_; $httpBackend = _$httpBackend_;
$httpParamSerializer = _$httpParamSerializer_; $httpParamSerializer = _$httpParamSerializer_;
controller = $componentController('vnClaimDescriptor'); $scope = $rootScope.$new();
$element = angular.element('<vn-claim-descriptor></vn-claim-descriptor>');
controller = $componentController('vnClaimDescriptor', {$element, $scope});
controller.claim = {id: 2, clientFk: 101, client: {email: 'client@email'}}; controller.claim = {id: 2, clientFk: 101, client: {email: 'client@email'}};
})); }));
@ -31,13 +36,13 @@ describe('Item Component vnClaimDescriptor', () => {
describe('confirmPickupOrder()', () => { describe('confirmPickupOrder()', () => {
it('should call confirmPickupOrder.show()', () => { it('should call confirmPickupOrder.show()', () => {
controller.$scope.confirmPickupOrder = { controller.$.confirmPickupOrder = {
show: jasmine.createSpy('show') show: jasmine.createSpy('show')
}; };
controller.claim = {id: 2}; controller.claim = {id: 2};
controller.confirmPickupOrder(); controller.confirmPickupOrder();
expect(controller.$scope.confirmPickupOrder.show).toHaveBeenCalledWith(); expect(controller.$.confirmPickupOrder.show).toHaveBeenCalledWith();
}); });
}); });
@ -63,13 +68,13 @@ describe('Item Component vnClaimDescriptor', () => {
describe('confirmDeleteClaim()', () => { describe('confirmDeleteClaim()', () => {
it('should call confirmDeleteClaim.show()', () => { it('should call confirmDeleteClaim.show()', () => {
controller.$scope.confirmDeleteClaim = { controller.$.confirmDeleteClaim = {
show: jasmine.createSpy('show') show: jasmine.createSpy('show')
}; };
controller.claim = {id: 2}; controller.claim = {id: 2};
controller.confirmDeleteClaim(); controller.confirmDeleteClaim();
expect(controller.$scope.confirmDeleteClaim.show).toHaveBeenCalledWith(); expect(controller.$.confirmDeleteClaim.show).toHaveBeenCalledWith();
}); });
}); });

View File

@ -191,7 +191,6 @@ module.exports = Self => {
if (socialNameChanged && !isAlpha(changes.socialName)) if (socialNameChanged && !isAlpha(changes.socialName))
throw new UserError('The socialName has an invalid format'); throw new UserError('The socialName has an invalid format');
if (changes.salesPerson === null) { if (changes.salesPerson === null) {
changes.credit = 0; changes.credit = 0;
changes.discount = 0; changes.discount = 0;
@ -238,7 +237,9 @@ module.exports = Self => {
const httpCtx = {req: loopBackContext.active}; const httpCtx = {req: loopBackContext.active};
const httpRequest = httpCtx.req.http.req; const httpRequest = httpCtx.req.http.req;
const $t = httpRequest.__; const $t = httpRequest.__;
const origin = httpRequest.headers.origin; const headers = httpRequest.headers;
const origin = headers.origin;
const authorization = headers.authorization;
const salesPersonId = instance.salesPersonFk; const salesPersonId = instance.salesPersonFk;
@ -254,12 +255,14 @@ module.exports = Self => {
// Send email to client // Send email to client
if (!instance.email) return; if (!instance.email) return;
const serializedParams = httpParamSerializer({ const params = {
authorization: authorization,
clientId: instance.id, clientId: instance.id,
recipient: instance.email recipient: instance.email
};
await request.get(`${origin}/api/email/payment-update`, {
qs: params
}); });
const query = `${origin}/api/email/payment-update?${serializedParams}`;
await request.get(query);
} }
}); });

View File

@ -5,6 +5,7 @@ class Controller extends Component {
constructor($element, $, $httpParamSerializer) { constructor($element, $, $httpParamSerializer) {
super($element, $); super($element, $);
this.$httpParamSerializer = $httpParamSerializer; this.$httpParamSerializer = $httpParamSerializer;
this.moreOptions = [ this.moreOptions = [
{name: 'Simple ticket', callback: this.newTicket}, {name: 'Simple ticket', callback: this.newTicket},
{name: 'Send SMS', callback: this.showSMSDialog}, {name: 'Send SMS', callback: this.showSMSDialog},
@ -72,8 +73,13 @@ class Controller extends Component {
sendConsumerReport(response) { sendConsumerReport(response) {
if (response === 'accept') { if (response === 'accept') {
const data = {from: this.from, to: this.to, clientId: this.client.id}; const params = {
const serializedParams = this.$httpParamSerializer(data); authorization: this.vnToken.token,
clientId: this.client.id,
from: this.from,
to: this.to,
};
const serializedParams = this.$httpParamSerializer(params);
const url = `api/report/campaign-metrics?${serializedParams}`; const url = `api/report/campaign-metrics?${serializedParams}`;
window.open(url); window.open(url);
} }

View File

@ -2,10 +2,10 @@ import ngModule from '../module';
import Component from 'core/lib/component'; import Component from 'core/lib/component';
class Controller extends Component { class Controller extends Component {
constructor($element, $, $httpParamSerializer, vnConfig) { constructor($element, $, $httpParamSerializer) {
super($element, $); super($element, $);
this.vnConfig = vnConfig;
this.$httpParamSerializer = $httpParamSerializer; this.$httpParamSerializer = $httpParamSerializer;
this.moreOptions = [ this.moreOptions = [
{name: 'Show entry report', callback: this.showEntryReport} {name: 'Show entry report', callback: this.showEntryReport}
]; ];
@ -59,6 +59,7 @@ class Controller extends Component {
showEntryReport() { showEntryReport() {
const params = { const params = {
authorization: this.vnToken.token,
clientId: this.vnConfig.storage.currentUserWorkerId, clientId: this.vnConfig.storage.currentUserWorkerId,
entryId: this.entry.id entryId: this.entry.id
}; };
@ -68,7 +69,7 @@ class Controller extends Component {
} }
} }
Controller.$inject = ['$element', '$scope', '$httpParamSerializer', 'vnConfig']; Controller.$inject = ['$element', '$scope', '$httpParamSerializer'];
ngModule.component('vnEntryDescriptor', { ngModule.component('vnEntryDescriptor', {
template: require('./index.html'), template: require('./index.html'),

View File

@ -1,12 +1,10 @@
import ngModule from '../module'; import ngModule from '../module';
import Component from 'core/lib/component';
class Controller extends Component {
constructor($element, $scope, $httpParamSerializer) {
super($element, $scope);
class Controller {
constructor($, $http, vnApp, $translate, aclService, $httpParamSerializer) {
this.$http = $http;
this.vnApp = vnApp;
this.$translate = $translate;
this.$ = $;
this.aclService = aclService;
this.$httpParamSerializer = $httpParamSerializer; this.$httpParamSerializer = $httpParamSerializer;
this.moreOptions = [ this.moreOptions = [
{callback: this.showRouteReport, name: 'Show route report'}, {callback: this.showRouteReport, name: 'Show route report'},
@ -39,6 +37,7 @@ class Controller {
showRouteReport() { showRouteReport() {
const user = this.route.worker.user; const user = this.route.worker.user;
const params = { const params = {
authorization: this.vnToken.token,
clientId: user.id, clientId: user.id,
routeId: this.route.id routeId: this.route.id
}; };
@ -54,9 +53,7 @@ class Controller {
clientId: user.id, clientId: user.id,
routeId: this.route.id routeId: this.route.id
}; };
const serializedParams = this.$httpParamSerializer(params); this.$http.get(`email/driver-route`, {params}).then(() => {
const url = `email/driver-route?${serializedParams}`;
this.$http.get(url).then(() => {
this.vnApp.showSuccess(this.$translate.instant('Report sent')); this.vnApp.showSuccess(this.$translate.instant('Report sent'));
}); });
} }
@ -76,7 +73,7 @@ class Controller {
} }
} }
Controller.$inject = ['$scope', '$http', 'vnApp', '$translate', 'aclService', '$httpParamSerializer']; Controller.$inject = ['$element', '$scope', '$httpParamSerializer'];
ngModule.component('vnRouteDescriptor', { ngModule.component('vnRouteDescriptor', {
template: require('./index.html'), template: require('./index.html'),

View File

@ -2,10 +2,10 @@ import ngModule from '../module';
import Component from 'core/lib/component'; import Component from 'core/lib/component';
class Controller extends Component { class Controller extends Component {
constructor($element, $, aclService, $httpParamSerializer) { constructor($element, $, $httpParamSerializer) {
super($element, $); super($element, $);
this.aclService = aclService;
this.$httpParamSerializer = $httpParamSerializer; this.$httpParamSerializer = $httpParamSerializer;
this.moreOptions = [ this.moreOptions = [
{ {
name: 'Add turn', name: 'Add turn',
@ -220,7 +220,8 @@ class Controller extends Component {
showDeliveryNote() { showDeliveryNote() {
const params = { const params = {
clientId: this.ticket.client.id, clientId: this.ticket.client.id,
ticketId: this.ticket.id ticketId: this.ticket.id,
authorization: this.vnToken.token
}; };
const serializedParams = this.$httpParamSerializer(params); const serializedParams = this.$httpParamSerializer(params);
let url = `api/report/delivery-note?${serializedParams}`; let url = `api/report/delivery-note?${serializedParams}`;
@ -332,7 +333,7 @@ class Controller extends Component {
} }
} }
Controller.$inject = ['$element', '$scope', 'aclService', '$httpParamSerializer']; Controller.$inject = ['$element', '$scope', '$httpParamSerializer'];
ngModule.component('vnTicketDescriptor', { ngModule.component('vnTicketDescriptor', {
template: require('./index.html'), template: require('./index.html'),

View File

@ -1,6 +1,5 @@
import './index.js'; import './index.js';
describe('Component vnWorkerTimeControl', () => { describe('Component vnWorkerTimeControl', () => {
let $httpBackend; let $httpBackend;
let $scope; let $scope;
@ -9,7 +8,7 @@ describe('Component vnWorkerTimeControl', () => {
beforeEach(ngModule('worker')); beforeEach(ngModule('worker'));
beforeEach(angular.mock.inject(($componentController, $compile, $rootScope, $stateParams, _$httpBackend_) => { beforeEach(angular.mock.inject(($componentController, $rootScope, $stateParams, _$httpBackend_) => {
$stateParams.id = 1; $stateParams.id = 1;
$httpBackend = _$httpBackend_; $httpBackend = _$httpBackend_;
$scope = $rootScope.$new(); $scope = $rootScope.$new();

View File

@ -52,5 +52,3 @@ module.exports = app => {
}); });
}); });
}; };

View File

@ -19,7 +19,6 @@ class Email extends Component {
return `../templates/email/${this.name}`; return `../templates/email/${this.name}`;
} }
async getSubject() { async getSubject() {
if (!this.lang) await this.getLang(); if (!this.lang) await this.getLang();
const locale = this.locale.messages; const locale = this.locale.messages;

View File

@ -3,3 +3,5 @@ require('./date');
require('./uppercase'); require('./uppercase');
require('./currency'); require('./currency');
require('./percentage'); require('./percentage');
require('./number');

View File

@ -0,0 +1,10 @@
const Vue = require('vue');
const config = require('../config');
const defaultLocale = config.i18n.locale;
Vue.filter('number', function(value, locale = defaultLocale) {
if (!locale) locale = defaultLocale;
return new Intl.NumberFormat(locale, {
style: 'decimal'
}).format(parseFloat(value));
});

View File

@ -1,6 +1,62 @@
const path = require('path');
const fs = require('fs');
const db = require('./database');
module.exports = app => { module.exports = app => {
// Import methods const methodsPath = path.resolve(__dirname, '../methods');
require('../methods/closure')(app); const methodsDir = fs.readdirSync(methodsPath);
require('../methods/report')(app); const methods = [];
require('../methods/email')(app);
// Get all methods
methodsDir.forEach(method => {
methods.push(method.replace('.js', ''));
});
// Auth middleware
const paths = [];
for (let method of methods)
paths.push(`/api/${method}/*`);
app.use(paths, async function(request, response, next) {
const authorization = getToken(request);
const query = `SELECT userId, ttl, created
FROM salix.AccessToken WHERE id = ?`;
try {
const authToken = await db.findOne(query, [authorization]);
if (!authToken || isTokenExpired(authToken.created, authToken.ttl))
throw new Error('Invalid authorization token');
next();
} catch (error) {
next(error);
}
});
function getToken(request) {
const headers = request.headers;
const params = request.query;
if (headers.authorization)
params.authorization = headers.authorization;
return headers.authorization || params.authorization;
}
function isTokenExpired(created, ttl) {
const date = new Date(created);
const currentDate = new Date();
date.setSeconds(date.getSeconds() + ttl);
if (currentDate > date)
return true;
return false;
}
// Mount methods
for (let method of methods)
require(`../methods/${method}`)(app);
}; };

View File

@ -37,6 +37,9 @@ module.exports = {
'attachment': attachment.build() 'attachment': attachment.build()
}, },
props: { props: {
authorization: {
required: true
},
clientId: { clientId: {
required: true required: true
}, },

View File

@ -37,11 +37,14 @@ module.exports = {
'attachment': attachment.build() 'attachment': attachment.build()
}, },
props: { props: {
authorization: {
required: true
},
clientId: { clientId: {
required: true required: true
}, },
companyId: { companyId: {
required: true required: true
} },
} }
}; };

View File

@ -61,7 +61,7 @@
<th>{{$t('reference')}}</th> <th>{{$t('reference')}}</th>
<th class="number">{{$t('quantity')}}</th> <th class="number">{{$t('quantity')}}</th>
<th class="number">{{$t('claims')}}</th> <th class="number">{{$t('claims')}}</th>
<th>{{$t('concept')}}</th> <th width="50%">{{$t('concept')}}</th>
</tr> </tr>
</thead> </thead>
<tbody v-for="sale in sales" v-bind:key="sale.id"> <tbody v-for="sale in sales" v-bind:key="sale.id">
@ -69,7 +69,7 @@
<td class="font gray">{{sale.id}}</td> <td class="font gray">{{sale.id}}</td>
<td class="number">{{sale.quantity}}</td> <td class="number">{{sale.quantity}}</td>
<td class="number">{{sale.claimQuantity}}</td> <td class="number">{{sale.claimQuantity}}</td>
<td>{{sale.concept}}</td> <td width="50%">{{sale.concept}}</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>

View File

@ -63,12 +63,12 @@
<table class="column-oriented"> <table class="column-oriented">
<thead> <thead>
<tr> <tr>
<td>{{$t('boxes')}}</td> <td class="number">{{$t('boxes')}}</td>
<td class="number">{{$t('packing')}}</td> <td class="number">{{$t('packing')}}</td>
<td width="50%">{{$t('concept')}}</td> <td width="50%">{{$t('concept')}}</td>
<td class="number">{{$t('quantity')}}</td> <td width="10%" class="number">{{$t('quantity')}}</td>
<td class="number">{{$t('price')}}</td> <td width="15%" class="number">{{$t('price')}}</td>
<td class="number">{{$t('amount')}}</td> <td width="15%" class="number">{{$t('amount')}}</td>
</tr> </tr>
</thead> </thead>
<tbody v-for="buy in buys"> <tbody v-for="buy in buys">
@ -76,9 +76,9 @@
<td class="number">{{buy.box}}</td> <td class="number">{{buy.box}}</td>
<td class="number">{{buy.packing}}</td> <td class="number">{{buy.packing}}</td>
<td width="50%">{{buy.itemName}}</td> <td width="50%">{{buy.itemName}}</td>
<td class="number">{{buy.quantity}}</td> <td width="10%" class="number">{{buy.quantity | number}}</td>
<td class="number">{{buy.buyingValue | currency('EUR', locale)}}</td> <td width="15%" class="number">{{buy.buyingValue | currency('EUR', locale)}}</td>
<td class="number">{{buy.buyingValue * buy.quantity | currency('EUR', locale)}}</td> <td width="15%" class="number">{{buy.buyingValue * buy.quantity | currency('EUR', locale)}}</td>
</tr> </tr>
<tr class="description"> <tr class="description">
<td colspan="2"> <td colspan="2">