From 43ff04b2af8ae2826a6abbe4f32cabf7ef45397a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Salgado?= Date: Fri, 23 Jul 2021 04:27:15 +0100 Subject: [PATCH] fix: dynamic query --- lib/loyalty.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/loyalty.js b/lib/loyalty.js index d49bba36..606c70d7 100644 --- a/lib/loyalty.js +++ b/lib/loyalty.js @@ -78,9 +78,9 @@ function getCustomersWithDiscounts (discounts) { const idCardNumbersSql = _.map(pgp.as.text, idCardNumbers).join(',') const hasPhoneNumbers = !_.isEmpty(phoneNumbers) - const hasIDNumbers = !_.isEmpty(phoneNumbers) + const hasIDNumbers = !_.isEmpty(idCardNumbers) - const sql = `SELECT * FROM customers WHERE ${hasPhoneNumbers && `phone IN ($1^)`} ${hasPhoneNumbers && hasIDNumbers && `OR`} ${hasIDNumbers && `id_card_data_number IN ($2^)`}` + const sql = `SELECT * FROM customers WHERE ${hasPhoneNumbers ? `phone IN ($1^)` : ``} ${hasPhoneNumbers && hasIDNumbers ? `OR` : ``} ${hasIDNumbers ? `id_card_data_number IN ($2^)` : ``}` return db.any(sql, [phoneNumbersSql, idCardNumbersSql]) .then(res => _.map(it => it ? _.mapKeys(_.camelCase, it) : null, res)) }