Merge pull request '2239 - Added services' (#310) from 2239-report_service_refactor into dev
gitea/salix/pipeline/head This commit looks good Details

Reviewed-by: Bernat Exposito <bernat@verdnatura.es>
This commit is contained in:
Bernat Exposito 2020-06-15 06:14:51 +00:00
commit db1bceb5f4
25 changed files with 203 additions and 95 deletions

View File

@ -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);

View File

@ -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);

View File

@ -7,3 +7,6 @@ import './modules';
import './interceptor';
import './config';
import './week-days';
import './report';
import './email';
import './file';

View File

@ -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);

View File

@ -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: {

View File

@ -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

View File

@ -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);
});
});

View File

@ -13,8 +13,8 @@
</section>
<section class="photo" ng-repeat="photo in $ctrl.photos">
<section class="image vn-shadow" on-error-src
ng-style="{'background': 'url(/api/dms/' + photo.dmsFk + '/downloadFile?access_token=' + $ctrl.vnToken.token + ')'}"
zoom-image="/api/dms/{{::photo.dmsFk}}/downloadFile?access_token={{::$ctrl.vnToken.token}}">
ng-style="{'background': 'url(' + $ctrl.getImagePath(photo.dmsFk) + ')'}"
zoom-image="{{$ctrl.getImagePath(photo.dmsFk)}}">
</section>
<section class="actions">
<vn-button

View File

@ -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;
}
deleteDms(index) {
const dmsFk = this.photos[index].dmsFk;
return this.$http.post(`ClaimDms/${dmsFk}/removeFile`)
@ -80,8 +85,14 @@ class Controller extends Section {
this.$.model.refresh();
});
}
getImagePath(dmsId) {
return this.vnFile.getPath(`/api/dms/${dmsId}/downloadFile`);
}
}
Controller.$inject = ['$element', '$scope', 'vnFile'];
ngModule.component('vnClaimPhotos', {
template: require('./index.html'),
controller: Controller,

View File

@ -90,8 +90,8 @@
<vn-horizontal class="photo-list">
<section class="photo" ng-repeat="photo in photos">
<section class="image" on-error-src
ng-style="{'background': 'url(/api/dms/' + photo.dmsFk + '/downloadFile?access_token=' + $ctrl.vnToken.token + ')'}"
zoom-image="/api/dms/{{::photo.dmsFk}}/downloadFile?access_token={{::$ctrl.vnToken.token}}">
ng-style="{'background': 'url(' + $ctrl.getImagePath(photo.dmsFk) + ')'}"
zoom-image="{{$ctrl.getImagePath(photo.dmsFk)}}">
</section>
</section>
</vn-horizontal>

View File

@ -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,

View File

@ -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,

View File

@ -54,11 +54,10 @@
</span>
</vn-td>
<vn-td shrink>
<a target="_blank"
title="{{'Download file' | translate}}"
href="api/dms/{{::document.dmsFk}}/downloadFile?access_token={{::$ctrl.vnToken.token}}">
<span title="{{'Download file' | translate}}" class="link"
ng-click="$ctrl.downloadFile(document.dmsFk)">
{{::document.dms.file}}
</a>
</span>
</vn-td>
<vn-td shrink>
<span class="link"
@ -69,13 +68,10 @@
{{::document.dms.created | date:'dd/MM/yyyy HH:mm'}}
</vn-td>
<vn-td shrink>
<a target="_blank"
href="api/dms/{{::document.dmsFk}}/downloadFile?access_token={{::$ctrl.vnToken.token}}">
<vn-icon-button
icon="cloud_download"
title="{{'Download file' | translate}}">
</vn-icon-button>
</a>
<vn-icon-button title="{{'Download file' | translate}}"
icon="cloud_download"
ng-click="$ctrl.downloadFile(document.dmsFk)">
</vn-icon-button>
</vn-td>
<vn-td shrink>
<vn-icon-button ui-sref="client.card.dms.edit({dmsId: {{::document.dmsFk}}})"

View File

@ -3,8 +3,9 @@ import Section from 'salix/components/section';
import './style.scss';
class Controller extends Section {
constructor($element, $) {
super($element, $);
constructor($element, $, vnFile) {
super($element, $, vnFile);
this.vnFile = vnFile;
this.filter = {
include: {
relation: 'dms',
@ -49,9 +50,13 @@ class Controller extends Section {
this.vnApp.showSuccess(this.$t('Data saved!'));
});
}
downloadFile(dmsId) {
this.vnFile.download(`api/dms/${dmsId}/downloadFile`);
}
}
Controller.$inject = ['$element', '$scope'];
Controller.$inject = ['$element', '$scope', 'vnFile'];
ngModule.component('vnClientDmsIndex', {
template: require('./index.html'),

View File

@ -36,7 +36,7 @@ class Controller extends Descriptor {
}
showEntryReport() {
this.showReport('entry-order', {
this.vnReport.show('entry-order', {
entryId: this.entry.id
});
}

View File

@ -12,15 +12,16 @@ describe('Entry Component vnEntryDescriptor', () => {
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);
});
});
});

View File

@ -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
});

View File

@ -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

View File

@ -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);
});
});

View File

@ -52,11 +52,10 @@
</span>
</vn-td>
<vn-td shrink>
<a target="_blank"
title="{{'Download file' | translate}}"
href="api/dms/{{::document.dmsFk}}/downloadFile?access_token={{::$ctrl.vnToken.token}}">
<span title="{{'Download file' | translate}}" class="link"
ng-click="$ctrl.downloadFile(document.dmsFk)">
{{::document.dms.file}}
</a>
</span>
</vn-td>
<vn-td shrink>
<span class="link"
@ -67,13 +66,10 @@
{{::document.dms.created | date:'dd/MM/yyyy HH:mm'}}
</vn-td>
<vn-td shrink>
<a target="_blank"
href="api/dms/{{::document.dmsFk}}/downloadFile?access_token={{::$ctrl.vnToken.token}}">
<vn-icon-button
icon="cloud_download"
title="{{'Download file' | translate}}">
</vn-icon-button>
</a>
<vn-icon-button title="{{'Download file' | translate}}"
icon="cloud_download"
ng-click="$ctrl.downloadFile(document.dmsFk)">
</vn-icon-button>
</vn-td>
<vn-td shrink>
<vn-icon-button icon="edit"

View File

@ -3,8 +3,9 @@ import Section from 'salix/components/section';
import './style.scss';
class Controller extends Section {
constructor($element, $) {
constructor($element, $, vnFile) {
super($element, $);
this.vnFile = vnFile;
this.filter = {
include: {
relation: 'dms',
@ -50,8 +51,14 @@ class Controller extends Section {
this.vnApp.showSuccess(this.$t('Data saved!'));
});
}
downloadFile(dmsId) {
this.vnFile.download(`api/dms/${dmsId}/downloadFile`);
}
}
Controller.$inject = ['$element', '$scope', 'vnFile'];
ngModule.component('vnTicketDmsIndex', {
template: require('./index.html'),
controller: Controller,

View File

@ -29,13 +29,10 @@
<vn-td>{{::thermograph.warehouse.name}}</vn-td>
<vn-td>{{::thermograph.created | date: 'dd/MM/yyyy'}}</vn-td>
<vn-td shrink>
<a target="_blank"
href="api/dms/{{::thermograph.dmsFk}}/downloadFile?access_token={{::$ctrl.vnToken.token}}">
<vn-icon-button
icon="cloud_download"
title="{{'Download file' | translate}}">
</vn-icon-button>
</a>
<vn-icon-button title="{{'Download file' | translate}}"
icon="cloud_download"
ng-click="$ctrl.downloadFile(thermograph.dmsFk)">
</vn-icon-button>
</vn-td>
<vn-td shrink>
<vn-icon-button ui-sref="travel.card.thermograph.edit({thermographId: {{::thermograph.id}}})"

View File

@ -3,7 +3,9 @@ import Section from 'salix/components/section';
import './style.scss';
class Controller extends Section {
$onInit() {
constructor($element, $, vnFile) {
super($element, $);
this.vnFile = vnFile;
this.filter = {
include:
{relation: 'warehouse',
@ -29,8 +31,14 @@ class Controller extends Section {
this.thermographIndex = null;
});
}
downloadFile(dmsId) {
this.vnFile.download(`api/dms/${dmsId}/downloadFile`);
}
}
Controller.$inject = ['$element', '$scope', 'vnFile'];
ngModule.component('vnTravelThermographIndex', {
template: require('./index.html'),
controller: Controller,

View File

@ -38,22 +38,19 @@
</span>
</vn-td >
<vn-td shrink>
<a target="_blank"
title="{{'Download file' | translate}}"
href="api/workerDms/{{::document.dmsFk}}/downloadFile?access_token={{::$ctrl.vnToken.token}}">{{::document.file}}
</a>
<span title="{{'Download file' | translate}}" class="link"
ng-click="$ctrl.downloadFile(document.dmsFk)">
{{::document.file}}
</span>
</vn-td>
<vn-td>
{{::document.created | date:'dd/MM/yyyy HH:mm'}}
</vn-td>
<vn-td shrink>
<a target="_blank"
href="api/workerDms/{{::document.dmsFk}}/downloadFile?access_token={{::$ctrl.vnToken.token}}">
<vn-icon-button
icon="cloud_download"
title="{{'Download file' | translate}}">
</vn-icon-button>
</a>
<vn-icon-button title="{{'Download file' | translate}}"
icon="cloud_download"
ng-click="$ctrl.downloadFile(document.dmsFk)">
</vn-icon-button>
</vn-td>
<vn-td shrink>
<vn-icon-button ui-sref="worker.card.edit({dmsId: {{::document.dmsFk}}})"

View File

@ -3,8 +3,9 @@ import Component from 'core/lib/component';
import './style.scss';
class Controller extends Component {
constructor($element, $) {
constructor($element, $, vnFile) {
super($element, $);
this.vnFile = vnFile;
this.filter = {
include: {
relation: 'dms',
@ -51,8 +52,14 @@ class Controller extends Component {
this.vnApp.showSuccess(this.$t('Data saved!'));
});
}
downloadFile(dmsId) {
this.vnFile.download(`api/workerDms/${dmsId}/downloadFile`);
}
}
Controller.$inject = ['$element', '$scope', 'vnFile'];
ngModule.component('vnWorkerDmsIndex', {
template: require('./index.html'),
controller: Controller,