From c10e4c04b68ddc3faad0560af700d442e1736716 Mon Sep 17 00:00:00 2001 From: vicent <vicent@verdnatura.es> Date: Fri, 29 Apr 2022 08:23:35 +0200 Subject: [PATCH] refactor: delete file --- .../back/methods/claim/deleteOldFiles.js | 63 ------------------- 1 file changed, 63 deletions(-) delete mode 100644 modules/claim/back/methods/claim/deleteOldFiles.js diff --git a/modules/claim/back/methods/claim/deleteOldFiles.js b/modules/claim/back/methods/claim/deleteOldFiles.js deleted file mode 100644 index 6beb99c88b..0000000000 --- a/modules/claim/back/methods/claim/deleteOldFiles.js +++ /dev/null @@ -1,63 +0,0 @@ -const fs = require('fs-extra'); -const path = require('path'); - -module.exports = Self => { - Self.remoteMethodCtx('deleteOldFiles', { - description: 'Delete files that are 6 months old from dms', - accessType: 'WRITE', - returns: { - type: 'object', - root: true - }, - http: { - path: `/deleteOldFiles`, - verb: 'POST' - } - }); - - Self.deleteOldFiles = async(ctx, options) => { - const tx = await Self.beginTransaction({}); - const myOptions = {}; - - if (typeof options == 'object') - Object.assign(myOptions, options); - - if (!myOptions.transaction) - myOptions.transaction = tx; - - try { - const models = Self.app.models; - const sixMonthsAgo = new Date(); - sixMonthsAgo.setMonth(sixMonthsAgo.getMonth() - 6); - - const claimDmsType = await models.DmsType.findOne({where: {code: 'claim'}}, myOptions); - const oldDms = await models.Dms.find({ - where: { - and: [{ - created: {lt: sixMonthsAgo}}, { - dmsTypeFk: claimDmsType.id - }] - } - }); - - for (let dms of oldDms) { - const ClaimContainer = models.ClaimContainer; - const pathHash = await ClaimContainer.getHash(dms.id); - const claimContainer = await ClaimContainer.container(pathHash); - const dstFile = path.join(claimContainer.client.root, pathHash, dms.file); - await fs.unlink(dstFile); - await models.ClaimDms.removeFile(ctx, dms.id); - } - if (tx) await tx.commit(); - - return oldDms; - } catch (e) { - if (tx) await tx.rollback(); - - if (fs.existsSync(srcFile)) - await fs.unlink(srcFile); - - throw e; - } - }; -};