From 588250b1c1e27ac6ce5196aa54a1561f0c128d75 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Mon, 15 Jul 2019 11:40:11 +0200 Subject: [PATCH 1/8] dms update files --- back/methods/dms/specs/updateFile.spec.js | 22 +++ back/methods/dms/updateFile.js | 158 ++++++++++++++++++++ back/methods/dms/uploadFile.js | 14 +- back/models/dms.js | 1 + front/core/components/table/style.scss | 6 + loopback/server/boot/storage.js | 2 +- modules/client/front/dms/edit/index.html | 63 ++++++++ modules/client/front/dms/edit/index.js | 86 +++++++++++ modules/client/front/dms/edit/index.spec.js | 60 ++++++++ modules/client/front/dms/edit/locale/es.yml | 3 + modules/client/front/dms/edit/style.scss | 7 + modules/client/front/dms/index/index.html | 19 ++- modules/client/front/index.js | 1 + modules/client/front/routes.json | 9 ++ modules/ticket/front/dms/edit/index.html | 63 ++++++++ modules/ticket/front/dms/edit/index.js | 86 +++++++++++ modules/ticket/front/dms/edit/index.spec.js | 60 ++++++++ modules/ticket/front/dms/edit/locale/es.yml | 3 + modules/ticket/front/dms/edit/style.scss | 7 + modules/ticket/front/dms/index/index.html | 7 +- modules/ticket/front/index.js | 1 + modules/ticket/front/routes.json | 9 ++ 22 files changed, 670 insertions(+), 17 deletions(-) create mode 100644 back/methods/dms/specs/updateFile.spec.js create mode 100644 back/methods/dms/updateFile.js create mode 100644 modules/client/front/dms/edit/index.html create mode 100644 modules/client/front/dms/edit/index.js create mode 100644 modules/client/front/dms/edit/index.spec.js create mode 100644 modules/client/front/dms/edit/locale/es.yml create mode 100644 modules/client/front/dms/edit/style.scss create mode 100644 modules/ticket/front/dms/edit/index.html create mode 100644 modules/ticket/front/dms/edit/index.js create mode 100644 modules/ticket/front/dms/edit/index.spec.js create mode 100644 modules/ticket/front/dms/edit/locale/es.yml create mode 100644 modules/ticket/front/dms/edit/style.scss diff --git a/back/methods/dms/specs/updateFile.spec.js b/back/methods/dms/specs/updateFile.spec.js new file mode 100644 index 0000000000..2945b1ac9e --- /dev/null +++ b/back/methods/dms/specs/updateFile.spec.js @@ -0,0 +1,22 @@ +const app = require('vn-loopback/server/server'); + +describe('dms updateFile()', () => { + it(`should return an error for a user without enough privileges`, async() => { + let clientId = 101; + let companyId = 442; + let warehouseId = 1; + let dmsTypeId = 14; + + let dmsId = 1; + let ctx = {req: {accessToken: {userId: clientId}}, args: {dmsTypeId: dmsTypeId}}; + + let error; + await app.models.Dms.updateFile(ctx, dmsId, warehouseId, companyId, dmsTypeId).catch(e => { + error = e; + }).finally(() => { + expect(error.message).toEqual(`You don't have enough privileges`); + }); + + expect(error).toBeDefined(); + }); +}); diff --git a/back/methods/dms/updateFile.js b/back/methods/dms/updateFile.js new file mode 100644 index 0000000000..e502c7b941 --- /dev/null +++ b/back/methods/dms/updateFile.js @@ -0,0 +1,158 @@ +const UserError = require('vn-loopback/util/user-error'); +const fs = require('fs-extra'); + +module.exports = Self => { + Self.remoteMethodCtx('updateFile', { + description: 'updates a file properties or file', + accessType: 'WRITE', + accepts: [{ + arg: 'id', + type: 'Number', + description: 'The document id', + http: {source: 'path'} + }, + { + 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: 'hasFile', + type: 'Boolean', + description: 'True if has an attached file' + }], + returns: { + type: 'Object', + root: true + }, + http: { + path: `/:id/updateFile`, + verb: 'POST' + } + }); + + Self.updateFile = async(ctx, id, warehouseId, companyId, + dmsTypeId, reference, description, hasFile, options) => { + const storageConnector = Self.app.dataSources.storage.connector; + const models = Self.app.models; + const fileOptions = {}; + + let tx; + let myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + try { + const hasWriteRole = await models.DmsType.hasWriteRole(ctx, dmsTypeId); + if (!hasWriteRole) + throw new UserError(`You don't have enough privileges`); + + // Upload file to temporary path + const tempContainer = await getContainer('temp'); + let files = []; + try { + const uploaded = await models.Container.upload(tempContainer.name, ctx.req, ctx.result, fileOptions); + files = Object.values(uploaded.files).map(file => { + return file[0]; + }); + } catch (err) { + if (err.message != 'No file content uploaded') + throw e; + } + + const updatedDmsList = []; + for (const file of files) { + const updatedDms = await updateDms(id, dmsTypeId, companyId, warehouseId, + reference, description, hasFile, file.name, myOptions); + + const pathHash = storageConnector.getPathHash(updatedDms.id); + const container = await getContainer(pathHash); + + const originPath = `${tempContainer.client.root}/${tempContainer.name}/${file.name}`; + const destinationPath = `${container.client.root}/${pathHash}/${updatedDms.file}`; + + fs.rename(originPath, destinationPath); + + updatedDmsList.push(updatedDms); + } + + if (tx) await tx.commit(); + return updatedDmsList; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } + }; + + async function updateDms(id, dmsTypeId, companyId, warehouseId, + reference, description, hasFile, fileName, myOptions) { + const storageConnector = Self.app.dataSources.storage.connector; + const dms = await Self.findById(id, null, myOptions); + const updatedDms = await dms.updateAttributes({ + dmsTypeFk: dmsTypeId, + companyFk: companyId, + warehouseFk: warehouseId, + reference: reference, + description: description, + hasFile: hasFile + }, myOptions); + + const oldExtension = storageConnector.getFileExtension(dms.file); + const newExtension = storageConnector.getFileExtension(fileName); + + try { + if (oldExtension != newExtension) { + const pathHash = storageConnector.getPathHash(updatedDms.id); + + await Self.app.models.Container.removeFile(pathHash, dms.file); + } + } catch (err) {} + + fileName = `${updatedDms.id}.${newExtension}`; + + return updatedDms.updateAttribute('file', fileName, myOptions); + } + + + /** + * Returns a container instance + * If doesn't exists creates a new one + * + * @param {String} name Container name + * @return {Object} Container instance + */ + async function getContainer(name) { + const models = Self.app.models; + let container; + try { + container = await models.Container.getContainer(name); + } catch (err) { + if (err.code === 'ENOENT') { + container = await models.Container.createContainer({ + name: name + }); + } else throw err; + } + + return container; + } +}; diff --git a/back/methods/dms/uploadFile.js b/back/methods/dms/uploadFile.js index 9ef60c0053..5d4634aaed 100644 --- a/back/methods/dms/uploadFile.js +++ b/back/methods/dms/uploadFile.js @@ -9,27 +9,25 @@ module.exports = Self => { { arg: 'warehouseId', type: 'Number', - description: '' + description: 'The warehouse id' }, { arg: 'companyId', type: 'Number', - description: '' + description: 'The company id' }, { arg: 'dmsTypeId', type: 'Number', - description: '' + description: 'The dms type id' }, { arg: 'reference', - type: 'String', - description: '' + type: 'String' }, { arg: 'description', - type: 'String', - description: '' + type: 'String' }, { arg: 'hasFile', type: 'Boolean', - description: '' + description: 'True if has an attached file' }], returns: { type: 'Object', diff --git a/back/models/dms.js b/back/models/dms.js index d3471178b9..9a06928dbe 100644 --- a/back/models/dms.js +++ b/back/models/dms.js @@ -2,4 +2,5 @@ module.exports = Self => { require('../methods/dms/downloadFile')(Self); require('../methods/dms/uploadFile')(Self); require('../methods/dms/removeFile')(Self); + require('../methods/dms/updateFile')(Self); }; diff --git a/front/core/components/table/style.scss b/front/core/components/table/style.scss index 87af108345..7d985c4373 100644 --- a/front/core/components/table/style.scss +++ b/front/core/components/table/style.scss @@ -141,6 +141,12 @@ vn-table { background-color: $color-alert-medium; } + & > vn-td vn-icon-menu { + display: inline-block; + color: $color-main; + padding: .25em + } + & > [actions] { width: 1px; diff --git a/loopback/server/boot/storage.js b/loopback/server/boot/storage.js index d34804059e..12662ab73f 100644 --- a/loopback/server/boot/storage.js +++ b/loopback/server/boot/storage.js @@ -9,7 +9,7 @@ module.exports = app => { }; storageConnector.getFileExtension = function(fileName) { - return fileName.split('.').pop(); + return fileName.split('.').pop().toLowerCase(); }; storageConnector.getPathHash = function(id) { diff --git a/modules/client/front/dms/edit/index.html b/modules/client/front/dms/edit/index.html new file mode 100644 index 0000000000..65c280c5de --- /dev/null +++ b/modules/client/front/dms/edit/index.html @@ -0,0 +1,63 @@ + + +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
diff --git a/modules/client/front/dms/edit/index.js b/modules/client/front/dms/edit/index.js new file mode 100644 index 0000000000..ba0108e6bc --- /dev/null +++ b/modules/client/front/dms/edit/index.js @@ -0,0 +1,86 @@ +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(); + } + + setDefaultParams() { + const path = `/api/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, + files: [] + }; + }); + } + + onSubmit() { + const query = `/api/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) { + if (files.length > 0) { + this.$.$applyAsync(() => { + this.dms.hasFile = true; + }); + } + } +} + +Controller.$inject = ['$scope', '$http', '$state', '$translate', 'vnApp']; + +ngModule.component('vnClientDmsEdit', { + template: require('./index.html'), + controller: Controller, + bindings: { + client: '<' + } +}); diff --git a/modules/client/front/dms/edit/index.spec.js b/modules/client/front/dms/edit/index.spec.js new file mode 100644 index 0000000000..2825d4d259 --- /dev/null +++ b/modules/client/front/dms/edit/index.spec.js @@ -0,0 +1,60 @@ +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()', () => { + spyOn(controller, 'setDefaultParams'); + controller.client = { + id: 15, + name: 'Bruce wayne' + }; + + expect(controller.client).toBeDefined(); + expect(controller.setDefaultParams).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', `/api/DmsTypes/findOne?${serializedParams}`).respond({id: 12, code: 'paymentsLaw'}); + $httpBackend.expect('GET', `/api/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 hasFile property to true if has any files', () => { + const files = [{id: 1, name: 'MyFile'}]; + controller.onFileChange(files); + $scope.$apply(); + + expect(controller.dms.hasFile).toBeTruthy(); + }); + }); + }); +}); diff --git a/modules/client/front/dms/edit/locale/es.yml b/modules/client/front/dms/edit/locale/es.yml new file mode 100644 index 0000000000..7625d5195b --- /dev/null +++ b/modules/client/front/dms/edit/locale/es.yml @@ -0,0 +1,3 @@ +Edit file: Editar fichero +File: Fichero +Attached file: Fichero adjunto \ No newline at end of file diff --git a/modules/client/front/dms/edit/style.scss b/modules/client/front/dms/edit/style.scss new file mode 100644 index 0000000000..b47544b120 --- /dev/null +++ b/modules/client/front/dms/edit/style.scss @@ -0,0 +1,7 @@ +vn-ticket-request { + vn-textfield { + margin: 0!important; + max-width: 100px; + } +} + diff --git a/modules/client/front/dms/index/index.html b/modules/client/front/dms/index/index.html index 2cf6cdd4ff..b55bc137f5 100644 --- a/modules/client/front/dms/index/index.html +++ b/modules/client/front/dms/index/index.html @@ -16,11 +16,11 @@ Type Reference Description - Attached file + Original File - Employee + Employee Created - + @@ -41,13 +41,13 @@ {{::document.dms.description}} - + {{::document.dms.file}} - + {{::document.dms.worker.user.nickname | dashIfEmpty}} @@ -55,16 +55,21 @@ {{::document.dms.created | dateTime:'dd/MM/yyyy HH:mm'}} - + + ng-show="document.dms.hasFile"> + + + +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
diff --git a/modules/ticket/front/dms/edit/index.js b/modules/ticket/front/dms/edit/index.js new file mode 100644 index 0000000000..c35c4140d8 --- /dev/null +++ b/modules/ticket/front/dms/edit/index.js @@ -0,0 +1,86 @@ +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 ticket() { + return this._ticket; + } + + set ticket(value) { + this._ticket = value; + + if (value) + this.setDefaultParams(); + } + + setDefaultParams() { + const path = `/api/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, + files: [] + }; + }); + } + + onSubmit() { + const query = `/api/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('ticket.card.dms.index'); + } + }); + } + + onFileChange(files) { + if (files.length > 0) { + this.$.$applyAsync(() => { + this.dms.hasFile = true; + }); + } + } +} + +Controller.$inject = ['$scope', '$http', '$state', '$translate', 'vnApp']; + +ngModule.component('vnTicketDmsEdit', { + template: require('./index.html'), + controller: Controller, + bindings: { + ticket: '<' + } +}); diff --git a/modules/ticket/front/dms/edit/index.spec.js b/modules/ticket/front/dms/edit/index.spec.js new file mode 100644 index 0000000000..2825d4d259 --- /dev/null +++ b/modules/ticket/front/dms/edit/index.spec.js @@ -0,0 +1,60 @@ +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()', () => { + spyOn(controller, 'setDefaultParams'); + controller.client = { + id: 15, + name: 'Bruce wayne' + }; + + expect(controller.client).toBeDefined(); + expect(controller.setDefaultParams).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', `/api/DmsTypes/findOne?${serializedParams}`).respond({id: 12, code: 'paymentsLaw'}); + $httpBackend.expect('GET', `/api/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 hasFile property to true if has any files', () => { + const files = [{id: 1, name: 'MyFile'}]; + controller.onFileChange(files); + $scope.$apply(); + + expect(controller.dms.hasFile).toBeTruthy(); + }); + }); + }); +}); diff --git a/modules/ticket/front/dms/edit/locale/es.yml b/modules/ticket/front/dms/edit/locale/es.yml new file mode 100644 index 0000000000..7625d5195b --- /dev/null +++ b/modules/ticket/front/dms/edit/locale/es.yml @@ -0,0 +1,3 @@ +Edit file: Editar fichero +File: Fichero +Attached file: Fichero adjunto \ No newline at end of file diff --git a/modules/ticket/front/dms/edit/style.scss b/modules/ticket/front/dms/edit/style.scss new file mode 100644 index 0000000000..b47544b120 --- /dev/null +++ b/modules/ticket/front/dms/edit/style.scss @@ -0,0 +1,7 @@ +vn-ticket-request { + vn-textfield { + margin: 0!important; + max-width: 100px; + } +} + diff --git a/modules/ticket/front/dms/index/index.html b/modules/ticket/front/dms/index/index.html index eb51710b6c..9200350604 100644 --- a/modules/ticket/front/dms/index/index.html +++ b/modules/ticket/front/dms/index/index.html @@ -59,12 +59,17 @@ + ng-show="document.dms.hasFile"> + + Date: Mon, 15 Jul 2019 11:56:17 +0200 Subject: [PATCH 2/8] removed download file conditional --- modules/client/front/dms/index/index.html | 3 +-- modules/ticket/front/dms/index/index.html | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/modules/client/front/dms/index/index.html b/modules/client/front/dms/index/index.html index b55bc137f5..155e11c92a 100644 --- a/modules/client/front/dms/index/index.html +++ b/modules/client/front/dms/index/index.html @@ -58,8 +58,7 @@ + href="api/dms/{{::document.dmsFk}}/downloadFile?access_token={{::$ctrl.accessToken}}"> diff --git a/modules/ticket/front/dms/index/index.html b/modules/ticket/front/dms/index/index.html index 9200350604..e85c1e30a3 100644 --- a/modules/ticket/front/dms/index/index.html +++ b/modules/ticket/front/dms/index/index.html @@ -58,8 +58,7 @@ + href="api/dms/{{::document.dmsFk}}/downloadFile?access_token={{::$ctrl.accessToken}}"> From f0895d33f094a1705d87eb4400e0edfe36f5edb5 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Mon, 15 Jul 2019 13:16:02 +0200 Subject: [PATCH 3/8] save file content-type #1597 --- back/methods/dms/downloadFile.js | 4 ++-- back/methods/dms/updateFile.js | 6 ++++-- back/methods/dms/uploadFile.js | 6 ++++-- back/models/dms.json | 3 +++ 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/back/methods/dms/downloadFile.js b/back/methods/dms/downloadFile.js index 72090f33bb..01ba01b843 100644 --- a/back/methods/dms/downloadFile.js +++ b/back/methods/dms/downloadFile.js @@ -46,7 +46,7 @@ module.exports = Self => { if (env && env != 'development') { const pathHash = storageConnector.getPathHash(dms.id); file = { - contentType: 'application/octet-stream', + contentType: dms.contentType, container: pathHash, name: dms.file }; @@ -60,6 +60,6 @@ module.exports = Self => { const stream = await models.Container.downloadStream(file.container, file.name); - return [stream, file.contentType, `filename="${file.name}"`]; + return [stream, file.contentType, `inline; filename="${file.name}"`]; }; }; diff --git a/back/methods/dms/updateFile.js b/back/methods/dms/updateFile.js index e502c7b941..eaafd7f7d6 100644 --- a/back/methods/dms/updateFile.js +++ b/back/methods/dms/updateFile.js @@ -82,7 +82,7 @@ module.exports = Self => { const updatedDmsList = []; for (const file of files) { const updatedDms = await updateDms(id, dmsTypeId, companyId, warehouseId, - reference, description, hasFile, file.name, myOptions); + reference, description, hasFile, file, myOptions); const pathHash = storageConnector.getPathHash(updatedDms.id); const container = await getContainer(pathHash); @@ -104,7 +104,7 @@ module.exports = Self => { }; async function updateDms(id, dmsTypeId, companyId, warehouseId, - reference, description, hasFile, fileName, myOptions) { + reference, description, hasFile, file, myOptions) { const storageConnector = Self.app.dataSources.storage.connector; const dms = await Self.findById(id, null, myOptions); const updatedDms = await dms.updateAttributes({ @@ -113,9 +113,11 @@ module.exports = Self => { warehouseFk: warehouseId, reference: reference, description: description, + contentType: file.type, hasFile: hasFile }, myOptions); + let fileName = file.name; const oldExtension = storageConnector.getFileExtension(dms.file); const newExtension = storageConnector.getFileExtension(fileName); diff --git a/back/methods/dms/uploadFile.js b/back/methods/dms/uploadFile.js index 5d4634aaed..b50aaf824d 100644 --- a/back/methods/dms/uploadFile.js +++ b/back/methods/dms/uploadFile.js @@ -70,7 +70,7 @@ module.exports = Self => { const addedDms = []; for (const file of files) { - const newDms = await createDms(ctx, file.name, myOptions); + const newDms = await createDms(ctx, file, myOptions); const pathHash = storageConnector.getPathHash(newDms.id); const container = await getContainer(pathHash); @@ -90,7 +90,7 @@ module.exports = Self => { } }; - async function createDms(ctx, fileName, myOptions) { + async function createDms(ctx, file, myOptions) { const models = Self.app.models; const storageConnector = Self.app.dataSources.storage.connector; const myUserId = ctx.req.accessToken.userId; @@ -104,9 +104,11 @@ module.exports = Self => { warehouseFk: args.warehouseId, reference: args.reference, description: args.description, + contentType: file.type, hasFile: args.hasFile }, myOptions); + let fileName = file.name; const extension = storageConnector.getFileExtension(fileName); fileName = `${newDms.id}.${extension}`; diff --git a/back/models/dms.json b/back/models/dms.json index dc421a7a12..bf6e44311d 100644 --- a/back/models/dms.json +++ b/back/models/dms.json @@ -17,6 +17,9 @@ "file": { "type": "string" }, + "contentType": { + "type": "string" + }, "reference": { "type": "string" }, From b85aa6d3dab568b47b5487d5355ffd5c65cd48a3 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Mon, 15 Jul 2019 13:20:50 +0200 Subject: [PATCH 4/8] fixed icons column size --- modules/client/front/dms/index/index.html | 4 ++-- modules/ticket/front/dms/index/index.html | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/modules/client/front/dms/index/index.html b/modules/client/front/dms/index/index.html index 155e11c92a..cc237c1577 100644 --- a/modules/client/front/dms/index/index.html +++ b/modules/client/front/dms/index/index.html @@ -16,7 +16,7 @@ Type Reference Description - Original + Original File Employee Created @@ -41,7 +41,7 @@ {{::document.dms.description}}
- + diff --git a/modules/ticket/front/dms/index/index.html b/modules/ticket/front/dms/index/index.html index e85c1e30a3..9e4beb58d2 100644 --- a/modules/ticket/front/dms/index/index.html +++ b/modules/ticket/front/dms/index/index.html @@ -16,11 +16,11 @@ Type Reference Description - Attached file + Original File - Employee + Employee Created - + @@ -41,13 +41,13 @@ {{::document.dms.description}} - + {{::document.dms.file}} - + {{::document.dms.worker.user.nickname | dashIfEmpty}} @@ -55,7 +55,7 @@ {{::document.dms.created | dateTime:'dd/MM/yyyy HH:mm'}} - + From 9565899af22f890dc19ee99316d490a0554a37f6 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Mon, 15 Jul 2019 14:52:41 +0200 Subject: [PATCH 5/8] download zip files only --- back/methods/dms/downloadFile.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/back/methods/dms/downloadFile.js b/back/methods/dms/downloadFile.js index 01ba01b843..bb03dda897 100644 --- a/back/methods/dms/downloadFile.js +++ b/back/methods/dms/downloadFile.js @@ -60,6 +60,6 @@ module.exports = Self => { const stream = await models.Container.downloadStream(file.container, file.name); - return [stream, file.contentType, `inline; filename="${file.name}"`]; + return [stream, file.contentType, `filename="${file.name}"`]; }; }; From 72fcf807b0a4580f00bcdd943c5cc92e3b293a90 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Tue, 16 Jul 2019 07:22:29 +0200 Subject: [PATCH 6/8] 7z mimetype --- modules/client/front/dms/create/index.html | 2 +- modules/client/front/dms/edit/index.html | 2 +- modules/ticket/front/dms/create/index.html | 2 +- modules/ticket/front/dms/edit/index.html | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/client/front/dms/create/index.html b/modules/client/front/dms/create/index.html index 3fa96a9da1..879210907b 100644 --- a/modules/client/front/dms/create/index.html +++ b/modules/client/front/dms/create/index.html @@ -46,7 +46,7 @@ label="File" model="$ctrl.dms.files" on-change="$ctrl.onFileChange(files)" - accept=".pdf, .png, .jpg, .jpeg, application/zip, application/rar"> + accept=".pdf, .png, .jpg, .jpeg, application/zip, application/rar, application/x-7z-compressed"> diff --git a/modules/client/front/dms/edit/index.html b/modules/client/front/dms/edit/index.html index 65c280c5de..6f6392d666 100644 --- a/modules/client/front/dms/edit/index.html +++ b/modules/client/front/dms/edit/index.html @@ -45,7 +45,7 @@ label="File" model="$ctrl.dms.files" on-change="$ctrl.onFileChange(files)" - accept=".pdf, .png, .jpg, .jpeg, application/zip, application/rar"> + accept=".pdf, .png, .jpg, .jpeg, application/zip, application/rar, application/x-7z-compressed"> diff --git a/modules/ticket/front/dms/create/index.html b/modules/ticket/front/dms/create/index.html index 527c6030d7..d6ebb01305 100644 --- a/modules/ticket/front/dms/create/index.html +++ b/modules/ticket/front/dms/create/index.html @@ -45,7 +45,7 @@ label="File" model="$ctrl.dms.files" on-change="$ctrl.onFileChange(files)" - accept=".pdf, .png, .jpg, .jpeg, application/zip, application/rar"> + accept=".pdf, .png, .jpg, .jpeg, application/zip, application/rar, application/x-7z-compressed"> diff --git a/modules/ticket/front/dms/edit/index.html b/modules/ticket/front/dms/edit/index.html index 275d330bb2..c740d5103b 100644 --- a/modules/ticket/front/dms/edit/index.html +++ b/modules/ticket/front/dms/edit/index.html @@ -45,7 +45,7 @@ label="File" model="$ctrl.dms.files" on-change="$ctrl.onFileChange(files)" - accept=".pdf, .png, .jpg, .jpeg, application/zip, application/rar"> + accept=".pdf, .png, .jpg, .jpeg, application/zip, application/rar, application/x-7z-compressed"> From 3c3f4bfeae77a3616563e5deaad138b77263a2e7 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Tue, 16 Jul 2019 08:26:57 +0200 Subject: [PATCH 7/8] address.edit fixed location autocompletion --- modules/client/front/address/edit/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/client/front/address/edit/index.html b/modules/client/front/address/edit/index.html index b7361b71b6..cb769617b6 100644 --- a/modules/client/front/address/edit/index.html +++ b/modules/client/front/address/edit/index.html @@ -42,7 +42,7 @@ Date: Tue, 16 Jul 2019 13:37:25 +0200 Subject: [PATCH 8/8] dms fixes --- back/methods/dms/downloadFile.js | 29 +++-- back/methods/dms/updateFile.js | 105 +++++++++--------- loopback/locale/es.json | 3 +- modules/client/front/dms/create/index.html | 2 +- modules/client/front/dms/create/index.js | 15 ++- modules/client/front/dms/create/locale/es.yml | 2 +- modules/client/front/dms/edit/index.html | 2 +- modules/client/front/dms/edit/index.js | 13 ++- modules/client/front/dms/edit/locale/es.yml | 2 +- modules/client/front/dms/index/index.html | 9 +- modules/ticket/front/dms/create/index.html | 2 +- modules/ticket/front/dms/create/index.js | 15 ++- modules/ticket/front/dms/create/locale/es.yml | 2 +- modules/ticket/front/dms/edit/index.html | 2 +- modules/ticket/front/dms/edit/index.js | 13 ++- modules/ticket/front/dms/edit/locale/es.yml | 2 +- modules/ticket/front/dms/index/index.html | 10 +- 17 files changed, 122 insertions(+), 106 deletions(-) diff --git a/back/methods/dms/downloadFile.js b/back/methods/dms/downloadFile.js index bb03dda897..f3096aabb4 100644 --- a/back/methods/dms/downloadFile.js +++ b/back/methods/dms/downloadFile.js @@ -34,7 +34,6 @@ module.exports = Self => { }); Self.downloadFile = async function(ctx, id) { - const env = process.env.NODE_ENV; const storageConnector = Self.app.dataSources.storage.connector; const models = Self.app.models; const dms = await Self.findById(id); @@ -43,23 +42,21 @@ module.exports = Self => { if (!hasReadRole) throw new UserError(`You don't have enough privileges`); - if (env && env != 'development') { - const pathHash = storageConnector.getPathHash(dms.id); - file = { - contentType: dms.contentType, - container: pathHash, - name: dms.file - }; - } else { - file = { - contentType: 'text/plain', - container: 'temp', - name: `file.txt` - }; + const pathHash = storageConnector.getPathHash(dms.id); + try { + await models.Container.getFile(pathHash, dms.file); + } catch (e) { + if (e.code != 'ENOENT') + throw e; + + const error = new UserError(`File doesn't exists`); + error.statusCode = 404; + + throw error; } - const stream = await models.Container.downloadStream(file.container, file.name); + const stream = models.Container.downloadStream(pathHash, dms.file); - return [stream, file.contentType, `filename="${file.name}"`]; + return [stream, dms.contentType, `filename="${dms.file}"`]; }; }; diff --git a/back/methods/dms/updateFile.js b/back/methods/dms/updateFile.js index eaafd7f7d6..ff3c0806f4 100644 --- a/back/methods/dms/updateFile.js +++ b/back/methods/dms/updateFile.js @@ -32,6 +32,10 @@ module.exports = Self => { }, { arg: 'hasFile', type: 'Boolean', + description: 'True if has original file' + }, { + arg: 'hasAttachedFile', + type: 'Boolean', description: 'True if has an attached file' }], returns: { @@ -45,10 +49,8 @@ module.exports = Self => { }); Self.updateFile = async(ctx, id, warehouseId, companyId, - dmsTypeId, reference, description, hasFile, options) => { - const storageConnector = Self.app.dataSources.storage.connector; + dmsTypeId, reference, description, hasFile, hasAttachedFile, options) => { const models = Self.app.models; - const fileOptions = {}; let tx; let myOptions = {}; @@ -66,75 +68,68 @@ module.exports = Self => { if (!hasWriteRole) throw new UserError(`You don't have enough privileges`); - // Upload file to temporary path - const tempContainer = await getContainer('temp'); - let files = []; - try { - const uploaded = await models.Container.upload(tempContainer.name, ctx.req, ctx.result, fileOptions); - files = Object.values(uploaded.files).map(file => { - return file[0]; - }); - } catch (err) { - if (err.message != 'No file content uploaded') - throw e; - } + const dms = await Self.findById(id, null, myOptions); + await dms.updateAttributes({ + dmsTypeFk: dmsTypeId, + companyFk: companyId, + warehouseFk: warehouseId, + reference: reference, + description: description, + hasFile: hasFile + }, myOptions); - const updatedDmsList = []; - for (const file of files) { - const updatedDms = await updateDms(id, dmsTypeId, companyId, warehouseId, - reference, description, hasFile, file, myOptions); - - const pathHash = storageConnector.getPathHash(updatedDms.id); - const container = await getContainer(pathHash); - - const originPath = `${tempContainer.client.root}/${tempContainer.name}/${file.name}`; - const destinationPath = `${container.client.root}/${pathHash}/${updatedDms.file}`; - - fs.rename(originPath, destinationPath); - - updatedDmsList.push(updatedDms); - } + if (hasAttachedFile) + updatedDms = await uploadNewFile(ctx, dms, myOptions); if (tx) await tx.commit(); - return updatedDmsList; + return updatedDms; } catch (e) { if (tx) await tx.rollback(); throw e; } }; - async function updateDms(id, dmsTypeId, companyId, warehouseId, - reference, description, hasFile, file, myOptions) { + async function uploadNewFile(ctx, dms, myOptions) { const storageConnector = Self.app.dataSources.storage.connector; - const dms = await Self.findById(id, null, myOptions); - const updatedDms = await dms.updateAttributes({ - dmsTypeFk: dmsTypeId, - companyFk: companyId, - warehouseFk: warehouseId, - reference: reference, - description: description, - contentType: file.type, - hasFile: hasFile - }, myOptions); + const models = Self.app.models; + const fileOptions = {}; - let fileName = file.name; - const oldExtension = storageConnector.getFileExtension(dms.file); - const newExtension = storageConnector.getFileExtension(fileName); + const tempContainer = await getContainer('temp'); + const makeUpload = await models.Container.upload(tempContainer.name, ctx.req, ctx.result, fileOptions); + const keys = Object.values(makeUpload.files); + const files = keys.map(file => file[0]); + const file = files[0]; - try { - if (oldExtension != newExtension) { - const pathHash = storageConnector.getPathHash(updatedDms.id); + if (file) { + const oldExtension = storageConnector.getFileExtension(dms.file); + const newExtension = storageConnector.getFileExtension(file.name); + const fileName = `${dms.id}.${newExtension}`; - await Self.app.models.Container.removeFile(pathHash, dms.file); - } - } catch (err) {} + try { + if (oldExtension != newExtension) { + const pathHash = storageConnector.getPathHash(dms.id); - fileName = `${updatedDms.id}.${newExtension}`; + await models.Container.removeFile(pathHash, dms.file); + } + } catch (err) {} - return updatedDms.updateAttribute('file', fileName, myOptions); + const updatedDms = await dms.updateAttributes({ + contentType: file.type, + file: fileName + }, myOptions); + + const pathHash = storageConnector.getPathHash(updatedDms.id); + const container = await getContainer(pathHash); + + const originPath = `${tempContainer.client.root}/${tempContainer.name}/${file.name}`; + const destinationPath = `${container.client.root}/${pathHash}/${updatedDms.file}`; + + fs.rename(originPath, destinationPath); + + return updatedDms; + } } - /** * Returns a container instance * If doesn't exists creates a new one diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 256b51205e..94c4252792 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -94,5 +94,6 @@ "Invalid parameters to create a new ticket": "Parámetros inválidos para crear un nuevo ticket", "This item is not available": "Este artículo no está disponible", "This postcode already exists": "Este código postal ya existe", - "Concept cannot be blank": "Concept cannot be blank" + "Concept cannot be blank": "El concepto no puede quedar en blanco", + "File doesn't exists": "El archivo no existe" } \ No newline at end of file diff --git a/modules/client/front/dms/create/index.html b/modules/client/front/dms/create/index.html index 879210907b..6010fb4a9b 100644 --- a/modules/client/front/dms/create/index.html +++ b/modules/client/front/dms/create/index.html @@ -51,7 +51,7 @@ diff --git a/modules/client/front/dms/create/index.js b/modules/client/front/dms/create/index.js index a3b32228fe..f3738fc2b5 100644 --- a/modules/client/front/dms/create/index.js +++ b/modules/client/front/dms/create/index.js @@ -10,7 +10,8 @@ class Controller { this.vnApp = vnApp; this.dms = { files: [], - hasFile: false + hasFile: false, + hasAttachedFile: false }; } @@ -78,11 +79,13 @@ class Controller { } onFileChange(files) { - if (files.length > 0) { - this.$.$applyAsync(() => { - this.dms.hasFile = true; - }); - } + let hasAttachedFile = false; + if (files.length > 0) + hasAttachedFile = true; + + this.$.$applyAsync(() => { + this.dms.hasAttachedFile = hasAttachedFile; + }); } } diff --git a/modules/client/front/dms/create/locale/es.yml b/modules/client/front/dms/create/locale/es.yml index 2de9e68d35..2ea3b31d86 100644 --- a/modules/client/front/dms/create/locale/es.yml +++ b/modules/client/front/dms/create/locale/es.yml @@ -2,4 +2,4 @@ Upload file: Subir fichero Upload: Subir File: Fichero ClientFileDescription: "{{dmsTypeName}} del cliente {{clientName}} id {{clientId}}" -Attached file: Fichero adjunto \ No newline at end of file +Generate identifier for original file: Generar identificador para archivo original \ No newline at end of file diff --git a/modules/client/front/dms/edit/index.html b/modules/client/front/dms/edit/index.html index 6f6392d666..e3dccf1c01 100644 --- a/modules/client/front/dms/edit/index.html +++ b/modules/client/front/dms/edit/index.html @@ -50,7 +50,7 @@ diff --git a/modules/client/front/dms/edit/index.js b/modules/client/front/dms/edit/index.js index ba0108e6bc..00e3e4d28c 100644 --- a/modules/client/front/dms/edit/index.js +++ b/modules/client/front/dms/edit/index.js @@ -33,6 +33,7 @@ class Controller { dmsTypeId: dms.dmsTypeFk, description: dms.description, hasFile: dms.hasFile, + hasAttachedFile: false, files: [] }; }); @@ -67,11 +68,13 @@ class Controller { } onFileChange(files) { - if (files.length > 0) { - this.$.$applyAsync(() => { - this.dms.hasFile = true; - }); - } + let hasAttachedFile = false; + if (files.length > 0) + hasAttachedFile = true; + + this.$.$applyAsync(() => { + this.dms.hasAttachedFile = hasAttachedFile; + }); } } diff --git a/modules/client/front/dms/edit/locale/es.yml b/modules/client/front/dms/edit/locale/es.yml index 7625d5195b..9d97564ba6 100644 --- a/modules/client/front/dms/edit/locale/es.yml +++ b/modules/client/front/dms/edit/locale/es.yml @@ -1,3 +1,3 @@ Edit file: Editar fichero File: Fichero -Attached file: Fichero adjunto \ No newline at end of file +Generate identifier for original file: Generar identificador para archivo original \ No newline at end of file diff --git a/modules/client/front/dms/index/index.html b/modules/client/front/dms/index/index.html index cc237c1577..26a36d0868 100644 --- a/modules/client/front/dms/index/index.html +++ b/modules/client/front/dms/index/index.html @@ -17,7 +17,7 @@ Reference Description Original - File + File Employee Created @@ -46,7 +46,12 @@ field="document.dms.hasFile">
- {{::document.dms.file}} + + {{::document.dms.file}} + +
diff --git a/modules/ticket/front/dms/create/index.html b/modules/ticket/front/dms/create/index.html index d6ebb01305..7c8ae6ad2d 100644 --- a/modules/ticket/front/dms/create/index.html +++ b/modules/ticket/front/dms/create/index.html @@ -50,7 +50,7 @@ diff --git a/modules/ticket/front/dms/create/index.js b/modules/ticket/front/dms/create/index.js index 1b28b2417f..8f6f7e3225 100644 --- a/modules/ticket/front/dms/create/index.js +++ b/modules/ticket/front/dms/create/index.js @@ -10,7 +10,8 @@ class Controller { this.vnApp = vnApp; this.dms = { files: [], - hasFile: false + hasFile: false, + hasAttachedFile: false }; } @@ -76,11 +77,13 @@ class Controller { } onFileChange(files) { - if (files.length > 0) { - this.$.$applyAsync(() => { - this.dms.hasFile = true; - }); - } + let hasAttachedFile = false; + if (files.length > 0) + hasAttachedFile = true; + + this.$.$applyAsync(() => { + this.dms.hasAttachedFile = hasAttachedFile; + }); } } diff --git a/modules/ticket/front/dms/create/locale/es.yml b/modules/ticket/front/dms/create/locale/es.yml index e074da48b0..999826352b 100644 --- a/modules/ticket/front/dms/create/locale/es.yml +++ b/modules/ticket/front/dms/create/locale/es.yml @@ -2,4 +2,4 @@ Upload file: Subir fichero Upload: Subir File: Fichero FileDescription: Ticket id {{ticketId}} del cliente {{clientName}} id {{clientId}} -Attached file: Fichero adjunto \ No newline at end of file +Generate identifier for original file: Generar identificador para archivo original \ No newline at end of file diff --git a/modules/ticket/front/dms/edit/index.html b/modules/ticket/front/dms/edit/index.html index c740d5103b..ac12e49840 100644 --- a/modules/ticket/front/dms/edit/index.html +++ b/modules/ticket/front/dms/edit/index.html @@ -50,7 +50,7 @@ diff --git a/modules/ticket/front/dms/edit/index.js b/modules/ticket/front/dms/edit/index.js index c35c4140d8..df25cea944 100644 --- a/modules/ticket/front/dms/edit/index.js +++ b/modules/ticket/front/dms/edit/index.js @@ -33,6 +33,7 @@ class Controller { dmsTypeId: dms.dmsTypeFk, description: dms.description, hasFile: dms.hasFile, + hasAttachedFile: false, files: [] }; }); @@ -67,11 +68,13 @@ class Controller { } onFileChange(files) { - if (files.length > 0) { - this.$.$applyAsync(() => { - this.dms.hasFile = true; - }); - } + let hasAttachedFile = false; + if (files.length > 0) + hasAttachedFile = true; + + this.$.$applyAsync(() => { + this.dms.hasAttachedFile = hasAttachedFile; + }); } } diff --git a/modules/ticket/front/dms/edit/locale/es.yml b/modules/ticket/front/dms/edit/locale/es.yml index 7625d5195b..9d97564ba6 100644 --- a/modules/ticket/front/dms/edit/locale/es.yml +++ b/modules/ticket/front/dms/edit/locale/es.yml @@ -1,3 +1,3 @@ Edit file: Editar fichero File: Fichero -Attached file: Fichero adjunto \ No newline at end of file +Generate identifier for original file: Generar identificador para archivo original \ No newline at end of file diff --git a/modules/ticket/front/dms/index/index.html b/modules/ticket/front/dms/index/index.html index 9e4beb58d2..222a14b048 100644 --- a/modules/ticket/front/dms/index/index.html +++ b/modules/ticket/front/dms/index/index.html @@ -17,7 +17,7 @@ Reference Description Original - File + File Employee Created @@ -46,7 +46,13 @@ field="document.dms.hasFile"> - {{::document.dms.file}} + + + {{::document.dms.file}} + +