diff --git a/front/core/lib/component.js b/front/core/lib/component.js index b9f04dba6..4552dfbe7 100644 --- a/front/core/lib/component.js +++ b/front/core/lib/component.js @@ -108,7 +108,10 @@ function runFn( $filter, $interpolate, $window, - vnApp) { + vnApp, + vnToken, + vnConfig, + aclService) { Object.assign(Component.prototype, { $translate, $q, @@ -121,7 +124,10 @@ function runFn( $filter, $interpolate, $window, - vnApp + vnApp, + vnToken, + vnConfig, + aclService }); } runFn.$inject = [ @@ -136,7 +142,10 @@ runFn.$inject = [ '$filter', '$interpolate', '$window', - 'vnApp' + 'vnApp', + 'vnToken', + 'vnConfig', + 'aclService' ]; ngModule.run(runFn); diff --git a/modules/claim/front/action/index.js b/modules/claim/front/action/index.js index 7a04f95d5..dc22cb4be 100644 --- a/modules/claim/front/action/index.js +++ b/modules/claim/front/action/index.js @@ -155,7 +155,6 @@ class Controller { }); } - onUpdateGreugeResponse(response) { if (response == 'accept') { const promises = []; diff --git a/modules/claim/front/descriptor/index.js b/modules/claim/front/descriptor/index.js index 7bc9c831a..fd00368b8 100644 --- a/modules/claim/front/descriptor/index.js +++ b/modules/claim/front/descriptor/index.js @@ -1,14 +1,11 @@ import ngModule from '../module'; +import Component from 'core/lib/component'; -class Controller { - constructor($scope, $state, $http, $translate, vnApp, aclService, $httpParamSerializer) { - this.$scope = $scope; - this.$state = $state; - this.$http = $http; - this.$translate = $translate; - this.vnApp = vnApp; - this.aclService = aclService; +class Controller extends Component { + constructor($element, $scope, $httpParamSerializer) { + super($element, $scope); this.$httpParamSerializer = $httpParamSerializer; + this.moreOptions = [ {callback: this.showPickupOrder, name: 'Show Pickup order'}, {callback: this.confirmPickupOrder, name: 'Send Pickup order'}, @@ -22,7 +19,7 @@ class Controller { return !hasAclProperty || (hasAclProperty && this.aclService.hasAny([option.acl])); }); - this.$scope.moreButton.data = options; + this.$.moreButton.data = options; } onMoreChange(callback) { @@ -63,7 +60,8 @@ class Controller { showPickupOrder() { const params = { clientId: this.claim.clientFk, - claimId: this.claim.id + claimId: this.claim.id, + authorization: this.vnToken.token }; const serializedParams = this.$httpParamSerializer(params); let url = `api/report/claim-pickup-order?${serializedParams}`; @@ -71,7 +69,7 @@ class Controller { } confirmPickupOrder() { - this.$scope.confirmPickupOrder.show(); + this.$.confirmPickupOrder.show(); } sendPickupOrder(response) { @@ -81,16 +79,14 @@ class Controller { clientId: this.claim.clientFk, claimId: this.claim.id }; - const serializedParams = this.$httpParamSerializer(params); - const url = `email/claim-pickup-order?${serializedParams}`; - this.$http.get(url).then( + this.$http.get(`email/claim-pickup-order`, {params}).then( () => this.vnApp.showMessage(this.$translate.instant('Notification sent!')) ); } } confirmDeleteClaim() { - this.$scope.confirmDeleteClaim.show(); + this.$.confirmDeleteClaim.show(); } 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', { template: require('./index.html'), diff --git a/modules/claim/front/descriptor/index.spec.js b/modules/claim/front/descriptor/index.spec.js index a64d5ecc1..4a78bda5a 100644 --- a/modules/claim/front/descriptor/index.spec.js +++ b/modules/claim/front/descriptor/index.spec.js @@ -3,14 +3,19 @@ import './index.js'; describe('Item Component vnClaimDescriptor', () => { let $httpParamSerializer; let $httpBackend; + let $element; + let $scope; let controller; beforeEach(ngModule('claim')); - beforeEach(angular.mock.inject(($componentController, _$httpBackend_, _$httpParamSerializer_) => { + beforeEach(angular.mock.inject(($componentController, $rootScope, _$httpBackend_, _$httpParamSerializer_) => { $httpBackend = _$httpBackend_; $httpParamSerializer = _$httpParamSerializer_; - controller = $componentController('vnClaimDescriptor'); + $scope = $rootScope.$new(); + + $element = angular.element(''); + controller = $componentController('vnClaimDescriptor', {$element, $scope}); controller.claim = {id: 2, clientFk: 101, client: {email: 'client@email'}}; })); @@ -31,13 +36,13 @@ describe('Item Component vnClaimDescriptor', () => { describe('confirmPickupOrder()', () => { it('should call confirmPickupOrder.show()', () => { - controller.$scope.confirmPickupOrder = { + controller.$.confirmPickupOrder = { show: jasmine.createSpy('show') }; controller.claim = {id: 2}; controller.confirmPickupOrder(); - expect(controller.$scope.confirmPickupOrder.show).toHaveBeenCalledWith(); + expect(controller.$.confirmPickupOrder.show).toHaveBeenCalledWith(); }); }); @@ -63,13 +68,13 @@ describe('Item Component vnClaimDescriptor', () => { describe('confirmDeleteClaim()', () => { it('should call confirmDeleteClaim.show()', () => { - controller.$scope.confirmDeleteClaim = { + controller.$.confirmDeleteClaim = { show: jasmine.createSpy('show') }; controller.claim = {id: 2}; controller.confirmDeleteClaim(); - expect(controller.$scope.confirmDeleteClaim.show).toHaveBeenCalledWith(); + expect(controller.$.confirmDeleteClaim.show).toHaveBeenCalledWith(); }); }); diff --git a/modules/client/back/models/client.js b/modules/client/back/models/client.js index 367e0f0eb..417370e9e 100644 --- a/modules/client/back/models/client.js +++ b/modules/client/back/models/client.js @@ -191,7 +191,6 @@ module.exports = Self => { if (socialNameChanged && !isAlpha(changes.socialName)) throw new UserError('The socialName has an invalid format'); - if (changes.salesPerson === null) { changes.credit = 0; changes.discount = 0; @@ -238,7 +237,9 @@ module.exports = Self => { const httpCtx = {req: loopBackContext.active}; const httpRequest = httpCtx.req.http.req; const $t = httpRequest.__; - const origin = httpRequest.headers.origin; + const headers = httpRequest.headers; + const origin = headers.origin; + const authorization = headers.authorization; const salesPersonId = instance.salesPersonFk; @@ -254,12 +255,14 @@ module.exports = Self => { // Send email to client if (!instance.email) return; - const serializedParams = httpParamSerializer({ + const params = { + authorization: authorization, clientId: instance.id, 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); } }); diff --git a/modules/client/front/descriptor/index.js b/modules/client/front/descriptor/index.js index 95754b4df..4cf35d1e9 100644 --- a/modules/client/front/descriptor/index.js +++ b/modules/client/front/descriptor/index.js @@ -5,6 +5,7 @@ class Controller extends Component { constructor($element, $, $httpParamSerializer) { super($element, $); this.$httpParamSerializer = $httpParamSerializer; + this.moreOptions = [ {name: 'Simple ticket', callback: this.newTicket}, {name: 'Send SMS', callback: this.showSMSDialog}, @@ -72,8 +73,13 @@ class Controller extends Component { sendConsumerReport(response) { if (response === 'accept') { - const data = {from: this.from, to: this.to, clientId: this.client.id}; - const serializedParams = this.$httpParamSerializer(data); + const params = { + 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}`; window.open(url); } diff --git a/modules/entry/front/descriptor/index.js b/modules/entry/front/descriptor/index.js index 8f51308f2..7b004754c 100644 --- a/modules/entry/front/descriptor/index.js +++ b/modules/entry/front/descriptor/index.js @@ -2,10 +2,10 @@ import ngModule from '../module'; import Component from 'core/lib/component'; class Controller extends Component { - constructor($element, $, $httpParamSerializer, vnConfig) { + constructor($element, $, $httpParamSerializer) { super($element, $); - this.vnConfig = vnConfig; this.$httpParamSerializer = $httpParamSerializer; + this.moreOptions = [ {name: 'Show entry report', callback: this.showEntryReport} ]; @@ -59,6 +59,7 @@ class Controller extends Component { showEntryReport() { const params = { + authorization: this.vnToken.token, clientId: this.vnConfig.storage.currentUserWorkerId, 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', { template: require('./index.html'), diff --git a/modules/route/front/descriptor/index.js b/modules/route/front/descriptor/index.js index 1beb7e095..e28654b99 100644 --- a/modules/route/front/descriptor/index.js +++ b/modules/route/front/descriptor/index.js @@ -1,12 +1,10 @@ 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.moreOptions = [ {callback: this.showRouteReport, name: 'Show route report'}, @@ -39,6 +37,7 @@ class Controller { showRouteReport() { const user = this.route.worker.user; const params = { + authorization: this.vnToken.token, clientId: user.id, routeId: this.route.id }; @@ -54,9 +53,7 @@ class Controller { clientId: user.id, routeId: this.route.id }; - const serializedParams = this.$httpParamSerializer(params); - const url = `email/driver-route?${serializedParams}`; - this.$http.get(url).then(() => { + this.$http.get(`email/driver-route`, {params}).then(() => { 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', { template: require('./index.html'), diff --git a/modules/ticket/front/descriptor/index.js b/modules/ticket/front/descriptor/index.js index d804a2eb2..c633dd3c1 100644 --- a/modules/ticket/front/descriptor/index.js +++ b/modules/ticket/front/descriptor/index.js @@ -2,10 +2,10 @@ import ngModule from '../module'; import Component from 'core/lib/component'; class Controller extends Component { - constructor($element, $, aclService, $httpParamSerializer) { + constructor($element, $, $httpParamSerializer) { super($element, $); - this.aclService = aclService; this.$httpParamSerializer = $httpParamSerializer; + this.moreOptions = [ { name: 'Add turn', @@ -220,7 +220,8 @@ class Controller extends Component { showDeliveryNote() { const params = { clientId: this.ticket.client.id, - ticketId: this.ticket.id + ticketId: this.ticket.id, + authorization: this.vnToken.token }; const serializedParams = this.$httpParamSerializer(params); 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', { template: require('./index.html'), diff --git a/modules/worker/front/time-control/index.spec.js b/modules/worker/front/time-control/index.spec.js index 7987c6a50..519446864 100644 --- a/modules/worker/front/time-control/index.spec.js +++ b/modules/worker/front/time-control/index.spec.js @@ -1,6 +1,5 @@ import './index.js'; - describe('Component vnWorkerTimeControl', () => { let $httpBackend; let $scope; @@ -9,7 +8,7 @@ describe('Component vnWorkerTimeControl', () => { beforeEach(ngModule('worker')); - beforeEach(angular.mock.inject(($componentController, $compile, $rootScope, $stateParams, _$httpBackend_) => { + beforeEach(angular.mock.inject(($componentController, $rootScope, $stateParams, _$httpBackend_) => { $stateParams.id = 1; $httpBackend = _$httpBackend_; $scope = $rootScope.$new(); diff --git a/print/boot.js b/print/boot.js index ae604db2c..02bb27817 100644 --- a/print/boot.js +++ b/print/boot.js @@ -52,5 +52,3 @@ module.exports = app => { }); }); }; - - diff --git a/print/core/email.js b/print/core/email.js index 25d44df65..faf744e77 100644 --- a/print/core/email.js +++ b/print/core/email.js @@ -19,7 +19,6 @@ class Email extends Component { return `../templates/email/${this.name}`; } - async getSubject() { if (!this.lang) await this.getLang(); const locale = this.locale.messages; diff --git a/print/core/filters/index.js b/print/core/filters/index.js index 1d2eb182c..bfd99c504 100644 --- a/print/core/filters/index.js +++ b/print/core/filters/index.js @@ -3,3 +3,5 @@ require('./date'); require('./uppercase'); require('./currency'); require('./percentage'); +require('./number'); + diff --git a/print/core/filters/number.js b/print/core/filters/number.js new file mode 100644 index 000000000..c785706fe --- /dev/null +++ b/print/core/filters/number.js @@ -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)); +}); diff --git a/print/core/router.js b/print/core/router.js index f015ac03b..3be6cdd35 100644 --- a/print/core/router.js +++ b/print/core/router.js @@ -1,6 +1,62 @@ +const path = require('path'); +const fs = require('fs'); +const db = require('./database'); + module.exports = app => { - // Import methods - require('../methods/closure')(app); - require('../methods/report')(app); - require('../methods/email')(app); + const methodsPath = path.resolve(__dirname, '../methods'); + const methodsDir = fs.readdirSync(methodsPath); + const methods = []; + + // 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); }; diff --git a/print/templates/email/letter-debtor-nd/letter-debtor-nd.js b/print/templates/email/letter-debtor-nd/letter-debtor-nd.js index 7d7cc84ef..b2809ac28 100755 --- a/print/templates/email/letter-debtor-nd/letter-debtor-nd.js +++ b/print/templates/email/letter-debtor-nd/letter-debtor-nd.js @@ -37,6 +37,9 @@ module.exports = { 'attachment': attachment.build() }, props: { + authorization: { + required: true + }, clientId: { required: true }, diff --git a/print/templates/email/letter-debtor-st/letter-debtor-st.js b/print/templates/email/letter-debtor-st/letter-debtor-st.js index c34a9320a..61f3c01ad 100755 --- a/print/templates/email/letter-debtor-st/letter-debtor-st.js +++ b/print/templates/email/letter-debtor-st/letter-debtor-st.js @@ -37,11 +37,14 @@ module.exports = { 'attachment': attachment.build() }, props: { + authorization: { + required: true + }, clientId: { required: true }, companyId: { required: true - } + }, } }; diff --git a/print/templates/reports/claim-pickup-order/claim-pickup-order.html b/print/templates/reports/claim-pickup-order/claim-pickup-order.html index c5e2d05f3..f23ee3401 100644 --- a/print/templates/reports/claim-pickup-order/claim-pickup-order.html +++ b/print/templates/reports/claim-pickup-order/claim-pickup-order.html @@ -61,7 +61,7 @@ {{$t('reference')}} {{$t('quantity')}} {{$t('claims')}} - {{$t('concept')}} + {{$t('concept')}} @@ -69,7 +69,7 @@ {{sale.id}} {{sale.quantity}} {{sale.claimQuantity}} - {{sale.concept}} + {{sale.concept}} diff --git a/print/templates/reports/entry-order/entry-order.html b/print/templates/reports/entry-order/entry-order.html index 094936fd0..5ee45787c 100644 --- a/print/templates/reports/entry-order/entry-order.html +++ b/print/templates/reports/entry-order/entry-order.html @@ -63,12 +63,12 @@ - + - - - + + + @@ -76,9 +76,9 @@ - - - + + +
{{$t('boxes')}}{{$t('boxes')}} {{$t('packing')}} {{$t('concept')}}{{$t('quantity')}}{{$t('price')}}{{$t('amount')}}{{$t('quantity')}}{{$t('price')}}{{$t('amount')}}
{{buy.box}} {{buy.packing}} {{buy.itemName}}{{buy.quantity}}{{buy.buyingValue | currency('EUR', locale)}}{{buy.buyingValue * buy.quantity | currency('EUR', locale)}}{{buy.quantity | number}}{{buy.buyingValue | currency('EUR', locale)}}{{buy.buyingValue * buy.quantity | currency('EUR', locale)}}