diff --git a/back/methods/dms/deleteTrashFiles.js b/back/methods/dms/deleteTrashFiles.js index 9d16e9d81..9da508285 100644 --- a/back/methods/dms/deleteTrashFiles.js +++ b/back/methods/dms/deleteTrashFiles.js @@ -1,3 +1,4 @@ +const UserError = require('vn-loopback/util/user-error'); const fs = require('fs-extra'); const path = require('path'); @@ -15,7 +16,7 @@ module.exports = Self => { } }); - Self.deleteTrashFiles = async(options) => { + Self.deleteTrashFiles = async options => { const tx = await Self.beginTransaction({}); const myOptions = {}; @@ -26,6 +27,9 @@ module.exports = Self => { myOptions.transaction = tx; try { + if (process.env.NODE_ENV == 'test') + throw new UserError(`Action not allowed on the test environment`); + const models = Self.app.models; const DmsContainer = models.DmsContainer; @@ -33,9 +37,15 @@ module.exports = Self => { where: {code: 'trash'} }, myOptions); + const date = new Date(); + date.setMonth(date.getMonth() - 4); + const dmsToDelete = await models.Dms.find({ where: { - dmsTypeFk: trashDmsType.id + and: [ + {dmsTypeFk: trashDmsType.id}, + {created: {lt: date}} + ] } }, myOptions); @@ -43,14 +53,19 @@ module.exports = Self => { const pathHash = DmsContainer.getHash(dms.id); const dmsContainer = await DmsContainer.container(pathHash); const dstFile = path.join(dmsContainer.client.root, pathHash, dms.file); + const dstFolder = path.join(dmsContainer.client.root, pathHash); await fs.unlink(dstFile); - await dms.destroy(myOptions); + try { + await fs.rmdir(dstFolder); + await dms.destroy(myOptions); + } catch (err) { + await dms.destroy(myOptions); + } } if (tx) await tx.commit(); - } catch (e) { if (tx) await tx.rollback(); - + throw e; } }; diff --git a/db/changes/10480-june/00-albaran_gestdoc.sql b/db/changes/10480-june/00-albaran_gestdoc.sql new file mode 100644 index 000000000..a0ba93bd3 --- /dev/null +++ b/db/changes/10480-june/00-albaran_gestdoc.sql @@ -0,0 +1,2 @@ +ALTER TABLE `vn2008`.`albaran_gestdoc` DROP FOREIGN KEY fk_albaran_gestdoc_gestdoc1; +ALTER TABLE `vn2008`.`albaran_gestdoc` ADD CONSTRAINT albaran_gestdoc_FK FOREIGN KEY (gestdoc_id) REFERENCES `vn`.`dms`(id) ON DELETE CASCADE ON UPDATE CASCADE; \ No newline at end of file diff --git a/db/dump/structure.sql b/db/dump/structure.sql index 015b620d1..1f787f864 100644 --- a/db/dump/structure.sql +++ b/db/dump/structure.sql @@ -23,6 +23,7 @@ CREATE DATABASE /*!32312 IF NOT EXISTS*/ `account` /*!40100 DEFAULT CHARACTER SE USE `account`; + -- -- Table structure for table `account` --