diff --git a/back/methods/dms/deleteTrashFiles.js b/back/methods/dms/deleteTrashFiles.js new file mode 100644 index 000000000..9d16e9d81 --- /dev/null +++ b/back/methods/dms/deleteTrashFiles.js @@ -0,0 +1,57 @@ +const fs = require('fs-extra'); +const path = require('path'); + +module.exports = Self => { + Self.remoteMethod('deleteTrashFiles', { + description: 'Deletes files that have trash type', + accessType: 'WRITE', + returns: { + type: 'object', + root: true + }, + http: { + path: `/deleteTrashFiles`, + verb: 'POST' + } + }); + + Self.deleteTrashFiles = async(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 DmsContainer = models.DmsContainer; + + const trashDmsType = await models.DmsType.findOne({ + where: {code: 'trash'} + }, myOptions); + + const dmsToDelete = await models.Dms.find({ + where: { + dmsTypeFk: trashDmsType.id + } + }, myOptions); + + for (let dms of dmsToDelete) { + const pathHash = DmsContainer.getHash(dms.id); + const dmsContainer = await DmsContainer.container(pathHash); + const dstFile = path.join(dmsContainer.client.root, pathHash, dms.file); + await fs.unlink(dstFile); + await dms.destroy(myOptions); + } + if (tx) await tx.commit(); + + } catch (e) { + if (tx) await tx.rollback(); + + throw e; + } + }; +}; diff --git a/back/models/dms.js b/back/models/dms.js index 91291a0c2..24c072f56 100644 --- a/back/models/dms.js +++ b/back/models/dms.js @@ -5,6 +5,7 @@ module.exports = Self => { require('../methods/dms/uploadFile')(Self); require('../methods/dms/removeFile')(Self); require('../methods/dms/updateFile')(Self); + require('../methods/dms/deleteTrashFiles')(Self); Self.checkRole = async function(ctx, id) { const models = Self.app.models; diff --git a/db/changes/10460-mother/00-dmsForeignKey.sql b/db/changes/10460-mother/00-dmsForeignKey.sql new file mode 100644 index 000000000..f44391a17 --- /dev/null +++ b/db/changes/10460-mother/00-dmsForeignKey.sql @@ -0,0 +1,8 @@ +ALTER TABLE vn.propertyDms DROP FOREIGN KEY propertyDms_FK; +ALTER TABLE vn.propertyDms ADD CONSTRAINT propertyDms_FK FOREIGN KEY (dmsFk) REFERENCES vn.dms(id) ON DELETE CASCADE ON UPDATE CASCADE; + +ALTER TABLE vn.clientDms DROP FOREIGN KEY clientDms_ibfk_2; +ALTER TABLE vn.clientDms ADD CONSTRAINT clientDms_ibfk_2 FOREIGN KEY (dmsFk) REFERENCES vn.dms(id) ON DELETE CASCADE ON UPDATE CASCADE; + +ALTER TABLE vn.workerDocument DROP FOREIGN KEY workerDocument_ibfk_2; +ALTER TABLE vn.workerDocument ADD CONSTRAINT workerDocument_ibfk_2 FOREIGN KEY (document) REFERENCES vn.dms(id) ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/db/changes/10460-mother/01-dmsType.sql b/db/changes/10460-mother/01-dmsType.sql new file mode 100644 index 000000000..e82c086cf --- /dev/null +++ b/db/changes/10460-mother/01-dmsType.sql @@ -0,0 +1,5 @@ +ALTER TABLE vn.dmsType ADD monthToDelete INT UNSIGNED DEFAULT NULL NULL; +ALTER TABLE vn.dmsType MODIFY COLUMN monthToDelete int(10) unsigned DEFAULT NULL NULL COMMENT 'Meses en el pasado para ir borrando registros, dejar a null para no borrarlos nunca'; +UPDATE vn.dmsType + SET monthToDelete=6 + WHERE id=20; diff --git a/db/changes/10460-mother/02-dmsTrigger.sql b/db/changes/10460-mother/02-dmsTrigger.sql new file mode 100644 index 000000000..a92efcfab --- /dev/null +++ b/db/changes/10460-mother/02-dmsTrigger.sql @@ -0,0 +1,18 @@ + +DELIMITER $$ +$$ +CREATE TRIGGER dms_beforeDelete +BEFORE DELETE +ON dms FOR EACH ROW +BEGIN + DECLARE vCanNotBeDeleted INT; + SELECT COUNT(*) INTO vCanNotBeDeleted + FROM dmsType dt + WHERE NOT (code <=> 'trash') + AND dt.id = OLD.dmsTypeFk; + + IF vCanNotBeDeleted THEN + CALL util.throw('A dms can not be deleted'); + END IF; +END$$ +DELIMITER ; diff --git a/db/changes/10460-mother/03-clean.sql b/db/changes/10460-mother/03-clean.sql new file mode 100644 index 000000000..13951394f --- /dev/null +++ b/db/changes/10460-mother/03-clean.sql @@ -0,0 +1,175 @@ +DROP PROCEDURE IF EXISTS vn.clean; + +DELIMITER $$ +$$ +CREATE DEFINER=`root`@`localhost` PROCEDURE `vn`.`clean`() +BEGIN + DECLARE vDateShort DATETIME; + DECLARE vOneYearAgo DATE; + DECLARE vFourYearsAgo DATE; + DECLARE v18Month DATE; + DECLARE v26Month DATE; + DECLARE v3Month DATE; + DECLARE vTrashId varchar(15); + + SET vDateShort = TIMESTAMPADD(MONTH, -2, CURDATE()); + SET vOneYearAgo = TIMESTAMPADD(YEAR,-1,CURDATE()); + SET vFourYearsAgo = TIMESTAMPADD(YEAR,-4,CURDATE()); + SET v18Month = TIMESTAMPADD(MONTH, -18,CURDATE()); + SET v26Month = TIMESTAMPADD(MONTH, -26,CURDATE()); + SET v3Month = TIMESTAMPADD(MONTH, -3, CURDATE()); + + DELETE FROM ticketParking WHERE created < vDateShort; + DELETE FROM routesMonitor WHERE dated < vDateShort; + DELETE FROM workerTimeControlLog WHERE created < vDateShort; + DELETE FROM `message` WHERE sendDate < vDateShort; + DELETE FROM messageInbox WHERE sendDate < vDateShort; + DELETE FROM messageInbox WHERE sendDate < vDateShort; + DELETE FROM workerTimeControl WHERE timed < vFourYearsAgo; + DELETE FROM itemShelving WHERE created < CURDATE() AND visible = 0; + DELETE FROM ticketDown WHERE created < TIMESTAMPADD(DAY,-1,CURDATE()); + DELETE FROM entryLog WHERE creationDate < vDateShort; + DELETE IGNORE FROM expedition WHERE created < v26Month; + DELETE FROM sms WHERE created < v18Month; + DELETE FROM saleTracking WHERE created < vOneYearAgo; + DELETE tobs FROM ticketObservation tobs + JOIN ticket t ON tobs.ticketFk = t.id WHERE t.shipped < TIMESTAMPADD(YEAR,-2,CURDATE()); + DELETE sc.* FROM saleCloned sc JOIN sale s ON s.id = sc.saleClonedFk JOIN ticket t ON t.id = s.ticketFk WHERE t.shipped < vOneYearAgo; + DELETE FROM sharingCart where ended < vDateShort; + DELETE FROM sharingClient where ended < vDateShort; + DELETE tw.* FROM ticketWeekly tw + LEFT JOIN sale s ON s.ticketFk = tw.ticketFk WHERE s.itemFk IS NULL; + DELETE FROM claim WHERE ticketCreated < vFourYearsAgo; + DELETE FROM message WHERE sendDate < vDateShort; + -- Robert ubicacion anterior de trevelLog comentario para debug + DELETE sc FROM saleChecked sc + JOIN sale s ON sc.saleFk = s.id WHERE s.created < vDateShort; + DELETE FROM zoneEvent WHERE `type` = 'day' AND dated < v3Month; + DELETE bm + FROM buyMark bm + JOIN buy b ON b.id = bm.id + JOIN entry e ON e.id = b.entryFk + JOIN travel t ON t.id = e.travelFk + WHERE t.landed <= vDateShort; + DELETE FROM stowaway WHERE created < v3Month; + DELETE FROM vn.buy WHERE created < vDateShort AND entryFk = 9200; + DELETE FROM vn.itemShelvingLog WHERE created < vDateShort; + DELETE FROM vn.stockBuyed WHERE creationDate < vDateShort; + + + -- Equipos duplicados + DELETE w.* + FROM workerTeam w + JOIN (SELECT id, team, workerFk, COUNT(*) - 1 as duplicated + FROM workerTeam + GROUP BY team,workerFk + HAVING duplicated + ) d ON d.team = w.team AND d.workerFk = w.workerFk AND d.id != w.id; + + DELETE sc + FROM saleComponent sc + JOIN sale s ON s.id= sc.saleFk + JOIN ticket t ON t.id= s.ticketFk + WHERE t.shipped < v18Month; + + DELETE c + FROM vn.claim c + JOIN vn.claimState cs ON cs.id = c.claimStateFk + WHERE cs.description = "Anulado" AND + c.created < vDateShort; + DELETE + FROM vn.expeditionTruck + WHERE ETD < v3Month; + + -- borrar travels sin entradas + DROP TEMPORARY TABLE IF EXISTS tmp.thermographToDelete; + CREATE TEMPORARY TABLE tmp.thermographToDelete + SELECT th.id,th.dmsFk + FROM vn.travel t + LEFT JOIN vn.entry e ON e.travelFk = t.id + JOIN vn.travelThermograph th ON th.travelFk = t.id + WHERE t.shipped < TIMESTAMPADD(MONTH, -3, CURDATE()) AND e.travelFk IS NULL; + + SELECT dt.id into vTrashId + FROM vn.dmsType dt + WHERE dt.code = 'trash'; + + UPDATE tmp.thermographToDelete th + JOIN vn.dms d ON d.id = th.dmsFk + SET d.dmsTypeFk = vTrashId; + + DELETE th + FROM tmp.thermographToDelete tmp + JOIN vn.travelThermograph th ON th.id = tmp.id; + + DELETE t + FROM vn.travel t + LEFT JOIN vn.entry e ON e.travelFk = t.id + WHERE t.shipped < TIMESTAMPADD(MONTH, -3, CURDATE()) AND e.travelFk IS NULL; + + UPDATE dms d + JOIN dmsType dt ON dt.id = d.dmsTypeFk + SET d.dmsTypeFk = vTrashId + WHERE created < TIMESTAMPADD(MONTH, -dt.monthToDelete, CURDATE()); + + -- borrar entradas sin compras + DROP TEMPORARY TABLE IF EXISTS tmp.entryToDelete; + CREATE TEMPORARY TABLE tmp.entryToDelete + SELECT e.* + FROM vn.entry e + LEFT JOIN vn.buy b ON b.entryFk = e.id + JOIN vn.entryConfig ec ON e.id != ec.defaultEntry + WHERE e.dated < TIMESTAMPADD(MONTH, -3, CURDATE()) AND b.entryFK IS NULL; + + DELETE e + FROM vn.entry e + JOIN tmp.entryToDelete tmp ON tmp.id = e.id; + + -- borrar de route registros menores a 4 años + DROP TEMPORARY TABLE IF EXISTS tmp.routeToDelete; + CREATE TEMPORARY TABLE tmp.routeToDelete + SELECT * + FROM vn.route r + WHERE created < TIMESTAMPADD(YEAR,-4,CURDATE()); + + UPDATE tmp.routeToDelete tmp + JOIN vn.dms d ON d.id = tmp.gestdocFk + SET d.dmsTypeFk = vTrashId; + + DELETE r + FROM tmp.routeToDelete tmp + JOIN vn.route r ON r.id = tmp.id; + + -- borrar registros de dua y awb menores a 2 años + DROP TEMPORARY TABLE IF EXISTS tmp.duaToDelete; + CREATE TEMPORARY TABLE tmp.duaToDelete + SELECT * + FROM vn.dua + WHERE operated < TIMESTAMPADD(YEAR,-2,CURDATE()); + + UPDATE tmp.duaToDelete tm + JOIN vn.dms d ON d.id = tm.gestdocFk + SET d.dmsTypeFk = vTrashId; + + DELETE d + FROM tmp.duaToDelete tmp + JOIN vn.dua d ON d.id = tmp.id; + + DELETE FROM vn.awb WHERE created < TIMESTAMPADD(YEAR,-2,CURDATE()); + + -- Borra los ficheros gestDoc + INSERT INTO vn.printServerQueue(priorityFk, labelReportFk)VALUES(1,11); + + -- Borra los registros de collection y ticketcollection + DELETE FROM vn.collection WHERE created < vDateShort; + + DROP TEMPORARY TABLE IF EXISTS tmp.thermographToDelete; + DROP TEMPORARY TABLE IF EXISTS tmp.entryToDelete; + DROP TEMPORARY TABLE IF EXISTS tmp.duaToDelete; + + DELETE FROM travelLog WHERE creationDate < v3Month; + + CALL shelving_clean; + +END$$ +DELIMITER ; diff --git a/db/changes/10460-mother/04-acl.sql b/db/changes/10460-mother/04-acl.sql new file mode 100644 index 000000000..c576bf1b0 --- /dev/null +++ b/db/changes/10460-mother/04-acl.sql @@ -0,0 +1,2 @@ +INSERT INTO salix.ACL (model,property,accessType,permission,principalType,principalId) + VALUES ('Dms','deleteTrashFiles','WRITE','ALLOW','ROLE','employee') \ No newline at end of file diff --git a/modules/claim/back/methods/claim/deleteOldFiles.js b/modules/claim/back/methods/claim/deleteOldFiles.js deleted file mode 100644 index 6beb99c88..000000000 --- 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; - } - }; -}; diff --git a/modules/claim/back/models/claim.js b/modules/claim/back/models/claim.js index 339eb6051..18b9b3c87 100644 --- a/modules/claim/back/models/claim.js +++ b/modules/claim/back/models/claim.js @@ -8,5 +8,4 @@ module.exports = Self => { require('../methods/claim/updateClaimAction')(Self); require('../methods/claim/isEditable')(Self); require('../methods/claim/downloadFile')(Self); - require('../methods/claim/deleteOldFiles')(Self); }; diff --git a/modules/invoiceIn/front/dueDay/index.html b/modules/invoiceIn/front/dueDay/index.html index 579ef3609..1a1935e72 100644 --- a/modules/invoiceIn/front/dueDay/index.html +++ b/modules/invoiceIn/front/dueDay/index.html @@ -25,6 +25,9 @@ ng-model="invoiceInDueDay.bankFk" url="Banks" show-field="bank" + select-fields="['id','bank']" + order="id" + search-function="$ctrl.bankSearchFunc($search)" rule> {{id}}: {{bank}} diff --git a/modules/invoiceIn/front/dueDay/index.js b/modules/invoiceIn/front/dueDay/index.js index 22b697f7e..3cc1c81e8 100644 --- a/modules/invoiceIn/front/dueDay/index.js +++ b/modules/invoiceIn/front/dueDay/index.js @@ -17,6 +17,12 @@ class Controller extends Section { this.card.reload(); }); } + + bankSearchFunc($search) { + return /^\d+$/.test($search) + ? {id: $search} + : {bank: {like: '%' + $search + '%'}}; + } } ngModule.vnComponent('vnInvoiceInDueDay', {