2239 - Added services
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Joan Sanchez 2020-06-12 14:28:29 +02:00
parent a66cc4a36d
commit 25b1c6bd8f
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 './interceptor';
import './config'; import './config';
import './week-days'; 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. * Small card with basing entity information and actions.
*/ */
export default class Descriptor extends Component { export default class Descriptor extends Component {
constructor($element, $, vnReport, vnEmail) {
super($element, $);
this.vnReport = vnReport;
this.vnEmail = vnEmail;
}
$postLink() { $postLink() {
const content = this.element.querySelector('vn-descriptor-content'); const content = this.element.querySelector('vn-descriptor-content');
if (!content) throw new Error('Directive vnDescriptorContent not found'); if (!content) throw new Error('Directive vnDescriptorContent not found');
@ -74,34 +81,9 @@ export default class Descriptor extends Component {
return this.$http.get(url, options) return this.$http.get(url, options)
.finally(() => this.canceler = null); .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}`);
} }
/** Descriptor.$inject = ['$element', '$scope', 'vnReport', 'vnEmail'];
* 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!')));
}
}
ngModule.vnComponent('vnDescriptor', { ngModule.vnComponent('vnDescriptor', {
controller: Descriptor, controller: Descriptor,

View File

@ -11,14 +11,14 @@ class Controller extends Descriptor {
} }
showPickupOrder() { showPickupOrder() {
this.showReport('claim-pickup-order', { this.vnReport.show('claim-pickup-order', {
recipientId: this.claim.clientFk, recipientId: this.claim.clientFk,
claimId: this.claim.id claimId: this.claim.id
}); });
} }
sendPickupOrder() { sendPickupOrder() {
return this.sendEmail('claim-pickup-order', { return this.vnEmail.send('claim-pickup-order', {
recipient: this.claim.client.email, recipient: this.claim.client.email,
recipientId: this.claim.clientFk, recipientId: this.claim.clientFk,
claimId: this.claim.id claimId: this.claim.id

View File

@ -20,21 +20,22 @@ describe('Item Component vnClaimDescriptor', () => {
describe('showPickupOrder()', () => { describe('showPickupOrder()', () => {
it('should open a new window showing a pickup order PDF document', () => { 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 = { const params = {
recipientId: claim.clientFk, recipientId: claim.clientFk,
claimId: claim.id claimId: claim.id
}; };
controller.showPickupOrder(); controller.showPickupOrder();
expect(controller.showReport).toHaveBeenCalledWith('claim-pickup-order', params); expect(controller.vnReport.show).toHaveBeenCalledWith('claim-pickup-order', params);
}); });
}); });
describe('sendPickupOrder()', () => { describe('sendPickupOrder()', () => {
it('should make a query and call vnApp.showMessage() if the response is accept', () => { 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 = { const params = {
recipient: claim.client.email, recipient: claim.client.email,
@ -43,7 +44,7 @@ describe('Item Component vnClaimDescriptor', () => {
}; };
controller.sendPickupOrder(); 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>
<section class="photo" ng-repeat="photo in $ctrl.photos"> <section class="photo" ng-repeat="photo in $ctrl.photos">
<section class="image vn-shadow" on-error-src <section class="image vn-shadow" on-error-src
ng-style="{'background': 'url(/api/dms/' + photo.dmsFk + '/downloadFile?access_token=' + $ctrl.vnToken.token + ')'}" ng-style="{'background': 'url(' + $ctrl.getImagePath(photo.dmsFk) + ')'}"
zoom-image="/api/dms/{{::photo.dmsFk}}/downloadFile?access_token={{::$ctrl.vnToken.token}}"> zoom-image="{{$ctrl.getImagePath(photo.dmsFk)}}">
</section> </section>
<section class="actions"> <section class="actions">
<vn-button <vn-button

View File

@ -3,6 +3,11 @@ import Section from 'salix/components/section';
import './style.scss'; import './style.scss';
class Controller extends Section { class Controller extends Section {
constructor($element, $, vnFile) {
super($element, $);
this.vnFile = vnFile;
}
deleteDms(index) { deleteDms(index) {
const dmsFk = this.photos[index].dmsFk; const dmsFk = this.photos[index].dmsFk;
return this.$http.post(`ClaimDms/${dmsFk}/removeFile`) return this.$http.post(`ClaimDms/${dmsFk}/removeFile`)
@ -80,7 +85,13 @@ class Controller extends Section {
this.$.model.refresh(); this.$.model.refresh();
}); });
} }
getImagePath(dmsId) {
return this.vnFile.getPath(`/api/dms/${dmsId}/downloadFile`);
} }
}
Controller.$inject = ['$element', '$scope', 'vnFile'];
ngModule.component('vnClaimPhotos', { ngModule.component('vnClaimPhotos', {
template: require('./index.html'), template: require('./index.html'),

View File

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

View File

@ -3,6 +3,11 @@ import Section from 'salix/components/section';
import './style.scss'; import './style.scss';
class Controller extends Section { class Controller extends Section {
constructor($element, $, vnFile) {
super($element, $);
this.vnFile = vnFile;
}
$onChanges() { $onChanges() {
if (this.claim && this.claim.id) if (this.claim && this.claim.id)
this.getSummary(); this.getSummary();
@ -32,7 +37,13 @@ class Controller extends Section {
this.summary = response.data; this.summary = response.data;
}); });
} }
getImagePath(dmsId) {
return this.vnFile.getPath(`/api/dms/${dmsId}/downloadFile`);
} }
}
Controller.$inject = ['$element', '$scope', 'vnFile'];
ngModule.component('vnClaimSummary', { ngModule.component('vnClaimSummary', {
template: require('./index.html'), template: require('./index.html'),

View File

@ -41,7 +41,7 @@ class Controller extends Descriptor {
} }
onConsumerReportAccept() { onConsumerReportAccept() {
this.showReport('campaign-metrics', { this.vnReport.show('campaign-metrics', {
recipientId: this.id, recipientId: this.id,
from: this.from, from: this.from,
to: this.to, to: this.to,

View File

@ -54,11 +54,10 @@
</span> </span>
</vn-td> </vn-td>
<vn-td shrink> <vn-td shrink>
<a target="_blank" <span title="{{'Download file' | translate}}" class="link"
title="{{'Download file' | translate}}" ng-click="$ctrl.downloadFile(document.dmsFk)">
href="api/dms/{{::document.dmsFk}}/downloadFile?access_token={{::$ctrl.vnToken.token}}">
{{::document.dms.file}} {{::document.dms.file}}
</a> </span>
</vn-td> </vn-td>
<vn-td shrink> <vn-td shrink>
<span class="link" <span class="link"
@ -69,13 +68,10 @@
{{::document.dms.created | date:'dd/MM/yyyy HH:mm'}} {{::document.dms.created | date:'dd/MM/yyyy HH:mm'}}
</vn-td> </vn-td>
<vn-td shrink> <vn-td shrink>
<a target="_blank" <vn-icon-button title="{{'Download file' | translate}}"
href="api/dms/{{::document.dmsFk}}/downloadFile?access_token={{::$ctrl.vnToken.token}}">
<vn-icon-button
icon="cloud_download" icon="cloud_download"
title="{{'Download file' | translate}}"> ng-click="$ctrl.downloadFile(document.dmsFk)">
</vn-icon-button> </vn-icon-button>
</a>
</vn-td> </vn-td>
<vn-td shrink> <vn-td shrink>
<vn-icon-button ui-sref="client.card.dms.edit({dmsId: {{::document.dmsFk}}})" <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'; import './style.scss';
class Controller extends Section { class Controller extends Section {
constructor($element, $) { constructor($element, $, vnFile) {
super($element, $); super($element, $, vnFile);
this.vnFile = vnFile;
this.filter = { this.filter = {
include: { include: {
relation: 'dms', relation: 'dms',
@ -49,9 +50,13 @@ class Controller extends Section {
this.vnApp.showSuccess(this.$t('Data saved!')); 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', { ngModule.component('vnClientDmsIndex', {
template: require('./index.html'), template: require('./index.html'),

View File

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

View File

@ -12,15 +12,16 @@ describe('Entry Component vnEntryDescriptor', () => {
describe('showEntryReport()', () => { describe('showEntryReport()', () => {
it('should open a new window showing a delivery note PDF document', () => { 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 = { const params = {
clientId: controller.vnConfig.storage.currentUserWorkerId, clientId: controller.vnConfig.storage.currentUserWorkerId,
entryId: entry.id entryId: entry.id
}; };
controller.showEntryReport(); 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() { showRouteReport() {
this.showReport('driver-route', { this.vnReport.show('driver-route', {
routeId: this.id routeId: this.id
}); });
} }
sendRouteReport() { sendRouteReport() {
const workerUser = this.route.worker.user; const workerUser = this.route.worker.user;
this.sendEmail('driver-route', { this.vnEmail.send('driver-route', {
recipient: workerUser.emailUser.email, recipient: workerUser.emailUser.email,
routeId: this.id routeId: this.id
}); });

View File

@ -101,14 +101,14 @@ class Controller extends Descriptor {
} }
showDeliveryNote() { showDeliveryNote() {
this.showReport('delivery-note', { this.vnReport.show('delivery-note', {
recipientId: this.ticket.client.id, recipientId: this.ticket.client.id,
ticketId: this.id, ticketId: this.id,
}); });
} }
sendDeliveryNote() { sendDeliveryNote() {
return this.sendEmail('delivery-note', { return this.vnEmail.send('delivery-note', {
recipientId: this.ticket.client.id, recipientId: this.ticket.client.id,
recipient: this.ticket.client.email, recipient: this.ticket.client.email,
ticketId: this.id ticketId: this.id

View File

@ -64,21 +64,22 @@ describe('Ticket Component vnTicketDescriptor', () => {
describe('showDeliveryNote()', () => { describe('showDeliveryNote()', () => {
it('should open a new window showing a delivery note PDF document', () => { 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 = { const params = {
clientId: ticket.client.id, clientId: ticket.client.id,
ticketId: ticket.id ticketId: ticket.id
}; };
controller.showDeliveryNote(); controller.showDeliveryNote();
expect(controller.showReport).toHaveBeenCalledWith('delivery-note', params); expect(controller.vnReport.show).toHaveBeenCalledWith('delivery-note', params);
}); });
}); });
describe('sendDeliveryNote()', () => { describe('sendDeliveryNote()', () => {
it('should make a query and call vnApp.showMessage()', () => { it('should make a query and call vnApp.showMessage()', () => {
jest.spyOn(controller, 'sendEmail'); jest.spyOn(controller.vnEmail, 'send');
const params = { const params = {
recipient: ticket.client.email, recipient: ticket.client.email,
@ -87,7 +88,7 @@ describe('Ticket Component vnTicketDescriptor', () => {
}; };
controller.sendDeliveryNote(); controller.sendDeliveryNote();
expect(controller.sendEmail).toHaveBeenCalledWith('delivery-note', params); expect(controller.vnEmail.send).toHaveBeenCalledWith('delivery-note', params);
}); });
}); });

View File

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

View File

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

View File

@ -29,13 +29,10 @@
<vn-td>{{::thermograph.warehouse.name}}</vn-td> <vn-td>{{::thermograph.warehouse.name}}</vn-td>
<vn-td>{{::thermograph.created | date: 'dd/MM/yyyy'}}</vn-td> <vn-td>{{::thermograph.created | date: 'dd/MM/yyyy'}}</vn-td>
<vn-td shrink> <vn-td shrink>
<a target="_blank" <vn-icon-button title="{{'Download file' | translate}}"
href="api/dms/{{::thermograph.dmsFk}}/downloadFile?access_token={{::$ctrl.vnToken.token}}">
<vn-icon-button
icon="cloud_download" icon="cloud_download"
title="{{'Download file' | translate}}"> ng-click="$ctrl.downloadFile(thermograph.dmsFk)">
</vn-icon-button> </vn-icon-button>
</a>
</vn-td> </vn-td>
<vn-td shrink> <vn-td shrink>
<vn-icon-button ui-sref="travel.card.thermograph.edit({thermographId: {{::thermograph.id}}})" <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'; import './style.scss';
class Controller extends Section { class Controller extends Section {
$onInit() { constructor($element, $, vnFile) {
super($element, $);
this.vnFile = vnFile;
this.filter = { this.filter = {
include: include:
{relation: 'warehouse', {relation: 'warehouse',
@ -29,7 +31,13 @@ class Controller extends Section {
this.thermographIndex = null; this.thermographIndex = null;
}); });
} }
downloadFile(dmsId) {
this.vnFile.download(`api/dms/${dmsId}/downloadFile`);
} }
}
Controller.$inject = ['$element', '$scope', 'vnFile'];
ngModule.component('vnTravelThermographIndex', { ngModule.component('vnTravelThermographIndex', {
template: require('./index.html'), template: require('./index.html'),

View File

@ -38,22 +38,19 @@
</span> </span>
</vn-td > </vn-td >
<vn-td shrink> <vn-td shrink>
<a target="_blank" <span title="{{'Download file' | translate}}" class="link"
title="{{'Download file' | translate}}" ng-click="$ctrl.downloadFile(document.dmsFk)">
href="api/workerDms/{{::document.dmsFk}}/downloadFile?access_token={{::$ctrl.vnToken.token}}">{{::document.file}} {{::document.file}}
</a> </span>
</vn-td> </vn-td>
<vn-td> <vn-td>
{{::document.created | date:'dd/MM/yyyy HH:mm'}} {{::document.created | date:'dd/MM/yyyy HH:mm'}}
</vn-td> </vn-td>
<vn-td shrink> <vn-td shrink>
<a target="_blank" <vn-icon-button title="{{'Download file' | translate}}"
href="api/workerDms/{{::document.dmsFk}}/downloadFile?access_token={{::$ctrl.vnToken.token}}">
<vn-icon-button
icon="cloud_download" icon="cloud_download"
title="{{'Download file' | translate}}"> ng-click="$ctrl.downloadFile(document.dmsFk)">
</vn-icon-button> </vn-icon-button>
</a>
</vn-td> </vn-td>
<vn-td shrink> <vn-td shrink>
<vn-icon-button ui-sref="worker.card.edit({dmsId: {{::document.dmsFk}}})" <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'; import './style.scss';
class Controller extends Component { class Controller extends Component {
constructor($element, $) { constructor($element, $, vnFile) {
super($element, $); super($element, $);
this.vnFile = vnFile;
this.filter = { this.filter = {
include: { include: {
relation: 'dms', relation: 'dms',
@ -51,7 +52,13 @@ class Controller extends Component {
this.vnApp.showSuccess(this.$t('Data saved!')); this.vnApp.showSuccess(this.$t('Data saved!'));
}); });
} }
downloadFile(dmsId) {
this.vnFile.download(`api/workerDms/${dmsId}/downloadFile`);
} }
}
Controller.$inject = ['$element', '$scope', 'vnFile'];
ngModule.component('vnWorkerDmsIndex', { ngModule.component('vnWorkerDmsIndex', {
template: require('./index.html'), template: require('./index.html'),