Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 4.2.1

June 18, 2021

#157 - `getFlattenedFields()` Did not return correct results if the aggregate function was nested in another function, such as `FORMAT(MAX(CreatedDate))`.
This bug only applied if there was not a field alias defined.

## 4.2.0

June 8, 2021
Expand Down
3 changes: 2 additions & 1 deletion src/api/public-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
isGroupByFn,
isHavingClauseWithRightCondition,
isNegationCondition,
isNestedParamAggregateFunction,
isOrderByField,
isOrderByFn,
isString,
Expand Down Expand Up @@ -209,7 +210,7 @@ export function getFlattenedFields(
return field.alias;
}
// Non-aliased aggregate fields use computed name expr0, expr1, etc..
if (field.isAggregateFn || isAggregateResult) {
if (field.isAggregateFn || isNestedParamAggregateFunction(field) || isAggregateResult) {
currUnAliasedAggExp++;
return `expr${currUnAliasedAggExp}`;
}
Expand Down
11 changes: 11 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@ export function getParams(functionFieldExp: FieldFunctionExpression): string[] {
return getParams(functionFieldExp.parameters[0] as FieldFunctionExpression);
}

export function isNestedParamAggregateFunction(functionFieldExp: FieldFunctionExpression): boolean {
if (!functionFieldExp.parameters || functionFieldExp.parameters.length === 0) {
return false;
}
const parameter = functionFieldExp.parameters[0];
if (isString(parameter)) {
return false;
}
return !!parameter.isAggregateFn;
}

export function hasAlias(value: any): value is FieldWithAlias | FieldRelationshipWithAlias {
return value && !isNil(value.alias);
}
Expand Down
113 changes: 113 additions & 0 deletions test/public-utils-test-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,4 +446,117 @@ export const testCases: FlattenedObjTestCase[] = [
AcctCreatedDate: '2020-02-28T03:00:31.000+0000',
},
},
{
testCase: 14,
expectedFields: ['expr0', 'myDate', 'Email'],
query: {
fields: [
{
type: 'FieldFunctionExpression',
functionName: 'COUNT',
parameters: ['Id'],
isAggregateFn: true,
rawValue: 'COUNT(Id)',
},
{
type: 'FieldFunctionExpression',
functionName: 'FORMAT',
alias: 'myDate',
parameters: [
{
type: 'FieldFunctionExpression',
functionName: 'MAX',
parameters: ['CreatedDate'],
isAggregateFn: true,
rawValue: 'MAX(CreatedDate)',
},
],
rawValue: 'FORMAT(MAX(CreatedDate))',
},
{
type: 'Field',
field: 'Email',
},
],
sObject: 'Contact',
groupBy: [
{
field: 'Email',
},
],
having: {
left: {
fn: {
functionName: 'COUNT',
parameters: ['Id'],
rawValue: 'COUNT(Id)',
},
operator: '>=',
value: '0',
literalType: 'INTEGER',
},
},
},
sfdcObj: {
expr0: 16,
myDate: '7/13/2018 7:07 AM',
Email: 'agreen@foo.com',
},
},
{
testCase: 15,
expectedFields: ['expr0', 'expr1', 'Email'],
query: {
fields: [
{
type: 'FieldFunctionExpression',
functionName: 'COUNT',
parameters: ['Id'],
isAggregateFn: true,
rawValue: 'COUNT(Id)',
},
{
type: 'FieldFunctionExpression',
functionName: 'FORMAT',
parameters: [
{
type: 'FieldFunctionExpression',
functionName: 'MAX',
parameters: ['CreatedDate'],
isAggregateFn: true,
rawValue: 'MAX(CreatedDate)',
},
],
rawValue: 'FORMAT(MAX(CreatedDate))',
},
{
type: 'Field',
field: 'Email',
},
],
sObject: 'Contact',
groupBy: [
{
field: 'Email',
},
],
having: {
left: {
fn: {
functionName: 'COUNT',
parameters: ['Id'],
rawValue: 'COUNT(Id)',
},
operator: '>=',
value: '0',
literalType: 'INTEGER',
},
},
},
sfdcObj: {
expr0: 16,
expr1: '7/13/2018 7:07 AM',
Email: 'agreen@foo.com',
},
},
];
2 changes: 1 addition & 1 deletion test/public-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ describe('getFlattenedFields', () => {
const fields = utils.getFlattenedFields(testCase.query);
expect(fields).to.deep.equal(testCase.expectedFields);
fields.forEach(field => {
expect(lodashGet(testCase.sfdcObj, field)).to.not.be.undefined;
expect(lodashGet(testCase.sfdcObj, field), `${field} does not exist on sfdc record`).to.not.be.undefined;
});
});
});
Expand Down