refs #6184 fixSaveSign #2096

Merged
guillermo merged 5 commits from 6184-fixSaveSign into test 2024-02-26 13:00:35 +00:00
2 changed files with 24 additions and 21 deletions

View File

@ -57,7 +57,7 @@ module.exports = Self => {
AND dt.code = 'cmr' AND dt.code = 'cmr'
`, [ticketId]); `, [ticketId]);
if (!dms.lenght) throw new UserError('Cmr file does not exist'); if (!dms.length) throw new UserError('Cmr file does not exist');
const response = await models.Dms.downloadFile(ctx, dms[0].id); const response = await models.Dms.downloadFile(ctx, dms[0].id);

View File

@ -34,6 +34,8 @@ module.exports = Self => {
const myOptions = {userId: ctx.req.accessToken.userId}; const myOptions = {userId: ctx.req.accessToken.userId};
let tx; let tx;
let ticket; let ticket;
let dms;
let isSignUploaded;
let externalTickets = []; let externalTickets = [];
if (typeof options == 'object') if (typeof options == 'object')
@ -59,19 +61,15 @@ module.exports = Self => {
} }
async function hasSignDms(ticketId) { async function hasSignDms(ticketId) {
const ticketDms = await models.TicketDms.findOne({ const hasTicketDms = await Self.rawSql(`
where: {ticketFk: ticketId}, SELECT d.id
include: [ FROM ticketDms td
{ JOIN dms d ON d.id = td.dmsFk
relation: 'dms', WHERE td.ticketFk = ?
fields: ['id'], AND d.dmsTypeFk = ?
scope: { `, [ticketId, dmsTypeTicket.id], myOptions);
where: {dmsTypeFk: dmsTypeTicket.id}
} if (hasTicketDms.length) return true;
}
]
});
if (ticketDms?.dms()?.id) return true;
} }
async function createGestDoc() { async function createGestDoc() {
@ -84,8 +82,10 @@ module.exports = Self => {
description: `Firma del cliente - Ruta ${ticket.route().id}`, description: `Firma del cliente - Ruta ${ticket.route().id}`,
hasFile: true hasFile: true
}; };
const dms = await models.Dms.uploadFile(ctxUploadFile, myOptions); dms = await models.Dms.uploadFile(ctxUploadFile, myOptions);
await models.TicketDms.create({ticketFk: ticket.id, dmsFk: dms[0].id}, myOptions); // 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 { try {
@ -112,17 +112,17 @@ module.exports = Self => {
fields: ['id'] fields: ['id']
} }
}] }]
}); }, myOptions);
const ticketState = await models.TicketState.findOne({ const ticketState = await models.TicketState.findOne({
where: {ticketFk: ticketId}, where: {ticketFk: ticketId},
fields: ['alertLevel'] fields: ['alertLevel']
}); }, myOptions);
const packedAlertLevel = await models.AlertLevel.findOne({ const packedAlertLevel = await models.AlertLevel.findOne({
where: {code: 'PACKED'}, where: {code: 'PACKED'},
fields: ['id'] fields: ['id']
}); }, myOptions);
if (!ticketState) if (!ticketState)
throw new UserError('Ticket does not exist'); throw new UserError('Ticket does not exist');
@ -134,12 +134,15 @@ module.exports = Self => {
throw new UserError('Ticket is already signed'); throw new UserError('Ticket is already signed');
if (location) await setLocation(ticketId); if (location) await setLocation(ticketId);
if (!await hasSignDms(ticketId)) await createGestDoc(ticketId); if (!await hasSignDms(ticketId) && !isSignUploaded)
await createGestDoc(ticketId);
if (isSignUploaded)
await models.TicketDms.create({ticketFk: ticket.id, dmsFk: dms[0].id}, myOptions);
await ticket.updateAttribute('isSigned', true, myOptions); await ticket.updateAttribute('isSigned', true, myOptions);
const deliveryState = await models.State.findOne({ const deliveryState = await models.State.findOne({
where: {code: 'DELIVERED'} where: {code: 'DELIVERED'}
}); }, myOptions);
await models.Ticket.state(ctx, { await models.Ticket.state(ctx, {
ticketFk: ticketId, ticketFk: ticketId,