This commit is contained in:
parent
3f0d301870
commit
413f7eb8c8
|
@ -48,23 +48,18 @@ module.exports = Self => {
|
||||||
if (!recipient)
|
if (!recipient)
|
||||||
throw new UserError('There is no assigned email for this client');
|
throw new UserError('There is no assigned email for this client');
|
||||||
|
|
||||||
const dms = await models.TicketDms.findOne({
|
const dms = await Self.rawSql(`
|
||||||
where: {ticketFk: ticketId},
|
SELECT d.id
|
||||||
include: [{
|
FROM ticketDms td
|
||||||
relation: 'dms',
|
JOIN dms d ON d.id = td.dmsFk
|
||||||
fields: ['id'],
|
JOIN dmsType dt ON dt.id = d.dmsTypeFk
|
||||||
scope: {
|
WHERE td.ticketFk = ?
|
||||||
relation: 'dmsType',
|
AND dt.code = 'cmr'
|
||||||
scope: {
|
`, [ticketId]);
|
||||||
where: {code: 'cmr'}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}]
|
|
||||||
}, myOptions);
|
|
||||||
|
|
||||||
if (!dms) throw new UserError('Cmr file does not exist');
|
if (!dms.lenght) throw new UserError('Cmr file does not exist');
|
||||||
|
|
||||||
const response = await models.Dms.downloadFile(ctx, dms.id);
|
const response = await models.Dms.downloadFile(ctx, dms[0].id);
|
||||||
|
|
||||||
const email = new Email('cmr', {
|
const email = new Email('cmr', {
|
||||||
ticketId,
|
ticketId,
|
||||||
|
|
|
@ -42,18 +42,15 @@ module.exports = Self => {
|
||||||
const ticket = await models.Ticket.findById(ticketId, myOptions);
|
const ticket = await models.Ticket.findById(ticketId, myOptions);
|
||||||
|
|
||||||
if (ticket.cmrFk) {
|
if (ticket.cmrFk) {
|
||||||
const hasDmsCmr = await models.TicketDms.findOne({
|
const hasDmsCmr = await Self.rawSql(`
|
||||||
where: {ticketFk: ticketId},
|
SELECT d.id
|
||||||
include: {
|
FROM ticketDms td
|
||||||
relation: 'dms',
|
JOIN dms d ON d.id = td.dmsFk
|
||||||
fields: ['dmsFk'],
|
WHERE td.ticketFk = ?
|
||||||
scope: {
|
AND d.dmsTypeFk = ?
|
||||||
where: {dmsTypeFk: dmsTypeCmr.id}
|
`, [ticketId, dmsTypeCmr.id]);
|
||||||
}
|
|
||||||
}
|
|
||||||
}, myOptions);
|
|
||||||
|
|
||||||
if (hasDmsCmr?.dms())
|
if (hasDmsCmr.length)
|
||||||
throw new UserError('This ticket already has a cmr saved');
|
throw new UserError('This ticket already has a cmr saved');
|
||||||
|
|
||||||
ctx.args.id = ticket.cmrFk;
|
ctx.args.id = ticket.cmrFk;
|
||||||
|
|
|
@ -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 dms;
|
let ticket;
|
||||||
let gestDocCreated = false;
|
let externalTickets = [];
|
||||||
|
|
||||||
if (typeof options == 'object')
|
if (typeof options == 'object')
|
||||||
Object.assign(myOptions, options);
|
Object.assign(myOptions, options);
|
||||||
|
@ -44,6 +44,11 @@ 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,
|
||||||
|
@ -53,102 +58,104 @@ module.exports = Self => {
|
||||||
}, myOptions);
|
}, myOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function gestDocExists(ticketId) {
|
async function hasSignDms(ticketId) {
|
||||||
const ticketDms = await models.TicketDms.findOne({
|
const ticketDms = await models.TicketDms.findOne({
|
||||||
where: {ticketFk: ticketId},
|
where: {ticketFk: ticketId},
|
||||||
fields: ['dmsFk']
|
include: [
|
||||||
}, myOptions);
|
{
|
||||||
|
relation: 'dms',
|
||||||
if (!ticketDms) return false;
|
fields: ['id'],
|
||||||
|
scope: {
|
||||||
const ticket = await models.Ticket.findById(ticketId, {fields: ['isSigned']}, myOptions);
|
where: {dmsTypeFk: dmsTypeTicket.id}
|
||||||
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(id) {
|
async function createGestDoc() {
|
||||||
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: dmsType.id,
|
dmsTypeId: dmsTypeTicket.id,
|
||||||
reference: '',
|
reference: ticket.id,
|
||||||
description: `Firma del cliente - Ruta ${ticket.route().id}`,
|
description: `Firma del cliente - Ruta ${ticket.route().id}`,
|
||||||
hasFile: false
|
hasFile: true
|
||||||
};
|
};
|
||||||
dms = await models.Dms.uploadFile(ctxUploadFile, myOptions);
|
const dms = await models.Dms.uploadFile(ctxUploadFile, myOptions);
|
||||||
gestDocCreated = true;
|
await models.TicketDms.create({ticketFk: ticket.id, dmsFk: dms[0].id}, myOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
for (const ticketId of tickets) {
|
for (const ticketId of tickets) {
|
||||||
const ticketState = await models.TicketState.findOne(
|
ticket = await models.Ticket.findById(ticketId, {
|
||||||
{where: {ticketFk: ticketId},
|
include: [{
|
||||||
fields: ['alertLevel']
|
relation: 'address',
|
||||||
}, myOptions);
|
scope: {
|
||||||
|
include: {
|
||||||
|
relation: 'province',
|
||||||
|
scope: {
|
||||||
|
include: {
|
||||||
|
relation: 'country',
|
||||||
|
scope: {
|
||||||
|
fields: ['code']
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
relation: 'route',
|
||||||
|
scope: {
|
||||||
|
fields: ['id']
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
});
|
||||||
|
|
||||||
const packedAlertLevel = await models.AlertLevel.findOne({where: {code: 'PACKED'},
|
const ticketState = await models.TicketState.findOne({
|
||||||
|
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 gestDocExists(ticketId))
|
if (await ticket.isSigned)
|
||||||
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 (!gestDocCreated) await createGestDoc(ticketId);
|
if (!await hasSignDms(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: {
|
where: {code: 'DELIVERED'}
|
||||||
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' && ticket.cmrFk) {
|
||||||
|
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);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue