salix/modules/ticket/back/methods/ticket/saveSign.js

167 lines
6.0 KiB
JavaScript
Raw Normal View History

2023-03-09 07:31:38 +00:00
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('saveSign', {
description: 'Save sign',
accessType: 'WRITE',
accepts:
[
{
arg: 'tickets',
type: ['number'],
required: true,
description: 'The tickets'
},
{
arg: 'location',
type: 'object',
description: 'The employee location the moment the sign is saved'
},
{
arg: 'signedTime',
type: 'date',
description: 'The signed time'
}
],
http: {
path: `/saveSign`,
verb: 'POST'
}
});
2023-05-24 10:16:19 +00:00
Self.saveSign = async(ctx, tickets, location, signedTime, options) => {
const models = Self.app.models;
2023-05-30 07:49:39 +00:00
const myOptions = {userId: ctx.req.accessToken.userId};
let tx;
2024-02-26 13:09:53 +00:00
let ticket;
2024-02-22 13:46:10 +00:00
let dms;
2024-02-26 13:09:53 +00:00
let isSignUploaded;
let externalTickets = [];
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
2024-02-26 13:09:53 +00:00
const dmsTypeTicket = await models.DmsType.findOne({
where: {code: 'ticket'},
fields: ['id']
});
2024-07-19 05:16:24 +00:00
async function setLocation() {
2023-03-09 07:31:38 +00:00
await models.Delivery.create({
2024-07-19 05:16:24 +00:00
ticketFk: ticket.id,
2023-05-24 10:16:19 +00:00
longitude: location.Longitude,
latitude: location.Latitude,
dated: signedTime || new Date()
2023-03-09 07:31:38 +00:00
}, myOptions);
}
2024-07-19 05:16:24 +00:00
async function hasSignDms() {
2024-02-26 13:09:53 +00:00
const hasTicketDms = await Self.rawSql(`
SELECT d.id
FROM ticketDms td
JOIN dms d ON d.id = td.dmsFk
WHERE td.ticketFk = ?
AND d.dmsTypeFk = ?
2024-07-19 05:16:24 +00:00
`, [ticket.id, dmsTypeTicket.id], myOptions);
2024-02-22 13:46:10 +00:00
2024-02-26 13:09:53 +00:00
if (hasTicketDms.length) return true;
}
2024-02-26 13:09:53 +00:00
async function createGestDoc() {
const ctxUploadFile = Object.assign({}, ctx);
ctxUploadFile.args = {
warehouseId: ticket.warehouseFk,
companyId: ticket.companyFk,
2024-02-26 13:09:53 +00:00
dmsTypeId: dmsTypeTicket.id,
reference: ticket.id,
2023-04-20 06:24:43 +00:00
description: `Firma del cliente - Ruta ${ticket.route().id}`,
hasFile: false
};
2024-02-22 13:46:10 +00:00
dms = await models.Dms.uploadFile(ctxUploadFile, myOptions);
2024-02-26 13:09:53 +00:00
// Si se ha subido ya la firma, no se vuelve a subir, ya que si no
// da un error de deadlock en la db
isSignUploaded = true;
}
try {
2023-05-24 10:16:19 +00:00
for (const ticketId of tickets) {
2024-02-26 13:09:53 +00:00
ticket = await models.Ticket.findById(ticketId, {
include: [{
relation: 'address',
scope: {
include: {
relation: 'province',
scope: {
include: {
relation: 'country',
scope: {
fields: ['code']
}
}
}
}
}
}, {
relation: 'route',
scope: {
fields: ['id']
}
}]
}, myOptions);
2024-01-26 09:05:24 +00:00
2024-02-26 13:09:53 +00:00
const ticketState = await models.TicketState.findOne({
where: {ticketFk: ticketId},
fields: ['alertLevel']
}, myOptions);
const packedAlertLevel = await models.AlertLevel.findOne({
where: {code: 'PACKED'},
2024-01-26 09:05:24 +00:00
fields: ['id']
2024-02-22 13:46:10 +00:00
}, myOptions);
2024-01-26 09:05:24 +00:00
if (!ticketState)
throw new UserError('Ticket does not exist');
2024-02-26 13:09:53 +00:00
if (!ticket.route())
throw new UserError('Ticket without route');
2024-01-26 09:05:24 +00:00
if (ticketState.alertLevel < packedAlertLevel.id)
throw new UserError('This ticket cannot be signed because it has not been boxed');
2024-02-26 13:09:53 +00:00
if (await ticket.isSigned)
2024-01-26 09:05:24 +00:00
throw new UserError('Ticket is already signed');
2024-07-19 05:16:24 +00:00
if (location) await setLocation();
if (!await hasSignDms() && !isSignUploaded)
await createGestDoc();
2024-02-26 13:09:53 +00:00
if (isSignUploaded)
await models.TicketDms.create({ticketFk: ticket.id, dmsFk: dms[0].id}, myOptions);
2023-05-24 10:16:19 +00:00
await ticket.updateAttribute('isSigned', true, myOptions);
2024-07-15 14:18:30 +00:00
const [{stateCode}] = await Self.rawSql(`
SELECT
IF((SUM(CASE WHEN est.code = 'DELIVERED' THEN 1 ELSE 0 END) = COUNT(*)),
'DELIVERED','PARTIAL_DELIVERED') stateCode
FROM vn.expedition e
JOIN vn.expeditionStateType est ON est.id = e.stateTypeFk
WHERE e.ticketFk = ?;
`, [ticketId], myOptions);
await Self.rawSql(`CALL vn.ticket_setState(?, ?)`, [ticketId, stateCode], myOptions);
2024-02-22 13:46:10 +00:00
2024-11-12 07:25:23 +00:00
if (ticket?.address()?.province()?.country()?.code != 'ES' && ticket.$cmrFk) {
2024-02-26 13:09:53 +00:00
await models.Ticket.saveCmr(ctx, [ticketId], myOptions);
externalTickets.push(ticketId);
}
}
if (tx) await tx.commit();
} catch (e) {
2024-02-13 08:41:26 +00:00
if (tx) await tx.rollback();
throw e;
}
2024-11-12 07:25:23 +00:00
await models.Ticket.sendCmrEmail(ctx, externalTickets);
};
};