From ded25bb18170b84becc562893a7a36caf4895d2b Mon Sep 17 00:00:00 2001 From: Ryan Kotzen Date: Tue, 19 Nov 2024 14:14:06 +0200 Subject: [PATCH 1/2] fix for additional edge case --- lib/make/makeFilterCondition.js | 13 ++- test/bug-fix-tests/bug-fix.json | 156 +++++++++++++++++++++++++++++ test/bug-fix-tests/bug-fix.test.js | 42 ++++++++ 3 files changed, 207 insertions(+), 4 deletions(-) diff --git a/lib/make/makeFilterCondition.js b/lib/make/makeFilterCondition.js index 15f13fc..8034790 100644 --- a/lib/make/makeFilterCondition.js +++ b/lib/make/makeFilterCondition.js @@ -201,7 +201,8 @@ function processBinaryExpression( side, prefixLeft, prefixTable, - aliases + aliases, + inSwitchStatement ); } @@ -378,6 +379,7 @@ function processQueryOperator( * @param {boolean} [prefixLeft] - include $$ for inner variables * @param {boolean} [prefixTable] - include the table in the prefix * @param {string[]} [aliases] - the aliases used in the joins + * @param {boolean} [inSwitchStatement] - specifies if we are in a switch for special conditions, default false * @returns {any} - the filter expression */ function processOperator( @@ -388,7 +390,8 @@ function processOperator( side, prefixLeft, prefixTable, - aliases + aliases, + inSwitchStatement = false ) { const operation = operatorMap[queryPart.operator]; if (!operation) { @@ -402,7 +405,8 @@ function processOperator( 'left', prefixLeft, prefixTable, - aliases + aliases, + inSwitchStatement ); const secondFilter = makeFilterCondition( queryPart.right, @@ -412,7 +416,8 @@ function processOperator( 'right', prefixLeft, prefixTable, - aliases + aliases, + inSwitchStatement ); return { [operation]: [firstFilter, secondFilter], diff --git a/test/bug-fix-tests/bug-fix.json b/test/bug-fix-tests/bug-fix.json index f04d292..d436d67 100644 --- a/test/bug-fix-tests/bug-fix.json +++ b/test/bug-fix-tests/bug-fix.json @@ -1098,6 +1098,162 @@ "Funded": 0 } ] + }, + "case-2": { + "pipeline": [ + { + "$project": { + "_id": "$_id", + "Id": "$Id", + "Account": "$Account__c", + "AccountName": "$Account_Name", + "AccountFundingStatus": "$Account_Account_Funding_Status", + "AccountLegalName": "$Account_Legal_Name__c", + "AccountContractPrinciple": "$Account_Contract_Principle", + "ApplicationStatus": "$Application_Status__c", + "ClosedBy": "$Closed_By__c", + "Funded": { + "$switch": { + "branches": [ + { + "case": { + "$and": [ + { + "$and": [ + { + "$and": [ + { + "$and": [ + { + "$and": [ + { + "$and": [ + { + "$and": [ + { + "$and": [ + { + "$and": [ + { + "$and": [ + { + "$ne": [ + "$Source_Initiative", + "Automated Mining" + ] + }, + { + "$eq": [ + "$Product__c", + "merchantcashadvance" + ] + } + ] + }, + { + "$eq": [ + "$Type__c", + "New Deal" + ] + } + ] + }, + { + "$ne": [ + "$Sales_Executive_User", + "Pool of Hope" + ] + } + ] + }, + { + "$ne": [ + "$Closed_By__c", + "System" + ] + } + ] + }, + { + "$ne": [ + "$Account_Name", + null + ] + } + ] + }, + { + "$ne": [ + "$Account_Account_Funding_Status", + "Contactless" + ] + } + ] + }, + { + "$and": [ + { + "$not": { + "$in": [ + "$Closed_Reason_New__c", + [ + "Duplicate", + "Qualifying Criteria", + "Contact data wrong", + "poor contactability", + "unsupported industry", + "MC Balance too high", + "wants to become MC partner", + "poor MC payment efficiency" + ] + ] + } + }, + { + "$eq": [ + "$Application_Status__c", + "Complete" + ] + } + ] + } + ] + }, + { + "$ne": [ + "$Closed_By__c", + "Underwriting" + ] + } + ] + }, + { + "$eq": [ + "$Credit_Status__c", + "Approved" + ] + } + ] + }, + { + "$eq": [ + "$Status__c", + "Active - Disbursed" + ] + } + ] + }, + "then": 1 + } + ], + "default": { + "$literal": 0 + } + } + } + } + } + ] } } } \ No newline at end of file diff --git a/test/bug-fix-tests/bug-fix.test.js b/test/bug-fix-tests/bug-fix.test.js index 703aab0..852627e 100644 --- a/test/bug-fix-tests/bug-fix.test.js +++ b/test/bug-fix-tests/bug-fix.test.js @@ -2012,5 +2012,47 @@ describe('bug-fixes', function () { schemas: {}, }); }); + it('should generate and execute a valid query when complicated', async () => { + // noinspection SqlNoDataSourceInspection + const queryString = ` + SELECT + _id, + Id, + Account__c AS Account, + Account_Name AS AccountName, + Account_Account_Funding_Status AS AccountFundingStatus, + Account_Legal_Name__c AS AccountLegalName, + Account_Contract_Principle AS AccountContractPrinciple, + Application_Status__c AS ApplicationStatus, + Closed_By__c AS ClosedBy, + CASE + WHEN Source_Initiative != 'Automated Mining' + AND Product__c = 'merchantcashadvance' + AND Type__c = 'New Deal' + AND Sales_Executive_User != 'Pool of Hope' + AND Closed_By__c != 'System' + AND Account_Name IS NOT NULL + AND Account_Account_Funding_Status != 'Contactless' + AND Closed_Reason_New__c NOT IN ('Duplicate','Qualifying Criteria','Contact data wrong','poor contactability','unsupported industry','MC Balance too high','wants to become MC partner','poor MC payment efficiency') + AND Application_Status__c = 'Complete' + AND Closed_By__c != 'Underwriting' + AND Credit_Status__c = 'Approved' + AND Status__c = 'Active - Disbursed' + THEN 1 + ELSE 0 + END AS "Funded" + FROM public."global-00--00--flat-offers-full-set" + `; + await queryResultTester({ + queryString: queryString, + casePath: 'nin.case-2', + mode: 'write', + outputPipeline: true, + skipDbQuery: true, + optimizeJoins: false, + unsetId: true, + schemas: {}, + }); + }); }); }); From ad89b8d9f4831b31a10883aa036b447dadce24ee Mon Sep 17 00:00:00 2001 From: Ryan Kotzen Date: Tue, 19 Nov 2024 14:14:21 +0200 Subject: [PATCH 2/2] 4.1.18 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8a8314e..ec9408a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@synatic/noql", - "version": "4.1.17", + "version": "4.1.18", "description": "Convert SQL statements to mongo queries or aggregates", "main": "index.js", "files": [