From 25b1c6bd8f6a9c301fb2ce731163aa7761c16687 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Fri, 12 Jun 2020 14:28:29 +0200 Subject: [PATCH] 2239 - Added services --- front/core/services/email.js | 24 +++++++++++++ front/core/services/file.js | 35 ++++++++++++++++++ front/core/services/index.js | 3 ++ front/core/services/report.js | 26 ++++++++++++++ front/salix/components/descriptor/index.js | 36 +++++-------------- modules/claim/front/descriptor/index.js | 4 +-- modules/claim/front/descriptor/index.spec.js | 9 ++--- modules/claim/front/photos/index.html | 4 +-- modules/claim/front/photos/index.js | 11 ++++++ modules/claim/front/summary/index.html | 4 +-- modules/claim/front/summary/index.js | 11 ++++++ modules/client/front/descriptor/index.js | 2 +- modules/client/front/dms/index/index.html | 18 ++++------ modules/client/front/dms/index/index.js | 11 ++++-- modules/entry/front/descriptor/index.js | 2 +- modules/entry/front/descriptor/index.spec.js | 5 +-- modules/route/front/descriptor/index.js | 4 +-- modules/ticket/front/descriptor/index.js | 4 +-- modules/ticket/front/descriptor/index.spec.js | 9 ++--- modules/ticket/front/dms/index/index.html | 18 ++++------ modules/ticket/front/dms/index/index.js | 9 ++++- .../travel/front/thermograph/index/index.html | 11 +++--- .../travel/front/thermograph/index/index.js | 10 +++++- modules/worker/front/dms/index/index.html | 19 +++++----- modules/worker/front/dms/index/index.js | 9 ++++- 25 files changed, 203 insertions(+), 95 deletions(-) create mode 100644 front/core/services/email.js create mode 100644 front/core/services/file.js create mode 100644 front/core/services/report.js diff --git a/front/core/services/email.js b/front/core/services/email.js new file mode 100644 index 000000000..4385eed5f --- /dev/null +++ b/front/core/services/email.js @@ -0,0 +1,24 @@ +import ngModule from '../module'; + +class Email { + constructor($http, $translate, vnApp) { + this.$http = $http; + this.vnApp = vnApp; + this.$t = $translate.instant; + } + + /** + * Sends an email displaying a notification when it's sent. + * + * @param {String} template The email report name + * @param {Object} params The email parameters + * @return {Promise} Promise resolved when it's sent + */ + send(template, params) { + return this.$http.get(`email/${template}`, {params}) + .then(() => this.vnApp.showMessage(this.$t('Notification sent!'))); + } +} +Email.$inject = ['$http', '$translate', 'vnApp']; + +ngModule.service('vnEmail', Email); diff --git a/front/core/services/file.js b/front/core/services/file.js new file mode 100644 index 000000000..25ace4470 --- /dev/null +++ b/front/core/services/file.js @@ -0,0 +1,35 @@ +import ngModule from '../module'; + +class File { + constructor($httpParamSerializer, vnToken) { + this.$httpParamSerializer = $httpParamSerializer; + this.vnToken = vnToken; + } + + /** + * Returns the full download path + * + * @param {String} dmsUrl The file download path + * @return {String} The full download path + */ + getPath(dmsUrl) { + const serializedParams = this.$httpParamSerializer({ + access_token: this.vnToken.token + }); + + return `${dmsUrl}?${serializedParams}`; + } + + /** + * Downloads a file in another window, automatically adds the authorization + * token to params. + * + * @param {String} dmsUrl The file download path + */ + download(dmsUrl) { + window.open(this.getPath(dmsUrl)); + } +} +File.$inject = ['$httpParamSerializer', 'vnToken']; + +ngModule.service('vnFile', File); diff --git a/front/core/services/index.js b/front/core/services/index.js index 4573ab5c9..ff1d438ed 100644 --- a/front/core/services/index.js +++ b/front/core/services/index.js @@ -7,3 +7,6 @@ import './modules'; import './interceptor'; import './config'; import './week-days'; +import './report'; +import './email'; +import './file'; diff --git a/front/core/services/report.js b/front/core/services/report.js new file mode 100644 index 000000000..32ccb52a3 --- /dev/null +++ b/front/core/services/report.js @@ -0,0 +1,26 @@ +import ngModule from '../module'; + +class Report { + constructor($httpParamSerializer, vnToken) { + this.$httpParamSerializer = $httpParamSerializer; + this.vnToken = vnToken; + } + + /** + * Shows a report in another window, automatically adds the authorization + * token to params. + * + * @param {String} report The report name + * @param {Object} params The report parameters + */ + show(report, params) { + params = Object.assign({ + authorization: this.vnToken.token + }, params); + const serializedParams = this.$httpParamSerializer(params); + window.open(`api/report/${report}?${serializedParams}`); + } +} +Report.$inject = ['$httpParamSerializer', 'vnToken']; + +ngModule.service('vnReport', Report); diff --git a/front/salix/components/descriptor/index.js b/front/salix/components/descriptor/index.js index 6b10b1ff9..d87a4c395 100644 --- a/front/salix/components/descriptor/index.js +++ b/front/salix/components/descriptor/index.js @@ -7,6 +7,13 @@ import './quick-link'; * Small card with basing entity information and actions. */ export default class Descriptor extends Component { + constructor($element, $, vnReport, vnEmail) { + super($element, $); + + this.vnReport = vnReport; + this.vnEmail = vnEmail; + } + $postLink() { const content = this.element.querySelector('vn-descriptor-content'); if (!content) throw new Error('Directive vnDescriptorContent not found'); @@ -74,35 +81,10 @@ export default class Descriptor extends Component { return this.$http.get(url, options) .finally(() => this.canceler = null); } - - /** - * Shows a report in another window, automatically adds the authorization - * token to params. - * - * @param {String} report The report name - * @param {Object} params The report parameters - */ - showReport(report, params) { - params = Object.assign({ - authorization: this.vnToken.token - }, params); - const serializedParams = this.$httpParamSerializer(params); - window.open(`api/report/${report}?${serializedParams}`); - } - - /** - * Sends an email displaying a notification when it's sent. - * - * @param {String} report The email report name - * @param {Object} params The email parameters - * @return {Promise} Promise resolved when it's sent - */ - sendEmail(report, params) { - return this.$http.get(`email/${report}`, {params}) - .then(() => this.vnApp.showMessage(this.$t('Notification sent!'))); - } } +Descriptor.$inject = ['$element', '$scope', 'vnReport', 'vnEmail']; + ngModule.vnComponent('vnDescriptor', { controller: Descriptor, bindings: { diff --git a/modules/claim/front/descriptor/index.js b/modules/claim/front/descriptor/index.js index 5b09b1886..674ac91e1 100644 --- a/modules/claim/front/descriptor/index.js +++ b/modules/claim/front/descriptor/index.js @@ -11,14 +11,14 @@ class Controller extends Descriptor { } showPickupOrder() { - this.showReport('claim-pickup-order', { + this.vnReport.show('claim-pickup-order', { recipientId: this.claim.clientFk, claimId: this.claim.id }); } sendPickupOrder() { - return this.sendEmail('claim-pickup-order', { + return this.vnEmail.send('claim-pickup-order', { recipient: this.claim.client.email, recipientId: this.claim.clientFk, claimId: this.claim.id diff --git a/modules/claim/front/descriptor/index.spec.js b/modules/claim/front/descriptor/index.spec.js index 7cdca1b82..bca47409c 100644 --- a/modules/claim/front/descriptor/index.spec.js +++ b/modules/claim/front/descriptor/index.spec.js @@ -20,21 +20,22 @@ describe('Item Component vnClaimDescriptor', () => { describe('showPickupOrder()', () => { it('should open a new window showing a pickup order PDF document', () => { - controller.showReport = jest.fn(); + jest.spyOn(controller.vnReport, 'show'); + window.open = jasmine.createSpy('open'); const params = { recipientId: claim.clientFk, claimId: claim.id }; controller.showPickupOrder(); - expect(controller.showReport).toHaveBeenCalledWith('claim-pickup-order', params); + expect(controller.vnReport.show).toHaveBeenCalledWith('claim-pickup-order', params); }); }); describe('sendPickupOrder()', () => { it('should make a query and call vnApp.showMessage() if the response is accept', () => { - jest.spyOn(controller, 'sendEmail'); + jest.spyOn(controller.vnEmail, 'send'); const params = { recipient: claim.client.email, @@ -43,7 +44,7 @@ describe('Item Component vnClaimDescriptor', () => { }; controller.sendPickupOrder(); - expect(controller.sendEmail).toHaveBeenCalledWith('claim-pickup-order', params); + expect(controller.vnEmail.send).toHaveBeenCalledWith('claim-pickup-order', params); }); }); diff --git a/modules/claim/front/photos/index.html b/modules/claim/front/photos/index.html index cb3f55b65..9cc6c649c 100644 --- a/modules/claim/front/photos/index.html +++ b/modules/claim/front/photos/index.html @@ -13,8 +13,8 @@
+ ng-style="{'background': 'url(' + $ctrl.getImagePath(photo.dmsFk) + ')'}" + zoom-image="{{$ctrl.getImagePath(photo.dmsFk)}}">
+ ng-style="{'background': 'url(' + $ctrl.getImagePath(photo.dmsFk) + ')'}" + zoom-image="{{$ctrl.getImagePath(photo.dmsFk)}}">
diff --git a/modules/claim/front/summary/index.js b/modules/claim/front/summary/index.js index 0ccf6dcd2..6e62252d5 100644 --- a/modules/claim/front/summary/index.js +++ b/modules/claim/front/summary/index.js @@ -3,6 +3,11 @@ import Section from 'salix/components/section'; import './style.scss'; class Controller extends Section { + constructor($element, $, vnFile) { + super($element, $); + this.vnFile = vnFile; + } + $onChanges() { if (this.claim && this.claim.id) this.getSummary(); @@ -32,8 +37,14 @@ class Controller extends Section { this.summary = response.data; }); } + + getImagePath(dmsId) { + return this.vnFile.getPath(`/api/dms/${dmsId}/downloadFile`); + } } +Controller.$inject = ['$element', '$scope', 'vnFile']; + ngModule.component('vnClaimSummary', { template: require('./index.html'), controller: Controller, diff --git a/modules/client/front/descriptor/index.js b/modules/client/front/descriptor/index.js index d079b38da..7d3f628b4 100644 --- a/modules/client/front/descriptor/index.js +++ b/modules/client/front/descriptor/index.js @@ -41,7 +41,7 @@ class Controller extends Descriptor { } onConsumerReportAccept() { - this.showReport('campaign-metrics', { + this.vnReport.show('campaign-metrics', { recipientId: this.id, from: this.from, to: this.to, diff --git a/modules/client/front/dms/index/index.html b/modules/client/front/dms/index/index.html index fd1527e23..3402074cc 100644 --- a/modules/client/front/dms/index/index.html +++ b/modules/client/front/dms/index/index.html @@ -54,11 +54,10 @@ - + {{::document.dms.file}} - + - - - - + + { describe('showEntryReport()', () => { it('should open a new window showing a delivery note PDF document', () => { - controller.showReport = jest.fn(); + jest.spyOn(controller.vnReport, 'show'); + window.open = jasmine.createSpy('open'); const params = { clientId: controller.vnConfig.storage.currentUserWorkerId, entryId: entry.id }; controller.showEntryReport(); - expect(controller.showReport).toHaveBeenCalledWith('entry-order', params); + expect(controller.vnReport.show).toHaveBeenCalledWith('entry-order', params); }); }); }); diff --git a/modules/route/front/descriptor/index.js b/modules/route/front/descriptor/index.js index c14035015..32411aa58 100644 --- a/modules/route/front/descriptor/index.js +++ b/modules/route/front/descriptor/index.js @@ -11,14 +11,14 @@ class Controller extends Descriptor { } showRouteReport() { - this.showReport('driver-route', { + this.vnReport.show('driver-route', { routeId: this.id }); } sendRouteReport() { const workerUser = this.route.worker.user; - this.sendEmail('driver-route', { + this.vnEmail.send('driver-route', { recipient: workerUser.emailUser.email, routeId: this.id }); diff --git a/modules/ticket/front/descriptor/index.js b/modules/ticket/front/descriptor/index.js index 546c5bec7..d6fe85372 100644 --- a/modules/ticket/front/descriptor/index.js +++ b/modules/ticket/front/descriptor/index.js @@ -101,14 +101,14 @@ class Controller extends Descriptor { } showDeliveryNote() { - this.showReport('delivery-note', { + this.vnReport.show('delivery-note', { recipientId: this.ticket.client.id, ticketId: this.id, }); } sendDeliveryNote() { - return this.sendEmail('delivery-note', { + return this.vnEmail.send('delivery-note', { recipientId: this.ticket.client.id, recipient: this.ticket.client.email, ticketId: this.id diff --git a/modules/ticket/front/descriptor/index.spec.js b/modules/ticket/front/descriptor/index.spec.js index f152f1f0f..41a2cceea 100644 --- a/modules/ticket/front/descriptor/index.spec.js +++ b/modules/ticket/front/descriptor/index.spec.js @@ -64,21 +64,22 @@ describe('Ticket Component vnTicketDescriptor', () => { describe('showDeliveryNote()', () => { it('should open a new window showing a delivery note PDF document', () => { - jest.spyOn(controller, 'showReport'); + jest.spyOn(controller.vnReport, 'show'); + window.open = jasmine.createSpy('open'); const params = { clientId: ticket.client.id, ticketId: ticket.id }; controller.showDeliveryNote(); - expect(controller.showReport).toHaveBeenCalledWith('delivery-note', params); + expect(controller.vnReport.show).toHaveBeenCalledWith('delivery-note', params); }); }); describe('sendDeliveryNote()', () => { it('should make a query and call vnApp.showMessage()', () => { - jest.spyOn(controller, 'sendEmail'); + jest.spyOn(controller.vnEmail, 'send'); const params = { recipient: ticket.client.email, @@ -87,7 +88,7 @@ describe('Ticket Component vnTicketDescriptor', () => { }; controller.sendDeliveryNote(); - expect(controller.sendEmail).toHaveBeenCalledWith('delivery-note', params); + expect(controller.vnEmail.send).toHaveBeenCalledWith('delivery-note', params); }); }); diff --git a/modules/ticket/front/dms/index/index.html b/modules/ticket/front/dms/index/index.html index 176eed754..80ddbf899 100644 --- a/modules/ticket/front/dms/index/index.html +++ b/modules/ticket/front/dms/index/index.html @@ -52,11 +52,10 @@ - + {{::document.dms.file}} - + - - - - + + {{::thermograph.warehouse.name}} {{::thermograph.created | date: 'dd/MM/yyyy'}} - - - - + + - {{::document.file}} - + + {{::document.file}} + {{::document.created | date:'dd/MM/yyyy HH:mm'}} - - - - + +