From 0f0c4c0554090d5f1d68a32815cc0ab375e6cda4 Mon Sep 17 00:00:00 2001 From: Pau Navarro Date: Wed, 16 Nov 2022 10:28:13 +0100 Subject: [PATCH] feat(save-sign implemented in salix back, refs 3681 @2h --- back/methods/dms/saveSign.js | 123 +++++++++++++----- db/changes/10503-november/00-ACL_saveSign.sql | 2 +- .../01-add_data_to_hedera.config.sql | 24 ++-- loopback/locale/es.json | 7 +- 4 files changed, 106 insertions(+), 50 deletions(-) rename db/changes/{10502-november => 10503-november}/01-add_data_to_hedera.config.sql (56%) diff --git a/back/methods/dms/saveSign.js b/back/methods/dms/saveSign.js index 51da29684..efe7179fb 100644 --- a/back/methods/dms/saveSign.js +++ b/back/methods/dms/saveSign.js @@ -1,4 +1,6 @@ const md5 = require('md5'); +const fs = require('fs-extra'); +const {file} = require('jszip'); module.exports = Self => { Self.remoteMethodCtx('saveSign', { @@ -38,7 +40,7 @@ module.exports = Self => { }); async function createGestDoc(ticketId, userFk) { - if (!gestDocExists(ticketId)) { + if (! await gestDocExists(ticketId)) { const query = `SELECT t.warehouseFk, t.companyFk, c.name, @@ -76,26 +78,26 @@ module.exports = Self => { return resultDms.insertId; } - return null; + + return 'null'; } async function gestDocExists(ticket) { const dMSQuery = `SELECT dmsFk as id FROM vn.ticketDms WHERE ticketFk = ?`; - const result = await Self.rawSql(dMSQuery, [ticket]); + result = await Self.rawSql(dMSQuery, [ticket]); - if (result.length > 0) { - const isSigned = await Self.rawSql( - `SELECT isSigned FROM vn.ticket WHERE id = ?`, [ticket] - ); + if (result.length < 0) + return false; - if (isSigned[0].isSigned) - return true; - else - deleteFromGestDoc(ticket); - } + const isSigned = await Self.rawSql( + `SELECT isSigned FROM vn.ticket WHERE id = ?`, [ticket] + ); - return false; + if (isSigned[0].isSigned) + return true; + else + deleteFromGestDoc(ticket); } async function deleteFromGestDoc(ticket) { @@ -104,43 +106,96 @@ module.exports = Self => { ); } + async function dmsRecover(ticket, signContent) { + const query = `INSERT INTO vn.dmsRecover (ticketFk, sign) VALUES (?, ?)`; + + await Self.rawSql(query, [ticket, signContent]); + } + + async function ticketGestdoc(ticket, dmsFk) { + const query = `REPLACE INTO vn.ticketDms(ticketFk, dmsFk) + VALUES (?, ?)`; + + await Self.rawSql(query, [ticket, dmsFk]); + + const queryVnTicketSetState = `CALL vn.ticket_setState(?, ?)`; + + await Self.rawSql(queryVnTicketSetState, [ticket, 'DELIVERED']); + } + + async function updateGestdoc(file, ticket) { + const query = `UPDATE vn.dms SET file=?, contentType = 'image/png' WHERE id=?`; + + await Self.rawSql(query, [file, ticket]); + } + Self.saveSign = async(ctx, signContent, tickets, signedTime, addressFk) => { let tx = await Self.beginTransaction({}); + try { + const userId = ctx.req.accessToken.userId; - const userId = ctx.req.accessToken.userId; + const dmsDir = `storage/dms`; - const dmsDir = await Self.rawSql(`SELECT dmsDir FROM hedera.config`); + let image = null; - 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); + 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; + alertLevel = result[0].alertLevel; - signedTime ? signedTime != undefined : signedTime = new Date(); + 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 (alertLevel >= 2) { + let dir; + let id = null; + let fileName = null; - if (!fs.existsSync(dir)) - fs.mkdirSync(dir); + if (!await gestDocExists(tickets[i])) { + id = await createGestDoc(tickets[i], userId); + + const hashDir = md5(id).substring(0, 3); + dir = `${dmsDir}/${hashDir}`; + + if (!fs.existsSync(dir)) + fs.mkdirSync(dir); + + fileName = `${id}.png`; + image = `${dir}/${fileName}`; + } + + if (image != null) { + // if the dir does not exist + if (!fs.existsSync(dir)) + dmsRecover(tickets[i], signContent); + else { + // Create the file with the image, + // the name is the variable image, + // the content is the signContent and is in base64 + console.log('image', image); + fs.writeFile(image, signContent, 'base64', async function(err) { + if (err) { + await tx.rollback(); + return 'ERROR'; + } + }); + } + } else + dmsRecover(tickets[i], signContent); + + if (id != null && fileName.length > 0) { + ticketGestdoc(tickets[i], id); + updateGestdoc(id, fileName); + } } } - } - try { if (tx) await tx.commit(); - return { - success: true, - message: 'Sign saved' - }; + return 'OK'; } catch (err) { await tx.rollback(); - throw err; + return 'ERROR'; } }; }; diff --git a/db/changes/10503-november/00-ACL_saveSign.sql b/db/changes/10503-november/00-ACL_saveSign.sql index 549464512..16f9931c4 100644 --- a/db/changes/10503-november/00-ACL_saveSign.sql +++ b/db/changes/10503-november/00-ACL_saveSign.sql @@ -1 +1 @@ -INSERT INTO salix.ACL (model,property,accessType,permission,principalId) VALUES ('Dms','saveSign','*','ALLOW','employee'); +INSERT INTO `salix`.`ACL` (`model`,`property`,`accessType`,`permission`,`principalId`) VALUES ('Dms','saveSign','*','ALLOW','employee'); diff --git a/db/changes/10502-november/01-add_data_to_hedera.config.sql b/db/changes/10503-november/01-add_data_to_hedera.config.sql similarity index 56% rename from db/changes/10502-november/01-add_data_to_hedera.config.sql rename to db/changes/10503-november/01-add_data_to_hedera.config.sql index 73305264c..de75692a9 100644 --- a/db/changes/10502-november/01-add_data_to_hedera.config.sql +++ b/db/changes/10503-november/01-add_data_to_hedera.config.sql @@ -1,17 +1,17 @@ INSERT INTO `hedera`.`config` ( - defaultLang, - https, - cookieLife, - jwtKey, - defaultForm, - restUri, - testRestUri, - guestUser, - testDomain, - productionDomain, - pdfsDir, - dmsDir + `defaultLang`, + `https`, + `cookieLife`, + `jwtKey`, + `defaultForm`, + `restUri`, + `testRestUri`, + `guestUser`, + `testDomain`, + `productionDomain`, + `pdfsDir`, + `dmsDir` ) VALUES ( diff --git a/loopback/locale/es.json b/loopback/locale/es.json index a41315dd1..1760e195a 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -236,6 +236,7 @@ "Modifiable user details only by an administrator": "Detalles de usuario modificables solo por un administrador", "Modifiable password only via recovery or by an administrator": "Contraseña modificable solo a través de la recuperación o por un administrador", "Not enough privileges to edit a client": "No tienes suficientes privilegios para editar un cliente", - "You don't have grant privilege": "No tienes privilegios para dar privilegios", - "You don't own the role and you can't assign it to another user": "No eres el propietario del rol y no puedes asignarlo a otro usuario" -} + "You don't have grant privilege": "No tienes privilegios para dar privilegios", + "You don't own the role and you can't assign it to another user": "No eres el propietario del rol y no puedes asignarlo a otro usuario", + "this warehouse has not dms": "this warehouse has not dms" +} \ No newline at end of file