diff --git a/back/methods/dms/removeFile.js b/back/methods/dms/removeFile.js index 350bea6bcd..93fae9728b 100644 --- a/back/methods/dms/removeFile.js +++ b/back/methods/dms/removeFile.js @@ -22,8 +22,10 @@ module.exports = Self => { Self.removeFile = async(ctx, id) => { const models = Self.app.models; - const trashDmsType = await models.DmsType.findOne({where: {code: 'trash'}}); const dms = await models.Dms.findById(id); + const trashDmsType = await models.DmsType.findOne({ + where: {code: 'trash'} + }); const hasWriteRole = await models.DmsType.hasWriteRole(ctx, dms.dmsTypeFk); if (!hasWriteRole) diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 92580b36eb..95ea7b20f1 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -1959,5 +1959,5 @@ INSERT INTO `vn`.`travelThermograph`(`thermographFk`, `created`, `warehouseFk`, ('TL.BBA85422', DATE_ADD(CURDATE(), INTERVAL -1 MONTH), 2, 2, 'COOL', 'Ok', NULL), ('TL.BBA85422', CURDATE(), 2, 1, 'COOL', 'can not read the temperature', NULL), ('TZ1905012010', CURDATE(), 1, 1, 'WARM', 'Temperature in range', 5), - ('138350-0', DATE_ADD(CURDATE(), INTERVAL -1 MONTH), 1, NULL, 'WARM', NULL, 5), - ('138350-0', CURDATE(), 1, 1, 'WARM', 'Ok', 5); \ No newline at end of file + ('138350-0', DATE_ADD(CURDATE(), INTERVAL -1 MONTH), 1, 1, 'WARM', NULL, 5), + ('138350-0', CURDATE(), 1, NULL, 'COOL', NULL, NULL); \ No newline at end of file diff --git a/modules/travel/back/methods/travel-thermograph/delete.js b/modules/travel/back/methods/travel-thermograph/delete.js deleted file mode 100644 index a8d6bc7629..0000000000 --- a/modules/travel/back/methods/travel-thermograph/delete.js +++ /dev/null @@ -1,61 +0,0 @@ - -module.exports = Self => { - Self.remoteMethod('delete', { - description: 'Delete a invoiceOut', - accessType: 'WRITE', - accepts: { - arg: 'id', - type: 'string', - required: true, - description: 'The invoiceOut id', - http: {source: 'path'} - }, - returns: { - type: 'object', - root: true - }, - http: { - path: '/:id/delete', - verb: 'POST' - } - }); - - Self.delete = async id => { - const models = Self.app.models; - - const targetClaimDms = await models.ClaimDms.findById(id); - const targetDms = await models.Dms.findById(targetClaimDms.dmsFk); - - - const trashDmsType = await models.DmsType.findOne({ - where: {code: 'trash'} - }); - - await models.Dms.removeFile(ctx, targetClaimDms.dmsFk); - await targetClaimDms.destroy(); - - return targetDms.updateAttribute('dmsTypeFk', trashDmsType.id); - - - const transaction = await Self.beginTransaction({}); - try { - let options = {transaction: transaction}; - - let invoiceOut = await Self.findById(id); - let tickets = await Self.app.models.Ticket.find({where: {refFk: invoiceOut.ref}}); - - const promises = []; - tickets.forEach(ticket => { - promises.push(ticket.updateAttribute('refFk', null, options)); - }); - - await Promise.all(promises); - await invoiceOut.destroy(options); - await transaction.commit(); - return tickets; - } catch (e) { - await transaction.rollback(); - throw e; - } - }; -}; diff --git a/modules/travel/back/methods/travel-thermograph/uploadFile.js b/modules/travel/back/methods/travel/createThermograph.js similarity index 89% rename from modules/travel/back/methods/travel-thermograph/uploadFile.js rename to modules/travel/back/methods/travel/createThermograph.js index 6ac1b4c09c..56a27ddb37 100644 --- a/modules/travel/back/methods/travel-thermograph/uploadFile.js +++ b/modules/travel/back/methods/travel/createThermograph.js @@ -1,16 +1,16 @@ module.exports = Self => { - Self.remoteMethodCtx('uploadFile', { + Self.remoteMethodCtx('createThermograph', { description: 'Upload and attach a document', accessType: 'WRITE', accepts: [{ arg: 'id', - type: 'String', - description: 'The thermograph id', - http: {source: 'path'} - }, { - arg: 'travelId', type: 'Number', description: 'The travel id', + http: {source: 'path'} + }, { + arg: 'thermographId', + type: 'String', + description: 'The thermograph id', required: true }, { arg: 'warehouseId', @@ -41,12 +41,12 @@ module.exports = Self => { root: true }, http: { - path: `/:id/uploadFile`, + path: `/:id/createThermograph`, verb: 'POST' } }); - Self.uploadFile = async(ctx, thermographId, travelId) => { + Self.createThermograph = async(ctx, id, thermographId) => { const models = Self.app.models; const tx = await Self.beginTransaction({}); @@ -64,7 +64,7 @@ module.exports = Self => { await travelThermograph.updateAttributes({ dmsFk: firstDms.id, - travelFk: travelId + travelFk: id }, options); await tx.commit(); diff --git a/modules/travel/back/methods/travel/deleteThermograph.js b/modules/travel/back/methods/travel/deleteThermograph.js new file mode 100644 index 0000000000..ba541c560c --- /dev/null +++ b/modules/travel/back/methods/travel/deleteThermograph.js @@ -0,0 +1,53 @@ + +module.exports = Self => { + Self.remoteMethodCtx('deleteThermograph', { + description: 'Deletes a travel thermograph', + accessType: 'WRITE', + accepts: { + arg: 'id', + type: 'Number', + description: 'The thermograph id', + required: true + }, + returns: { + type: 'object', + root: true + }, + http: { + path: '/deleteThermograph', + verb: 'DELETE' + } + }); + + Self.deleteThermograph = async(ctx, id) => { + const models = Self.app.models; + const userId = ctx.req.accessToken.userId; + const travelThermograph = await models.TravelThermograph.findById(id); + + await models.Dms.removeFile(ctx, travelThermograph.dmsFk); + await Self.rawSql(` + UPDATE travelThermograph + SET travelFk = NULL, dmsFk = NULL + WHERE id = ?`, [id]); + + const oldInstance = { + travelFk: travelThermograph.travelFk, + dmsFk: travelThermograph.dmsFk + }; + + await models.TravelLog.create({ + originFk: travelThermograph.travelFk, + userFk: userId, + action: 'delete', + changedModel: 'TravelThermograph', + changedModelId: id, + oldInstance: oldInstance, + newInstance: {} + }); + + travelThermograph.travelFk = null; + travelThermograph.dmsFk = null; + + return travelThermograph; + }; +}; diff --git a/modules/travel/back/models/travel-thermograph.js b/modules/travel/back/models/travel-thermograph.js deleted file mode 100644 index 8eab0ab7be..0000000000 --- a/modules/travel/back/models/travel-thermograph.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = Self => { - require('../methods/travel-thermograph/uploadFile')(Self); -}; diff --git a/modules/travel/back/models/travel-thermograph.json b/modules/travel/back/models/travel-thermograph.json index 80a5286554..b8f7fa41a1 100644 --- a/modules/travel/back/models/travel-thermograph.json +++ b/modules/travel/back/models/travel-thermograph.json @@ -12,14 +12,13 @@ } }, "properties": { - "thermographFk": { - "type": "String", + "id": { + "type": "Number", "description": "Identifier", "id": true }, "created": { - "type": "Date", - "description": "Identifier" + "type": "Date" }, "temperature": { "type": "String" diff --git a/modules/travel/back/models/travel.js b/modules/travel/back/models/travel.js index 5fa55a366c..895de7af19 100644 --- a/modules/travel/back/models/travel.js +++ b/modules/travel/back/models/travel.js @@ -2,4 +2,6 @@ module.exports = Self => { require('../methods/travel/getTravel')(Self); require('../methods/travel/getEntries')(Self); require('../methods/travel/filter')(Self); + require('../methods/travel/createThermograph')(Self); + require('../methods/travel/deleteThermograph')(Self); }; diff --git a/modules/travel/front/thermograph/create/index.js b/modules/travel/front/thermograph/create/index.js index b12bf47ff5..40a0fafd05 100644 --- a/modules/travel/front/thermograph/create/index.js +++ b/modules/travel/front/thermograph/create/index.js @@ -9,7 +9,6 @@ class Controller { this.vnApp = vnApp; this.vnConfig = vnConfig; this.dms = { - travelId: $state.params.id, hasFileAttached: false, files: [] }; @@ -65,7 +64,7 @@ class Controller { } onSubmit() { - const query = `TravelThermographs/${this.dms.thermographId}/uploadFile`; + const query = `Travels/${this.travel.id}/createThermograph`; const options = { method: 'POST', url: query, diff --git a/modules/travel/front/thermograph/index/index.html b/modules/travel/front/thermograph/index/index.html index 09814e5e97..ca9ebcaea7 100644 --- a/modules/travel/front/thermograph/index/index.html +++ b/modules/travel/front/thermograph/index/index.html @@ -28,6 +28,15 @@ <vn-td expand>{{thermograph.result}}</vn-td> <vn-td>{{thermograph.warehouse.name}}</vn-td> <vn-td>{{thermograph.created | date: 'dd/MM/yyyy'}}</vn-td> + <vn-td shrink> + <a target="_blank" + href="api/dms/{{::thermograph.dmsFk}}/downloadFile?access_token={{::$ctrl.accessToken}}"> + <vn-icon-button + icon="cloud_download" + title="{{'Download file' | translate}}"> + </vn-icon-button> + </a> + </vn-td> <vn-td shrink> <vn-icon-button icon="delete" @@ -45,8 +54,8 @@ <vn-confirm vn-id="confirm" - question="Delete thermograph from travel?" - on-response="$ctrl.removeThermographFromTravel($response)"> + question="Are you sure you want to remove the thermograph?" + on-accept="$ctrl.deleteThermograph()"> </vn-confirm> <a diff --git a/modules/travel/front/thermograph/index/index.js b/modules/travel/front/thermograph/index/index.js index 5a4f13a7b1..48487eb7c5 100644 --- a/modules/travel/front/thermograph/index/index.js +++ b/modules/travel/front/thermograph/index/index.js @@ -3,8 +3,9 @@ import './style.scss'; import Component from 'core/lib/component'; class Controller extends Component { - constructor($element, $) { + constructor($element, $, vnToken) { super($element, $); + this.accessToken = vnToken.token; this.filter = { include: {relation: 'warehouse', @@ -14,8 +15,26 @@ class Controller extends Component { } }; } + + showDeleteConfirm(index) { + this.thermographIndex = index; + this.$.confirm.show(); + } + + deleteThermograph() { + const data = this.travelThermographs; + const thermographId = data[this.thermographIndex].id; + const query = `Travels/deleteThermograph?id=${thermographId}`; + this.$http.delete(query).then(() => { + this.vnApp.showSuccess(this.$translate.instant('Thermograph deleted')); + this.$.model.remove(this.thermographIndex); + this.thermographIndex = null; + }); + } } +Controller.$inject = ['$element', '$scope', 'vnToken']; + ngModule.component('vnTravelThermographIndex', { template: require('./index.html'), controller: Controller, diff --git a/modules/travel/front/thermograph/locale/es.yml b/modules/travel/front/thermograph/locale/es.yml index d6e634ce3a..184e95e738 100644 --- a/modules/travel/front/thermograph/locale/es.yml +++ b/modules/travel/front/thermograph/locale/es.yml @@ -3,7 +3,7 @@ Temperature: Temperatura State: Estado Destination: Destino Created: Creado -Remove thermograph: Eliminar termómetro +Remove thermograph: Eliminar termógrafo Upload file: Subir fichero Edit file: Editar fichero Upload: Subir @@ -11,4 +11,7 @@ File: Fichero FileDescription: Travel id {{travelId}} ContentTypesInfo: 'Tipos de archivo permitidos: {{allowedContentTypes}}' Are you sure you want to continue?: ¿Seguro que quieres continuar? -Add thermograph: Añadir termógrafo \ No newline at end of file +Add thermograph: Añadir termógrafo +Thermograph deleted: Termógrafo eliminado +Thermograph: Termógrafo +Are you sure you want to remove the thermograph?: ¿Seguro que quieres quitar el termógrafo? \ No newline at end of file