From f6674448f83d775cc211588cf6e13ddc73c1484d Mon Sep 17 00:00:00 2001 From: Bernat Exposito Domenech Date: Mon, 10 Feb 2020 13:22:43 +0100 Subject: [PATCH] ticket/client.sms validations --- modules/client/front/sms/index.html | 5 ++++- modules/client/front/sms/index.js | 19 +++++++++++++++---- modules/client/front/sms/index.spec.js | 20 ++++++++++++++++++++ modules/client/front/sms/locale/es.yml | 4 +++- modules/ticket/front/sms/index.html | 5 ++++- modules/ticket/front/sms/index.js | 19 +++++++++++++++---- modules/ticket/front/sms/index.spec.js | 20 ++++++++++++++++++++ modules/ticket/front/sms/locale/es.yml | 4 +++- 8 files changed, 84 insertions(+), 12 deletions(-) diff --git a/modules/client/front/sms/index.html b/modules/client/front/sms/index.html index ac7a20651..facbb7694 100644 --- a/modules/client/front/sms/index.html +++ b/modules/client/front/sms/index.html @@ -8,7 +8,9 @@ + ng-model="$ctrl.sms.destination" + required="true" + rule> @@ -18,6 +20,7 @@ ng-model="$ctrl.sms.message" rows="5" maxlength="160" + required="true" rule> diff --git a/modules/client/front/sms/index.js b/modules/client/front/sms/index.js index 1bf2fb99c..851ce1a66 100644 --- a/modules/client/front/sms/index.js +++ b/modules/client/front/sms/index.js @@ -28,12 +28,23 @@ class Controller extends Component { onResponse(response) { if (response === 'accept') { - this.$http.post(`Clients/${this.$params.id}/sendSms`, this.sms).then(res => { - this.vnApp.showMessage(this.$translate.instant('SMS sent!')); + 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 (res.data) this.emit('send', {response: res.data}); - }); + 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}); + }); + } catch (e) { + this.vnApp.showError(this.$translate.instant(e.message)); + return false; + } } + return true; } } diff --git a/modules/client/front/sms/index.spec.js b/modules/client/front/sms/index.spec.js index c2a7eb935..26a597c17 100644 --- a/modules/client/front/sms/index.spec.js +++ b/modules/client/front/sms/index.spec.js @@ -30,6 +30,26 @@ describe('Client', () => { expect(controller.vnApp.showMessage).toHaveBeenCalledWith('SMS sent!'); }); + + it('should call onResponse without the destination and show an error snackbar', () => { + controller.sms = {destinationFk: 101, message: 'My SMS'}; + + spyOn(controller.vnApp, 'showError'); + + controller.onResponse('accept'); + + 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: 101, destination: 222222222}; + + spyOn(controller.vnApp, 'showError'); + + controller.onResponse('accept'); + + expect(controller.vnApp.showError).toHaveBeenCalledWith(`The message can't be empty`); + }); }); describe('charactersRemaining()', () => { diff --git a/modules/client/front/sms/locale/es.yml b/modules/client/front/sms/locale/es.yml index f26c8ba24..4438e4fce 100644 --- a/modules/client/front/sms/locale/es.yml +++ b/modules/client/front/sms/locale/es.yml @@ -2,4 +2,6 @@ Send SMS: Enviar SMS Destination: Destinatario Message: Mensaje SMS sent!: ¡SMS enviado! -Characters remaining: Carácteres restantes \ No newline at end of file +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 \ No newline at end of file diff --git a/modules/ticket/front/sms/index.html b/modules/ticket/front/sms/index.html index ac7a20651..f74bc29e5 100644 --- a/modules/ticket/front/sms/index.html +++ b/modules/ticket/front/sms/index.html @@ -8,7 +8,9 @@ + ng-model="$ctrl.sms.destination" + required="true" + rule> @@ -18,6 +20,7 @@ ng-model="$ctrl.sms.message" rows="5" maxlength="160" + required="true" rule> diff --git a/modules/ticket/front/sms/index.js b/modules/ticket/front/sms/index.js index 1f2c7f9c0..0d639d46e 100644 --- a/modules/ticket/front/sms/index.js +++ b/modules/ticket/front/sms/index.js @@ -28,12 +28,23 @@ class Controller extends Component { 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!')); + 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 (res.data) this.emit('send', {response: res.data}); - }); + 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}); + }); + } catch (e) { + this.vnApp.showError(this.$translate.instant(e.message)); + return false; + } } + return true; } } diff --git a/modules/ticket/front/sms/index.spec.js b/modules/ticket/front/sms/index.spec.js index 5565c3623..d02b3f3eb 100644 --- a/modules/ticket/front/sms/index.spec.js +++ b/modules/ticket/front/sms/index.spec.js @@ -29,6 +29,26 @@ describe('Ticket', () => { expect(controller.vnApp.showMessage).toHaveBeenCalledWith('SMS sent!'); }); + + it('should call onResponse without the destination and show an error snackbar', () => { + controller.sms = {destinationFk: 101, message: 'My SMS'}; + + spyOn(controller.vnApp, 'showError'); + + controller.onResponse('accept'); + + 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: 101, destination: 222222222}; + + spyOn(controller.vnApp, 'showError'); + + controller.onResponse('accept'); + + expect(controller.vnApp.showError).toHaveBeenCalledWith(`The message can't be empty`); + }); }); describe('charactersRemaining()', () => { diff --git a/modules/ticket/front/sms/locale/es.yml b/modules/ticket/front/sms/locale/es.yml index f26c8ba24..4438e4fce 100644 --- a/modules/ticket/front/sms/locale/es.yml +++ b/modules/ticket/front/sms/locale/es.yml @@ -2,4 +2,6 @@ Send SMS: Enviar SMS Destination: Destinatario Message: Mensaje SMS sent!: ¡SMS enviado! -Characters remaining: Carácteres restantes \ No newline at end of file +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 \ No newline at end of file