refactor sms send
gitea/salix/1998-client_sms Something is wrong with the build of this commit Details

This commit is contained in:
Bernat Exposito Domenech 2020-01-15 13:27:14 +01:00
parent 1ad9f9bb2a
commit b42b2ec8c3
14 changed files with 261 additions and 14 deletions

View File

@ -0,0 +1,55 @@
module.exports = Self => {
Self.remoteMethodCtx('sendSms', {
description: 'Log the message in clientLog and call the send method',
accessType: 'WRITE',
accepts: [{
arg: 'id',
type: 'Number',
required: true,
description: 'The ticket id',
http: {source: 'path'}
},
{
arg: 'destination',
type: 'String',
required: true,
},
{
arg: 'message',
type: 'String',
required: true,
}],
returns: {
type: 'Object',
root: true
},
http: {
path: `/:id/sendSms`,
verb: 'POST'
}
});
Self.sendSms = async(ctx, id, destination, message) => {
const userId = ctx.req.accessToken.userId;
let sms = await Self.app.models.Sms.send(ctx, id, destination, message);
let logRecord = {
originFk: id,
userFk: userId,
action: 'insert',
changedModel: 'sms',
newInstance: {
destinationFk: id,
destination: destination,
message: message,
statusCode: sms.statusCode,
status: sms.status
}
};
await Self.app.models.ClientLog.create(logRecord);
return sms;
};
};

View File

@ -3,7 +3,7 @@ const xmlParser = require('xml2js').parseString;
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('send', {
Self.remoteMethod('send', {
description: 'Sends SMS to a destination phone',
accessType: 'WRITE',
accepts: [{
@ -83,7 +83,6 @@ module.exports = Self => {
};
const sms = await Self.create(newSms);
if (statusCode != 200)
throw new UserError(`We weren't able to send this SMS`);

View File

@ -24,6 +24,7 @@ module.exports = Self => {
require('../methods/client/canBeInvoiced')(Self);
require('../methods/client/uploadFile')(Self);
require('../methods/client/lastActiveTickets')(Self);
require('../methods/client/sendSms')(Self);
// Validations

View File

@ -1,11 +1,7 @@
{
"name": "Sms",
"description": "Sms sent to client",
"base": "Loggable",
"log": {
"model":"ClientLog",
"relation": "recipient"
},
"base": "VnModel",
"options": {
"mysql": {
"table": "sms"
@ -45,11 +41,6 @@
"type": "belongsTo",
"model": "Account",
"foreignKey": "senderFk"
},
"recipient": {
"type": "belongsTo",
"model": "Client",
"foreignKey": "destinationFk"
}
}
}

View File

@ -28,7 +28,7 @@ class Controller extends Component {
onResponse(response) {
if (response === 'accept') {
this.$http.post(`Sms/send`, this.sms).then(res => {
this.$http.post(`Clients/${this.$params.id}/sendSms`, this.sms).then(res => {
this.vnApp.showMessage(this.$translate.instant('SMS sent!'));
if (res.data) this.emit('send', {response: res.data});

View File

@ -0,0 +1,55 @@
module.exports = Self => {
Self.remoteMethodCtx('sendSms', {
description: 'Log the message in ticketLog and call the send method',
accessType: 'WRITE',
accepts: [{
arg: 'id',
type: 'Number',
required: true,
description: 'The ticket id',
http: {source: 'path'}
},
{
arg: 'destination',
type: 'String',
required: true,
},
{
arg: 'message',
type: 'String',
required: true,
}],
returns: {
type: 'Object',
root: true
},
http: {
path: `/:id/sendSms`,
verb: 'POST'
}
});
Self.sendSms = async(ctx, id, destination, message) => {
const userId = ctx.req.accessToken.userId;
let sms = await Self.app.models.Sms.send(ctx, id, destination, message);
let logRecord = {
originFk: id,
userFk: userId,
action: 'insert',
changedModel: 'sms',
newInstance: {
destinationFk: id,
destination: destination,
message: message,
statusCode: sms.statusCode,
status: sms.status
}
};
await Self.app.models.TicketLog.create(logRecord);
return sms;
};
};

View File

@ -28,6 +28,7 @@ module.exports = Self => {
require('../methods/ticket/canHaveStowaway')(Self);
require('../methods/ticket/recalculateComponents')(Self);
require('../methods/ticket/deleteStowaway')(Self);
require('../methods/ticket/sendSms')(Self);
Self.observe('before save', async function(ctx) {
if (ctx.isNewInstance) return;

View File

@ -193,7 +193,7 @@
<!-- Regenerate invoice dialog -->
<!-- SMS Dialog -->
<vn-client-sms vn-id="sms" sms="$ctrl.newSMS"></vn-client-sms>
<vn-ticket-sms vn-id="sms" sms="$ctrl.newSMS"></vn-ticket-sms>
<!-- SMS Dialog -->
<vn-confirm

View File

@ -34,3 +34,4 @@ import './weekly';
import './dms/index';
import './dms/create';
import './dms/edit';
import './sms';

View File

@ -0,0 +1,35 @@
<vn-dialog
vn-id="SMSDialog"
on-response="$ctrl.onResponse($response)">
<tpl-body>
<section class="SMSDialog">
<h5 class="vn-py-sm" translate>Send SMS</h5>
<vn-horizontal>
<vn-textfield
vn-one
label="Destination"
ng-model="$ctrl.sms.destination">
</vn-textfield>
</vn-horizontal>
<vn-horizontal >
<vn-textarea vn-one
vn-id="message"
label="Message"
ng-model="$ctrl.sms.message"
rows="5"
maxlength="160"
rule>
</vn-textarea>
</vn-horizontal>
<vn-horizontal>
<span>
{{'Characters remaining' | translate}}: {{$ctrl.charactersRemaining()}}
</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>

View File

@ -0,0 +1,48 @@
import ngModule from '../module';
import Component from 'core/lib/component';
import './style.scss';
class Controller extends Component {
constructor($element, $scope, $http, $translate, vnApp) {
super($element, $scope);
this.$scope = $scope;
this.$http = $http;
this.$translate = $translate;
this.vnApp = vnApp;
}
open() {
this.$scope.SMSDialog.show();
}
charactersRemaining() {
let elementMaxLength;
let textAreaLength;
const element = this.$scope.message;
textAreaLength = element.input.textLength;
elementMaxLength = element.maxlength;
return elementMaxLength - textAreaLength;
}
onResponse(response) {
if (response === 'accept') {
this.$http.post(`Tickets/${this.$params.id}/sendSms`, this.sms).then(res => {
this.vnApp.showMessage(this.$translate.instant('SMS sent!'));
if (res.data) this.emit('send', {response: res.data});
});
}
}
}
Controller.$inject = ['$element', '$scope', '$http', '$translate', 'vnApp'];
ngModule.component('vnTicketSms', {
template: require('./index.html'),
controller: Controller,
bindings: {
sms: '<',
}
});

View File

@ -0,0 +1,51 @@
import './index';
describe('Client', () => {
describe('Component vnClientSms', () => {
let controller;
let $httpBackend;
let $element;
beforeEach(ngModule('client'));
beforeEach(angular.mock.inject(($componentController, $rootScope, _$httpBackend_) => {
$httpBackend = _$httpBackend_;
let $scope = $rootScope.$new();
$element = angular.element('<vn-dialog></vn-dialog>');
controller = $componentController('vnClientSms', {$element, $scope});
controller.client = {id: 101};
}));
describe('onResponse()', () => {
it('should perform a POST query and show a success snackbar', () => {
const ticketId = 11;
let params = {destinationFk: 101, destination: 111111111, message: 'My SMS'};
controller.sms = {destinationFk: 101, destination: 111111111, message: 'My SMS'};
spyOn(controller.vnApp, 'showMessage');
$httpBackend.when('POST', `Ticket/${ticketId}/sendSms`, params).respond(200, params);
$httpBackend.expect('POST', `Sms/send`, params).respond(params);
controller.onResponse('accept');
$httpBackend.flush();
expect(controller.vnApp.showMessage).toHaveBeenCalledWith('SMS sent!');
});
});
describe('charactersRemaining()', () => {
it('should return the characters remaining in a element', () => {
controller.$scope.message = {
input: {
textLength: 50
},
maxlength: 150
};
let result = controller.charactersRemaining();
expect(result).toEqual(100);
});
});
});
});

View File

@ -0,0 +1,5 @@
Send SMS: Enviar SMS
Destination: Destinatario
Message: Mensaje
SMS sent!: ¡SMS enviado!
Characters remaining: Carácteres restantes

View File

@ -0,0 +1,5 @@
@import "variables";
.SMSDialog {
min-width: 25em
}