From 28b7f5d3a2e32065c196616e09d245537dce9818 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Fri, 28 Feb 2020 14:18:55 +0100 Subject: [PATCH] 2151 - Added thermograph edit section --- back/methods/dms/updateFile.js | 21 ++-- e2e/helpers/selectors.js | 4 +- modules/client/front/dms/edit/index.html | 11 ++- modules/client/front/dms/index/index.html | 5 - modules/ticket/front/dms/index/index.html | 5 - .../back/methods/travel/createThermograph.js | 1 - .../back/methods/travel/updateThermograph.js | 83 ++++++++++++++++ modules/travel/back/models/travel.js | 1 + modules/travel/front/routes.json | 2 +- .../travel/front/thermograph/edit/index.html | 21 ++-- .../travel/front/thermograph/edit/index.js | 40 +++++--- .../front/thermograph/edit/index.spec.js | 98 +++++++++++++------ .../travel/front/thermograph/index/index.html | 12 +-- modules/worker/front/dms/index/index.html | 5 - 14 files changed, 219 insertions(+), 90 deletions(-) create mode 100644 modules/travel/back/methods/travel/updateThermograph.js diff --git a/back/methods/dms/updateFile.js b/back/methods/dms/updateFile.js index dff45f640b..7585dd1b08 100644 --- a/back/methods/dms/updateFile.js +++ b/back/methods/dms/updateFile.js @@ -10,8 +10,7 @@ module.exports = Self => { type: 'Number', description: 'The document id', http: {source: 'path'} - }, - { + }, { arg: 'warehouseId', type: 'Number', description: 'The warehouse id' @@ -44,9 +43,9 @@ module.exports = Self => { } }); - Self.updateFile = async(ctx, id, warehouseId, companyId, - dmsTypeId, reference, description, hasFileAttached, options) => { + Self.updateFile = async(ctx, id, options) => { const models = Self.app.models; + const args = ctx.args; let tx; let myOptions = {}; @@ -60,20 +59,20 @@ module.exports = Self => { } try { - const hasWriteRole = await models.DmsType.hasWriteRole(ctx, dmsTypeId); + const hasWriteRole = await models.DmsType.hasWriteRole(ctx, args.dmsTypeId); if (!hasWriteRole) throw new UserError(`You don't have enough privileges`); const dms = await Self.findById(id, null, myOptions); await dms.updateAttributes({ - dmsTypeFk: dmsTypeId, - companyFk: companyId, - warehouseFk: warehouseId, - reference: reference, - description: description + dmsTypeFk: args.dmsTypeId, + companyFk: args.companyId, + warehouseFk: args.warehouseId, + reference: args.reference, + description: args.description }, myOptions); - if (hasFileAttached) + if (args.hasFileAttached) await uploadNewFile(ctx, dms, myOptions); if (tx) await tx.commit(); diff --git a/e2e/helpers/selectors.js b/e2e/helpers/selectors.js index 7e3e726e42..4480759f40 100644 --- a/e2e/helpers/selectors.js +++ b/e2e/helpers/selectors.js @@ -192,7 +192,7 @@ export default { }, dms: { deleteFileButton: 'vn-client-dms-index vn-tr:nth-child(1) vn-icon-button[icon="delete"]', - firstDocWorker: 'vn-client-dms-index vn-td:nth-child(8) > span', + firstDocWorker: 'vn-client-dms-index vn-td:nth-child(7) > span', firstDocWorkerDescriptor: '.vn-popover.shown vn-worker-descriptor', acceptDeleteButton: '.vn-confirm.shown button[response="accept"]' }, @@ -786,7 +786,7 @@ export default { travelThermograph: { add: 'vn-travel-thermograph-index vn-float-button[icon="add"]', thermographID: 'vn-travel-thermograph-create vn-autocomplete[ng-model="$ctrl.dms.thermographId"]', - uploadIcon: 'vn-travel-thermograph-create vn-icon[icon="cloud_upload"]', + uploadIcon: 'vn-travel-thermograph-create vn-icon[icon="attach_file"]', createdThermograph: 'vn-travel-thermograph-index vn-tbody > vn-tr', upload: 'vn-travel-thermograph-create button[type=submit]' }, diff --git a/modules/client/front/dms/edit/index.html b/modules/client/front/dms/edit/index.html index dbc2e0ed1f..87d69fdcde 100644 --- a/modules/client/front/dms/edit/index.html +++ b/modules/client/front/dms/edit/index.html @@ -56,7 +56,16 @@ label="File" ng-model="$ctrl.dms.files" on-change="$ctrl.onFileChange($files)" - accept=".pdf, .png, .jpg, .jpeg, application/zip, application/rar, application/x-7z-compressed"> + accept="{{$ctrl.allowedContentTypes}}" + required="true" + multiple="true"> + + + + diff --git a/modules/client/front/dms/index/index.html b/modules/client/front/dms/index/index.html index 9ecafe887e..78bc45b574 100644 --- a/modules/client/front/dms/index/index.html +++ b/modules/client/front/dms/index/index.html @@ -53,11 +53,6 @@ {{::document.dms.description}} - - - - - - - - { try { const options = {transaction: tx}; - const travelThermograph = await models.TravelThermograph.findOne({ where: { thermographFk: thermographId, diff --git a/modules/travel/back/methods/travel/updateThermograph.js b/modules/travel/back/methods/travel/updateThermograph.js new file mode 100644 index 0000000000..efad606eb1 --- /dev/null +++ b/modules/travel/back/methods/travel/updateThermograph.js @@ -0,0 +1,83 @@ +const UserError = require('vn-loopback/util/user-error'); + +module.exports = Self => { + Self.remoteMethodCtx('updateThermograph', { + description: 'updates a file properties or file', + accessType: 'WRITE', + accepts: [{ + arg: 'id', + type: 'Number', + description: 'The travel id', + http: {source: 'path'} + }, { + arg: 'thermographId', + type: 'String', + description: 'The thermograph id', + required: true + }, { + arg: 'state', + type: 'String', + required: true + }, { + arg: 'warehouseId', + type: 'Number', + description: 'The warehouse id' + }, { + arg: 'companyId', + type: 'Number', + description: 'The company id' + }, { + arg: 'dmsTypeId', + type: 'Number', + description: 'The dms type id' + }, { + arg: 'reference', + type: 'String' + }, { + arg: 'description', + type: 'String' + }, { + arg: 'hasFileAttached', + type: 'Boolean', + description: 'True if has an attached file' + }], + returns: { + type: 'Object', + root: true + }, + http: { + path: `/:id/updateThermograph`, + verb: 'POST' + } + }); + + Self.updateThermograph = async(ctx, id, thermographId, state) => { + const models = Self.app.models; + const tx = await Self.beginTransaction({}); + + try { + const options = {transaction: tx}; + const travelThermograph = await models.TravelThermograph.findOne({ + where: { + thermographFk: thermographId, + travelFk: id + } + }, options); + + if (!travelThermograph) + throw new UserError('No valid travel thermograph found'); + + const dmsFk = travelThermograph.dmsFk; + await models.Dms.updateFile(ctx, dmsFk, options); + await travelThermograph.updateAttributes({ + result: state + }, options); + + await tx.commit(); + return travelThermograph; + } catch (e) { + await tx.rollback(); + throw e; + } + }; +}; diff --git a/modules/travel/back/models/travel.js b/modules/travel/back/models/travel.js index 895de7af19..4643f79fd5 100644 --- a/modules/travel/back/models/travel.js +++ b/modules/travel/back/models/travel.js @@ -4,4 +4,5 @@ module.exports = Self => { require('../methods/travel/filter')(Self); require('../methods/travel/createThermograph')(Self); require('../methods/travel/deleteThermograph')(Self); + require('../methods/travel/updateThermograph')(Self); }; diff --git a/modules/travel/front/routes.json b/modules/travel/front/routes.json index 8e03705126..50e2368891 100644 --- a/modules/travel/front/routes.json +++ b/modules/travel/front/routes.json @@ -82,7 +82,7 @@ }, "acl": ["buyer"] }, { - "url" : "/:dmsId/edit", + "url" : "/:thermographId/edit", "state": "travel.card.thermograph.edit", "component": "vn-travel-thermograph-edit", "description": "Edit thermograph", diff --git a/modules/travel/front/thermograph/edit/index.html b/modules/travel/front/thermograph/edit/index.html index 7dc122b88f..3fe448b56c 100644 --- a/modules/travel/front/thermograph/edit/index.html +++ b/modules/travel/front/thermograph/edit/index.html @@ -12,27 +12,27 @@ + value-field="thermographFk" + disabled="true"> @@ -41,14 +41,14 @@ @@ -57,7 +57,7 @@ @@ -65,7 +65,8 @@ diff --git a/modules/travel/front/thermograph/edit/index.js b/modules/travel/front/thermograph/edit/index.js index 98c5fbbb5d..0180983127 100644 --- a/modules/travel/front/thermograph/edit/index.js +++ b/modules/travel/front/thermograph/edit/index.js @@ -30,16 +30,20 @@ class Controller extends Component { } setDefaultParams() { - const path = `Dms/${this.$params.dmsId}`; + const filterObj = {include: {relation: 'dms'}}; + const filter = encodeURIComponent(JSON.stringify(filterObj)); + const path = `TravelThermographs/${this.$params.thermographId}?filter=${filter}`; this.$http.get(path).then(res => { - const dms = res.data && res.data; - this.dms = { - reference: dms.reference, - warehouseId: dms.warehouseFk, - companyId: dms.companyFk, - dmsTypeId: dms.dmsTypeFk, - description: dms.description, - hasFile: dms.hasFile, + const thermograph = res.data && res.data; + this.thermograph = { + thermographId: thermograph.thermographFk, + state: thermograph.result, + reference: thermograph.dms.reference, + warehouseId: thermograph.dms.warehouseFk, + companyId: thermograph.dms.companyFk, + dmsTypeId: thermograph.dms.dmsTypeFk, + description: thermograph.dms.description, + hasFile: thermograph.dms.hasFile, hasFileAttached: false, files: [] }; @@ -47,11 +51,11 @@ class Controller extends Component { } onSubmit() { - const query = `dms/${this.$params.dmsId}/updateFile`; + const query = `travels/${this.$params.id}/updateThermograph`; const options = { method: 'POST', url: query, - params: this.dms, + params: this.thermograph, headers: { 'Content-Type': undefined }, @@ -63,16 +67,26 @@ class Controller extends Component { return formData; }, - data: this.dms.files + data: this.thermograph.files }; this.$http(options).then(res => { if (res) { this.vnApp.showSuccess(this.$translate.instant('Data saved!')); this.$.watcher.updateOriginalData(); - this.$state.go('worker.card.dms.index'); + this.$state.go('travel.card.thermograph.index'); } }); } + + onFileChange(files) { + let hasFileAttached = false; + if (files.length > 0) + hasFileAttached = true; + + this.$.$applyAsync(() => { + this.thermograph.hasFileAttached = hasFileAttached; + }); + } } ngModule.component('vnTravelThermographEdit', { diff --git a/modules/travel/front/thermograph/edit/index.spec.js b/modules/travel/front/thermograph/edit/index.spec.js index 7dfad9643f..eac92ba0fa 100644 --- a/modules/travel/front/thermograph/edit/index.spec.js +++ b/modules/travel/front/thermograph/edit/index.spec.js @@ -1,78 +1,87 @@ import './index'; +import watcher from 'core/mocks/watcher.js'; describe('Worker', () => { - describe('Component vnClientDmsEdit', () => { + describe('Component vnTravelThermographEdit', () => { let controller; let $scope; let $element; let $httpBackend; + let $httpParamSerializer; - beforeEach(ngModule('worker')); + beforeEach(ngModule('travel')); - beforeEach(angular.mock.inject(($componentController, $rootScope, _$httpBackend_) => { + beforeEach(angular.mock.inject(($componentController, $rootScope, _$httpBackend_, _$httpParamSerializer_) => { $scope = $rootScope.$new(); $httpBackend = _$httpBackend_; - $element = angular.element(` { - it('should set the worker data and then call setDefaultParams() and getAllowedContentTypes()', () => { + describe('travel() setter', () => { + it('should set the travel data and then call setDefaultParams() and getAllowedContentTypes()', () => { jest.spyOn(controller, 'setDefaultParams'); jest.spyOn(controller, 'getAllowedContentTypes'); - controller._worker = undefined; - controller.worker = { - id: 106 + controller._travel = undefined; + controller.travel = { + id: 3 }; expect(controller.setDefaultParams).toHaveBeenCalledWith(); - expect(controller.worker).toBeDefined(); + expect(controller.travel).toBeDefined(); expect(controller.getAllowedContentTypes).toHaveBeenCalledWith(); }); }); describe('setDefaultParams()', () => { it('should perform a GET query and define the dms property on controller', () => { - const dmsId = 4; + const thermographId = 6; const expectedResponse = { - reference: 101, - warehouseFk: 1, - companyFk: 442, - dmsTypeFk: 3, - description: 'Test', - hasFile: false, - hasFileAttached: false + thermographFk: 6, + result: 'Ok', + dms: { + reference: '123456-01', + warehouseFk: 1, + companyFk: 442, + dmsTypeFk: 3, + description: 'Test' + } }; - $httpBackend.when('GET', `Dms/${dmsId}`).respond(expectedResponse); - $httpBackend.expect('GET', `Dms/${dmsId}`).respond(expectedResponse); + const filterObj = {include: {relation: 'dms'}}; + const filter = encodeURIComponent(JSON.stringify(filterObj)); + const query = `TravelThermographs/${thermographId}?filter=${filter}`; + $httpBackend.expect('GET', query).respond(expectedResponse); controller.setDefaultParams(); $httpBackend.flush(); - expect(controller.dms).toBeDefined(); - expect(controller.dms.reference).toEqual(101); - expect(controller.dms.dmsTypeId).toEqual(3); + expect(controller.thermograph).toBeDefined(); + expect(controller.thermograph.reference).toEqual('123456-01'); + expect(controller.thermograph.dmsTypeId).toEqual(3); + expect(controller.thermograph.state).toEqual('Ok'); }); }); describe('onFileChange()', () => { it('should set dms hasFileAttached property to true if has any files', () => { const files = [{id: 1, name: 'MyFile'}]; - controller.dms = {hasFileAttached: false}; + controller.thermograph = {hasFileAttached: false}; controller.onFileChange(files); $scope.$apply(); - expect(controller.dms.hasFileAttached).toBeTruthy(); + expect(controller.thermograph.hasFileAttached).toBeTruthy(); }); }); describe('getAllowedContentTypes()', () => { it('should make an HTTP GET request to get the allowed content types', () => { const expectedResponse = ['image/png', 'image/jpg']; - $httpBackend.when('GET', `WorkerDms/allowedContentTypes`).respond(expectedResponse); - $httpBackend.expect('GET', `WorkerDms/allowedContentTypes`); + $httpBackend.when('GET', `TravelThermographs/allowedContentTypes`).respond(expectedResponse); + $httpBackend.expect('GET', `TravelThermographs/allowedContentTypes`); controller.getAllowedContentTypes(); $httpBackend.flush(); @@ -80,5 +89,34 @@ describe('Worker', () => { expect(controller.allowedContentTypes).toEqual('image/png, image/jpg'); }); }); + + describe('contentTypesInfo()', () => { + it('should return a description with a list of allowed content types', () => { + controller.allowedContentTypes = ['image/png', 'image/jpg']; + const expectedTypes = controller.allowedContentTypes.join(', '); + + const expectedResult = `Allowed content types: ${expectedTypes}`; + jest.spyOn(controller.$translate, 'instant').mockReturnValue(expectedResult); + + const result = controller.contentTypesInfo; + + expect(result).toEqual(expectedResult); + }); + }); + + describe('onSubmit()', () => { + it('should make an HTTP POST request to save the form data', () => { + jest.spyOn(controller.$.watcher, 'updateOriginalData'); + + const files = [{id: 1, name: 'MyFile'}]; + controller.thermograph = {files}; + const serializedParams = $httpParamSerializer(controller.thermograph); + const query = `travels/${controller.$params.id}/updateThermograph?${serializedParams}`; + + $httpBackend.expect('POST', query).respond({}); + controller.onSubmit(); + $httpBackend.flush(); + }); + }); }); }); diff --git a/modules/travel/front/thermograph/index/index.html b/modules/travel/front/thermograph/index/index.html index 3f17fa8d41..583b40eed0 100644 --- a/modules/travel/front/thermograph/index/index.html +++ b/modules/travel/front/thermograph/index/index.html @@ -23,11 +23,11 @@ - {{thermograph.thermographFk}} - {{thermograph.temperature}} - {{thermograph.result}} - {{thermograph.warehouse.name}} - {{thermograph.created | date: 'dd/MM/yyyy'}} + {{::thermograph.thermographFk}} + {{::thermograph.temperature}} + {{::thermograph.result}} + {{::thermograph.warehouse.name}} + {{::thermograph.created | date: 'dd/MM/yyyy'}} @@ -38,7 +38,7 @@ - diff --git a/modules/worker/front/dms/index/index.html b/modules/worker/front/dms/index/index.html index 2deffecf69..697d3d5413 100644 --- a/modules/worker/front/dms/index/index.html +++ b/modules/worker/front/dms/index/index.html @@ -39,11 +39,6 @@ {{::document.dms.description}} - - - -