From 5e2b5b4f536a9247b164c7154f93221d3a827b78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Fern=C3=A1ndez=20Sancho?= Date: Sun, 11 Dec 2022 12:48:19 +0100 Subject: [PATCH] Fix database issues with the Increment Custom Metric solution --- .../increment-custom-metric.js | 96 ++++++++++--------- 1 file changed, 53 insertions(+), 43 deletions(-) diff --git a/custom solutions/Increment Custom Metric/increment-custom-metric.js b/custom solutions/Increment Custom Metric/increment-custom-metric.js index 458f796..2f73a48 100644 --- a/custom solutions/Increment Custom Metric/increment-custom-metric.js +++ b/custom solutions/Increment Custom Metric/increment-custom-metric.js @@ -1,55 +1,65 @@ - /** - * Increments a metric in the bot_analytics table - * @title Increment Metric in bot_analytics - * @category Custom - * @author Botpress - * @param metric {string} the name of the metric to increment - * @param n {int} how much to increment it - */ +/** + * Increments a metric in the bot_analytics table + * @title Increment Metric in bot_analytics + * @category Custom + * @author Botpress + * @param metric {string} the name of the metric to increment + * @param n {int} how much to increment it + */ - async function getNewLeads(botId, date, metric) { - var new_leads = await bp.database.raw( - `SELECT * FROM bot_analytics WHERE - botId='${botId}' AND - date = '${date}' AND - channel = '${event.channel}' AND - metric = '${metric}' AND - subMetric = 'n/a'` - ) - try { - return new_leads[0] - } catch (error) { - return undefined - } +async function getNewLeads(botId, date, metric) { + var new_leads = await bp.database.raw( + `SELECT * + FROM bot_analytics + WHERE "botId" = '${botId}' + AND + date = '${date}' + AND + channel = '${event.channel}' + AND + metric = '${metric}' + AND + "subMetric" = 'n/a'` + ) + try { + return new_leads[0] + } catch (error) { + return undefined } +} - const myAction = async (metric, n) => { - let date = event.createdOn.toISOString().split('T')[0] - bp.logger.info(date) +const incrementCustomMetric = async (metric, n) => { + let date = event.createdOn.toISOString().split('T')[0] + bp.logger.info(date) - var new_leads = await getNewLeads(event.botId, date, metric) - if (new_leads) { - new_leads = { ...new_leads, value: new_leads.value + n } // Increment the value - await bp.database.raw( - //Update the database - `UPDATE bot_analytics - SET value = ${new_leads.value} - WHERE botId='${event.botId}' AND - date = '${date}' AND - channel = '${event.channel}' AND - metric = '${metric}' AND - subMetric = 'n/a'` - ) - } else { - await bp.database.insertAndRetrieve('bot_analytics', { + var new_leads = await getNewLeads(event.botId, date, metric) + if (new_leads) { + new_leads = { ...new_leads, value: new_leads.value + n } // Increment the value + await bp.database.raw( + //Update the database + `UPDATE bot_analytics + SET value = ${new_leads.value} + WHERE "botId" = '${event.botId}' + AND + date = '${date}' + AND + channel = '${event.channel}' + AND + metric = '${metric}' + AND + "subMetric" = 'n/a'` + ) + } else { + await bp.database.insertAndRetrieve('bot_analytics', { botId: event.botId, date: date, channel: event.channel, metric: metric, subMetric: 'n/a', value: 1 - }) - } + }, + ['botId', 'date', 'channel', 'metric', 'subMetric']) } +} - return myAction(args.metric, parseInt(args.n)) +return incrementCustomMetric(args.metric, parseInt(args.n))