fixes #5677 Fallo portainer Ticket/SaveSign #1556

Merged
alexandre merged 4 commits from 5677-fallo-saveSign into dev 2023-05-24 11:34:48 +00:00
2 changed files with 21 additions and 17 deletions

View File

@ -290,5 +290,7 @@
"isTaxDataChecked": "Datos comprobados", "isTaxDataChecked": "Datos comprobados",
"comercialId": "Id comercial", "comercialId": "Id comercial",
"comercialName": "Comercial", "comercialName": "Comercial",
"Invalid NIF for VIES": "Invalid NIF for VIES" "Invalid NIF for VIES": "Invalid NIF for VIES",
"Ticket does not exist": "Este ticket no existe",
"Ticket is already signed": "Este ticket ya ha sido firmado"
} }

View File

@ -29,8 +29,7 @@ module.exports = Self => {
} }
}); });
Self.saveSign = async(ctx, options) => { Self.saveSign = async(ctx, tickets, location, signedTime, options) => {
const args = Object.assign({}, ctx.args);
const models = Self.app.models; const models = Self.app.models;
const myOptions = {}; const myOptions = {};
let tx; let tx;
@ -48,9 +47,9 @@ module.exports = Self => {
async function setLocation(ticketId) { async function setLocation(ticketId) {
await models.Delivery.create({ await models.Delivery.create({
ticketFk: ticketId, ticketFk: ticketId,
longitude: args.location.Longitude, longitude: location.Longitude,
latitude: args.location.Latitude, latitude: location.Latitude,
dated: args.signedTime || new Date() dated: signedTime || new Date()
}, myOptions); }, myOptions);
} }
@ -107,9 +106,9 @@ module.exports = Self => {
} }
try { try {
for (let i = 0; i < args.tickets.length; i++) { for (const ticketId of tickets) {
const ticketState = await models.TicketState.findOne( const ticketState = await models.TicketState.findOne(
{where: {ticketFk: args.tickets[i]}, {where: {ticketFk: ticketId},
fields: ['alertLevel'] fields: ['alertLevel']
}, myOptions); }, myOptions);
@ -117,16 +116,19 @@ module.exports = Self => {
fields: ['id'] fields: ['id']
}, myOptions); }, myOptions);
if (!ticketState)
throw new UserError('Ticket does not exist');
if (ticketState.alertLevel < packedAlertLevel.id) if (ticketState.alertLevel < packedAlertLevel.id)
throw new UserError('This ticket cannot be signed because it has not been boxed'); throw new UserError('This ticket cannot be signed because it has not been boxed');
else if (!await gestDocExists(args.tickets[i])) { if (await gestDocExists(ticketId))
if (args.location) setLocation(args.tickets[i]); throw new UserError('Ticket is already signed');
if (!gestDocCreated) await createGestDoc(args.tickets[i]);
await models.TicketDms.create({ticketFk: args.tickets[i], dmsFk: dms[0].id}, myOptions); if (location) setLocation(ticketId);
const ticket = await models.Ticket.findById(args.tickets[i], null, myOptions); if (!gestDocCreated) await createGestDoc(ticketId);
await ticket.updateAttribute('isSigned', true, myOptions); await models.TicketDms.create({ticketFk: ticketId, dmsFk: dms[0].id}, myOptions);
await Self.rawSql(`CALL vn.ticket_setState(?, ?)`, [args.tickets[i], 'DELIVERED'], myOptions); const ticket = await models.Ticket.findById(ticketId, null, myOptions);
} await ticket.updateAttribute('isSigned', true, myOptions);
await Self.rawSql(`CALL vn.ticket_setState(?, ?)`, [ticketId, 'DELIVERED'], myOptions);
} }
if (tx) await tx.commit(); if (tx) await tx.commit();