From f0b4c26aeac80bfec5bc59ba9584382f497b3e78 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Tue, 21 Jan 2020 12:21:53 +0100 Subject: [PATCH] added travel thermograph create & remove methods --- back/methods/dms/removeFile.js | 4 +- db/dump/fixtures.sql | 4 +- .../back/methods/travel-thermograph/delete.js | 61 ------------------- .../createThermograph.js} | 18 +++--- .../back/methods/travel/deleteThermograph.js | 53 ++++++++++++++++ .../travel/back/models/travel-thermograph.js | 3 - .../back/models/travel-thermograph.json | 7 +-- modules/travel/back/models/travel.js | 2 + .../travel/front/thermograph/create/index.js | 3 +- .../travel/front/thermograph/index/index.html | 13 +++- .../travel/front/thermograph/index/index.js | 21 ++++++- .../travel/front/thermograph/locale/es.yml | 7 ++- 12 files changed, 109 insertions(+), 87 deletions(-) delete mode 100644 modules/travel/back/methods/travel-thermograph/delete.js rename modules/travel/back/methods/{travel-thermograph/uploadFile.js => travel/createThermograph.js} (89%) create mode 100644 modules/travel/back/methods/travel/deleteThermograph.js delete mode 100644 modules/travel/back/models/travel-thermograph.js diff --git a/back/methods/dms/removeFile.js b/back/methods/dms/removeFile.js index 350bea6bc..93fae9728 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 92580b36e..95ea7b20f 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 a8d6bc762..000000000 --- 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 6ac1b4c09..56a27ddb3 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 000000000..ba541c560 --- /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 8eab0ab7b..000000000 --- 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 80a528655..b8f7fa41a 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 5fa55a366..895de7af1 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 b12bf47ff..40a0fafd0 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 09814e5e9..ca9ebcaea 100644 --- a/modules/travel/front/thermograph/index/index.html +++ b/modules/travel/front/thermograph/index/index.html @@ -28,6 +28,15 @@ {{thermograph.result}} {{thermograph.warehouse.name}} {{thermograph.created | date: 'dd/MM/yyyy'}} + + + + + + + question="Are you sure you want to remove the thermograph?" + on-accept="$ctrl.deleteThermograph()"> { + 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 d6e634ce3..184e95e73 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