4157-send-sms-to-routes #1106
|
@ -0,0 +1,39 @@
|
|||
|
||||
pau marked this conversation as resolved
|
||||
module.exports = Self => {
|
||||
Self.remoteMethodCtx('sendSms', {
|
||||
description: 'Sends a SMS to each client of the routes, each client only recieves the SMS once',
|
||||
accessType: 'WRITE',
|
||||
accepts: [
|
||||
{
|
||||
arg: 'destination',
|
||||
type: 'string',
|
||||
description: 'A comma separated string of destinations',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
arg: 'message',
|
||||
type: 'string',
|
||||
required: true,
|
||||
}],
|
||||
returns: {
|
||||
type: 'object',
|
||||
root: true
|
||||
},
|
||||
http: {
|
||||
path: `/sendSms`,
|
||||
verb: 'POST'
|
||||
}
|
||||
});
|
||||
|
||||
Self.sendSms = async(ctx, destination, message) => {
|
||||
const targetClients = destination.split(',');
|
||||
|
||||
const allSms = [];
|
||||
for (let client of targetClients) {
|
||||
let sms = await Self.app.models.Sms.send(ctx, client, message);
|
||||
allSms.push(sms);
|
||||
}
|
||||
|
||||
return allSms;
|
||||
};
|
||||
};
|
|
@ -12,6 +12,7 @@ module.exports = Self => {
|
|||
require('../methods/route/updateWorkCenter')(Self);
|
||||
require('../methods/route/driverRoutePdf')(Self);
|
||||
require('../methods/route/driverRouteEmail')(Self);
|
||||
require('../methods/route/sendSms')(Self);
|
||||
|
||||
Self.validate('kmStart', validateDistance, {
|
||||
message: 'Distance must be lesser than 1000'
|
||||
|
|
|
@ -15,3 +15,4 @@ import './agency-term/index';
|
|||
import './agency-term/createInvoiceIn';
|
||||
import './agency-term-search-panel';
|
||||
import './ticket-popup';
|
||||
import './sms';
|
||||
|
|
|
@ -9,3 +9,5 @@ Go to route: Ir a la ruta
|
|||
You must select a valid time: Debe seleccionar una hora válida
|
||||
You must select a valid date: Debe seleccionar una fecha válida
|
||||
Mark as served: Marcar como servidas
|
||||
Retrieving data from the routes: Recuperando datos de las rutas
|
||||
Send SMS to all clients: Mandar sms a todos los clientes de las rutas
|
|
@ -0,0 +1,36 @@
|
|||
<vn-dialog
|
||||
vn-id="SMSDialog"
|
||||
on-accept="$ctrl.onResponse()"
|
||||
message="Send SMS to the selected tickets">
|
||||
<tpl-body>
|
||||
<section class="SMSDialog">
|
||||
<vn-horizontal>
|
||||
<vn-textarea vn-one
|
||||
vn-id="message"
|
||||
label="Message"
|
||||
ng-model="$ctrl.sms.message"
|
||||
info="Special characters like accents counts as a multiple"
|
||||
rows="5"
|
||||
required="true"
|
||||
rule>
|
||||
</vn-textarea>
|
||||
</vn-horizontal>
|
||||
<vn-horizontal>
|
||||
<span>
|
||||
{{'Characters remaining' | translate}}:
|
||||
<vn-chip translate-attr="{title: 'Packing'}" ng-class="{
|
||||
'colored': $ctrl.charactersRemaining() > 25,
|
||||
'warning': $ctrl.charactersRemaining() <= 25,
|
||||
'alert': $ctrl.charactersRemaining() < 0,
|
||||
}">
|
||||
{{$ctrl.charactersRemaining()}}
|
||||
</vn-chip>
|
||||
</span>
|
||||
</vn-horizontal>
|
||||
</section>
|
||||
</tpl-body>
|
||||
<tpl-buttons>
|
||||
<input type="button" response="cancel" translate-attr="{value: 'Cancel'}"/>
|
||||
<button response="accept" translate>Send</button>
|
||||
</tpl-buttons>
|
||||
</vn-dialog>
|
|
@ -0,0 +1,47 @@
|
|||
import ngModule from '../module';
|
||||
import Component from 'core/lib/component';
|
||||
import './style.scss';
|
||||
|
||||
class Controller extends Component {
|
||||
open() {
|
||||
this.$.SMSDialog.show();
|
||||
}
|
||||
|
||||
charactersRemaining() {
|
||||
const element = this.$.message;
|
||||
const value = element.input.value;
|
||||
|
||||
const maxLength = 160;
|
||||
const textAreaLength = new Blob([value]).size;
|
||||
return maxLength - textAreaLength;
|
||||
}
|
||||
|
||||
onResponse() {
|
||||
try {
|
||||
if (!this.sms.destination)
|
||||
throw new Error(`The destination can't be empty`);
|
||||
if (!this.sms.message)
|
||||
throw new Error(`The message can't be empty`);
|
||||
if (this.charactersRemaining() < 0)
|
||||
throw new Error(`The message it's too long`);
|
||||
|
||||
this.$http.post(`Routes/sendSms`, this.sms).then(res => {
|
||||
this.vnApp.showMessage(this.$t('SMS sent!'));
|
||||
|
||||
if (res.data) this.emit('send', {response: res.data});
|
||||
});
|
||||
} catch (e) {
|
||||
this.vnApp.showError(this.$t(e.message));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
ngModule.vnComponent('vnRouteSms', {
|
||||
template: require('./index.html'),
|
||||
controller: Controller,
|
||||
bindings: {
|
||||
sms: '<',
|
||||
}
|
||||
});
|
|
@ -0,0 +1,71 @@
|
|||
import './index';
|
||||
|
||||
describe('Route', () => {
|
||||
describe('Component vnRouteSms', () => {
|
||||
let controller;
|
||||
let $httpBackend;
|
||||
|
||||
beforeEach(ngModule('route'));
|
||||
|
||||
beforeEach(inject(($componentController, $rootScope, _$httpBackend_) => {
|
||||
$httpBackend = _$httpBackend_;
|
||||
let $scope = $rootScope.$new();
|
||||
const $element = angular.element('<vn-dialog></vn-dialog>');
|
||||
controller = $componentController('vnRouteSms', {$element, $scope});
|
||||
controller.$.message = {
|
||||
input: {
|
||||
value: 'My SMS'
|
||||
}
|
||||
};
|
||||
}));
|
||||
|
||||
describe('onResponse()', () => {
|
||||
it('should perform a POST query and show a success snackbar', () => {
|
||||
let params = {destinationFk: 1101, destination: 111111111, message: 'My SMS'};
|
||||
controller.sms = {destinationFk: 1101, destination: 111111111, message: 'My SMS'};
|
||||
|
||||
jest.spyOn(controller.vnApp, 'showMessage');
|
||||
$httpBackend.expect('POST', `Routes/sendSms`, params).respond(200, params);
|
||||
|
||||
controller.onResponse();
|
||||
$httpBackend.flush();
|
||||
|
||||
expect(controller.vnApp.showMessage).toHaveBeenCalledWith('SMS sent!');
|
||||
});
|
||||
|
||||
it('should call onResponse without the destination and show an error snackbar', () => {
|
||||
controller.sms = {destinationFk: 1101, message: 'My SMS'};
|
||||
|
||||
jest.spyOn(controller.vnApp, 'showError');
|
||||
|
||||
controller.onResponse();
|
||||
|
||||
expect(controller.vnApp.showError).toHaveBeenCalledWith(`The destination can't be empty`);
|
||||
});
|
||||
|
||||
it('should call onResponse without the message and show an error snackbar', () => {
|
||||
controller.sms = {destinationFk: 1101, destination: 222222222};
|
||||
|
||||
jest.spyOn(controller.vnApp, 'showError');
|
||||
|
||||
controller.onResponse();
|
||||
|
||||
expect(controller.vnApp.showError).toHaveBeenCalledWith(`The message can't be empty`);
|
||||
});
|
||||
});
|
||||
|
||||
describe('charactersRemaining()', () => {
|
||||
it('should return the characters remaining in a element', () => {
|
||||
controller.$.message = {
|
||||
input: {
|
||||
value: 'My message 0€'
|
||||
}
|
||||
};
|
||||
|
||||
let result = controller.charactersRemaining();
|
||||
|
||||
expect(result).toEqual(145);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
|
@ -0,0 +1,9 @@
|
|||
Send SMS to the selected tickets: Enviar SMS a los tickets seleccionados
|
||||
Routes to notify: Rutas a notificar
|
||||
Message: Mensaje
|
||||
SMS sent!: ¡SMS enviado!
|
||||
Characters remaining: Carácteres restantes
|
||||
The destination can't be empty: El destinatario no puede estar vacio
|
||||
The message can't be empty: El mensaje no puede estar vacio
|
||||
The message it's too long: El mensaje es demasiado largo
|
||||
Special characters like accents counts as a multiple: Carácteres especiales como los acentos cuentan como varios
|
|
@ -0,0 +1,5 @@
|
|||
@import "variables";
|
||||
|
||||
.SMSDialog {
|
||||
min-width: 400px
|
||||
}
|
|
@ -29,13 +29,21 @@
|
|||
disabled="!$ctrl.isChecked"
|
||||
ng-click="$ctrl.deletePriority()"
|
||||
vn-tooltip="Delete priority"
|
||||
icon="filter_alt_off">
|
||||
icon="filter_alt">
|
||||
</vn-button>
|
||||
<vn-button
|
||||
ng-click="$ctrl.setOrderedPriority($ctrl.tickets)"
|
||||
vn-tooltip="Renumber all tickets in the order you see on the screen"
|
||||
icon="format_list_numbered">
|
||||
</vn-button>
|
||||
<vn-button
|
||||
vn-acl="deliveryBoss"
|
||||
vn-acl-action="remove"
|
||||
disabled="!$ctrl.isChecked"
|
||||
icon="sms"
|
||||
vn-tooltip="Send SMS to all clients"
|
||||
ng-click="$ctrl.sendSms()">
|
||||
</vn-button>
|
||||
</vn-tool-bar>
|
||||
<vn-table class="vn-pt-md" model="model" auto-load="false" vn-droppable="$ctrl.onDrop($event)">
|
||||
<vn-thead>
|
||||
|
@ -149,19 +157,29 @@
|
|||
route="$ctrl.$params"
|
||||
parent-reload="$ctrl.$.model.refresh()">
|
||||
</vn-route-ticket-popup>
|
||||
|
||||
<vn-float-button
|
||||
icon="add"
|
||||
<div fixed-bottom-right>
|
||||
<vn-vertical style="align-items: center;">
|
||||
<a vn-bind="+">
|
||||
<vn-button
|
||||
class="round md vn-mb-sm"
|
||||
ng-click="$ctrl.$.ticketPopup.show()"
|
||||
icon="add"
|
||||
vn-tooltip="Add ticket"
|
||||
vn-acl="delivery"
|
||||
vn-acl-action="remove"
|
||||
vn-bind="+"
|
||||
fixed-bottom-right>
|
||||
</vn-float-button>
|
||||
tooltip-position="left">
|
||||
</vn-button>
|
||||
</a>
|
||||
</vn-vertical>
|
||||
</div>
|
||||
<vn-ticket-descriptor-popover
|
||||
vn-id="ticket-descriptor">
|
||||
</vn-ticket-descriptor-popover>
|
||||
<vn-client-descriptor-popover
|
||||
vn-id="client-descriptor">
|
||||
</vn-client-descriptor-popover>
|
||||
<!-- SMS Dialog -->
|
||||
<vn-route-sms
|
||||
vn-id="sms"
|
||||
sms="$ctrl.newSMS">
|
||||
</vn-route-sms>
|
|
@ -161,6 +161,37 @@ class Controller extends Section {
|
|||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
async sendSms() {
|
||||
try {
|
||||
const clientsFk = [];
|
||||
const clientsName = [];
|
||||
const clients = [];
|
||||
|
||||
const selectedTickets = this.getSelectedItems(this.$.$ctrl.tickets);
|
||||
|
||||
for (let ticket of selectedTickets) {
|
||||
clientsFk.push(ticket.clientFk);
|
||||
let userContact = await this.$http.get(`Clients/${ticket.clientFk}`);
|
||||
clientsName.push(userContact.data.name);
|
||||
clients.push(userContact.data.phone);
|
||||
}
|
||||
|
||||
const destinationFk = String(clientsFk);
|
||||
const destination = String(clients);
|
||||
|
||||
this.newSMS = Object.assign({
|
||||
destinationFk: destinationFk,
|
||||
destination: destination
|
||||
});
|
||||
|
||||
this.$.sms.open();
|
||||
return true;
|
||||
} catch (e) {
|
||||
this.vnApp.showError(this.$t(e.message));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ngModule.vnComponent('vnRouteTickets', {
|
||||
|
|
Loading…
Reference in New Issue
Esto parece que sobra