closes #3681 move-sign_save-to-salix #1151
|
@ -1,4 +1,6 @@
|
||||||
const md5 = require('md5');
|
const md5 = require('md5');
|
||||||
|
const fs = require('fs-extra');
|
||||||
|
const {file} = require('jszip');
|
||||||
|
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
Self.remoteMethodCtx('saveSign', {
|
Self.remoteMethodCtx('saveSign', {
|
||||||
|
@ -38,7 +40,7 @@ module.exports = Self => {
|
||||||
});
|
});
|
||||||
|
|
||||||
async function createGestDoc(ticketId, userFk) {
|
async function createGestDoc(ticketId, userFk) {
|
||||||
if (!gestDocExists(ticketId)) {
|
if (! await gestDocExists(ticketId)) {
|
||||||
pau marked this conversation as resolved
Outdated
|
|||||||
const query = `SELECT t.warehouseFk,
|
const query = `SELECT t.warehouseFk,
|
||||||
t.companyFk,
|
t.companyFk,
|
||||||
c.name,
|
c.name,
|
||||||
|
@ -76,15 +78,18 @@ module.exports = Self => {
|
||||||
|
|
||||||
return resultDms.insertId;
|
return resultDms.insertId;
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
pau marked this conversation as resolved
Outdated
joan
commented
este return no es necesario, además de que es un string este return no es necesario, además de que es un string
|
|||||||
|
return 'null';
|
||||||
}
|
}
|
||||||
|
|
||||||
async function gestDocExists(ticket) {
|
async function gestDocExists(ticket) {
|
||||||
pau marked this conversation as resolved
Outdated
joan
commented
Utilizar modelos siempre que sea posible Utilizar modelos siempre que sea posible
|
|||||||
const dMSQuery = `SELECT dmsFk as id FROM vn.ticketDms WHERE ticketFk = ?`;
|
const dMSQuery = `SELECT dmsFk as id FROM vn.ticketDms WHERE ticketFk = ?`;
|
||||||
|
|
||||||
const result = await Self.rawSql(dMSQuery, [ticket]);
|
result = await Self.rawSql(dMSQuery, [ticket]);
|
||||||
|
|
||||||
pau marked this conversation as resolved
Outdated
joan
commented
Si no hay resultados siempre devolverá cero Si no hay resultados siempre devolverá cero
pau
commented
Al cambiarlo por los modelos si no hay resultados devuelve null Al cambiarlo por los modelos si no hay resultados devuelve null
|
|||||||
|
if (result.length < 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
if (result.length > 0) {
|
|
||||||
const isSigned = await Self.rawSql(
|
const isSigned = await Self.rawSql(
|
||||||
`SELECT isSigned FROM vn.ticket WHERE id = ?`, [ticket]
|
`SELECT isSigned FROM vn.ticket WHERE id = ?`, [ticket]
|
||||||
);
|
);
|
||||||
|
@ -95,21 +100,43 @@ module.exports = Self => {
|
||||||
deleteFromGestDoc(ticket);
|
deleteFromGestDoc(ticket);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function deleteFromGestDoc(ticket) {
|
async function deleteFromGestDoc(ticket) {
|
||||||
await Self.rawSql(
|
await Self.rawSql(
|
||||||
`DELETE FROM vn.dms WHERE reference = ?`, [ticket]
|
`DELETE FROM vn.dms WHERE reference = ?`, [ticket]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) => {
|
Self.saveSign = async(ctx, signContent, tickets, signedTime, addressFk) => {
|
||||||
joan
commented
No parece que se esté utilizando. Añadir transacción a todos los métodos utilizados dentro de la transacción No parece que se esté utilizando. Añadir transacción a todos los métodos utilizados dentro de la transacción
|
|||||||
let tx = await Self.beginTransaction({});
|
let tx = await Self.beginTransaction({});
|
||||||
|
try {
|
||||||
const userId = ctx.req.accessToken.userId;
|
const userId = ctx.req.accessToken.userId;
|
||||||
|
|
||||||
const dmsDir = await Self.rawSql(`SELECT dmsDir FROM hedera.config`);
|
const dmsDir = `storage/dms`;
|
||||||
|
|
||||||
|
let image = null;
|
||||||
|
|
||||||
for (let i = 0; i < tickets.length; i++) {
|
for (let i = 0; i < tickets.length; i++) {
|
||||||
const call = `SELECT alertLevel FROM vn.ticketState WHERE ticketFk = ${tickets[i]}`;
|
const call = `SELECT alertLevel FROM vn.ticketState WHERE ticketFk = ${tickets[i]}`;
|
||||||
|
@ -120,27 +147,55 @@ module.exports = Self => {
|
||||||
signedTime ? signedTime != undefined : signedTime = new Date();
|
signedTime ? signedTime != undefined : signedTime = new Date();
|
||||||
|
|
||||||
if (alertLevel >= 2) {
|
if (alertLevel >= 2) {
|
||||||
|
let dir;
|
||||||
|
let id = null;
|
||||||
|
let fileName = null;
|
||||||
|
|
||||||
if (!await gestDocExists(tickets[i])) {
|
if (!await gestDocExists(tickets[i])) {
|
||||||
const id = createGestDoc(tickets[i], userId);
|
id = await createGestDoc(tickets[i], userId);
|
||||||
|
|
||||||
const hashDir = md5(id).substring(0, 3);
|
const hashDir = md5(id).substring(0, 3);
|
||||||
const dir = `${dmsDir[0].dmsDir}/${hashDir}`;
|
dir = `${dmsDir}/${hashDir}`;
|
||||||
|
|
||||||
if (!fs.existsSync(dir))
|
if (!fs.existsSync(dir))
|
||||||
fs.mkdirSync(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();
|
if (tx) await tx.commit();
|
||||||
|
|
||||||
return {
|
return 'OK';
|
||||||
success: true,
|
|
||||||
message: 'Sign saved'
|
|
||||||
};
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
await tx.rollback();
|
await tx.rollback();
|
||||||
throw err;
|
return 'ERROR';
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -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');
|
||||||
joan
commented
Mover a la carpeta actual Mover a la carpeta actual
|
|||||||
|
|
|
@ -1,17 +1,17 @@
|
||||||
INSERT INTO
|
INSERT INTO
|
||||||
`hedera`.`config` (
|
`hedera`.`config` (
|
||||||
defaultLang,
|
`defaultLang`,
|
||||||
https,
|
`https`,
|
||||||
cookieLife,
|
`cookieLife`,
|
||||||
jwtKey,
|
`jwtKey`,
|
||||||
defaultForm,
|
`defaultForm`,
|
||||||
restUri,
|
`restUri`,
|
||||||
testRestUri,
|
`testRestUri`,
|
||||||
guestUser,
|
`guestUser`,
|
||||||
testDomain,
|
`testDomain`,
|
||||||
productionDomain,
|
`productionDomain`,
|
||||||
pdfsDir,
|
`pdfsDir`,
|
||||||
dmsDir
|
`dmsDir`
|
||||||
)
|
)
|
||||||
VALUES
|
VALUES
|
||||||
(
|
(
|
|
@ -237,5 +237,6 @@
|
||||||
"Modifiable password only via recovery or by an administrator": "Contraseña modificable solo a través de la recuperación o 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",
|
"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 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 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",
|
||||||
joan
commented
Resolver conflicto Resolver conflicto
|
|||||||
|
"this warehouse has not dms": "this warehouse has not dms"
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Utilizar modelos siempre que sea posible