From c1f457c2637a8f4a1e8dba4876ca0f7a9c118291 Mon Sep 17 00:00:00 2001 From: carlosjr Date: Mon, 29 Mar 2021 15:42:14 +0200 Subject: [PATCH] allowContentTypes and removeFile refactors --- .../methods/claim-dms/allowedContentTypes.js | 2 +- .../back/methods/claim-dms/removeFile.js | 40 ++++++++++++++----- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/modules/claim/back/methods/claim-dms/allowedContentTypes.js b/modules/claim/back/methods/claim-dms/allowedContentTypes.js index 3d4b908767..58b7a9ba34 100644 --- a/modules/claim/back/methods/claim-dms/allowedContentTypes.js +++ b/modules/claim/back/methods/claim-dms/allowedContentTypes.js @@ -3,7 +3,7 @@ module.exports = Self => { description: 'Returns a list of allowed contentTypes', accessType: 'READ', returns: { - type: ['Object'], + type: ['object'], root: true }, http: { diff --git a/modules/claim/back/methods/claim-dms/removeFile.js b/modules/claim/back/methods/claim-dms/removeFile.js index ac546455a4..310d2b941e 100644 --- a/modules/claim/back/methods/claim-dms/removeFile.js +++ b/modules/claim/back/methods/claim-dms/removeFile.js @@ -4,12 +4,12 @@ module.exports = Self => { accessType: 'WRITE', accepts: { arg: 'id', - type: 'Number', + type: 'number', description: 'The document id', http: {source: 'path'} }, returns: { - type: 'Object', + type: 'object', root: true }, http: { @@ -18,16 +18,36 @@ module.exports = Self => { } }); - Self.removeFile = async(ctx, 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'}}); + Self.removeFile = async(ctx, id, options) => { + let tx; + let myOptions = {}; - await models.Dms.removeFile(ctx, targetClaimDms.dmsFk); - await targetClaimDms.destroy(); + if (typeof options == 'object') + Object.assign(myOptions, options); - return targetDms.updateAttribute('dmsTypeFk', trashDmsType.id); + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + try { + const models = Self.app.models; + const targetClaimDms = await models.ClaimDms.findById(id, null, myOptions); + const targetDms = await models.Dms.findById(targetClaimDms.dmsFk, null, myOptions); + const trashDmsType = await models.DmsType.findOne({where: {code: 'trash'}}, myOptions); + + await models.Dms.removeFile(ctx, targetClaimDms.dmsFk, myOptions); + await targetClaimDms.destroy(myOptions); + + await targetDms.updateAttribute('dmsTypeFk', trashDmsType.id, myOptions); + + if (tx) await tx.commit(); + + return targetDms; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } }; };