From 5aae48c7dcf689e036d35c1aff7713f575585376 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Fri, 22 Nov 2019 10:42:05 +0100 Subject: [PATCH 1/4] added gestdoc --- .../methods/worker-dms/allowedContentTypes.js | 23 ++++ .../back/methods/worker-dms/removeFile.js | 30 +++++ .../worker-dms/specs/removeFile.spec.js | 18 +++ .../worker/back/methods/worker/uploadFile.js | 76 ++++++++++++ modules/worker/back/model-config.json | 3 + modules/worker/back/models/worker-dms.js | 4 + modules/worker/back/models/worker-dms.json | 44 +++++++ modules/worker/back/models/worker.js | 1 + modules/worker/front/dms/create/index.html | 83 +++++++++++++ modules/worker/front/dms/create/index.js | 117 ++++++++++++++++++ modules/worker/front/dms/create/index.spec.js | 75 +++++++++++ modules/worker/front/dms/create/style.scss | 7 ++ modules/worker/front/dms/edit/index.html | 74 +++++++++++ modules/worker/front/dms/edit/index.js | 104 ++++++++++++++++ modules/worker/front/dms/edit/index.spec.js | 83 +++++++++++++ modules/worker/front/dms/edit/style.scss | 7 ++ modules/worker/front/dms/index/index.html | 117 ++++++++++++++++++ modules/worker/front/dms/index/index.js | 79 ++++++++++++ modules/worker/front/dms/index/index.spec.js | 40 ++++++ modules/worker/front/dms/index/locale/es.yml | 9 ++ modules/worker/front/dms/index/style.scss | 6 + modules/worker/front/dms/locale/en.yml | 2 + modules/worker/front/dms/locale/es.yml | 14 +++ modules/worker/front/index.js | 3 + modules/worker/front/routes.json | 33 ++++- 25 files changed, 1051 insertions(+), 1 deletion(-) create mode 100644 modules/worker/back/methods/worker-dms/allowedContentTypes.js create mode 100644 modules/worker/back/methods/worker-dms/removeFile.js create mode 100644 modules/worker/back/methods/worker-dms/specs/removeFile.spec.js create mode 100644 modules/worker/back/methods/worker/uploadFile.js create mode 100644 modules/worker/back/models/worker-dms.js create mode 100644 modules/worker/back/models/worker-dms.json create mode 100644 modules/worker/front/dms/create/index.html create mode 100644 modules/worker/front/dms/create/index.js create mode 100644 modules/worker/front/dms/create/index.spec.js create mode 100644 modules/worker/front/dms/create/style.scss create mode 100644 modules/worker/front/dms/edit/index.html create mode 100644 modules/worker/front/dms/edit/index.js create mode 100644 modules/worker/front/dms/edit/index.spec.js create mode 100644 modules/worker/front/dms/edit/style.scss create mode 100644 modules/worker/front/dms/index/index.html create mode 100644 modules/worker/front/dms/index/index.js create mode 100644 modules/worker/front/dms/index/index.spec.js create mode 100644 modules/worker/front/dms/index/locale/es.yml create mode 100644 modules/worker/front/dms/index/style.scss create mode 100644 modules/worker/front/dms/locale/en.yml create mode 100644 modules/worker/front/dms/locale/es.yml diff --git a/modules/worker/back/methods/worker-dms/allowedContentTypes.js b/modules/worker/back/methods/worker-dms/allowedContentTypes.js new file mode 100644 index 000000000..2f5183f92 --- /dev/null +++ b/modules/worker/back/methods/worker-dms/allowedContentTypes.js @@ -0,0 +1,23 @@ +module.exports = Self => { + Self.remoteMethodCtx('allowedContentTypes', { + description: 'Returns a list of allowed contentTypes', + accessType: 'READ', + returns: { + type: ['Object'], + root: true + }, + http: { + path: `/allowedContentTypes`, + verb: 'GET' + } + }); + + Self.allowedContentTypes = async() => { + const storageConnector = Self.app.dataSources.storage.connector; + const allowedContentTypes = storageConnector.allowedContentTypes; + const modelAllowedContentTypes = Self.definition.settings.allowedContentTypes; + + return modelAllowedContentTypes || allowedContentTypes; + }; +}; + diff --git a/modules/worker/back/methods/worker-dms/removeFile.js b/modules/worker/back/methods/worker-dms/removeFile.js new file mode 100644 index 000000000..6cf7ccc41 --- /dev/null +++ b/modules/worker/back/methods/worker-dms/removeFile.js @@ -0,0 +1,30 @@ +module.exports = Self => { + Self.remoteMethodCtx('removeFile', { + description: 'Removes a client document', + accessType: 'WRITE', + accepts: { + arg: 'id', + type: 'Number', + description: 'The document id', + http: {source: 'path'} + }, + returns: { + type: 'Object', + root: true + }, + http: { + path: `/:id/removeFile`, + verb: 'POST' + } + }); + + Self.removeFile = async(ctx, id) => { + const models = Self.app.models; + const clientDms = await models.ClientDms.findById(id); + + await models.Dms.removeFile(ctx, clientDms.dmsFk); + + return clientDms.destroy(); + }; +}; + diff --git a/modules/worker/back/methods/worker-dms/specs/removeFile.spec.js b/modules/worker/back/methods/worker-dms/specs/removeFile.spec.js new file mode 100644 index 000000000..01cf1977b --- /dev/null +++ b/modules/worker/back/methods/worker-dms/specs/removeFile.spec.js @@ -0,0 +1,18 @@ +const app = require('vn-loopback/server/server'); + +describe('ClientDms removeFile()', () => { + const clientDmsFk = 3; + it(`should return an error for a user without enough privileges`, async() => { + let clientId = 101; + let ctx = {req: {accessToken: {userId: clientId}}}; + + let error; + await app.models.ClientDms.removeFile(ctx, clientDmsFk).catch(e => { + error = e; + }).finally(() => { + expect(error.message).toEqual(`You don't have enough privileges`); + }); + + expect(error).toBeDefined(); + }); +}); diff --git a/modules/worker/back/methods/worker/uploadFile.js b/modules/worker/back/methods/worker/uploadFile.js new file mode 100644 index 000000000..588cfe4bd --- /dev/null +++ b/modules/worker/back/methods/worker/uploadFile.js @@ -0,0 +1,76 @@ +module.exports = Self => { + Self.remoteMethodCtx('uploadFile', { + description: 'Upload and attach a file to a client', + accessType: 'WRITE', + accepts: [{ + arg: 'id', + type: 'Number', + description: 'The worker id', + http: {source: 'path'} + }, { + arg: 'warehouseId', + type: 'Number', + description: 'The warehouse id', + required: true + }, { + arg: 'companyId', + type: 'Number', + description: 'The company id', + required: true + }, { + arg: 'dmsTypeId', + type: 'Number', + description: 'The dms type id', + required: true + }, { + arg: 'reference', + type: 'String', + required: true + }, { + arg: 'description', + type: 'String', + required: true + }, { + arg: 'hasFile', + type: 'Boolean', + description: 'True if has an attached file', + required: true + }], + returns: { + type: 'Object', + root: true + }, + http: { + path: `/:id/uploadFile`, + verb: 'POST' + } + }); + + Self.uploadFile = async(ctx, id) => { + const models = Self.app.models; + const promises = []; + const tx = await Self.beginTransaction({}); + + try { + const options = {transaction: tx}; + + const uploadedFiles = await models.Dms.uploadFile(ctx, options); + uploadedFiles.forEach(dms => { + const newWorkerDms = models.WorkerDms.create({ + workerFk: id, + dmsFk: dms.id + }, options); + + promises.push(newWorkerDms); + }); + const resolvedPromises = await Promise.all(promises); + + await tx.commit(); + + return resolvedPromises; + } catch (err) { + await tx.rollback(); + throw err; + } + }; +}; diff --git a/modules/worker/back/model-config.json b/modules/worker/back/model-config.json index 35a039d3c..4948231b9 100644 --- a/modules/worker/back/model-config.json +++ b/modules/worker/back/model-config.json @@ -20,6 +20,9 @@ "WorkCenterHoliday": { "dataSource": "vn" }, + "WorkerDms": { + "dataSource": "vn" + }, "Worker": { "dataSource": "vn" }, diff --git a/modules/worker/back/models/worker-dms.js b/modules/worker/back/models/worker-dms.js new file mode 100644 index 000000000..4504b4ed4 --- /dev/null +++ b/modules/worker/back/models/worker-dms.js @@ -0,0 +1,4 @@ +module.exports = Self => { + require('../methods/worker-dms/removeFile')(Self); + require('../methods/worker-dms/allowedContentTypes')(Self); +}; diff --git a/modules/worker/back/models/worker-dms.json b/modules/worker/back/models/worker-dms.json new file mode 100644 index 000000000..f168388b0 --- /dev/null +++ b/modules/worker/back/models/worker-dms.json @@ -0,0 +1,44 @@ +{ + "name": "WorkerDms", + "base": "Loggable", + "log": { + "model":"ClientLog", + "relation": "worker", + "showField": "dmsFk" + }, + "options": { + "mysql": { + "table": "workerDocument" + } + }, + "properties": { + "dmsFk": { + "type": "Number", + "id": true, + "required": true, + "mysql": { + "columnName": "document" + } + }, + "workerFk": { + "type": "Number", + "required": true, + "mysql": { + "columnName": "worker" + } + } + }, + "relations": { + "worker": { + "type": "belongsTo", + "model": "Worker", + "foreignKey": "workerFk" + }, + "dms": { + "type": "belongsTo", + "model": "Dms", + "foreignKey": "dmsFk" + } + } +} + diff --git a/modules/worker/back/models/worker.js b/modules/worker/back/models/worker.js index 5abf65513..692c8c735 100644 --- a/modules/worker/back/models/worker.js +++ b/modules/worker/back/models/worker.js @@ -3,4 +3,5 @@ module.exports = Self => { require('../methods/worker/mySubordinates')(Self); require('../methods/worker/isSubordinate')(Self); require('../methods/worker/getWorkedHours')(Self); + require('../methods/worker/uploadFile')(Self); }; diff --git a/modules/worker/front/dms/create/index.html b/modules/worker/front/dms/create/index.html new file mode 100644 index 000000000..4ed221ae8 --- /dev/null +++ b/modules/worker/front/dms/create/index.html @@ -0,0 +1,83 @@ + + + +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
diff --git a/modules/worker/front/dms/create/index.js b/modules/worker/front/dms/create/index.js new file mode 100644 index 000000000..d14d9fd92 --- /dev/null +++ b/modules/worker/front/dms/create/index.js @@ -0,0 +1,117 @@ +import ngModule from '../../module'; +import './style.scss'; + +class Controller { + constructor($scope, $http, $state, $translate, vnApp, vnConfig) { + this.$ = $scope; + this.$http = $http; + this.$state = $state; + this.$translate = $translate; + this.vnApp = vnApp; + this.vnConfig = vnConfig; + this.dms = { + files: [], + hasFile: false, + hasFileAttached: false + }; + } + + 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 params = {filter: { + where: {code: 'hhrrData'} + }}; + this.$http.get('DmsTypes/findOne', {params}).then(res => { + const dmsType = res.data && res.data; + const companyId = this.vnConfig.companyFk; + const warehouseId = this.vnConfig.warehouseFk; + const defaultParams = { + reference: this.worker.id, + warehouseId: warehouseId, + companyId: companyId, + dmsTypeId: dmsType.id, + description: this.$translate.instant('WorkerFileDescription', { + dmsTypeName: dmsType.name, + workerId: this.worker.id, + workerName: this.worker.name + }).toUpperCase() + }; + + this.dms = Object.assign(this.dms, defaultParams); + }); + } + + onSubmit() { + const query = `Workers/${this.worker.id}/uploadFile`; + 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; + }); + } +} + +Controller.$inject = ['$scope', '$http', '$state', '$translate', 'vnApp', 'vnConfig']; + +ngModule.component('vnWorkerDmsCreate', { + template: require('./index.html'), + controller: Controller, + bindings: { + worker: '<' + } +}); diff --git a/modules/worker/front/dms/create/index.spec.js b/modules/worker/front/dms/create/index.spec.js new file mode 100644 index 000000000..4ff8ce122 --- /dev/null +++ b/modules/worker/front/dms/create/index.spec.js @@ -0,0 +1,75 @@ +import './index'; + +describe('Client', () => { + describe('Component vnClientDmsCreate', () => { + let controller; + let $scope; + let $httpBackend; + let $httpParamSerializer; + + beforeEach(ngModule('client')); + + beforeEach(angular.mock.inject(($componentController, $rootScope, _$httpBackend_, _$httpParamSerializer_) => { + $scope = $rootScope.$new(); + $httpBackend = _$httpBackend_; + $httpParamSerializer = _$httpParamSerializer_; + controller = $componentController('vnClientDmsCreate', {$scope}); + controller._client = {id: 101, name: 'Bruce wayne'}; + })); + + describe('client() setter', () => { + it('should set the client data and then call setDefaultParams() and getAllowedContentTypes()', () => { + spyOn(controller, 'setDefaultParams'); + spyOn(controller, 'getAllowedContentTypes'); + controller.client = { + id: 15, + name: 'Bruce wayne' + }; + + expect(controller.client).toBeDefined(); + expect(controller.setDefaultParams).toHaveBeenCalledWith(); + expect(controller.getAllowedContentTypes).toHaveBeenCalledWith(); + }); + }); + + describe('setDefaultParams()', () => { + it('should perform a GET query and define the dms property on controller', () => { + const params = {filter: { + where: {code: 'paymentsLaw'} + }}; + let serializedParams = $httpParamSerializer(params); + $httpBackend.when('GET', `DmsTypes/findOne?${serializedParams}`).respond({id: 12, code: 'paymentsLaw'}); + $httpBackend.expect('GET', `DmsTypes/findOne?${serializedParams}`); + controller.setDefaultParams(); + $httpBackend.flush(); + + expect(controller.dms).toBeDefined(); + expect(controller.dms.reference).toEqual(101); + expect(controller.dms.dmsTypeId).toEqual(12); + }); + }); + + describe('onFileChange()', () => { + it('should set dms hasFileAttached property to true if has any files', () => { + const files = [{id: 1, name: 'MyFile'}]; + 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', `clientDms/allowedContentTypes`).respond(expectedResponse); + $httpBackend.expect('GET', `clientDms/allowedContentTypes`); + controller.getAllowedContentTypes(); + $httpBackend.flush(); + + expect(controller.allowedContentTypes).toBeDefined(); + expect(controller.allowedContentTypes).toEqual('image/png, image/jpg'); + }); + }); + }); +}); diff --git a/modules/worker/front/dms/create/style.scss b/modules/worker/front/dms/create/style.scss new file mode 100644 index 000000000..73f136fc1 --- /dev/null +++ b/modules/worker/front/dms/create/style.scss @@ -0,0 +1,7 @@ +vn-ticket-request { + .vn-textfield { + margin: 0!important; + max-width: 100px; + } +} + diff --git a/modules/worker/front/dms/edit/index.html b/modules/worker/front/dms/edit/index.html new file mode 100644 index 000000000..dbc2e0ed1 --- /dev/null +++ b/modules/worker/front/dms/edit/index.html @@ -0,0 +1,74 @@ + + +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
diff --git a/modules/worker/front/dms/edit/index.js b/modules/worker/front/dms/edit/index.js new file mode 100644 index 000000000..460aa17a7 --- /dev/null +++ b/modules/worker/front/dms/edit/index.js @@ -0,0 +1,104 @@ +import ngModule from '../../module'; +import './style.scss'; + +class Controller { + constructor($scope, $http, $state, $translate, vnApp) { + this.$ = $scope; + this.$http = $http; + this.$state = $state; + this.$stateParams = $state.params; + this.$translate = $translate; + this.vnApp = vnApp; + } + + get client() { + return this._client; + } + + set client(value) { + this._client = value; + + if (value) { + this.setDefaultParams(); + this.getAllowedContentTypes(); + } + } + + getAllowedContentTypes() { + this.$http.get('clientDms/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.$stateParams.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.$stateParams.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('client.card.dms.index'); + } + }); + } + + onFileChange(files) { + let hasFileAttached = false; + if (files.length > 0) + hasFileAttached = true; + + this.$.$applyAsync(() => { + this.dms.hasFileAttached = hasFileAttached; + }); + } +} + +Controller.$inject = ['$scope', '$http', '$state', '$translate', 'vnApp']; + +ngModule.component('vnClientDmsEdit', { + template: require('./index.html'), + controller: Controller, + bindings: { + client: '<' + } +}); diff --git a/modules/worker/front/dms/edit/index.spec.js b/modules/worker/front/dms/edit/index.spec.js new file mode 100644 index 000000000..732a90868 --- /dev/null +++ b/modules/worker/front/dms/edit/index.spec.js @@ -0,0 +1,83 @@ +import './index'; + +describe('Client', () => { + describe('Component vnClientDmsEdit', () => { + let controller; + let $scope; + let $httpBackend; + let $state; + + beforeEach(ngModule('client')); + + beforeEach(angular.mock.inject(($componentController, $rootScope, _$httpBackend_) => { + $scope = $rootScope.$new(); + $httpBackend = _$httpBackend_; + $state = {params: {dmsId: 1}}; + controller = $componentController('vnClientDmsEdit', {$scope, $state}); + controller._client = {id: 1}; + })); + + describe('client() setter', () => { + it('should set the client data and then call setDefaultParams() and getAllowedContentTypes()', () => { + spyOn(controller, 'setDefaultParams'); + spyOn(controller, 'getAllowedContentTypes'); + controller._client = undefined; + controller.client = { + id: 15 + }; + + expect(controller.setDefaultParams).toHaveBeenCalledWith(); + expect(controller.client).toBeDefined(); + expect(controller.getAllowedContentTypes).toHaveBeenCalledWith(); + }); + }); + + describe('setDefaultParams()', () => { + it('should perform a GET query and define the dms property on controller', () => { + const dmsId = 1; + const expectedResponse = { + reference: 101, + warehouseFk: 1, + companyFk: 442, + dmsTypeFk: 12, + 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(12); + }); + }); + + 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', `clientDms/allowedContentTypes`).respond(expectedResponse); + $httpBackend.expect('GET', `clientDms/allowedContentTypes`); + controller.getAllowedContentTypes(); + $httpBackend.flush(); + + expect(controller.allowedContentTypes).toBeDefined(); + expect(controller.allowedContentTypes).toEqual('image/png, image/jpg'); + }); + }); + }); +}); diff --git a/modules/worker/front/dms/edit/style.scss b/modules/worker/front/dms/edit/style.scss new file mode 100644 index 000000000..73f136fc1 --- /dev/null +++ b/modules/worker/front/dms/edit/style.scss @@ -0,0 +1,7 @@ +vn-ticket-request { + .vn-textfield { + margin: 0!important; + max-width: 100px; + } +} + diff --git a/modules/worker/front/dms/index/index.html b/modules/worker/front/dms/index/index.html new file mode 100644 index 000000000..491248171 --- /dev/null +++ b/modules/worker/front/dms/index/index.html @@ -0,0 +1,117 @@ + + + + + + + + Id + Type + Order + Reference + Description + Original + File + Employee + Created + + + + + + + + {{::document.dmsFk}} + + + {{::document.dms.dmsType.name}} + + + + + {{::document.dms.hardCopyNumber}} + + + + + {{::document.dms.reference}} + + + + + {{::document.dms.description}} + + + + + + + + {{::document.dms.file}} + + + + + {{::document.dms.worker.user.nickname | dashIfEmpty}} + + + {{::document.dms.created | date:'dd/MM/yyyy HH:mm'}} + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/modules/worker/front/dms/index/index.js b/modules/worker/front/dms/index/index.js new file mode 100644 index 000000000..49929da73 --- /dev/null +++ b/modules/worker/front/dms/index/index.js @@ -0,0 +1,79 @@ +import ngModule from '../../module'; +import './style.scss'; + +class Controller { + constructor($stateParams, $scope, vnToken, $http, vnApp, $translate) { + this.$stateParams = $stateParams; + this.$ = $scope; + this.accessToken = vnToken.token; + this.$http = $http; + this.vnApp = vnApp; + this.$translate = $translate; + this.filter = { + include: { + relation: 'dms', + scope: { + fields: [ + 'dmsTypeFk', + 'reference', + 'hardCopyNumber', + 'workerFk', + 'description', + 'hasFile', + 'file', + 'created', + ], + include: [{ + relation: 'dmsType', + scope: { + fields: ['name'] + } + }, + { + relation: 'worker', + scope: { + fields: ['userFk'], + include: { + relation: 'user', + scope: { + fields: ['nickname'] + } + }, + } + }] + }, + } + }; + } + + showWorkerDescriptor(event, workerFk) { + event.preventDefault(); + event.stopImmediatePropagation(); + this.$.workerDescriptor.parent = event.target; + this.$.workerDescriptor.workerFk = workerFk; + this.$.workerDescriptor.show(); + } + + showDeleteConfirm(index) { + this.dmsIndex = index; + this.$.confirm.show(); + } + + deleteDms(response) { + if (response === 'accept') { + const dmsFk = this.workerDms[this.dmsIndex].dmsFk; + const query = `workerDms/${dmsFk}/removeFile`; + this.$http.post(query).then(() => { + this.$.model.remove(this.dmsIndex); + this.vnApp.showSuccess(this.$translate.instant('Data saved!')); + }); + } + } +} + +Controller.$inject = ['$stateParams', '$scope', 'vnToken', '$http', 'vnApp', '$translate']; + +ngModule.component('vnWorkerDmsIndex', { + template: require('./index.html'), + controller: Controller, +}); diff --git a/modules/worker/front/dms/index/index.spec.js b/modules/worker/front/dms/index/index.spec.js new file mode 100644 index 000000000..16190177b --- /dev/null +++ b/modules/worker/front/dms/index/index.spec.js @@ -0,0 +1,40 @@ +import './index'; +import crudModel from 'core/mocks/crud-model'; + +describe('Client', () => { + describe('Component vnClientDmsIndex', () => { + let $componentController; + let $scope; + let $httpBackend; + let controller; + + beforeEach(ngModule('client')); + + beforeEach(angular.mock.inject((_$componentController_, $rootScope, _$httpBackend_) => { + $componentController = _$componentController_; + $httpBackend = _$httpBackend_; + $scope = $rootScope.$new(); + controller = $componentController('vnClientDmsIndex', {$: $scope}); + controller.$.model = crudModel; + })); + + describe('deleteDms()', () => { + it('should make an HTTP Post query', () => { + const dmsId = 1; + const dmsIndex = 0; + spyOn(controller.vnApp, 'showSuccess'); + spyOn(controller.$.model, 'remove'); + controller.clientDms = [{dmsFk: 1}]; + controller.dmsIndex = dmsIndex; + + $httpBackend.when('POST', `clientDms/${dmsId}/removeFile`).respond({}); + $httpBackend.expect('POST', `clientDms/${dmsId}/removeFile`); + controller.deleteDms('accept'); + $httpBackend.flush(); + + expect(controller.$.model.remove).toHaveBeenCalledWith(dmsIndex); + expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!'); + }); + }); + }); +}); diff --git a/modules/worker/front/dms/index/locale/es.yml b/modules/worker/front/dms/index/locale/es.yml new file mode 100644 index 000000000..0994c7d86 --- /dev/null +++ b/modules/worker/front/dms/index/locale/es.yml @@ -0,0 +1,9 @@ +Type: Tipo +File management: Gestión documental +File: Fichero +Hard copy: Copia +This file will be deleted: Este fichero va a ser borrado +Are you sure?: Estas seguro? +File deleted: Fichero eliminado +Remove file: Eliminar fichero +Download file: Descargar fichero \ No newline at end of file diff --git a/modules/worker/front/dms/index/style.scss b/modules/worker/front/dms/index/style.scss new file mode 100644 index 000000000..a6758e2e6 --- /dev/null +++ b/modules/worker/front/dms/index/style.scss @@ -0,0 +1,6 @@ +vn-client-risk-index { + .totalBox { + display: table; + float: right; + } +} \ No newline at end of file diff --git a/modules/worker/front/dms/locale/en.yml b/modules/worker/front/dms/locale/en.yml new file mode 100644 index 000000000..766853fca --- /dev/null +++ b/modules/worker/front/dms/locale/en.yml @@ -0,0 +1,2 @@ +ClientFileDescription: "{{dmsTypeName}} from client {{clientName}} id {{clientId}}" +ContentTypesInfo: Allowed file types {{allowedContentTypes}} \ No newline at end of file diff --git a/modules/worker/front/dms/locale/es.yml b/modules/worker/front/dms/locale/es.yml new file mode 100644 index 000000000..4185098f3 --- /dev/null +++ b/modules/worker/front/dms/locale/es.yml @@ -0,0 +1,14 @@ +Upload file: Subir fichero +Edit file: Editar fichero +Upload: Subir +File: Fichero +ClientFileDescription: "{{dmsTypeName}} del cliente {{clientName}} id {{clientId}}" +ContentTypesInfo: "Tipos de archivo permitidos: {{allowedContentTypes}}" +Generate identifier for original file: Generar identificador para archivo original +File management: Gestión documental +Hard copy: Copia +This file will be deleted: Este fichero va a ser borrado +Are you sure?: Estas seguro? +File deleted: Fichero eliminado +Remove file: Eliminar fichero +Download file: Descargar fichero \ No newline at end of file diff --git a/modules/worker/front/index.js b/modules/worker/front/index.js index 3ef1c9217..775524a3d 100644 --- a/modules/worker/front/index.js +++ b/modules/worker/front/index.js @@ -14,3 +14,6 @@ import './calendar'; import './time-control'; import './log'; import './phones'; +import './dms/index'; +import './dms/create'; +import './dms/edit'; diff --git a/modules/worker/front/routes.json b/modules/worker/front/routes.json index 7cd5fda20..d117cf18c 100644 --- a/modules/worker/front/routes.json +++ b/modules/worker/front/routes.json @@ -13,7 +13,8 @@ {"state": "worker.card.pbx", "icon": "icon-pbx"}, {"state": "worker.card.calendar", "icon": "icon-calendar"}, {"state": "worker.card.timeControl", "icon": "access_time"}, - {"state": "worker.card.phones", "icon": "contact_phone"} + {"state": "worker.card.phones", "icon": "contact_phone"}, + {"state": "worker.card.dms.index", "icon": "cloud_upload"} ] }, "routes": [ @@ -89,6 +90,36 @@ "params": { "worker": "$ctrl.worker" } + }, + { + "url": "/dms", + "state": "worker.card.dms", + "abstract": true, + "component": "ui-view" + }, + { + "url": "/index", + "state": "worker.card.dms.index", + "component": "vn-worker-dms-index", + "description": "File management" + }, + { + "url": "/create", + "state": "worker.card.dms.create", + "component": "vn-worker-dms-create", + "description": "Upload file", + "params": { + "worker": "$ctrl.worker" + } + }, + { + "url": "/:dmsId/edit", + "state": "worker.card.dms.edit", + "component": "vn-worker-dms-edit", + "description": "Edit file", + "params": { + "worker": "$ctrl.worker" + } } ] } \ No newline at end of file -- 2.40.1 From 3c144ea0cdba735f259e1a3f37d9738ca62c81ad Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Fri, 22 Nov 2019 13:46:38 +0100 Subject: [PATCH 2/4] transaction changes --- back/methods/dms/uploadFile.js | 4 +-- back/models/account.js | 8 ++--- back/models/dmsType.js | 21 +++++++----- .../back/methods/claim-dms/removeFile.js | 2 +- .../back/methods/client-dms/removeFile.js | 2 +- .../back/methods/worker-dms/removeFile.js | 8 ++--- modules/worker/front/dms/create/index.html | 2 +- modules/worker/front/dms/create/index.js | 13 +++---- modules/worker/front/dms/edit/index.html | 12 +++++-- modules/worker/front/dms/edit/index.js | 34 +++++++------------ modules/worker/front/dms/index/index.html | 2 +- modules/worker/front/dms/index/index.js | 15 ++++---- modules/worker/front/dms/locale/es.yml | 12 +++++-- 13 files changed, 68 insertions(+), 67 deletions(-) diff --git a/back/methods/dms/uploadFile.js b/back/methods/dms/uploadFile.js index e077eea30..27e5169c9 100644 --- a/back/methods/dms/uploadFile.js +++ b/back/methods/dms/uploadFile.js @@ -63,7 +63,7 @@ module.exports = Self => { } try { - const hasWriteRole = await models.DmsType.hasWriteRole(ctx, args.dmsTypeId); + const hasWriteRole = await models.DmsType.hasWriteRole(ctx, args.dmsTypeId, myOptions); if (!hasWriteRole) throw new UserError(`You don't have enough privileges`); @@ -83,7 +83,7 @@ module.exports = Self => { const originPath = `${tempContainer.client.root}/${tempContainer.name}/${file.name}`; const destinationPath = `${container.client.root}/${pathHash}/${newDms.file}`; - fs.rename(originPath, destinationPath); + await fs.rename(originPath, destinationPath); addedDms.push(newDms); } diff --git a/back/models/account.js b/back/models/account.js index 00c583d31..92ec589c5 100644 --- a/back/models/account.js +++ b/back/models/account.js @@ -60,8 +60,8 @@ module.exports = Self => { * @param {String} name The role name * @return {Boolean} %true if user has the role, %false otherwise */ - Self.hasRole = async function(userId, name) { - let roles = await Self.getRoles(userId); + Self.hasRole = async function(userId, name, options) { + let roles = await Self.getRoles(userId, options); return roles.some(role => role == name); }; @@ -71,13 +71,13 @@ module.exports = Self => { * @param {Integer} userId The user id * @return {Object} User role list */ - Self.getRoles = async userId => { + Self.getRoles = async(userId, options) => { let result = await Self.rawSql( `SELECT r.name FROM account.user u JOIN account.roleRole rr ON rr.role = u.role JOIN account.role r ON r.id = rr.inheritsFrom - WHERE u.id = ?`, [userId]); + WHERE u.id = ?`, [userId], options); let roles = []; for (role of result) diff --git a/back/models/dmsType.js b/back/models/dmsType.js index f76b095df..267c905e9 100644 --- a/back/models/dmsType.js +++ b/back/models/dmsType.js @@ -5,17 +5,18 @@ module.exports = Self => { * * @param {Object} ctx - Request context * @param {Interger} id - DmsType id + * @param {Object} options - Query options * @return {Boolean} True for user with read privileges */ - Self.hasReadRole = async(ctx, id) => { + Self.hasReadRole = async(ctx, id, options) => { const models = Self.app.models; const dmsType = await models.DmsType.findById(id, { include: { relation: 'readRole' } - }); + }, options); - return await hasRole(ctx, dmsType); + return await hasRole(ctx, dmsType, options); }; /** @@ -24,17 +25,18 @@ module.exports = Self => { * * @param {Object} ctx - Request context * @param {Interger} id - DmsType id + * @param {Object} options - Query options * @return {Boolean} True for user with write privileges */ - Self.hasWriteRole = async(ctx, id) => { + Self.hasWriteRole = async(ctx, id, options) => { const models = Self.app.models; const dmsType = await models.DmsType.findById(id, { include: { relation: 'writeRole' } - }); + }, options); - return await hasRole(ctx, dmsType); + return await hasRole(ctx, dmsType, options); }; /** @@ -42,8 +44,9 @@ module.exports = Self => { * read or write privileges * @param {Object} ctx - Context * @param {Object} dmsType - Dms type [read/write] + * @param {Object} options - Query options */ - async function hasRole(ctx, dmsType) { + async function hasRole(ctx, dmsType, options) { const models = Self.app.models; const myUserId = ctx.req.accessToken.userId; @@ -51,8 +54,8 @@ module.exports = Self => { const writeRole = dmsType.writeRole() && dmsType.writeRole().name; const requiredRole = readRole || writeRole; - const hasRequiredRole = await models.Account.hasRole(myUserId, requiredRole); - const isRoot = await models.Account.hasRole(myUserId, 'root'); + const hasRequiredRole = await models.Account.hasRole(myUserId, requiredRole, options); + const isRoot = await models.Account.hasRole(myUserId, 'root', options); if (isRoot || hasRequiredRole) return true; diff --git a/modules/claim/back/methods/claim-dms/removeFile.js b/modules/claim/back/methods/claim-dms/removeFile.js index 8de764db0..ac546455a 100644 --- a/modules/claim/back/methods/claim-dms/removeFile.js +++ b/modules/claim/back/methods/claim-dms/removeFile.js @@ -1,6 +1,6 @@ module.exports = Self => { Self.remoteMethodCtx('removeFile', { - description: 'Removes a ticket document', + description: 'Removes a claim document', accessType: 'WRITE', accepts: { arg: 'id', diff --git a/modules/client/back/methods/client-dms/removeFile.js b/modules/client/back/methods/client-dms/removeFile.js index 6cf7ccc41..5ff123630 100644 --- a/modules/client/back/methods/client-dms/removeFile.js +++ b/modules/client/back/methods/client-dms/removeFile.js @@ -20,7 +20,7 @@ module.exports = Self => { Self.removeFile = async(ctx, id) => { const models = Self.app.models; - const clientDms = await models.ClientDms.findById(id); + const clientDms = await Self.findById(id); await models.Dms.removeFile(ctx, clientDms.dmsFk); diff --git a/modules/worker/back/methods/worker-dms/removeFile.js b/modules/worker/back/methods/worker-dms/removeFile.js index 6cf7ccc41..d0116c3c2 100644 --- a/modules/worker/back/methods/worker-dms/removeFile.js +++ b/modules/worker/back/methods/worker-dms/removeFile.js @@ -1,6 +1,6 @@ module.exports = Self => { Self.remoteMethodCtx('removeFile', { - description: 'Removes a client document', + description: 'Removes a worker document', accessType: 'WRITE', accepts: { arg: 'id', @@ -20,11 +20,11 @@ module.exports = Self => { Self.removeFile = async(ctx, id) => { const models = Self.app.models; - const clientDms = await models.ClientDms.findById(id); + const workerDms = await Self.findById(id); - await models.Dms.removeFile(ctx, clientDms.dmsFk); + await models.Dms.removeFile(ctx, workerDms.dmsFk); - return clientDms.destroy(); + return workerDms.destroy(); }; }; diff --git a/modules/worker/front/dms/create/index.html b/modules/worker/front/dms/create/index.html index 4ed221ae8..dcafa5986 100644 --- a/modules/worker/front/dms/create/index.html +++ b/modules/worker/front/dms/create/index.html @@ -77,7 +77,7 @@ - + diff --git a/modules/worker/front/dms/create/index.js b/modules/worker/front/dms/create/index.js index d14d9fd92..e7bfe7bfd 100644 --- a/modules/worker/front/dms/create/index.js +++ b/modules/worker/front/dms/create/index.js @@ -1,13 +1,10 @@ import ngModule from '../../module'; +import Component from 'core/lib/component'; import './style.scss'; -class Controller { - constructor($scope, $http, $state, $translate, vnApp, vnConfig) { - this.$ = $scope; - this.$http = $http; - this.$state = $state; - this.$translate = $translate; - this.vnApp = vnApp; +class Controller extends Component { + constructor($element, $, vnConfig) { + super($element, $); this.vnConfig = vnConfig; this.dms = { files: [], @@ -106,7 +103,7 @@ class Controller { } } -Controller.$inject = ['$scope', '$http', '$state', '$translate', 'vnApp', 'vnConfig']; +Controller.$inject = ['$element', '$scope', 'vnConfig']; ngModule.component('vnWorkerDmsCreate', { template: require('./index.html'), diff --git a/modules/worker/front/dms/edit/index.html b/modules/worker/front/dms/edit/index.html index dbc2e0ed1..13bf6f953 100644 --- a/modules/worker/front/dms/edit/index.html +++ b/modules/worker/front/dms/edit/index.html @@ -56,7 +56,15 @@ 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}}" + multiple="true"> + + + + @@ -68,7 +76,7 @@ - + diff --git a/modules/worker/front/dms/edit/index.js b/modules/worker/front/dms/edit/index.js index 460aa17a7..1a593414a 100644 --- a/modules/worker/front/dms/edit/index.js +++ b/modules/worker/front/dms/edit/index.js @@ -1,22 +1,14 @@ import ngModule from '../../module'; +import Component from 'core/lib/component'; import './style.scss'; -class Controller { - constructor($scope, $http, $state, $translate, vnApp) { - this.$ = $scope; - this.$http = $http; - this.$state = $state; - this.$stateParams = $state.params; - this.$translate = $translate; - this.vnApp = vnApp; +class Controller extends Component { + get worker() { + return this._worker; } - get client() { - return this._client; - } - - set client(value) { - this._client = value; + set worker(value) { + this._worker = value; if (value) { this.setDefaultParams(); @@ -25,7 +17,7 @@ class Controller { } getAllowedContentTypes() { - this.$http.get('clientDms/allowedContentTypes').then(res => { + this.$http.get('WorkerDms/allowedContentTypes').then(res => { const contentTypes = res.data.join(', '); this.allowedContentTypes = contentTypes; }); @@ -38,7 +30,7 @@ class Controller { } setDefaultParams() { - const path = `Dms/${this.$stateParams.dmsId}`; + const path = `Dms/${this.$params.dmsId}`; this.$http.get(path).then(res => { const dms = res.data && res.data; this.dms = { @@ -55,7 +47,7 @@ class Controller { } onSubmit() { - const query = `dms/${this.$stateParams.dmsId}/updateFile`; + const query = `dms/${this.$params.dmsId}/updateFile`; const options = { method: 'POST', url: query, @@ -77,7 +69,7 @@ class Controller { if (res) { this.vnApp.showSuccess(this.$translate.instant('Data saved!')); this.$.watcher.updateOriginalData(); - this.$state.go('client.card.dms.index'); + this.$state.go('worker.card.dms.index'); } }); } @@ -93,12 +85,10 @@ class Controller { } } -Controller.$inject = ['$scope', '$http', '$state', '$translate', 'vnApp']; - -ngModule.component('vnClientDmsEdit', { +ngModule.component('vnWorkerDmsEdit', { template: require('./index.html'), controller: Controller, bindings: { - client: '<' + worker: '<' } }); diff --git a/modules/worker/front/dms/index/index.html b/modules/worker/front/dms/index/index.html index 491248171..7859a50a4 100644 --- a/modules/worker/front/dms/index/index.html +++ b/modules/worker/front/dms/index/index.html @@ -1,7 +1,7 @@ { this.$.model.remove(this.dmsIndex); this.vnApp.showSuccess(this.$translate.instant('Data saved!')); @@ -71,7 +68,7 @@ class Controller { } } -Controller.$inject = ['$stateParams', '$scope', 'vnToken', '$http', 'vnApp', '$translate']; +Controller.$inject = ['$element', '$scope', 'vnToken']; ngModule.component('vnWorkerDmsIndex', { template: require('./index.html'), diff --git a/modules/worker/front/dms/locale/es.yml b/modules/worker/front/dms/locale/es.yml index 4185098f3..fa4178d35 100644 --- a/modules/worker/front/dms/locale/es.yml +++ b/modules/worker/front/dms/locale/es.yml @@ -1,14 +1,20 @@ +Reference: Referencia +Description: Descripción +Company: Empresa Upload file: Subir fichero Edit file: Editar fichero Upload: Subir File: Fichero -ClientFileDescription: "{{dmsTypeName}} del cliente {{clientName}} id {{clientId}}" +WorkerFileDescription: "{{dmsTypeName}} del empleado {{workerName}} id {{workerId}}" ContentTypesInfo: "Tipos de archivo permitidos: {{allowedContentTypes}}" Generate identifier for original file: Generar identificador para archivo original +Are you sure you want to continue?: ¿Seguro que quieres continuar? File management: Gestión documental Hard copy: Copia This file will be deleted: Este fichero va a ser borrado -Are you sure?: Estas seguro? +Are you sure?: ¿Seguro? File deleted: Fichero eliminado Remove file: Eliminar fichero -Download file: Descargar fichero \ No newline at end of file +Download file: Descargar fichero +Created: Creado +Employee: Empleado \ No newline at end of file -- 2.40.1 From a077dd51651579522bcf21e8960ba51a82afcca1 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Fri, 22 Nov 2019 14:34:20 +0100 Subject: [PATCH 3/4] test unitario --- db/dump/fixtures.sql | 5 ++++ .../worker-dms/specs/removeFile.spec.js | 6 ++-- modules/worker/front/dms/create/index.spec.js | 30 ++++++++++--------- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 86ee0a612..0d81f0a3d 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -1880,6 +1880,11 @@ INSERT INTO `vn`.`clientDms`(`clientFk`, `dmsFk`) (104, 2), (104, 3); +INSERT INTO `vn`.`workerDocument`(`worker`, `document`) + VALUES + (104, 2), + (104, 3); + INSERT INTO `vn`.`device` (`sn`, `model`, `userFk`) VALUES ('aaa', 'android', '9'); diff --git a/modules/worker/back/methods/worker-dms/specs/removeFile.spec.js b/modules/worker/back/methods/worker-dms/specs/removeFile.spec.js index 01cf1977b..f8fe36625 100644 --- a/modules/worker/back/methods/worker-dms/specs/removeFile.spec.js +++ b/modules/worker/back/methods/worker-dms/specs/removeFile.spec.js @@ -1,13 +1,13 @@ const app = require('vn-loopback/server/server'); -describe('ClientDms removeFile()', () => { - const clientDmsFk = 3; +fdescribe('WorkerDms removeFile()', () => { + const workerDmsFk = 1; it(`should return an error for a user without enough privileges`, async() => { let clientId = 101; let ctx = {req: {accessToken: {userId: clientId}}}; let error; - await app.models.ClientDms.removeFile(ctx, clientDmsFk).catch(e => { + await app.models.WorkerDms.removeFile(ctx, workerDmsFk).catch(e => { error = e; }).finally(() => { expect(error.message).toEqual(`You don't have enough privileges`); diff --git a/modules/worker/front/dms/create/index.spec.js b/modules/worker/front/dms/create/index.spec.js index 4ff8ce122..41fe0e0ca 100644 --- a/modules/worker/front/dms/create/index.spec.js +++ b/modules/worker/front/dms/create/index.spec.js @@ -1,32 +1,34 @@ import './index'; describe('Client', () => { - describe('Component vnClientDmsCreate', () => { + describe('Component vnWorkerDmsCreate', () => { + let $element; let controller; let $scope; let $httpBackend; let $httpParamSerializer; - beforeEach(ngModule('client')); + beforeEach(ngModule('worker')); - beforeEach(angular.mock.inject(($componentController, $rootScope, _$httpBackend_, _$httpParamSerializer_) => { + beforeEach(angular.mock.inject(($compile, $rootScope, _$httpBackend_, _$httpParamSerializer_) => { $scope = $rootScope.$new(); $httpBackend = _$httpBackend_; $httpParamSerializer = _$httpParamSerializer_; - controller = $componentController('vnClientDmsCreate', {$scope}); - controller._client = {id: 101, name: 'Bruce wayne'}; + $element = $compile(``)($rootScope); + controller = $element.controller('vnWorkerDmsCreate'); + controller._worker = {id: 101, name: 'Bruce wayne'}; })); - describe('client() setter', () => { - it('should set the client data and then call setDefaultParams() and getAllowedContentTypes()', () => { + describe('worker() setter', () => { + it('should set the worker data and then call setDefaultParams() and getAllowedContentTypes()', () => { spyOn(controller, 'setDefaultParams'); spyOn(controller, 'getAllowedContentTypes'); - controller.client = { + controller.worker = { id: 15, name: 'Bruce wayne' }; - expect(controller.client).toBeDefined(); + expect(controller.worker).toBeDefined(); expect(controller.setDefaultParams).toHaveBeenCalledWith(); expect(controller.getAllowedContentTypes).toHaveBeenCalledWith(); }); @@ -34,12 +36,12 @@ describe('Client', () => { describe('setDefaultParams()', () => { it('should perform a GET query and define the dms property on controller', () => { + $httpBackend.whenRoute('GET', `DmsTypes`).respond({id: 12, code: 'hhrrData'}); const params = {filter: { - where: {code: 'paymentsLaw'} + where: {code: 'hhrrData'} }}; let serializedParams = $httpParamSerializer(params); - $httpBackend.when('GET', `DmsTypes/findOne?${serializedParams}`).respond({id: 12, code: 'paymentsLaw'}); - $httpBackend.expect('GET', `DmsTypes/findOne?${serializedParams}`); + $httpBackend.when('GET', `DmsTypes/findOne?${serializedParams}`).respond({id: 12, code: 'hhrrData'}); controller.setDefaultParams(); $httpBackend.flush(); @@ -62,8 +64,8 @@ describe('Client', () => { describe('getAllowedContentTypes()', () => { it('should make an HTTP GET request to get the allowed content types', () => { const expectedResponse = ['image/png', 'image/jpg']; - $httpBackend.when('GET', `clientDms/allowedContentTypes`).respond(expectedResponse); - $httpBackend.expect('GET', `clientDms/allowedContentTypes`); + $httpBackend.when('GET', `workerDms/allowedContentTypes`).respond(expectedResponse); + $httpBackend.expect('GET', `workerDms/allowedContentTypes`); controller.getAllowedContentTypes(); $httpBackend.flush(); -- 2.40.1 From f9dba9489fa92c305f86e4178a2e122e56c500b2 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Mon, 25 Nov 2019 09:17:24 +0100 Subject: [PATCH 4/4] updated tests --- db/dump/fixtures.sql | 8 ++--- .../worker-dms/specs/removeFile.spec.js | 2 +- .../methods/worker/specs/uploadFile.spec.js | 19 ++++++++++ modules/worker/back/models/worker-dms.json | 5 ++- modules/worker/front/dms/edit/index.spec.js | 35 ++++++++++--------- modules/worker/front/dms/index/index.spec.js | 18 +++++----- 6 files changed, 56 insertions(+), 31 deletions(-) create mode 100644 modules/worker/back/methods/worker/specs/uploadFile.spec.js diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 0d81f0a3d..a437a4727 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -1869,7 +1869,8 @@ INSERT INTO `vn`.`dms`(`id`, `dmsTypeFk`, `file`, `contentType`, `workerFk`, `wa VALUES (1, 14, '1.txt', 'text/plain', 5, 1, 442, NULL, FALSE, 'Ticket:11', 'Ticket:11 dms for the ticket', CURDATE()), (2, 5, '2.txt', 'text/plain', 5, 1, 442, 1, TRUE, 'Client:104', 'Client:104 dms for the client', CURDATE()), - (3, 5, '3.txt', 'text/plain', 5, 1, 442, NULL, TRUE, 'Client: 104', 'Client:104 readme', CURDATE()); + (3, 5, '3.txt', 'text/plain', 5, 1, 442, NULL, TRUE, 'Client: 104', 'Client:104 readme', CURDATE()), + (4, 3, '3.txt', 'text/plain', 5, 1, 442, NULL, TRUE, 'Worker: 106', 'Worker:106 readme', CURDATE()); INSERT INTO `vn`.`ticketDms`(`ticketFk`, `dmsFk`) VALUES @@ -1880,10 +1881,9 @@ INSERT INTO `vn`.`clientDms`(`clientFk`, `dmsFk`) (104, 2), (104, 3); -INSERT INTO `vn`.`workerDocument`(`worker`, `document`) +INSERT INTO `vn`.`workerDocument`(`id`, `worker`, `document`) VALUES - (104, 2), - (104, 3); + (1, 106, 4); INSERT INTO `vn`.`device` (`sn`, `model`, `userFk`) VALUES diff --git a/modules/worker/back/methods/worker-dms/specs/removeFile.spec.js b/modules/worker/back/methods/worker-dms/specs/removeFile.spec.js index f8fe36625..7039d4f3e 100644 --- a/modules/worker/back/methods/worker-dms/specs/removeFile.spec.js +++ b/modules/worker/back/methods/worker-dms/specs/removeFile.spec.js @@ -1,6 +1,6 @@ const app = require('vn-loopback/server/server'); -fdescribe('WorkerDms removeFile()', () => { +describe('WorkerDms removeFile()', () => { const workerDmsFk = 1; it(`should return an error for a user without enough privileges`, async() => { let clientId = 101; diff --git a/modules/worker/back/methods/worker/specs/uploadFile.spec.js b/modules/worker/back/methods/worker/specs/uploadFile.spec.js new file mode 100644 index 000000000..26a50252d --- /dev/null +++ b/modules/worker/back/methods/worker/specs/uploadFile.spec.js @@ -0,0 +1,19 @@ +const app = require('vn-loopback/server/server'); + +describe('Worker uploadFile()', () => { + it(`should return an error for a user without enough privileges`, async() => { + let workerId = 106; + let currentUserId = 102; + let hhrrDataId = 3; + let ctx = {req: {accessToken: {userId: currentUserId}}, args: {dmsTypeId: hhrrDataId}}; + + let error; + await app.models.Worker.uploadFile(ctx, workerId).catch(e => { + error = e; + }).finally(() => { + expect(error.message).toEqual(`You don't have enough privileges`); + }); + + expect(error).toBeDefined(); + }); +}); diff --git a/modules/worker/back/models/worker-dms.json b/modules/worker/back/models/worker-dms.json index f168388b0..56cad65a6 100644 --- a/modules/worker/back/models/worker-dms.json +++ b/modules/worker/back/models/worker-dms.json @@ -12,9 +12,12 @@ } }, "properties": { + "id": { + "type": "Number", + "id": true + }, "dmsFk": { "type": "Number", - "id": true, "required": true, "mysql": { "columnName": "document" diff --git a/modules/worker/front/dms/edit/index.spec.js b/modules/worker/front/dms/edit/index.spec.js index 732a90868..b883d4472 100644 --- a/modules/worker/front/dms/edit/index.spec.js +++ b/modules/worker/front/dms/edit/index.spec.js @@ -1,45 +1,46 @@ import './index'; -describe('Client', () => { +describe('Worker', () => { describe('Component vnClientDmsEdit', () => { let controller; let $scope; + let $element; let $httpBackend; - let $state; - beforeEach(ngModule('client')); + beforeEach(ngModule('worker')); beforeEach(angular.mock.inject(($componentController, $rootScope, _$httpBackend_) => { $scope = $rootScope.$new(); $httpBackend = _$httpBackend_; - $state = {params: {dmsId: 1}}; - controller = $componentController('vnClientDmsEdit', {$scope, $state}); - controller._client = {id: 1}; + $element = angular.element(` { - it('should set the client data and then call setDefaultParams() and getAllowedContentTypes()', () => { + describe('worker() setter', () => { + it('should set the worker data and then call setDefaultParams() and getAllowedContentTypes()', () => { spyOn(controller, 'setDefaultParams'); spyOn(controller, 'getAllowedContentTypes'); - controller._client = undefined; - controller.client = { - id: 15 + controller._worker = undefined; + controller.worker = { + id: 106 }; expect(controller.setDefaultParams).toHaveBeenCalledWith(); - expect(controller.client).toBeDefined(); + 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 = 1; + const dmsId = 4; const expectedResponse = { reference: 101, warehouseFk: 1, companyFk: 442, - dmsTypeFk: 12, + dmsTypeFk: 3, description: 'Test', hasFile: false, hasFileAttached: false @@ -52,7 +53,7 @@ describe('Client', () => { expect(controller.dms).toBeDefined(); expect(controller.dms.reference).toEqual(101); - expect(controller.dms.dmsTypeId).toEqual(12); + expect(controller.dms.dmsTypeId).toEqual(3); }); }); @@ -70,8 +71,8 @@ describe('Client', () => { describe('getAllowedContentTypes()', () => { it('should make an HTTP GET request to get the allowed content types', () => { const expectedResponse = ['image/png', 'image/jpg']; - $httpBackend.when('GET', `clientDms/allowedContentTypes`).respond(expectedResponse); - $httpBackend.expect('GET', `clientDms/allowedContentTypes`); + $httpBackend.when('GET', `WorkerDms/allowedContentTypes`).respond(expectedResponse); + $httpBackend.expect('GET', `WorkerDms/allowedContentTypes`); controller.getAllowedContentTypes(); $httpBackend.flush(); diff --git a/modules/worker/front/dms/index/index.spec.js b/modules/worker/front/dms/index/index.spec.js index 16190177b..6220d051c 100644 --- a/modules/worker/front/dms/index/index.spec.js +++ b/modules/worker/front/dms/index/index.spec.js @@ -1,34 +1,36 @@ import './index'; import crudModel from 'core/mocks/crud-model'; -describe('Client', () => { - describe('Component vnClientDmsIndex', () => { +describe('Worker', () => { + describe('Component vnWorkerDmsIndex', () => { let $componentController; let $scope; + let $element; let $httpBackend; let controller; - beforeEach(ngModule('client')); + beforeEach(ngModule('worker')); beforeEach(angular.mock.inject((_$componentController_, $rootScope, _$httpBackend_) => { $componentController = _$componentController_; $httpBackend = _$httpBackend_; $scope = $rootScope.$new(); - controller = $componentController('vnClientDmsIndex', {$: $scope}); + $element = angular.element(` { it('should make an HTTP Post query', () => { - const dmsId = 1; + const dmsId = 4; const dmsIndex = 0; spyOn(controller.vnApp, 'showSuccess'); spyOn(controller.$.model, 'remove'); - controller.clientDms = [{dmsFk: 1}]; + controller.workerDms = [{dmsFk: 4}]; controller.dmsIndex = dmsIndex; - $httpBackend.when('POST', `clientDms/${dmsId}/removeFile`).respond({}); - $httpBackend.expect('POST', `clientDms/${dmsId}/removeFile`); + $httpBackend.when('POST', `WorkerDms/${dmsId}/removeFile`).respond({}); + $httpBackend.expect('POST', `WorkerDms/${dmsId}/removeFile`); controller.deleteDms('accept'); $httpBackend.flush(); -- 2.40.1