Removed temporary table on ticket closure
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Joan Sanchez 2020-09-14 08:51:46 +02:00
parent db081501eb
commit edf3f1e96e
1 changed files with 35 additions and 42 deletions

View File

@ -10,11 +10,9 @@ module.exports = app => {
message: 'Task executed successfully'
});
await db.rawSql(`DROP TEMPORARY TABLE IF EXISTS tmp.ticket_close`);
await db.rawSql(`
CREATE TEMPORARY TABLE tmp.ticket_close ENGINE = MEMORY (
const tickets = await db.rawSql(`
SELECT
t.id AS ticketFk
t.id
FROM expedition e
JOIN ticket t ON t.id = e.ticketFk
JOIN warehouse wh ON wh.id = t.warehouseFk AND wh.hasComission
@ -23,10 +21,10 @@ module.exports = app => {
WHERE al.code = 'PACKED'
AND DATE(t.shipped) BETWEEN DATE_ADD(CURDATE(), INTERVAL -2 DAY) AND CURDATE()
AND t.refFk IS NULL
GROUP BY e.ticketFk)`);
await closeAll(req.args);
GROUP BY e.ticketFk`);
const ticketIds = tickets.map(ticket => ticket.id);
await closeAll(ticketIds, req.args);
await db.rawSql(`
UPDATE ticket t
JOIN ticketState ts ON t.id = ts.ticketFk
@ -54,11 +52,9 @@ module.exports = app => {
message: 'Task executed successfully'
});
await db.rawSql(`DROP TEMPORARY TABLE IF EXISTS tmp.ticket_close`);
await db.rawSql(`
CREATE TEMPORARY TABLE tmp.ticket_close ENGINE = MEMORY (
const tickets = await db.rawSql(`
SELECT
t.id AS ticketFk
t.id
FROM expedition e
JOIN ticket t ON t.id = e.ticketFk
JOIN ticketState ts ON ts.ticketFk = t.id
@ -66,11 +62,12 @@ module.exports = app => {
WHERE al.code = 'PACKED'
AND t.id = :ticketId
AND t.refFk IS NULL
GROUP BY e.ticketFk)`, {
GROUP BY e.ticketFk`, {
ticketId: reqArgs.ticketId
});
const ticketIds = tickets.map(ticket => ticket.id);
await closeAll(reqArgs);
await closeAll(ticketIds, reqArgs);
} catch (error) {
next(error);
}
@ -92,11 +89,9 @@ module.exports = app => {
message: 'Task executed successfully'
});
await db.rawSql(`DROP TEMPORARY TABLE IF EXISTS tmp.ticket_close`);
await db.rawSql(`
CREATE TEMPORARY TABLE tmp.ticket_close ENGINE = MEMORY (
const tickets = await db.rawSql(`
SELECT
t.id AS ticketFk
t.id
FROM expedition e
JOIN ticket t ON t.id = e.ticketFk
JOIN ticketState ts ON ts.ticketFk = t.id
@ -106,13 +101,14 @@ module.exports = app => {
AND t.warehouseFk = :warehouseId
AND DATE(t.shipped) BETWEEN DATE_ADD(:to, INTERVAL -2 DAY) AND :to
AND t.refFk IS NULL
GROUP BY e.ticketFk)`, {
GROUP BY e.ticketFk`, {
agencyModeId: reqArgs.agencyModeId,
warehouseId: reqArgs.warehouseId,
to: reqArgs.to
});
const ticketIds = tickets.map(ticket => ticket.id);
await closeAll(reqArgs);
await closeAll(ticketIds, reqArgs);
} catch (error) {
next(error);
}
@ -128,11 +124,9 @@ module.exports = app => {
message: 'Task executed successfully'
});
await db.rawSql(`DROP TEMPORARY TABLE IF EXISTS tmp.ticket_close`);
await db.rawSql(`
CREATE TEMPORARY TABLE tmp.ticket_close ENGINE = MEMORY (
const tickets = await db.rawSql(`
SELECT
t.id AS ticketFk
t.id
FROM expedition e
JOIN ticket t ON t.id = e.ticketFk
JOIN ticketState ts ON ts.ticketFk = t.id
@ -140,17 +134,18 @@ module.exports = app => {
WHERE al.code = 'PACKED'
AND t.routeFk = :routeId
AND t.refFk IS NULL
GROUP BY e.ticketFk)`, {
GROUP BY e.ticketFk`, {
routeId: reqArgs.routeId
});
const ticketIds = tickets.map(ticket => ticket.id);
await closeAll(reqArgs);
await closeAll(ticketIds, reqArgs);
} catch (error) {
next(error);
}
});
async function closeAll(reqArgs) {
async function closeAll(ticketIds, reqArgs) {
const failedtickets = [];
const tickets = await db.rawSql(`
SELECT
@ -162,12 +157,12 @@ module.exports = app => {
c.hasToInvoice,
co.hasDailyInvoice,
eu.email salesPersonEmail
FROM tmp.ticket_close tt
JOIN ticket t ON t.id = tt.ticketFk
FROM ticket t
JOIN client c ON c.id = t.clientFk
JOIN province p ON p.id = c.provinceFk
JOIN country co ON co.id = p.countryFk
LEFT JOIN account.emailUser eu ON eu.userFk = c.salesPersonFk`);
LEFT JOIN account.emailUser eu ON eu.userFk = c.salesPersonFk
WHERE t.id IN(?)`, [ticketIds]);
for (const ticket of tickets) {
try {
@ -224,7 +219,5 @@ module.exports = app => {
html: body
});
}
await db.rawSql(`DROP TEMPORARY TABLE tmp.ticket_close`);
}
};