2022-11-16 07:11:29 +00:00
|
|
|
const md5 = require('md5');
|
2022-11-16 09:28:13 +00:00
|
|
|
const fs = require('fs-extra');
|
2022-11-16 07:11:29 +00:00
|
|
|
|
2022-11-10 12:37:16 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('saveSign', {
|
|
|
|
description: 'Save sign',
|
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts:
|
|
|
|
[
|
|
|
|
{
|
|
|
|
arg: 'singContent',
|
|
|
|
type: 'string',
|
|
|
|
required: true,
|
|
|
|
description: 'The sign content'
|
|
|
|
}, {
|
|
|
|
arg: 'tickets',
|
|
|
|
type: ['number'],
|
|
|
|
required: true,
|
|
|
|
description: 'The tickets'
|
|
|
|
}, {
|
|
|
|
arg: 'signedTime',
|
|
|
|
type: 'date',
|
|
|
|
description: 'The signed time'
|
|
|
|
}, {
|
|
|
|
arg: 'addressFk',
|
|
|
|
type: 'number',
|
|
|
|
required: true,
|
|
|
|
description: 'The address fk'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
returns: {
|
|
|
|
type: 'Object',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/saveSign`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-11-16 07:11:29 +00:00
|
|
|
async function createGestDoc(ticketId, userFk) {
|
2022-11-16 09:28:13 +00:00
|
|
|
if (! await gestDocExists(ticketId)) {
|
2022-11-16 07:11:29 +00:00
|
|
|
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 = ?`;
|
2022-11-10 12:37:16 +00:00
|
|
|
|
2022-11-16 07:11:29 +00:00
|
|
|
const result = await Self.rawSql(query, [ticketId]);
|
2022-11-10 12:37:16 +00:00
|
|
|
|
2022-11-16 07:11:29 +00:00
|
|
|
const warehouseFk = result[0].warehouseFk;
|
|
|
|
const companyFk = result[0].companyFk;
|
|
|
|
const client = result[0].name;
|
|
|
|
const route = result[0].id;
|
2022-11-10 12:37:16 +00:00
|
|
|
|
2022-11-16 07:11:29 +00:00
|
|
|
const queryDmsType = `SELECT id
|
|
|
|
FROM vn.dmsType
|
|
|
|
WHERE code='Ticket'`;
|
2022-11-10 12:37:16 +00:00
|
|
|
|
2022-11-16 07:11:29 +00:00
|
|
|
const resultDmsType = await Self.rawSql(queryDmsType, []);
|
|
|
|
|
|
|
|
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
|
|
|
|
]
|
|
|
|
);
|
2022-11-10 12:37:16 +00:00
|
|
|
|
2022-11-16 07:11:29 +00:00
|
|
|
return resultDms.insertId;
|
2022-11-10 12:37:16 +00:00
|
|
|
}
|
2022-11-16 09:28:13 +00:00
|
|
|
|
|
|
|
return 'null';
|
2022-11-16 07:11:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function gestDocExists(ticket) {
|
|
|
|
const dMSQuery = `SELECT dmsFk as id FROM vn.ticketDms WHERE ticketFk = ?`;
|
|
|
|
|
2022-11-16 09:28:13 +00:00
|
|
|
result = await Self.rawSql(dMSQuery, [ticket]);
|
2022-11-16 07:11:29 +00:00
|
|
|
|
2022-11-16 09:28:13 +00:00
|
|
|
if (result.length < 0)
|
|
|
|
return false;
|
2022-11-16 07:11:29 +00:00
|
|
|
|
2022-11-16 09:28:13 +00:00
|
|
|
const isSigned = await Self.rawSql(
|
|
|
|
`SELECT isSigned FROM vn.ticket WHERE id = ?`, [ticket]
|
|
|
|
);
|
2022-11-16 07:11:29 +00:00
|
|
|
|
2022-11-16 09:28:13 +00:00
|
|
|
if (isSigned[0].isSigned)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
deleteFromGestDoc(ticket);
|
2022-11-16 07:11:29 +00:00
|
|
|
}
|
2022-11-10 12:37:16 +00:00
|
|
|
|
2022-11-16 07:11:29 +00:00
|
|
|
async function deleteFromGestDoc(ticket) {
|
|
|
|
await Self.rawSql(
|
|
|
|
`DELETE FROM vn.dms WHERE reference = ?`, [ticket]
|
|
|
|
);
|
|
|
|
}
|
2022-11-10 12:37:16 +00:00
|
|
|
|
2022-11-16 09:28:13 +00:00
|
|
|
async function dmsRecover(ticket, signContent) {
|
|
|
|
const query = `INSERT INTO vn.dmsRecover (ticketFk, sign) VALUES (?, ?)`;
|
2022-11-10 12:37:16 +00:00
|
|
|
|
2022-11-16 09:28:13 +00:00
|
|
|
await Self.rawSql(query, [ticket, signContent]);
|
|
|
|
}
|
2022-11-16 07:11:29 +00:00
|
|
|
|
2022-11-16 09:28:13 +00:00
|
|
|
async function ticketGestdoc(ticket, dmsFk) {
|
|
|
|
const query = `REPLACE INTO vn.ticketDms(ticketFk, dmsFk)
|
|
|
|
VALUES (?, ?)`;
|
2022-11-16 07:11:29 +00:00
|
|
|
|
2022-11-16 09:28:13 +00:00
|
|
|
await Self.rawSql(query, [ticket, dmsFk]);
|
2022-11-16 07:11:29 +00:00
|
|
|
|
2022-11-16 09:28:13 +00:00
|
|
|
const queryVnTicketSetState = `CALL vn.ticket_setState(?, ?)`;
|
2022-11-16 07:11:29 +00:00
|
|
|
|
2022-11-16 09:28:13 +00:00
|
|
|
await Self.rawSql(queryVnTicketSetState, [ticket, 'DELIVERED']);
|
|
|
|
}
|
2022-11-16 07:11:29 +00:00
|
|
|
|
2022-11-16 09:28:13 +00:00
|
|
|
async function updateGestdoc(file, ticket) {
|
|
|
|
const query = `UPDATE vn.dms SET file=?, contentType = 'image/png' WHERE id=?`;
|
2022-11-16 07:11:29 +00:00
|
|
|
|
2022-11-16 09:28:13 +00:00
|
|
|
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 dmsDir = `storage/dms`;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
alertLevel = result[0].alertLevel;
|
|
|
|
|
|
|
|
signedTime ? signedTime != undefined : signedTime = new Date();
|
|
|
|
|
|
|
|
if (alertLevel >= 2) {
|
|
|
|
let dir;
|
|
|
|
let id = null;
|
|
|
|
let fileName = null;
|
|
|
|
|
|
|
|
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 (!fs.existsSync(dir))
|
|
|
|
dmsRecover(tickets[i], signContent);
|
|
|
|
else {
|
|
|
|
fs.writeFile(image, signContent, 'base64', async function(err) {
|
|
|
|
if (err) {
|
|
|
|
await tx.rollback();
|
2022-11-16 12:43:48 +00:00
|
|
|
return err.message;
|
2022-11-16 09:28:13 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
dmsRecover(tickets[i], signContent);
|
|
|
|
|
|
|
|
if (id != null && fileName.length > 0) {
|
|
|
|
ticketGestdoc(tickets[i], id);
|
|
|
|
updateGestdoc(id, fileName);
|
|
|
|
}
|
2022-11-16 07:11:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-10 12:37:16 +00:00
|
|
|
if (tx) await tx.commit();
|
|
|
|
|
2022-11-16 09:28:13 +00:00
|
|
|
return 'OK';
|
2022-11-10 12:37:16 +00:00
|
|
|
} catch (err) {
|
|
|
|
await tx.rollback();
|
2022-11-16 12:43:48 +00:00
|
|
|
throw err.message;
|
2022-11-10 12:37:16 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|