Merge branch 'master' of https://gitea.verdnatura.es/verdnatura/salix
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Juan Ferrer 2024-02-22 16:16:34 +01:00
commit 2bf62dca25
1 changed files with 61 additions and 70 deletions

View File

@ -33,8 +33,8 @@ module.exports = Self => {
const models = Self.app.models; const models = Self.app.models;
const myOptions = {userId: ctx.req.accessToken.userId}; const myOptions = {userId: ctx.req.accessToken.userId};
let tx; let tx;
let ticket; let dms;
let externalTickets = []; let gestDocCreated = false;
if (typeof options == 'object') if (typeof options == 'object')
Object.assign(myOptions, options); Object.assign(myOptions, options);
@ -44,11 +44,6 @@ module.exports = Self => {
myOptions.transaction = tx; myOptions.transaction = tx;
} }
const dmsTypeTicket = await models.DmsType.findOne({
where: {code: 'ticket'},
fields: ['id']
});
async function setLocation(ticketId) { async function setLocation(ticketId) {
await models.Delivery.create({ await models.Delivery.create({
ticketFk: ticketId, ticketFk: ticketId,
@ -58,106 +53,102 @@ module.exports = Self => {
}, myOptions); }, myOptions);
} }
async function hasSignDms(ticketId) { async function gestDocExists(ticketId) {
const ticketDms = await models.TicketDms.findOne({ const ticketDms = await models.TicketDms.findOne({
where: {ticketFk: ticketId}, where: {ticketFk: ticketId},
include: [ fields: ['dmsFk']
{ }, myOptions);
relation: 'dms',
fields: ['id'], if (!ticketDms) return false;
scope: {
where: {dmsTypeFk: dmsTypeTicket.id} const ticket = await models.Ticket.findById(ticketId, {fields: ['isSigned']}, myOptions);
} if (ticket.isSigned == true)
} return true;
] else
}); await models.Dms.destroyAll({where: {reference: ticketId}}, myOptions);
if (ticketDms?.dms()?.id) return true;
return false;
} }
async function createGestDoc() { async function createGestDoc(id) {
const ticket = await models.Ticket.findById(id,
{
include: [
{
relation: 'warehouse',
scope: {
fields: ['id']
}
}, {
relation: 'client',
scope: {
fields: ['name']
}
}, {
relation: 'route',
scope: {
fields: ['id']
}
}
]
}, myOptions);
const dmsType = await models.DmsType.findOne({where: {code: 'Ticket'}, fields: ['id']}, myOptions);
const ctxUploadFile = Object.assign({}, ctx); const ctxUploadFile = Object.assign({}, ctx);
if (ticket.route() === null)
throw new UserError('Ticket without route');
ctxUploadFile.args = { ctxUploadFile.args = {
warehouseId: ticket.warehouseFk, warehouseId: ticket.warehouseFk,
companyId: ticket.companyFk, companyId: ticket.companyFk,
dmsTypeId: dmsTypeTicket.id, dmsTypeId: dmsType.id,
reference: ticket.id, reference: '',
description: `Firma del cliente - Ruta ${ticket.route().id}`, description: `Firma del cliente - Ruta ${ticket.route().id}`,
contentType: 'image/png', hasFile: false
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); gestDocCreated = true;
} }
try { try {
for (const ticketId of tickets) { for (const ticketId of tickets) {
ticket = await models.Ticket.findById(ticketId, { const ticketState = await models.TicketState.findOne(
include: [{ {where: {ticketFk: ticketId},
relation: 'address', fields: ['alertLevel']
scope: { }, myOptions);
include: {
relation: 'province',
scope: {
include: {
relation: 'country',
scope: {
fields: ['code']
}
}
}
}
}
}, {
relation: 'route',
scope: {
fields: ['id']
}
}]
});
const ticketState = await models.TicketState.findOne({ const packedAlertLevel = await models.AlertLevel.findOne({where: {code: 'PACKED'},
where: {ticketFk: ticketId},
fields: ['alertLevel']
});
const packedAlertLevel = await models.AlertLevel.findOne({
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');
if (!ticket.route())
throw new UserError('Ticket without route');
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');
if (await ticket.isSigned) if (await gestDocExists(ticketId))
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)) if (!gestDocCreated) await createGestDoc(ticketId);
await createGestDoc(ticketId); await models.TicketDms.create({ticketFk: ticketId, dmsFk: dms[0].id}, myOptions);
const ticket = await models.Ticket.findById(ticketId, null, 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,
stateFk: deliveryState.id stateFk: deliveryState.id
}, myOptions); }, myOptions);
if (ticket?.address()?.province()?.country()?.code != 'ES') {
await models.Ticket.saveCmr(ctx, [ticketId], myOptions);
externalTickets.push(ticketId);
}
} }
if (tx) await tx.commit(); if (tx) await tx.commit();
return;
} catch (e) { } catch (e) {
if (tx) await tx.rollback(); if (tx) await tx.rollback();
throw e; throw e;
} }
await models.Route.cmrEmail(ctx, externalTickets);
}; };
}; };