diff --git a/client/ticket/src/volume/ticket-volume.js b/client/ticket/src/volume/ticket-volume.js new file mode 100644 index 000000000..4f89e282b --- /dev/null +++ b/client/ticket/src/volume/ticket-volume.js @@ -0,0 +1,147 @@ +import ngModule from '../module'; + +class Controller { + constructor($stateParams, $scope, $http, $translate, vnApp) { + this.params = $stateParams; + this.$scope = $scope; + this.$http = $http; + this.$translate = $translate; + this.vnApp = vnApp; + + this.ticketVolumes = []; + this.oldVolumes = {}; + this.removedVolumes = []; + } + + _getTicketVolumes(ticketFk) { + this.$http.get(`/tcket/api/Volumes/${ticketFk}/getVolumes`) + .then(response => { + this.ticketVolumes = response.data; + }); + } + + // _setIconAdd() { + // if (this.ticketVolumes.length) { + // this.ticketVolumes.map(element => { + // element.showAddIcon = false; + // return true; + // }); + // this.ticketVolumes[this.ticketVolumes.length - 1].showAddIcon = true; + // } + // } + + // _setDirtyForm() { + // if (this.$scope.form) { + // this.$scope.form.$setDirty(); + // } + // } + + // _unsetDirtyForm() { + // if (this.$scope.form) { + // this.$scope.form.$setPristine(); + // } + // } + + // addVolume() { + // this.ticketVolumes.push({description: null, ticketFk: this.params.id, showAddIcon: true}); + // this._setIconAdd(); + // } + + // removeVolume(index) { + // let item = this.ticketVolumes[index]; + // if (item) { + // this.ticketVolumes.splice(index, 1); + // this._setIconAdd(); + // if (item.id) { + // this.removedVolumes.push(item.id); + // this._setDirtyForm(); + // } + // } + // } + + // _equalVolumes(oldVolume, newVolume) { + // return oldVolume.id === newVolume.id && oldVolume.VolumeTypeFk === newVolume.VolumeTypeFk && oldVolume.description === newVolume.description; + // } + + // setOldVolumes(response) { + // this._setIconAdd(); + // response.data.forEach(Volume => { + // this.oldVolumes[Volume.id] = Object.assign({}, Volume); + // }); + // } + + // getVolumes() { + // let filter = { + // where: {ticketFk: this.params.id}, + // include: ['VolumeType'] + // }; + + // this.$http.get(`/ticket/api/TicketVolumes?filter=${JSON.stringify(filter)}`).then(response => { + // this.ticketVolumes = response.data; + // this.setOldVolumes(response); + // }); + // } + + // submit() { + // let typesDefined = []; + // let repeatedType = false; + // let canSubmit; + // let VolumesObj = { + // delete: this.removedVolumes, + // create: [], + // update: [] + // }; + + // this.ticketVolumes.forEach(Volume => { + // let isNewVolume = !Volume.id; + + // delete Volume.showAddIcon; + + // if (typesDefined.indexOf(Volume.VolumeTypeFk) !== -1) { + // repeatedType = true; + // return; + // } + // typesDefined.push(Volume.VolumeTypeFk); + + // if (isNewVolume && Volume.description && Volume.VolumeTypeFk) { + // VolumesObj.create.push(Volume); + // } + + // if (!isNewVolume && !this._equalVolumes(this.oldVolumes[Volume.id], Volume)) { + // VolumesObj.update.push(Volume); + // } + // }); + + // if (this.$scope.form.$invalid) { + // return this.vnApp.showMessage(this.$translate.instant('Some fields are invalid')); + // } + + // if (repeatedType) { + // return this.vnApp.showMessage(this.$translate.instant('The Volume type must be unique')); + // } + + // canSubmit = VolumesObj.update.length > 0 || VolumesObj.create.length > 0 || VolumesObj.delete.length > 0; + + // if (canSubmit) { + // return this.$http.post(`/ticket/api/TicketVolumes/crudTicketVolumes`, VolumesObj).then(() => { + // this.getVolumes(); + // this._unsetDirtyForm(); + // }); + // } + // this.vnApp.showMessage(this.$translate.instant('No changes to save')); + // } + + $onInit() { + // this.getVolumes(); + } +} + +Controller.$inject = ['$stateParams', '$scope', '$http', '$translate', 'vnApp']; + +ngModule.component('vnTicketVolume', { + template: require('./ticket-volume.html'), + controller: Controller, + bindings: { + ticket: '<' + } +}); diff --git a/client/ticket/src/volume/ticket-volume.spec.js b/client/ticket/src/volume/ticket-volume.spec.js new file mode 100644 index 000000000..66bff3566 --- /dev/null +++ b/client/ticket/src/volume/ticket-volume.spec.js @@ -0,0 +1,143 @@ +// import './ticket-observations.js'; + +// describe('ticket', () => { +// describe('Component vnTicketObservations', () => { +// let $componentController; +// let $state; +// let controller; +// let $httpBackend; + +// beforeEach(() => { +// angular.mock.module('ticket'); +// }); + +// beforeEach(angular.mock.inject((_$componentController_, _$state_, _$httpBackend_) => { +// $componentController = _$componentController_; +// $state = _$state_; +// $httpBackend = _$httpBackend_; +// controller = $componentController('vnTicketObservations', {$state: $state}); +// })); + +// describe('add / remove observation', () => { +// it('should add one empty observation into controller observations collection and call _setIconAdd()', () => { +// controller.ticketObservations = []; +// spyOn(controller, '_setIconAdd').and.callThrough(); +// controller.addObservation(); + +// expect(controller._setIconAdd).toHaveBeenCalledWith(); +// expect(controller.ticketObservations.length).toEqual(1); +// expect(controller.ticketObservations[0].id).toBe(undefined); +// expect(controller.ticketObservations[0].showAddIcon).toBeTruthy(); +// }); + +// it('should remove an observation that occupies the position in the index given and call _setIconAdd()', () => { +// let index = 2; +// controller.ticketObservations = [ +// {id: 1, observationTypeFk: 1, description: 'one', showAddIcon: false}, +// {id: 2, observationTypeFk: 2, description: 'two', showAddIcon: false}, +// {id: 3, observationTypeFk: 3, description: 'three', showAddIcon: true} +// ]; + +// spyOn(controller, '_setIconAdd').and.callThrough(); + +// controller.removeObservation(index); + +// expect(controller._setIconAdd).toHaveBeenCalledWith(); +// expect(controller.ticketObservations.length).toEqual(2); +// expect(controller.ticketObservations[0].showAddIcon).toBeFalsy(); +// expect(controller.ticketObservations[1].showAddIcon).toBeTruthy(); +// expect(controller.ticketObservations[index]).toBe(undefined); +// }); +// }); + +// describe('_equalObservations()', () => { +// it('should return true if two observations are equals independent of control attributes', () => { +// let observationOne = {id: 1, observationTypeFk: 1, description: 'one', showAddIcon: true}; +// let observationTwo = {id: 1, observationTypeFk: 1, description: 'one', showAddIcon: false}; +// let equals = controller._equalObservations(observationOne, observationTwo); + +// expect(equals).toBeTruthy(); +// }); + +// it('should return false if two observations aint equals independent of control attributes', () => { +// let observationOne = {id: 1, observationTypeFk: 1, description: 'one', showAddIcon: true}; +// let observationTwo = {id: 1, observationTypeFk: 1, description: 'two', showAddIcon: true}; +// let equals = controller._equalObservations(observationOne, observationTwo); + +// expect(equals).toBeFalsy(); +// }); +// }); + +// describe('get Observations()', () => { +// it('should perform a GET query to receive the ticket observations', () => { +// let res = [{id: 1, observationTypeFk: 1, description: 'one'}]; + +// $httpBackend.whenGET(`/ticket/api/TicketObservations?filter={"where":{},"include":["observationType"]}`).respond(res); +// $httpBackend.expectGET(`/ticket/api/TicketObservations?filter={"where":{},"include":["observationType"]}`); +// controller.getObservations(); +// $httpBackend.flush(); +// }); +// }); + +// describe('submit()', () => { +// it("should return an error message 'The observation type must be unique'", () => { +// controller.$scope.form = {}; +// spyOn(controller.vnApp, 'showMessage').and.callThrough(); +// controller.ticketObservations = [ +// {id: 1, observationTypeFk: 1, description: 'one', itemFk: 1}, +// {observationTypeFk: 1, description: 'one', itemFk: 1} +// ]; +// controller.oldObservations = {1: {id: 1, observationTypeFk: 1, description: 'one', itemFk: 1}}; +// controller.submit(); + +// expect(controller.vnApp.showMessage).toHaveBeenCalledWith('The observation type must be unique'); +// }); + +// it("should perfom a query to delete observations", () => { +// controller.$scope.form = {$setPristine: () => {}}; +// controller.oldObservations = {1: {id: 1, observationTypeFk: 1, description: 'one'}}; +// controller.ticketObservations = []; +// controller.removedObservations = [1]; + +// $httpBackend.whenGET(`/ticket/api/TicketObservations?filter={"where":{},"include":["observationType"]}`).respond([]); +// $httpBackend.expectPOST(`/ticket/api/TicketObservations/crudTicketObservations`).respond('ok!'); +// controller.submit(); +// $httpBackend.flush(); +// }); + +// it("should perfom a query to update observations", () => { +// controller.$scope.form = {$setPristine: () => {}}; +// controller.ticketObservations = [{id: 1, observationTypeFk: 1, description: 'number one!'}]; +// controller.oldObservations = {1: {id: 1, observationTypeFk: 1, description: 'one'}}; + +// $httpBackend.whenGET(`/ticket/api/TicketObservations?filter={"where":{},"include":["observationType"]}`).respond([]); +// $httpBackend.expectPOST(`/ticket/api/TicketObservations/crudTicketObservations`).respond('ok!'); +// controller.submit(); +// $httpBackend.flush(); +// }); + +// it("should perfom a query to create new observation", () => { +// controller.$scope.form = {$setPristine: () => {}}; +// controller.ticketObservations = [{observationTypeFk: 2, description: 'two'}]; + +// $httpBackend.whenGET(`/ticket/api/TicketObservations?filter={"where":{},"include":["observationType"]}`).respond([]); +// $httpBackend.expectPOST(`/ticket/api/TicketObservations/crudTicketObservations`).respond('ok!'); +// controller.submit(); +// $httpBackend.flush(); +// }); + +// it("should return a message 'No changes to save' when there are no changes to apply", () => { +// controller.$scope.form = {$setPristine: () => {}}; +// spyOn(controller.vnApp, 'showMessage').and.callThrough(); +// controller.oldObservations = [ +// {id: 1, observationTypeFk: 1, description: 'one', showAddIcon: false}, +// {id: 2, observationTypeFk: 2, description: 'two', showAddIcon: true} +// ]; +// controller.ticketObservations = []; +// controller.submit(); + +// expect(controller.vnApp.showMessage).toHaveBeenCalledWith('No changes to save'); +// }); +// }); +// }); +// }); diff --git a/services/loopback/common/methods/ticket/get-volume.js b/services/loopback/common/methods/ticket/get-volume.js new file mode 100644 index 000000000..9eb5d3943 --- /dev/null +++ b/services/loopback/common/methods/ticket/get-volume.js @@ -0,0 +1,26 @@ +module.exports = Self => { + Self.remoteMethod('getVolumes', { + description: 'Returns the volumes of a ticket', + accessType: 'READ', + accepts: [{ + arg: 'id', + type: 'number', + required: true, + description: 'ticket id', + http: {source: 'path'} + }], + returns: { + type: 'object' + }, + http: { + path: `/:id/getVolumes`, + verb: 'GET' + } + }); + + Self.getVolumes = async ticketFk => { + let query = `SELECT vn.ticketVolume(?) AS ticketVolumes`; + let response = await Self.rawSql(query, [ticketFk]); + return response[0]; + }; +}; diff --git a/services/loopback/common/methods/ticket/specs/get-volume.spec.js b/services/loopback/common/methods/ticket/specs/get-volume.spec.js new file mode 100644 index 000000000..9998414e9 --- /dev/null +++ b/services/loopback/common/methods/ticket/specs/get-volume.spec.js @@ -0,0 +1,16 @@ +// const getDebt = require('../getDebt'); + +// describe('client getDebt()', () => { +// it('should call the getDebt method', done => { +// let clientFk = 109; +// let self = jasmine.createSpyObj('self', ['remoteMethod', 'rawSql']); +// self.rawSql.and.returnValue(Promise.resolve([{debt: 100}])); +// getDebt(self); +// self.getDebt(clientFk) +// .then(result => { +// expect(result.debt).toEqual(100); +// done(); +// }); +// }); +// }); +