From 6c5acc75eb9c243e3127b28454b708f24d884539 Mon Sep 17 00:00:00 2001 From: joan Date: Fri, 7 Oct 2022 12:00:06 +0200 Subject: [PATCH] Consumption fix --- .../methods/client/consumptionSendQueued.js | 80 +++++++++---------- modules/client/front/notification/index.js | 3 +- 2 files changed, 42 insertions(+), 41 deletions(-) diff --git a/modules/client/back/methods/client/consumptionSendQueued.js b/modules/client/back/methods/client/consumptionSendQueued.js index 3f551d3d29..74ccf164d6 100644 --- a/modules/client/back/methods/client/consumptionSendQueued.js +++ b/modules/client/back/methods/client/consumptionSendQueued.js @@ -18,50 +18,50 @@ module.exports = Self => { Self.consumptionSendQueued = async() => { const queues = await Self.rawSql(` SELECT - ccq.id, - c.id AS clientFk, - c.email AS clientEmail, - eu.email salesPersonEmail, - REPLACE(json_extract(params, '$.from'), '"', '') AS fromDate, - REPLACE(json_extract(params, '$.to'), '"', '') AS toDate - FROM clientConsumptionQueue ccq - JOIN client c ON ( - JSON_SEARCH( - JSON_ARRAY( - json_extract(params, '$.clients') - ) - , 'all', c.id) IS NOT NULL) - JOIN account.emailUser eu ON eu.userFk = c.salesPersonFk - JOIN ticket t ON t.clientFk = c.id - JOIN sale s ON s.ticketFk = t.id - JOIN item i ON i.id = s.itemFk - JOIN itemType it ON it.id = i.typeFk - WHERE status = '' - AND it.isPackaging = FALSE - AND DATE(t.shipped) BETWEEN - REPLACE(json_extract(params, '$.from'), '"', '') AND - REPLACE(json_extract(params, '$.to'), '"', '') - GROUP BY c.id`); + id, + params + FROM clientConsumptionQueue + WHERE status = ''`); for (const queue of queues) { try { - const args = { - id: queue.clientFk, - recipient: queue.clientEmail, - replyTo: queue.salesPersonEmail, - from: queue.fromDate, - to: queue.toDate - }; + const params = JSON.parse(queue.params); - const email = new Email('campaign-metrics', args); - await email.send(); + const clients = await Self.rawSql(` + SELECT + c.id AS clientFk, + c.email AS clientEmail, + eu.email salesPersonEmail + FROM client c + JOIN account.emailUser eu ON eu.userFk = c.salesPersonFk + JOIN ticket t ON t.clientFk = c.id + JOIN sale s ON s.ticketFk = t.id + JOIN item i ON i.id = s.itemFk + JOIN itemType it ON it.id = i.typeFk + WHERE c.id IN(?) + AND it.isPackaging = FALSE + AND DATE(t.shipped) BETWEEN ? AND ? + GROUP BY c.id`, [params.clients, params.from, params.to]); - await Self.rawSql(` - UPDATE clientConsumptionQueue - SET status = 'printed', - printed = ? - WHERE id = ?`, - [new Date(), queue.id]); + for (const client of clients) { + const args = { + id: client.clientFk, + recipient: client.clientEmail, + replyTo: client.salesPersonEmail, + from: params.from, + to: params.to + }; + + const email = new Email('campaign-metrics', args); + await email.send(); + + await Self.rawSql(` + UPDATE clientConsumptionQueue + SET status = 'printed', + printed = ? + WHERE id = ?`, + [new Date(), queue.id]); + } } catch (error) { await Self.rawSql(` UPDATE clientConsumptionQueue @@ -69,7 +69,7 @@ module.exports = Self => { WHERE id = ?`, [error.message, queue.id]); - throw e; + throw error; } } diff --git a/modules/client/front/notification/index.js b/modules/client/front/notification/index.js index e70af12b2f..4be96bc17e 100644 --- a/modules/client/front/notification/index.js +++ b/modules/client/front/notification/index.js @@ -77,12 +77,13 @@ export default class Controller extends Section { onSendClientConsumption() { const clientIds = this.checked.map(client => client.id); - const params = { + const data = { clients: clientIds, from: this.campaign.from, to: this.campaign.to }; + const params = JSON.stringify(data); this.$http.post('ClientConsumptionQueues', {params}) .then(() => this.$.filters.hide()) .then(() => this.vnApp.showSuccess(this.$t('Notifications sent!')));