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:
|
|
|
|
[
|
|
|
|
{
|
2022-12-22 09:35:11 +00:00
|
|
|
arg: 'signContent',
|
2022-11-10 12:37:16 +00:00
|
|
|
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-12-22 09:35:11 +00:00
|
|
|
const models = Self.app.models;
|
|
|
|
if (!await gestDocExists(ticketId)) {
|
|
|
|
const result = await models.Ticket.findOne({
|
|
|
|
where: {
|
|
|
|
id: ticketId
|
|
|
|
},
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
relation: 'warehouse',
|
|
|
|
scope: {
|
|
|
|
fields: ['id']
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
relation: 'client',
|
|
|
|
scope: {
|
|
|
|
fields: ['name']
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
relation: 'route',
|
|
|
|
scope: {
|
|
|
|
fields: ['id']
|
|
|
|
}
|
|
|
|
}
|
2022-11-16 07:11:29 +00:00
|
|
|
]
|
2022-12-22 09:35:11 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const warehouseFk = result.warehouseFk;
|
|
|
|
const companyFk = result.companyFk;
|
|
|
|
const client = result.client.name;
|
|
|
|
const route = result.route.id;
|
|
|
|
|
|
|
|
const resultDmsType = await models.DmsType.findOne({
|
|
|
|
where: {
|
|
|
|
code: 'Ticket'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const resultDms = await models.Dms.create({
|
|
|
|
dmsTypeFk: resultDmsType.id,
|
|
|
|
reference: ticketId,
|
|
|
|
description: `Ticket ${ticketId} Cliente ${client} Ruta ${route}`,
|
|
|
|
companyFk: companyFk,
|
|
|
|
warehouseFk: warehouseFk,
|
|
|
|
workerFk: 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 07:11:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function gestDocExists(ticket) {
|
2022-12-22 09:35:11 +00:00
|
|
|
const models = Self.app.models;
|
|
|
|
const result = await models.TicketDms.findOne({
|
|
|
|
where: {
|
|
|
|
ticketFk: ticket
|
|
|
|
},
|
|
|
|
fields: ['dmsFk']
|
|
|
|
});
|
|
|
|
|
|
|
|
if (result == null)
|
2022-11-16 09:28:13 +00:00
|
|
|
return false;
|
2022-11-16 07:11:29 +00:00
|
|
|
|
2022-12-22 09:35:11 +00:00
|
|
|
const isSigned = await models.Ticket.findOne({
|
|
|
|
where: {
|
|
|
|
id: ticket
|
|
|
|
},
|
|
|
|
fields: ['isSigned']
|
|
|
|
});
|
2022-11-16 07:11:29 +00:00
|
|
|
|
2022-12-22 09:35:11 +00:00
|
|
|
if (isSigned)
|
2022-11-16 09:28:13 +00:00
|
|
|
return true;
|
|
|
|
else
|
2022-12-22 09:35:11 +00:00
|
|
|
await models.Dms.destroyById(ticket);
|
2022-11-16 07:11:29 +00:00
|
|
|
}
|
2022-11-10 12:37:16 +00:00
|
|
|
|
2022-11-16 09:28:13 +00:00
|
|
|
async function dmsRecover(ticket, signContent) {
|
2022-12-22 09:35:11 +00:00
|
|
|
const models = Self.app.models;
|
|
|
|
await models.DmsRecover.create({
|
|
|
|
ticketFk: ticket,
|
|
|
|
sign: signContent
|
|
|
|
});
|
2022-11-16 09:28:13 +00:00
|
|
|
}
|
2022-11-16 07:11:29 +00:00
|
|
|
|
2022-11-16 09:28:13 +00:00
|
|
|
async function ticketGestdoc(ticket, dmsFk) {
|
2022-12-22 09:35:11 +00:00
|
|
|
const models = Self.app.models;
|
|
|
|
models.TicketDms.replaceOrCreate({
|
|
|
|
ticketFk: ticket,
|
|
|
|
dmsFk: 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) {
|
2022-12-22 09:35:11 +00:00
|
|
|
const models = Self.app.models;
|
|
|
|
models.Dms.updateOne({
|
|
|
|
where: {
|
|
|
|
id: ticket
|
|
|
|
},
|
|
|
|
file: file,
|
|
|
|
contentType: 'image/png'
|
|
|
|
});
|
2022-11-16 09:28:13 +00:00
|
|
|
}
|
|
|
|
|
2022-12-22 09:35:11 +00:00
|
|
|
Self.saveSign = async(ctx, signContent, tickets, signedTime) => {
|
|
|
|
const models = Self.app.models;
|
2022-11-16 09:28:13 +00:00
|
|
|
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++) {
|
2022-12-22 09:35:11 +00:00
|
|
|
const alertLevel = await models.TicketState.findOne({
|
|
|
|
where: {
|
|
|
|
ticketFk: tickets[i]
|
|
|
|
},
|
|
|
|
fields: ['alertLevel']
|
|
|
|
});
|
2022-11-16 09:28:13 +00:00
|
|
|
|
2023-01-16 14:18:24 +00:00
|
|
|
signedTime ? signedTime != undefined : signedTime = Date.vnNew();
|
2022-11-16 09:28:13 +00:00
|
|
|
|
|
|
|
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}`;
|
2022-12-22 09:35:11 +00:00
|
|
|
} else
|
2022-11-16 09:28:13 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|