Skip to content

Commit a0457fe

Browse files
committed
fixed getFlattenedFields bug #157
`getFlattenedFields()` Did not return correct results if the aggregate function was nested in another function, such as `FORMAT(MAX(CreatedDate))` resolves #157
1 parent 0d9e977 commit a0457fe

File tree

5 files changed

+134
-2
lines changed

5 files changed

+134
-2
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## 4.2.1
4+
5+
June 18, 2021
6+
7+
#157 - `getFlattenedFields()` Did not return correct results if the aggregate function was nested in another function, such as `FORMAT(MAX(CreatedDate))`.
8+
This bug only applied if there was not a field alias defined.
9+
310
## 4.2.0
411

512
June 8, 2021

src/api/public-utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
isGroupByFn,
1212
isHavingClauseWithRightCondition,
1313
isNegationCondition,
14+
isNestedParamAggregateFunction,
1415
isOrderByField,
1516
isOrderByFn,
1617
isString,
@@ -209,7 +210,7 @@ export function getFlattenedFields(
209210
return field.alias;
210211
}
211212
// Non-aliased aggregate fields use computed name expr0, expr1, etc..
212-
if (field.isAggregateFn || isAggregateResult) {
213+
if (field.isAggregateFn || isNestedParamAggregateFunction(field) || isAggregateResult) {
213214
currUnAliasedAggExp++;
214215
return `expr${currUnAliasedAggExp}`;
215216
}

src/utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,17 @@ export function getParams(functionFieldExp: FieldFunctionExpression): string[] {
115115
return getParams(functionFieldExp.parameters[0] as FieldFunctionExpression);
116116
}
117117

118+
export function isNestedParamAggregateFunction(functionFieldExp: FieldFunctionExpression): boolean {
119+
if (!functionFieldExp.parameters || functionFieldExp.parameters.length === 0) {
120+
return false;
121+
}
122+
const parameter = functionFieldExp.parameters[0];
123+
if (isString(parameter)) {
124+
return false;
125+
}
126+
return !!parameter.isAggregateFn;
127+
}
128+
118129
export function hasAlias(value: any): value is FieldWithAlias | FieldRelationshipWithAlias {
119130
return value && !isNil(value.alias);
120131
}

test/public-utils-test-data.ts

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,4 +446,117 @@ export const testCases: FlattenedObjTestCase[] = [
446446
AcctCreatedDate: '2020-02-28T03:00:31.000+0000',
447447
},
448448
},
449+
{
450+
testCase: 14,
451+
expectedFields: ['expr0', 'myDate', 'Email'],
452+
query: {
453+
fields: [
454+
{
455+
type: 'FieldFunctionExpression',
456+
functionName: 'COUNT',
457+
parameters: ['Id'],
458+
isAggregateFn: true,
459+
rawValue: 'COUNT(Id)',
460+
},
461+
{
462+
type: 'FieldFunctionExpression',
463+
functionName: 'FORMAT',
464+
alias: 'myDate',
465+
parameters: [
466+
{
467+
type: 'FieldFunctionExpression',
468+
functionName: 'MAX',
469+
parameters: ['CreatedDate'],
470+
isAggregateFn: true,
471+
rawValue: 'MAX(CreatedDate)',
472+
},
473+
],
474+
rawValue: 'FORMAT(MAX(CreatedDate))',
475+
},
476+
{
477+
type: 'Field',
478+
field: 'Email',
479+
},
480+
],
481+
sObject: 'Contact',
482+
groupBy: [
483+
{
484+
field: 'Email',
485+
},
486+
],
487+
having: {
488+
left: {
489+
fn: {
490+
functionName: 'COUNT',
491+
parameters: ['Id'],
492+
rawValue: 'COUNT(Id)',
493+
},
494+
operator: '>=',
495+
value: '0',
496+
literalType: 'INTEGER',
497+
},
498+
},
499+
},
500+
sfdcObj: {
501+
expr0: 16,
502+
myDate: '7/13/2018 7:07 AM',
503+
Email: 'agreen@foo.com',
504+
},
505+
},
506+
{
507+
testCase: 15,
508+
expectedFields: ['expr0', 'expr1', 'Email'],
509+
query: {
510+
fields: [
511+
{
512+
type: 'FieldFunctionExpression',
513+
functionName: 'COUNT',
514+
parameters: ['Id'],
515+
isAggregateFn: true,
516+
rawValue: 'COUNT(Id)',
517+
},
518+
{
519+
type: 'FieldFunctionExpression',
520+
functionName: 'FORMAT',
521+
parameters: [
522+
{
523+
type: 'FieldFunctionExpression',
524+
functionName: 'MAX',
525+
parameters: ['CreatedDate'],
526+
isAggregateFn: true,
527+
rawValue: 'MAX(CreatedDate)',
528+
},
529+
],
530+
rawValue: 'FORMAT(MAX(CreatedDate))',
531+
},
532+
{
533+
type: 'Field',
534+
field: 'Email',
535+
},
536+
],
537+
sObject: 'Contact',
538+
groupBy: [
539+
{
540+
field: 'Email',
541+
},
542+
],
543+
having: {
544+
left: {
545+
fn: {
546+
functionName: 'COUNT',
547+
parameters: ['Id'],
548+
rawValue: 'COUNT(Id)',
549+
},
550+
operator: '>=',
551+
value: '0',
552+
literalType: 'INTEGER',
553+
},
554+
},
555+
},
556+
sfdcObj: {
557+
expr0: 16,
558+
expr1: '7/13/2018 7:07 AM',
559+
Email: 'agreen@foo.com',
560+
},
561+
},
449562
];

test/public-utils.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ describe('getFlattenedFields', () => {
211211
const fields = utils.getFlattenedFields(testCase.query);
212212
expect(fields).to.deep.equal(testCase.expectedFields);
213213
fields.forEach(field => {
214-
expect(lodashGet(testCase.sfdcObj, field)).to.not.be.undefined;
214+
expect(lodashGet(testCase.sfdcObj, field), `${field} does not exist on sfdc record`).to.not.be.undefined;
215215
});
216216
});
217217
});

0 commit comments

Comments
 (0)