diff --git a/modules/ticket/front/descriptor-menu/index.js b/modules/ticket/front/descriptor-menu/index.js index 8e3ac5863..9d4191530 100644 --- a/modules/ticket/front/descriptor-menu/index.js +++ b/modules/ticket/front/descriptor-menu/index.js @@ -25,7 +25,12 @@ class Controller extends Section { set id(value) { this._id = value; - if (value) this.loadData(); + if (!value) return; + + this.loadData().then(() => { + if (this.$params.sendSMS) + this.showSMSDialog(); + }); } loadData() { @@ -67,8 +72,10 @@ class Controller extends Section { } reload() { - return this.loadData().then( - () => this.cardReload()); + return this.loadData().then(() => { + if (this.parentReload) + this.parentReload(); + }); } get isInvoiced() { @@ -235,6 +242,6 @@ ngModule.vnComponent('vnTicketDescriptorMenu', { controller: Controller, bindings: { ticketId: '<', - reload: '&' + parentReload: '&' } }); diff --git a/modules/ticket/front/descriptor-menu/index.spec.js b/modules/ticket/front/descriptor-menu/index.spec.js new file mode 100644 index 000000000..3cd08fc38 --- /dev/null +++ b/modules/ticket/front/descriptor-menu/index.spec.js @@ -0,0 +1,223 @@ +import './index.js'; + +describe('Ticket Component vnTicketDescriptorMenu', () => { + let $httpBackend; + let controller; + let $state; + + const ticket = { + id: 16, + clientFk: 101, + invoiceOut: {id: 1}, + client: { + id: 101, + email: 'client@email' + }, + address: { + id: 101, + mobile: 111111111, + phone: 2222222222 + }, + tracking: { + state: {alertLevel: 3} + } + }; + + beforeEach(ngModule('ticket')); + + beforeEach(inject(($componentController, _$httpBackend_, _$state_) => { + $httpBackend = _$httpBackend_; + $state = _$state_; + $state.params.id = 16; + $state.getCurrentPath = () => [null, {state: {name: 'ticket'}}]; + + const $element = angular.element(''); + controller = $componentController('vnTicketDescriptorMenu', {$element}); + controller._ticketId = ticket.id; + controller._id = ticket.id; + controller.ticket = ticket; + })); + + describe('canRestoreTicket() getter', () => { + it('should return true for a ticket deleted within the last hour', () => { + controller.ticket.isDeleted = true; + controller.ticket.updated = new Date(); + + const result = controller.canRestoreTicket; + + expect(result).toBeTruthy(); + }); + + it('should return false for a ticket deleted more than one hour ago', () => { + const pastHour = new Date(); + pastHour.setHours(pastHour.getHours() - 2); + + controller.ticket.isDeleted = true; + controller.ticket.updated = pastHour; + + const result = controller.canRestoreTicket; + + expect(result).toBeFalsy(); + }); + }); + + describe('addTurn()', () => { + it('should make a query and call $.addTurn.hide() and vnApp.showSuccess()', () => { + controller.$.addTurn = {hide: () => {}}; + jest.spyOn(controller.$.addTurn, 'hide'); + + $httpBackend.expectPATCH(`TicketWeeklies`).respond(); + controller.addTurn(1); + $httpBackend.flush(); + + expect(controller.$.addTurn.hide).toHaveBeenCalledWith(); + }); + }); + + describe('deleteTicket()', () => { + it('should make a query and call vnApp.showSuccess()', () => { + jest.spyOn(controller, 'reload').mockReturnThis(); + jest.spyOn(controller.$state, 'go').mockReturnValue('ok'); + jest.spyOn(controller.vnApp, 'showSuccess'); + + $httpBackend.expectPOST(`Tickets/${ticket.id}/setDeleted`).respond(); + controller.deleteTicket(); + $httpBackend.flush(); + + expect(controller.reload).toHaveBeenCalled(); + expect(controller.$state.go).toHaveBeenCalledWith('ticket.index'); + expect(controller.vnApp.showSuccess).toHaveBeenCalled(); + }); + }); + + describe('restoreTicket()', () => { + it('should make a query to restore the ticket and call vnApp.showSuccess()', () => { + jest.spyOn(controller, 'reload').mockReturnThis(); + jest.spyOn(controller.vnApp, 'showSuccess'); + + $httpBackend.expectPOST(`Tickets/${ticket.id}/restore`).respond(); + controller.restoreTicket(); + $httpBackend.flush(); + + expect(controller.reload).toHaveBeenCalled(); + expect(controller.vnApp.showSuccess).toHaveBeenCalled(); + }); + }); + + describe('showDeliveryNote()', () => { + it('should open a new window showing a delivery note PDF document', () => { + jest.spyOn(controller.vnReport, 'show'); + + window.open = jasmine.createSpy('open'); + const params = { + recipientId: ticket.client.id, + ticketId: ticket.id + }; + controller.showDeliveryNote(); + + expect(controller.vnReport.show).toHaveBeenCalledWith('delivery-note', params); + }); + }); + + describe('sendDeliveryNote()', () => { + it('should make a query and call vnApp.showMessage()', () => { + jest.spyOn(controller.vnEmail, 'send'); + + const params = { + recipient: ticket.client.email, + recipientId: ticket.client.id, + ticketId: ticket.id + }; + controller.sendDeliveryNote(); + + expect(controller.vnEmail.send).toHaveBeenCalledWith('delivery-note', params); + }); + }); + + describe('makeInvoice()', () => { + it('should make a query and call $state.reload() method', () => { + jest.spyOn(controller, 'reload').mockReturnThis(); + jest.spyOn(controller.vnApp, 'showSuccess'); + + $httpBackend.expectPOST(`Tickets/${ticket.id}/makeInvoice`).respond(); + controller.makeInvoice(); + $httpBackend.flush(); + + expect(controller.reload).toHaveBeenCalledWith(); + expect(controller.vnApp.showSuccess).toHaveBeenCalled(); + }); + }); + + describe('regenerateInvoice()', () => { + it('should make a query and show a success snackbar', () => { + jest.spyOn(controller.vnApp, 'showSuccess'); + + $httpBackend.expectPOST(`InvoiceOuts/${ticket.invoiceOut.id}/regenerate`).respond(); + controller.regenerateInvoice(); + $httpBackend.flush(); + + expect(controller.vnApp.showSuccess).toHaveBeenCalled(); + }); + }); + + describe('changeShipped()', () => { + it('should make a query and change the shipped hour if the response is accept', () => { + jest.spyOn(controller, 'reload').mockReturnThis(); + jest.spyOn(controller.vnApp, 'showSuccess'); + + $httpBackend.expectPOST(`Tickets/${ticket.id}/updateEditableTicket`).respond(); + controller.changeShipped(); + $httpBackend.flush(); + + expect(controller.reload).toHaveBeenCalled(); + expect(controller.vnApp.showSuccess).toHaveBeenCalled(); + }); + }); + + describe('canStowaway()', () => { + it('should make a query and return if the ticket can be stowawayed', () => { + $httpBackend.expect('GET', `Tickets/${ticket.id}/canHaveStowaway`).respond(true); + controller.canStowaway(); + $httpBackend.flush(); + + expect(controller.canShowStowaway).toBeTruthy(); + }); + + it('should not make a query if is not on the ticket module', () => { + $state.getCurrentPath = () => [null, {state: {name: 'client'}}]; + controller.canStowaway(); + + expect(controller.canShowStowaway).toBeFalsy(); + }); + }); + + describe('recalculateComponents()', () => { + it('should make a query and show a success message', () => { + jest.spyOn(controller, 'reload').mockReturnThis(); + jest.spyOn(controller.vnApp, 'showSuccess'); + + $httpBackend.expect('POST', `Tickets/${ticket.id}/recalculateComponents`).respond(); + controller.recalculateComponents(); + $httpBackend.flush(); + + expect(controller.reload).toHaveBeenCalled(); + expect(controller.vnApp.showSuccess).toHaveBeenCalled(); + }); + }); + + describe('showSMSDialog()', () => { + it('should set the destionationFk and destination properties and then call the sms open() method', () => { + controller.$.sms = {open: () => {}}; + jest.spyOn(controller.$.sms, 'open'); + + controller.showSMSDialog(); + + expect(controller.$.sms.open).toHaveBeenCalledWith(); + expect(controller.newSMS).toEqual({ + destinationFk: ticket.clientFk, + destination: ticket.address.mobile, + ticketId: 16 + }); + }); + }); +}); diff --git a/modules/ticket/front/descriptor/index.html b/modules/ticket/front/descriptor/index.html index aaafec7e6..234eb9560 100644 --- a/modules/ticket/front/descriptor/index.html +++ b/modules/ticket/front/descriptor/index.html @@ -2,7 +2,7 @@ module="ticket" description="$ctrl.ticket.client.name"> - + @@ -112,85 +112,8 @@ vn-tooltip="Ship stowaways" tooltip-position="up" data="$ctrl.ticket.ship" - on-change="$ctrl.goToTicket(value)"> + ui-sref="ticket.card.sale({id: value})"> - - - - - - - - - - - - - \ No newline at end of file + \ No newline at end of file diff --git a/modules/ticket/front/descriptor/index.js b/modules/ticket/front/descriptor/index.js index d7fcf7508..28d5eb953 100644 --- a/modules/ticket/front/descriptor/index.js +++ b/modules/ticket/front/descriptor/index.js @@ -16,185 +16,6 @@ class Controller extends Descriptor { set entity(value) { super.entity = value; - // this.canStowaway(); - this.isTicketEditable(); - - if (value && this.$params.sendSMS) - this.showSMSDialog(); - } - - get isInvoiced() { - return this.ticket.refFk !== null; - } - - get isTicketModule() { - return this.$state.getCurrentPath()[1].state.name === 'ticket'; - } - - get shouldShowDeleteStowaway() { - if (!this.ticket || !this.isTicketModule) - return false; - - return this.ticket.stowaway || this.ticket.ship; - } - - get filter() { - if (this.ticket) - return JSON.stringify({clientFk: this.ticket.clientFk}); - return null; - } - - /* get canRestoreTicket() { - const isDeleted = this.ticket.isDeleted; - const now = new Date(); - const maxDate = new Date(this.ticket.updated); - maxDate.setHours(maxDate.getHours() + 1); - - return isDeleted && (now <= maxDate); - } */ - - isTicketEditable() { - if (!this.ticket) return; - this.$http.get(`Tickets/${this.id}/isEditable`).then(res => { - this.isEditable = res.data; - }); - } - - /* showChangeShipped() { - this.newShipped = this.ticket.shipped; - this.$.changeShippedDialog.show(); - } - - changeShipped() { - let data = { - shipped: this.newShipped - }; - return this.$http.post(`Tickets/${this.id}/updateEditableTicket`, data) - .then(() => this.cardReload()) - .then(() => this.vnApp.showSuccess(this.$t('Shipped hour updated'))); - } */ - - goToTicket(ticketId) { - this.$state.go('ticket.card.sale', {id: ticketId}, {absolute: true}); - } - - /* addTurn(day) { - let params = { - ticketFk: this.id, - weekDay: day, - agencyModeFk: this.ticket.agencyModeFk - }; - return this.$http.patch(`TicketWeeklies`, params) - .then(() => { - this.$.addTurn.hide(); - this.vnApp.showSuccess(this.$t('Data saved!')); - }); - } - - deleteTicket() { - return this.$http.post(`Tickets/${this.id}/setDeleted`) - .then(() => { - this.$state.go('ticket.index'); - this.vnApp.showSuccess(this.$t('Ticket deleted. You can undo this action within the first hour')); - }); - } - - restoreTicket() { - return this.$http.post(`Tickets/${this.id}/restore`) - .then(() => { - this.vnApp.showSuccess(this.$t('Data saved!')); - this.cardReload(); - }); - } */ - - /* canStowaway() { - this.canShowStowaway = false; - if (!this.isTicketModule || !this.ticket) return; - - this.$http.get(`Tickets/${this.id}/canHaveStowaway`) - .then(res => this.canShowStowaway = !!res.data); - } */ - - /* deleteStowaway() { - return this.$http.post(`Tickets/${this.id}/deleteStowaway`) - .then(() => { - this.vnApp.showSuccess(this.$t('Data saved!')); - this.cardReload(); - }); - } */ - - /* showDeliveryNote() { - this.vnReport.show('delivery-note', { - recipientId: this.ticket.client.id, - ticketId: this.id, - }); - } - - sendDeliveryNote() { - return this.vnEmail.send('delivery-note', { - recipientId: this.ticket.client.id, - recipient: this.ticket.client.email, - ticketId: this.id - }); - } - - sendImportSms() { - const params = { - ticketId: this.id, - created: this.ticket.updated - }; - this.showSMSDialog({ - message: this.$params.message || this.$t('Minimum is needed', params) - }); - } - - sendPaymentSms() { - this.showSMSDialog({ - message: this.$params.message || this.$t('Make a payment') - }); - } - - showSMSDialog(params) { - const address = this.ticket.address; - const client = this.ticket.client; - const phone = this.$params.phone - || address.mobile - || address.phone - || client.mobile - || client.phone; - - this.newSMS = Object.assign({ - destinationFk: this.ticket.clientFk, - destination: phone - }, params); - this.$.sms.open(); - } */ - - /* makeInvoice() { - return this.$http.post(`Tickets/${this.id}/makeInvoice`) - .then(() => { - this.vnApp.showSuccess(this.$t('Ticket invoiced')); - this.$state.reload(); - }); - } - - regenerateInvoice() { - const invoiceId = this.ticket.invoiceOut.id; - return this.$http.post(`InvoiceOuts/${invoiceId}/regenerate`) - .then(() => { - const snackbarMessage = this.$t( - `Invoice sent for a regeneration, will be available in a few minutes`); - this.vnApp.showSuccess(snackbarMessage); - }); - } */ - - /* recalculateComponents() { - return this.$http.post(`Tickets/${this.id}/recalculateComponents`) - .then(() => this.vnApp.showSuccess(this.$t('Data saved!'))); - } */ - - cardReload() { - // Prevents error when not defined } loadData() { @@ -243,6 +64,15 @@ class Controller extends Descriptor { return this.getData(`Tickets/${this.id}`, {filter}) .then(res => this.entity = res.data); } + + cardReload() { + // Prevents error when not defined + } + + get filter() { + return this.ticket ? + JSON.stringify({clientFk: this.ticket.clientFk}) : null; + } } ngModule.vnComponent('vnTicketDescriptor', { diff --git a/modules/ticket/front/descriptor/index.spec.js b/modules/ticket/front/descriptor/index.spec.js index 5910b3cf1..f0a8756b8 100644 --- a/modules/ticket/front/descriptor/index.spec.js +++ b/modules/ticket/front/descriptor/index.spec.js @@ -27,197 +27,22 @@ describe('Ticket Component vnTicketDescriptor', () => { beforeEach(inject(($componentController, _$httpBackend_, _$state_) => { $httpBackend = _$httpBackend_; - $httpBackend.whenGET(`Tickets/${ticket.id}/canHaveStowaway`).respond(true); - $httpBackend.expect('GET', `Tickets/${ticket.id}/isEditable`).respond(true); + // $httpBackend.whenGET(`Tickets/${ticket.id}/canHaveStowaway`).respond(true); + // $httpBackend.expect('GET', `Tickets/${ticket.id}/isEditable`).respond(true); $state = _$state_; $state.params.id = 1; - $state.getCurrentPath = () => [null, {state: {name: 'ticket'}}]; + // $state.getCurrentPath = () => [null, {state: {name: 'ticket'}}]; - controller = $componentController('vnTicketDescriptor', {$element: null}, {ticket}); + const $element = angular.element(''); + controller = $componentController('vnTicketDescriptor', {$element}); + controller.ticket = ticket; })); - describe('canRestoreTicket() getter', () => { - it('should return true for a ticket deleted within the last hour', () => { - controller.ticket.isDeleted = true; - controller.ticket.updated = new Date(); - - const result = controller.canRestoreTicket; - - expect(result).toBeTruthy(); - }); - - it('should return false for a ticket deleted more than one hour ago', () => { - const pastHour = new Date(); - pastHour.setHours(pastHour.getHours() - 2); - - controller.ticket.isDeleted = true; - controller.ticket.updated = pastHour; - - const result = controller.canRestoreTicket; - - expect(result).toBeFalsy(); - }); - }); - - describe('addTurn()', () => { - it('should make a query and call $.addTurn.hide() and vnApp.showSuccess()', () => { - controller.$.addTurn = {hide: () => {}}; - jest.spyOn(controller.$.addTurn, 'hide'); - - $httpBackend.expectPATCH(`TicketWeeklies`).respond(); - controller.addTurn(1); - $httpBackend.flush(); - - expect(controller.$.addTurn.hide).toHaveBeenCalledWith(); - }); - }); - - describe('deleteTicket()', () => { - it('should make a query and call vnApp.showSuccess()', () => { - jest.spyOn(controller.$state, 'go').mockReturnValue('ok'); - jest.spyOn(controller.vnApp, 'showSuccess'); - - $httpBackend.expectPOST(`Tickets/${ticket.id}/setDeleted`).respond(); - controller.deleteTicket(); - $httpBackend.flush(); - - expect(controller.$state.go).toHaveBeenCalledWith('ticket.index'); - expect(controller.vnApp.showSuccess).toHaveBeenCalled(); - }); - }); - - describe('restoreTicket()', () => { - it('should make a query to restore the ticket and call vnApp.showSuccess()', () => { - jest.spyOn(controller, 'cardReload'); - jest.spyOn(controller.vnApp, 'showSuccess'); - - $httpBackend.expectPOST(`Tickets/${ticket.id}/restore`).respond(); - controller.restoreTicket(); - $httpBackend.flush(); - - expect(controller.cardReload).toHaveBeenCalled(); - expect(controller.vnApp.showSuccess).toHaveBeenCalled(); - }); - }); - - describe('showDeliveryNote()', () => { - it('should open a new window showing a delivery note PDF document', () => { - jest.spyOn(controller.vnReport, 'show'); - - window.open = jasmine.createSpy('open'); - const params = { - recipientId: ticket.client.id, - ticketId: ticket.id - }; - controller.showDeliveryNote(); - - expect(controller.vnReport.show).toHaveBeenCalledWith('delivery-note', params); - }); - }); - - describe('sendDeliveryNote()', () => { - it('should make a query and call vnApp.showMessage()', () => { - jest.spyOn(controller.vnEmail, 'send'); - - const params = { - recipient: ticket.client.email, - recipientId: ticket.client.id, - ticketId: ticket.id - }; - controller.sendDeliveryNote(); - - expect(controller.vnEmail.send).toHaveBeenCalledWith('delivery-note', params); - }); - }); - - describe('makeInvoice()', () => { - it('should make a query and call $state.reload() method', () => { - jest.spyOn(controller.$state, 'reload').mockReturnThis(); - jest.spyOn(controller.vnApp, 'showSuccess'); - - $httpBackend.expectPOST(`Tickets/${ticket.id}/makeInvoice`).respond(); - controller.makeInvoice(); - $httpBackend.flush(); - - expect(controller.vnApp.showSuccess).toHaveBeenCalled(); - expect(controller.$state.reload).toHaveBeenCalledWith(); - }); - }); - - describe('regenerateInvoice()', () => { - it('should make a query and show a success snackbar', () => { - jest.spyOn(controller.vnApp, 'showSuccess'); - - $httpBackend.expectPOST(`InvoiceOuts/${ticket.invoiceOut.id}/regenerate`).respond(); - controller.regenerateInvoice(); - $httpBackend.flush(); - - expect(controller.vnApp.showSuccess).toHaveBeenCalled(); - }); - }); - - describe('changeShipped()', () => { - it('should make a query and change the shipped hour if the response is accept', () => { - jest.spyOn(controller.vnApp, 'showSuccess'); - jest.spyOn(controller, 'cardReload'); - - $httpBackend.expectPOST(`Tickets/${ticket.id}/updateEditableTicket`).respond(); - controller.changeShipped(); - $httpBackend.flush(); - - expect(controller.vnApp.showSuccess).toHaveBeenCalled(); - expect(controller.cardReload).toHaveBeenCalled(); - }); - }); - - describe('canStowaway()', () => { - it('should make a query and return if the ticket can be stowawayed', () => { - controller.canStowaway(); - $httpBackend.flush(); - - expect(controller.canShowStowaway).toBeTruthy(); - }); - - it('should not make a query if is not on the ticket module', () => { - $state.getCurrentPath = () => [null, {state: {name: 'client'}}]; - controller.canStowaway(); - - expect(controller.canShowStowaway).toBeFalsy(); - }); - }); - - describe('recalculateComponents()', () => { - it('should make a query and show a success message', () => { - jest.spyOn(controller.vnApp, 'showSuccess'); - - $httpBackend.expectPOST(`Tickets/${ticket.id}/recalculateComponents`).respond(); - controller.recalculateComponents(); - $httpBackend.flush(); - - expect(controller.vnApp.showSuccess).toHaveBeenCalled(); - }); - }); - - describe('showSMSDialog()', () => { - it('should set the destionationFk and destination properties and then call the sms open() method', () => { - controller.$.sms = {open: () => {}}; - jest.spyOn(controller.$.sms, 'open'); - - controller.showSMSDialog(); - - expect(controller.$.sms.open).toHaveBeenCalledWith(); - expect(controller.newSMS).toEqual({ - destinationFk: ticket.clientFk, - destination: ticket.address.mobile - }); - }); - }); - describe('loadData()', () => { it(`should perform a get query to store the ticket data into the controller`, () => { - $httpBackend.when('GET', `Tickets/${ticket.id}/isEditable`).respond(); - $httpBackend.expectRoute('GET', `Tickets/${ticket.id}`).respond(); + // $httpBackend.when('GET', `Tickets/${ticket.id}/isEditable`).respond(); + $httpBackend.expect('GET', `Tickets/${ticket.id}`).respond(); controller.loadData(); $httpBackend.flush(); }); diff --git a/modules/ticket/front/sms/index.spec.js b/modules/ticket/front/sms/index.spec.js index e918ffb54..627643398 100644 --- a/modules/ticket/front/sms/index.spec.js +++ b/modules/ticket/front/sms/index.spec.js @@ -12,7 +12,6 @@ describe('Ticket', () => { let $scope = $rootScope.$new(); const $element = angular.element(''); controller = $componentController('vnTicketSms', {$element, $scope}); - controller.$params = {id: 11}; controller.$.message = { input: { value: 'My SMS' @@ -22,8 +21,8 @@ describe('Ticket', () => { describe('onResponse()', () => { it('should perform a POST query and show a success snackbar', () => { - let params = {destinationFk: 101, destination: 111111111, message: 'My SMS'}; - controller.sms = {destinationFk: 101, destination: 111111111, message: 'My SMS'}; + let params = {ticketId: 11, destinationFk: 101, destination: 111111111, message: 'My SMS'}; + controller.sms = {ticketId: 11, destinationFk: 101, destination: 111111111, message: 'My SMS'}; jest.spyOn(controller.vnApp, 'showMessage'); $httpBackend.expect('POST', `Tickets/11/sendSms`, params).respond(200, params); diff --git a/modules/ticket/front/summary/index.html b/modules/ticket/front/summary/index.html index 577158489..2961644ad 100644 --- a/modules/ticket/front/summary/index.html +++ b/modules/ticket/front/summary/index.html @@ -18,7 +18,7 @@ ng-click="$ctrl.setOkState()" vn-tooltip="Change ticket state to 'Ok'"> - +