diff --git a/e2e/helpers/selectors.js b/e2e/helpers/selectors.js index 6db429ff0..af4a7e672 100644 --- a/e2e/helpers/selectors.js +++ b/e2e/helpers/selectors.js @@ -580,6 +580,10 @@ export default { searchButton: 'vn-order-index vn-searchbar vn-icon[icon="search"]', createOrderButton: `${components.vnFloatButton}`, }, + orderDescriptor: { + returnToModuleIndexButton: 'vn-order-descriptor a[ui-sref="order.index"]', + acceptNavigationButton: 'vn-order-basic-data vn-confirm button[response=ACCEPT]' + }, createOrderView: { clientAutocomplete: 'vn-autocomplete[label="Client"]', addressAutocomplete: 'vn-autocomplete[label="Address"]', diff --git a/e2e/paths/07-order-module/01_edit_basic_data.spec.js b/e2e/paths/07-order-module/01_edit_basic_data.spec.js index 973697e9c..8c54bd06a 100644 --- a/e2e/paths/07-order-module/01_edit_basic_data.spec.js +++ b/e2e/paths/07-order-module/01_edit_basic_data.spec.js @@ -28,8 +28,8 @@ describe('Order edit basic data path', () => { it('should now navigate to order index', async() => { const orderId = 16; const url = await nightmare - .waitToClick(selectors.globalItems.returnToModuleIndexButton) - .waitToClick(selectors.globalItems.acceptButton) + .waitToClick(selectors.orderDescriptor.returnToModuleIndexButton) + .waitToClick(selectors.orderDescriptor.acceptNavigationButton) .wait(selectors.ordersIndex.createOrderButton) .accessToSearchResult(orderId) .accessToSection('order.card.basicData') diff --git a/loopback/locale/en.json b/loopback/locale/en.json index c4a1526aa..cfcb5bbdb 100644 --- a/loopback/locale/en.json +++ b/loopback/locale/en.json @@ -52,5 +52,6 @@ "Concept cannot be blank": "Concept cannot be blank", "Ticket id cannot be blank": "Ticket id cannot be blank", "Weekday cannot be blank": "Weekday cannot be blank", - "This ticket can not be modified": "This ticket can not be modified" + "This ticket can not be modified": "This ticket can not be modified", + "You can't delete a confirmed order": "You can't delete a confirmed order" } \ No newline at end of file diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 1b91254e7..dfa637260 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -100,5 +100,6 @@ "You don't have privileges to change the zone": "No tienes permisos para cambiar la zona", "This ticket is already on weekly tickets": "Este ticket ya está en tickets programados", "Ticket id cannot be blank": "El id de ticket no puede quedar en blanco", - "Weekday cannot be blank": "El día de la semana no puede quedar en blanco" + "Weekday cannot be blank": "El día de la semana no puede quedar en blanco", + "You can't delete a confirmed order": "No puedes borrar un pedido confirmado" } \ No newline at end of file diff --git a/modules/order/back/models/order.js b/modules/order/back/models/order.js index b2f9da85e..330019941 100644 --- a/modules/order/back/models/order.js +++ b/modules/order/back/models/order.js @@ -1,3 +1,5 @@ +const UserError = require('vn-loopback/util/user-error'); + module.exports = Self => { require('../methods/order/new')(Self); require('../methods/order/getTotalVolume')(Self); @@ -14,4 +16,11 @@ module.exports = Self => { require('../methods/order/confirm')(Self); require('../methods/order/filter')(Self); require('../methods/order/getItemTypeAvailable')(Self); + + Self.beforeRemote('deleteById', async function(ctx) { + const targetOrder = await Self.findById(ctx.args.id); + + if (targetOrder.isConfirmed === 1) + throw new UserError(`You can't delete a confirmed order`); + }); }; diff --git a/modules/order/front/descriptor/index.html b/modules/order/front/descriptor/index.html index 9ea8d01fb..dcc9b16de 100644 --- a/modules/order/front/descriptor/index.html +++ b/modules/order/front/descriptor/index.html @@ -6,7 +6,15 @@ -
+ +
@@ -17,7 +25,7 @@ value="{{$ctrl.order.client.name}}"> + value="{{$ctrl.order.isConfirmed ? $ctrl.$translate.instant('Confirmed') : $ctrl.$translate.instant('Not confirmed')}}"> @@ -66,3 +74,9 @@
+ + \ No newline at end of file diff --git a/modules/order/front/descriptor/index.js b/modules/order/front/descriptor/index.js index 26fedd6a3..02bc185e2 100644 --- a/modules/order/front/descriptor/index.js +++ b/modules/order/front/descriptor/index.js @@ -2,8 +2,15 @@ import ngModule from '../module'; import './style.scss'; class Controller { - constructor($translate) { - this.translate = $translate; + constructor($translate, $scope, vnApp, $http, $state) { + this.$state = $state; + this.$scope = $scope; + this.vnApp = vnApp; + this.$http = $http; + this.$translate = $translate; + this.moreOptions = [ + {name: 'Delete order', callback: () => this.showDeleteOrderDialog()} + ]; } set order(value) { @@ -36,9 +43,23 @@ class Controller { get quicklinks() { return this._quicklinks; } + + deleteOrder(response) { + if (response === 'ACCEPT') { + const params = {id: this.order.id}; + this.$http.delete(`/api/Orders/${params.id}`).then(() => { + this.$state.go('order.index'); + this.vnApp.showSuccess(this.$translate.instant('Order deleted')); + }); + } + } + + showDeleteOrderDialog() { + this.$scope.deleteOrderConfirmation.show(); + } } -Controller.$inject = ['$translate']; +Controller.$inject = ['$translate', '$scope', 'vnApp', '$http', '$state']; ngModule.component('vnOrderDescriptor', { template: require('./index.html'), diff --git a/modules/order/front/descriptor/index.spec.js b/modules/order/front/descriptor/index.spec.js new file mode 100644 index 000000000..66ab7e9af --- /dev/null +++ b/modules/order/front/descriptor/index.spec.js @@ -0,0 +1,44 @@ +import './index.js'; + +describe('Order Component vnOrderDescriptor', () => { + let $httpBackend; + let controller; + + beforeEach(() => { + ngModule('order'); + }); + + beforeEach(angular.mock.inject(($componentController, _$httpBackend_) => { + $httpBackend = _$httpBackend_; + controller = $componentController('vnOrderDescriptor'); + controller.order = {id: 1}; + })); + + describe('deleteOrder()', () => { + it(`should do nothing if the response isn't ACCEPT`, () => { + let response = 'WAGH!'; + + spyOn(controller.vnApp, 'showSuccess'); + spyOn(controller.$state, 'go'); + controller.deleteOrder(response); + + expect(controller.vnApp.showSuccess).not.toHaveBeenCalledWith('Order deleted!'); + expect(controller.$state.go).not.toHaveBeenCalledWith('order.index'); + }); + + it(`should perform a DELETE query if the response was ACCEPT`, () => { + let response = 'ACCEPT'; + + spyOn(controller.vnApp, 'showSuccess'); + spyOn(controller.$state, 'go'); + $httpBackend.when('DELETE', `/api/Orders/${controller.order.id}`).respond(200); + $httpBackend.expect('DELETE', `/api/Orders/${controller.order.id}`); + controller.deleteOrder(response); + $httpBackend.flush(); + + expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Order deleted'); + expect(controller.$state.go).toHaveBeenCalledWith('order.index'); + }); + }); +}); + diff --git a/modules/order/front/descriptor/locale/es.yml b/modules/order/front/descriptor/locale/es.yml index 88a0c1dda..21f3051ca 100644 --- a/modules/order/front/descriptor/locale/es.yml +++ b/modules/order/front/descriptor/locale/es.yml @@ -6,4 +6,7 @@ Landed: F. entrega Items: Articulos Agency: Agencia Sales person: Comercial -Order ticket list: Ticket del pedido \ No newline at end of file +Order ticket list: Ticket del pedido +Delete order: Borrar pedido +You are going to delete this order: El pedido se borrará +continue anyway?: ¿Continuar de todos modos? \ No newline at end of file