feat(save-sign implemented in salix back, refs 3681 @2h
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
This commit is contained in:
parent
8e7e7901af
commit
0f0c4c0554
|
@ -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)) {
|
||||||
const query = `SELECT t.warehouseFk,
|
const query = `SELECT t.warehouseFk,
|
||||||
t.companyFk,
|
t.companyFk,
|
||||||
c.name,
|
c.name,
|
||||||
|
@ -76,26 +78,26 @@ module.exports = Self => {
|
||||||
|
|
||||||
return resultDms.insertId;
|
return resultDms.insertId;
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
|
return 'null';
|
||||||
}
|
}
|
||||||
|
|
||||||
async function gestDocExists(ticket) {
|
async function gestDocExists(ticket) {
|
||||||
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]);
|
||||||
|
|
||||||
if (result.length > 0) {
|
if (result.length < 0)
|
||||||
const isSigned = await Self.rawSql(
|
return false;
|
||||||
`SELECT isSigned FROM vn.ticket WHERE id = ?`, [ticket]
|
|
||||||
);
|
|
||||||
|
|
||||||
if (isSigned[0].isSigned)
|
const isSigned = await Self.rawSql(
|
||||||
return true;
|
`SELECT isSigned FROM vn.ticket WHERE id = ?`, [ticket]
|
||||||
else
|
);
|
||||||
deleteFromGestDoc(ticket);
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
if (isSigned[0].isSigned)
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
deleteFromGestDoc(ticket);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function 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) => {
|
Self.saveSign = async(ctx, signContent, tickets, signedTime, addressFk) => {
|
||||||
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 = `storage/dms`;
|
||||||
|
|
||||||
const dmsDir = await Self.rawSql(`SELECT dmsDir FROM hedera.config`);
|
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]}`;
|
||||||
const result = await Self.rawSql(call);
|
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 (alertLevel >= 2) {
|
||||||
if (!await gestDocExists(tickets[i])) {
|
let dir;
|
||||||
const id = createGestDoc(tickets[i], userId);
|
let id = null;
|
||||||
const hashDir = md5(id).substring(0, 3);
|
let fileName = null;
|
||||||
const dir = `${dmsDir[0].dmsDir}/${hashDir}`;
|
|
||||||
|
|
||||||
if (!fs.existsSync(dir))
|
if (!await gestDocExists(tickets[i])) {
|
||||||
fs.mkdirSync(dir);
|
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();
|
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');
|
||||||
|
|
|
@ -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
|
||||||
(
|
(
|
|
@ -236,6 +236,7 @@
|
||||||
"Modifiable user details only by an administrator": "Detalles de usuario modificables solo por un administrador",
|
"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",
|
"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",
|
||||||
}
|
"this warehouse has not dms": "this warehouse has not dms"
|
||||||
|
}
|
Loading…
Reference in New Issue