From 12aa5da355acb1c65aca232e88910e116685dcc5 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Fri, 28 Feb 2020 07:17:19 +0100 Subject: [PATCH 1/6] Changed fields order --- front/core/components/input-file/index.html | 2 +- .../back/methods/travel/createThermograph.js | 9 +++- .../front/thermograph/create/index.html | 51 +++++++++++-------- .../travel/front/thermograph/create/index.js | 2 +- 4 files changed, 38 insertions(+), 26 deletions(-) diff --git a/front/core/components/input-file/index.html b/front/core/components/input-file/index.html index 11478430a4..be4c15248a 100644 --- a/front/core/components/input-file/index.html +++ b/front/core/components/input-file/index.html @@ -30,7 +30,7 @@ ng-click="$ctrl.onClear($event)"> diff --git a/modules/travel/back/methods/travel/createThermograph.js b/modules/travel/back/methods/travel/createThermograph.js index cbf0678d1b..816e8cebd7 100644 --- a/modules/travel/back/methods/travel/createThermograph.js +++ b/modules/travel/back/methods/travel/createThermograph.js @@ -14,6 +14,10 @@ module.exports = Self => { type: 'String', description: 'The thermograph id', required: true + }, { + arg: 'state', + type: 'String', + required: true }, { arg: 'warehouseId', type: 'Number', @@ -48,7 +52,7 @@ module.exports = Self => { } }); - Self.createThermograph = async(ctx, id, thermographId) => { + Self.createThermograph = async(ctx, id, thermographId, state) => { const models = Self.app.models; const tx = await Self.beginTransaction({}); @@ -70,7 +74,8 @@ module.exports = Self => { await travelThermograph.updateAttributes({ dmsFk: firstDms.id, - travelFk: id + travelFk: id, + result: state }, options); await tx.commit(); diff --git a/modules/travel/front/thermograph/create/index.html b/modules/travel/front/thermograph/create/index.html index 02ef542640..4b1fc8cf4f 100644 --- a/modules/travel/front/thermograph/create/index.html +++ b/modules/travel/front/thermograph/create/index.html @@ -9,6 +9,35 @@ enctype="multipart/form-data">
+ + + + + + + + + + + + - - - - - - - - Date: Fri, 28 Feb 2020 08:19:20 +0100 Subject: [PATCH 2/6] Added edit section --- modules/travel/front/index.js | 1 + .../travel/front/thermograph/edit/index.html | 86 +++++++++++++++++ .../travel/front/thermograph/edit/index.js | 94 +++++++++++++++++++ .../front/thermograph/edit/index.spec.js | 84 +++++++++++++++++ .../travel/front/thermograph/edit/style.scss | 7 ++ .../travel/front/thermograph/index/index.html | 6 ++ 6 files changed, 278 insertions(+) create mode 100644 modules/travel/front/thermograph/edit/index.html create mode 100644 modules/travel/front/thermograph/edit/index.js create mode 100644 modules/travel/front/thermograph/edit/index.spec.js create mode 100644 modules/travel/front/thermograph/edit/style.scss diff --git a/modules/travel/front/index.js b/modules/travel/front/index.js index b72f9fd51c..28ec276939 100644 --- a/modules/travel/front/index.js +++ b/modules/travel/front/index.js @@ -11,4 +11,5 @@ import './log'; import './create'; import './thermograph/index/'; import './thermograph/create/'; +import './thermograph/edit/'; import './descriptor-popover'; diff --git a/modules/travel/front/thermograph/edit/index.html b/modules/travel/front/thermograph/edit/index.html new file mode 100644 index 0000000000..7dc122b88f --- /dev/null +++ b/modules/travel/front/thermograph/edit/index.html @@ -0,0 +1,86 @@ + + +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
diff --git a/modules/travel/front/thermograph/edit/index.js b/modules/travel/front/thermograph/edit/index.js new file mode 100644 index 0000000000..52dac01b4d --- /dev/null +++ b/modules/travel/front/thermograph/edit/index.js @@ -0,0 +1,94 @@ +import ngModule from '../../module'; +import Component from 'core/lib/component'; +import './style.scss'; + +class Controller extends Component { + get worker() { + return this._worker; + } + + set worker(value) { + this._worker = value; + + if (value) { + this.setDefaultParams(); + this.getAllowedContentTypes(); + } + } + + getAllowedContentTypes() { + this.$http.get('WorkerDms/allowedContentTypes').then(res => { + const contentTypes = res.data.join(', '); + this.allowedContentTypes = contentTypes; + }); + } + + get contentTypesInfo() { + return this.$translate.instant('ContentTypesInfo', { + allowedContentTypes: this.allowedContentTypes + }); + } + + setDefaultParams() { + const path = `Dms/${this.$params.dmsId}`; + 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, + hasFileAttached: false, + files: [] + }; + }); + } + + onSubmit() { + const query = `dms/${this.$params.dmsId}/updateFile`; + const options = { + method: 'POST', + url: query, + params: this.dms, + headers: { + 'Content-Type': undefined + }, + transformRequest: files => { + const formData = new FormData(); + + for (let i = 0; i < files.length; i++) + formData.append(files[i].name, files[i]); + + return formData; + }, + data: this.dms.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'); + } + }); + } + + onFileChange(files) { + let hasFileAttached = false; + if (files.length > 0) + hasFileAttached = true; + + this.$.$applyAsync(() => { + this.dms.hasFileAttached = hasFileAttached; + }); + } +} + +ngModule.component('vnTravelThermographEdit', { + template: require('./index.html'), + controller: Controller, + bindings: { + worker: '<' + } +}); diff --git a/modules/travel/front/thermograph/edit/index.spec.js b/modules/travel/front/thermograph/edit/index.spec.js new file mode 100644 index 0000000000..7dfad9643f --- /dev/null +++ b/modules/travel/front/thermograph/edit/index.spec.js @@ -0,0 +1,84 @@ +import './index'; + +describe('Worker', () => { + describe('Component vnClientDmsEdit', () => { + let controller; + let $scope; + let $element; + let $httpBackend; + + beforeEach(ngModule('worker')); + + beforeEach(angular.mock.inject(($componentController, $rootScope, _$httpBackend_) => { + $scope = $rootScope.$new(); + $httpBackend = _$httpBackend_; + $element = angular.element(` { + it('should set the worker data and then call setDefaultParams() and getAllowedContentTypes()', () => { + jest.spyOn(controller, 'setDefaultParams'); + jest.spyOn(controller, 'getAllowedContentTypes'); + controller._worker = undefined; + controller.worker = { + id: 106 + }; + + expect(controller.setDefaultParams).toHaveBeenCalledWith(); + expect(controller.worker).toBeDefined(); + expect(controller.getAllowedContentTypes).toHaveBeenCalledWith(); + }); + }); + + describe('setDefaultParams()', () => { + it('should perform a GET query and define the dms property on controller', () => { + const dmsId = 4; + const expectedResponse = { + reference: 101, + warehouseFk: 1, + companyFk: 442, + dmsTypeFk: 3, + description: 'Test', + hasFile: false, + hasFileAttached: false + }; + + $httpBackend.when('GET', `Dms/${dmsId}`).respond(expectedResponse); + $httpBackend.expect('GET', `Dms/${dmsId}`).respond(expectedResponse); + controller.setDefaultParams(); + $httpBackend.flush(); + + expect(controller.dms).toBeDefined(); + expect(controller.dms.reference).toEqual(101); + expect(controller.dms.dmsTypeId).toEqual(3); + }); + }); + + 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.onFileChange(files); + $scope.$apply(); + + expect(controller.dms.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`); + controller.getAllowedContentTypes(); + $httpBackend.flush(); + + expect(controller.allowedContentTypes).toBeDefined(); + expect(controller.allowedContentTypes).toEqual('image/png, image/jpg'); + }); + }); + }); +}); diff --git a/modules/travel/front/thermograph/edit/style.scss b/modules/travel/front/thermograph/edit/style.scss new file mode 100644 index 0000000000..73f136fc15 --- /dev/null +++ b/modules/travel/front/thermograph/edit/style.scss @@ -0,0 +1,7 @@ +vn-ticket-request { + .vn-textfield { + margin: 0!important; + max-width: 100px; + } +} + diff --git a/modules/travel/front/thermograph/index/index.html b/modules/travel/front/thermograph/index/index.html index ca9ebcaea7..f8e118b132 100644 --- a/modules/travel/front/thermograph/index/index.html +++ b/modules/travel/front/thermograph/index/index.html @@ -37,6 +37,12 @@ + + + + Date: Fri, 28 Feb 2020 09:21:00 +0100 Subject: [PATCH 3/6] Some changes --- modules/travel/front/routes.json | 9 ++++++++ .../travel/front/thermograph/edit/index.js | 22 +++++-------------- .../travel/front/thermograph/index/index.html | 2 +- .../travel/front/thermograph/locale/es.yml | 1 + 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/modules/travel/front/routes.json b/modules/travel/front/routes.json index fcbe5b92f4..8e03705126 100644 --- a/modules/travel/front/routes.json +++ b/modules/travel/front/routes.json @@ -81,6 +81,15 @@ "travel": "$ctrl.travel" }, "acl": ["buyer"] + }, { + "url" : "/:dmsId/edit", + "state": "travel.card.thermograph.edit", + "component": "vn-travel-thermograph-edit", + "description": "Edit thermograph", + "params": { + "travel": "$ctrl.travel" + }, + "acl": ["buyer"] } ] } \ No newline at end of file diff --git a/modules/travel/front/thermograph/edit/index.js b/modules/travel/front/thermograph/edit/index.js index 52dac01b4d..98c5fbbb5d 100644 --- a/modules/travel/front/thermograph/edit/index.js +++ b/modules/travel/front/thermograph/edit/index.js @@ -3,12 +3,12 @@ import Component from 'core/lib/component'; import './style.scss'; class Controller extends Component { - get worker() { - return this._worker; + get travel() { + return this._travel; } - set worker(value) { - this._worker = value; + set travel(value) { + this._travel = value; if (value) { this.setDefaultParams(); @@ -17,7 +17,7 @@ class Controller extends Component { } getAllowedContentTypes() { - this.$http.get('WorkerDms/allowedContentTypes').then(res => { + this.$http.get('TravelThermographs/allowedContentTypes').then(res => { const contentTypes = res.data.join(', '); this.allowedContentTypes = contentTypes; }); @@ -73,22 +73,12 @@ class Controller extends Component { } }); } - - onFileChange(files) { - let hasFileAttached = false; - if (files.length > 0) - hasFileAttached = true; - - this.$.$applyAsync(() => { - this.dms.hasFileAttached = hasFileAttached; - }); - } } ngModule.component('vnTravelThermographEdit', { template: require('./index.html'), controller: Controller, bindings: { - worker: '<' + travel: '<' } }); diff --git a/modules/travel/front/thermograph/index/index.html b/modules/travel/front/thermograph/index/index.html index f8e118b132..3f17fa8d41 100644 --- a/modules/travel/front/thermograph/index/index.html +++ b/modules/travel/front/thermograph/index/index.html @@ -38,7 +38,7 @@ - diff --git a/modules/travel/front/thermograph/locale/es.yml b/modules/travel/front/thermograph/locale/es.yml index 184e95e738..9f9be564b7 100644 --- a/modules/travel/front/thermograph/locale/es.yml +++ b/modules/travel/front/thermograph/locale/es.yml @@ -12,6 +12,7 @@ FileDescription: Travel id {{travelId}} ContentTypesInfo: 'Tipos de archivo permitidos: {{allowedContentTypes}}' Are you sure you want to continue?: ¿Seguro que quieres continuar? Add thermograph: Añadir termógrafo +Edit thermograph: Editar termógrafo Thermograph deleted: Termógrafo eliminado Thermograph: Termógrafo Are you sure you want to remove the thermograph?: ¿Seguro que quieres quitar el termógrafo? \ No newline at end of file From 28b7f5d3a2e32065c196616e09d245537dce9818 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Fri, 28 Feb 2020 14:18:55 +0100 Subject: [PATCH 4/6] 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}} - - - - Date: Mon, 2 Mar 2020 07:50:07 +0100 Subject: [PATCH 5/6] Worker dms changes --- db/dump/fixtures.sql | 4 ++-- modules/worker/back/methods/worker-dms/removeFile.js | 2 +- modules/worker/back/models/worker-dms.json | 4 ++-- modules/worker/front/dms/index/index.html | 2 +- modules/worker/front/dms/index/index.js | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 2f3a9378d9..9d9b84da46 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -1901,11 +1901,11 @@ INSERT INTO `vn`.`dmsType`(`id`, `name`, `path`, `readRoleFk`, `writeRoleFk`, `c VALUES (1, 'Facturas Recibidas', 'recibidas', NULL, NULL, 'invoiceIn'), (2, 'Doc oficial', 'oficial', NULL, NULL, 'officialDoc'), - (3, 'Laboral', 'laboral', NULL, NULL, 'hhrrData'), + (3, 'Laboral', 'laboral', 37, 37, 'hhrrData'), (4, 'Albaranes recibidos', 'entradas', NULL, NULL, 'deliveryNote'), (5, 'Otros', 'otros', 1, 1, 'miscellaneous'), (6, 'Pruebas', 'pruebas', NULL, NULL, 'tests'), - (7, 'IAE Clientes', 'IAE_Clientes', NULL, NULL, 'economicActivitiesTax'), + (7, 'IAE Clientes', 'IAE_Clientes', 1, 1, 'economicActivitiesTax'), (8, 'Fiscal', 'fiscal', NULL, NULL, 'fiscal'), (9, 'Vehiculos', 'vehiculos', NULL, NULL, 'vehicles'), (10, 'Plantillas', 'plantillas', NULL, NULL, 'templates'), diff --git a/modules/worker/back/methods/worker-dms/removeFile.js b/modules/worker/back/methods/worker-dms/removeFile.js index d0116c3c22..b441c56ce1 100644 --- a/modules/worker/back/methods/worker-dms/removeFile.js +++ b/modules/worker/back/methods/worker-dms/removeFile.js @@ -5,7 +5,7 @@ module.exports = Self => { accepts: { arg: 'id', type: 'Number', - description: 'The document id', + description: 'The worker document id', http: {source: 'path'} }, returns: { diff --git a/modules/worker/back/models/worker-dms.json b/modules/worker/back/models/worker-dms.json index f8ad824bc4..56cad65a64 100644 --- a/modules/worker/back/models/worker-dms.json +++ b/modules/worker/back/models/worker-dms.json @@ -13,10 +13,10 @@ }, "properties": { "id": { - "type": "Number" + "type": "Number", + "id": true }, "dmsFk": { - "id": true, "type": "Number", "required": true, "mysql": { diff --git a/modules/worker/front/dms/index/index.html b/modules/worker/front/dms/index/index.html index 697d3d5413..697d3d5aa1 100644 --- a/modules/worker/front/dms/index/index.html +++ b/modules/worker/front/dms/index/index.html @@ -19,7 +19,6 @@ Reference Description Original - File Created @@ -45,6 +44,7 @@ href="api/dms/{{::document.dmsFk}}/downloadFile?access_token={{::$ctrl.accessToken}}">{{::document.dms.file}} + {{::document.dms.created | date:'dd/MM/yyyy HH:mm'}} diff --git a/modules/worker/front/dms/index/index.js b/modules/worker/front/dms/index/index.js index 907047f970..ab3be34080 100644 --- a/modules/worker/front/dms/index/index.js +++ b/modules/worker/front/dms/index/index.js @@ -58,8 +58,8 @@ class Controller extends Component { deleteDms(response) { if (response === 'accept') { - const dmsFk = this.workerDms[this.dmsIndex].dmsFk; - const query = `WorkerDms/${dmsFk}/removeFile`; + const workerDmsId = this.workerDms[this.dmsIndex].id; + const query = `WorkerDms/${workerDmsId}/removeFile`; this.$http.post(query).then(() => { this.$.model.remove(this.dmsIndex); this.vnApp.showSuccess(this.$translate.instant('Data saved!')); From 03e952c83e8346079e272b2a378741956a61249f Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Mon, 2 Mar 2020 07:57:33 +0100 Subject: [PATCH 6/6] Updated unit test --- modules/worker/front/dms/index/index.spec.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/worker/front/dms/index/index.spec.js b/modules/worker/front/dms/index/index.spec.js index a354b74e97..7378ef3010 100644 --- a/modules/worker/front/dms/index/index.spec.js +++ b/modules/worker/front/dms/index/index.spec.js @@ -22,15 +22,15 @@ describe('Worker', () => { describe('deleteDms()', () => { it('should make an HTTP Post query', () => { - const dmsId = 4; + const workerDmsId = 1; const dmsIndex = 0; jest.spyOn(controller.vnApp, 'showSuccess'); jest.spyOn(controller.$.model, 'remove'); - controller.workerDms = [{dmsFk: 4}]; + controller.workerDms = [{id: 1, dmsFk: 4}]; controller.dmsIndex = dmsIndex; - $httpBackend.when('POST', `WorkerDms/${dmsId}/removeFile`).respond({}); - $httpBackend.expect('POST', `WorkerDms/${dmsId}/removeFile`); + $httpBackend.when('POST', `WorkerDms/${workerDmsId}/removeFile`).respond({}); + $httpBackend.expect('POST', `WorkerDms/${workerDmsId}/removeFile`); controller.deleteDms('accept'); $httpBackend.flush();