196 lines
5.9 KiB
JavaScript
196 lines
5.9 KiB
JavaScript
const md5 = require('md5');
|
|
const fs = require('fs-extra');
|
|
|
|
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'
|
|
}
|
|
});
|
|
|
|
async function createGestDoc(ticketId, userFk) {
|
|
if (! await 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 = ?`;
|
|
|
|
const result = await Self.rawSql(query, [ticketId]);
|
|
|
|
const warehouseFk = result[0].warehouseFk;
|
|
const companyFk = result[0].companyFk;
|
|
const client = result[0].name;
|
|
const route = result[0].id;
|
|
|
|
const queryDmsType = `SELECT id
|
|
FROM vn.dmsType
|
|
WHERE code='Ticket'`;
|
|
|
|
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
|
|
]
|
|
);
|
|
|
|
return resultDms.insertId;
|
|
}
|
|
|
|
return 'null';
|
|
}
|
|
|
|
async function gestDocExists(ticket) {
|
|
const dMSQuery = `SELECT dmsFk as id FROM vn.ticketDms WHERE ticketFk = ?`;
|
|
|
|
result = await Self.rawSql(dMSQuery, [ticket]);
|
|
|
|
if (result.length < 0)
|
|
return false;
|
|
|
|
const isSigned = await Self.rawSql(
|
|
`SELECT isSigned FROM vn.ticket WHERE id = ?`, [ticket]
|
|
);
|
|
|
|
if (isSigned[0].isSigned)
|
|
return true;
|
|
else
|
|
deleteFromGestDoc(ticket);
|
|
}
|
|
|
|
async function deleteFromGestDoc(ticket) {
|
|
await Self.rawSql(
|
|
`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) => {
|
|
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();
|
|
return err.message;
|
|
}
|
|
});
|
|
}
|
|
} else
|
|
dmsRecover(tickets[i], signContent);
|
|
|
|
if (id != null && fileName.length > 0) {
|
|
ticketGestdoc(tickets[i], id);
|
|
updateGestdoc(id, fileName);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
return 'OK';
|
|
} catch (err) {
|
|
await tx.rollback();
|
|
throw err.message;
|
|
}
|
|
};
|
|
};
|