From 8e7e7901afa56992e1d7dcbad8b899d1f3955eae Mon Sep 17 00:00:00 2001 From: Pau Navarro Date: Wed, 16 Nov 2022 08:11:29 +0100 Subject: [PATCH] Implement needed functions refs #3681 @1h --- back/methods/dms/saveSign.js | 103 +++++++++++++++--- .../00-ACL_saveSign.sql | 0 2 files changed, 87 insertions(+), 16 deletions(-) rename db/changes/{10502-november => 10503-november}/00-ACL_saveSign.sql (100%) diff --git a/back/methods/dms/saveSign.js b/back/methods/dms/saveSign.js index dc8564a18..51da29684 100644 --- a/back/methods/dms/saveSign.js +++ b/back/methods/dms/saveSign.js @@ -1,3 +1,5 @@ +const md5 = require('md5'); + module.exports = Self => { Self.remoteMethodCtx('saveSign', { description: 'Save sign', @@ -35,37 +37,106 @@ module.exports = Self => { } }); - Self.saveSign = async(ctx, signContent, tickets, signedTime, addressFk) => { - const userId = ctx.req.accessToken.userId; + async function createGestDoc(ticketId, userFk) { + if (!gestDocExists(ticketId)) { + const query = `SELECT t.warehouseFk, + t.companyFk, + c.name, + r.id + FROM vn.ticket t + JOIN vn.client c ON t.clientFk = c.id + JOIN vn.route r ON t.routeFk = r.id + WHERE t.id = ?`; - let dmsDir = await Self.rawSql(`SELECT dmsDir FROM hedera.config`); + const result = await Self.rawSql(query, [ticketId]); - let image; + const warehouseFk = result[0].warehouseFk; + const companyFk = result[0].companyFk; + const client = result[0].name; + const route = result[0].id; - let alertLevels = []; + const queryDmsType = `SELECT id + FROM vn.dmsType + WHERE code='Ticket'`; - for (let i = 0; i < tickets.length; i++) { - let call = `SELECT alertLevel FROM vn.ticketState WHERE ticketFk = ${tickets[i]}`; - let result = await Self.rawSql(call); + const resultDmsType = await Self.rawSql(queryDmsType, []); - alertLevels.push(result[0].alertLevel); + const dmsInsert = `INSERT INTO vn.dms + (dmsTypeFk, reference, description, companyFk, warehouseFk, workerFk) + VALUES (?, ?, ?, ?, ?, ?)`; + + const resultDms = await Self.rawSql( + dmsInsert, + [ + resultDmsType[0].id, ticketId, + `Ticket ${ticketId} Cliente ${client} Ruta ${route}`, + companyFk, warehouseFk, userFk + ] + ); + + return resultDms.insertId; + } + return null; + } + + async function gestDocExists(ticket) { + const dMSQuery = `SELECT dmsFk as id FROM vn.ticketDms WHERE ticketFk = ?`; + + const result = await Self.rawSql(dMSQuery, [ticket]); + + if (result.length > 0) { + const isSigned = await Self.rawSql( + `SELECT isSigned FROM vn.ticket WHERE id = ?`, [ticket] + ); + + if (isSigned[0].isSigned) + return true; + else + deleteFromGestDoc(ticket); } - signedTime ? signedTime != undefined : signedTime = new Date(); + return false; + } + async function deleteFromGestDoc(ticket) { + await Self.rawSql( + `DELETE FROM vn.dms WHERE reference = ?`, [ticket] + ); + } + + Self.saveSign = async(ctx, signContent, tickets, signedTime, addressFk) => { let tx = await Self.beginTransaction({}); + const userId = ctx.req.accessToken.userId; + + const dmsDir = await Self.rawSql(`SELECT dmsDir FROM hedera.config`); + + for (let i = 0; i < tickets.length; i++) { + const call = `SELECT alertLevel FROM vn.ticketState WHERE ticketFk = ${tickets[i]}`; + const result = await Self.rawSql(call); + + alertLevel = result[0].alertLevel; + + signedTime ? signedTime != undefined : signedTime = new Date(); + + if (alertLevel >= 2) { + if (!await gestDocExists(tickets[i])) { + const id = createGestDoc(tickets[i], userId); + const hashDir = md5(id).substring(0, 3); + const dir = `${dmsDir[0].dmsDir}/${hashDir}`; + + if (!fs.existsSync(dir)) + fs.mkdirSync(dir); + } + } + } + try { if (tx) await tx.commit(); return { success: true, - signContent: signContent, - tickets: tickets, - signedTime: signedTime, - addressFk: addressFk, - dmsDir: dmsDir, - alertLevels: alertLevels + message: 'Sign saved' }; } catch (err) { await tx.rollback(); diff --git a/db/changes/10502-november/00-ACL_saveSign.sql b/db/changes/10503-november/00-ACL_saveSign.sql similarity index 100% rename from db/changes/10502-november/00-ACL_saveSign.sql rename to db/changes/10503-november/00-ACL_saveSign.sql