Repository: paustint/soql-parser-js (ts)

Issue:
getFlattenedFields returns incorrect results if relationship field is grouped and you are grouping for only one field
- [ ] Feature
- [x] Bug

<!--If this is a bug, please include the steps to reproduce the bug-->
## Description

`getFlattenedFields()` does not return correct fields if a relationship field is used in a group-by.

For the following query, salesforce returns the records with the fields `Name: 'abc', expr0: 1'`

```sql
SELECT Account.Name, AVG(Amount)
FROM Opportunity
GROUP BY Account.Name
LIMIT 5
```

Related #98 

Pull request:
getFlattenedFields returns incorrect results if relationship field is grouped and you are grouping for only one field #113
Changes:
 - Fixed use case when the group by has only one field.
 - Added unit test.
 - Fixed typo on CONTRIBUTING.md.
 
Fixes #113

Patch:
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 2d6865e..3909055 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -8,7 +8,7 @@ Any feature that is part of the core project and does not deviate from what the
 
 # Making Changes
 
-- For the repository
+- Fork the repository
 - Create a new branch from master (usually) and with a meaningful name for your changes
 - Make you changes
 - Commit and push your change
diff --git a/src/api/public-utils.ts b/src/api/public-utils.ts
index bc1b58c..53c2e2e 100644
--- a/src/api/public-utils.ts
+++ b/src/api/public-utils.ts
@@ -124,12 +124,15 @@ export function getFlattenedFields(
   query = isFieldSubquery(query) ? query.subquery : query;
   const fields = query.fields;
   // if a relationship field is used in a group by, then Salesforce removes the relationship portion of the field in the returned records
-  const groupByFields: { [field: string]: string } = {};
-  if (!!query.groupBy && Array.isArray(query.groupBy.field)) {
-    query.groupBy.field.reduce((output, field) => {
+  let groupByFields: { [field: string]: string } = {};
+  if(!!query.groupBy && query.groupBy.field) {
+    if(!Array.isArray(query.groupBy.field)) {
+      query.groupBy.field = [query.groupBy.field];
+    }
+    groupByFields = query.groupBy.field.reduce((output: { [field: string]: string }, field) => {
       output[field.toLocaleLowerCase()] = field;
       return output;
-    }, groupByFields);
+    }, {});
   }
   let currUnAliasedAggExp = -1;
   let sObject = (isSubquery(query) ? query.relationshipName : query.sObject || '').toLowerCase();


Test patch:
diff --git a/test/public-utils-test-data.ts b/test/public-utils-test-data.ts
index 448e369..8b52a60 100644
--- a/test/public-utils-test-data.ts
+++ b/test/public-utils-test-data.ts
@@ -347,4 +347,39 @@ export const testCases: FlattenedObjTestCase[] = [
       SBQQ__Quote__c: 'a1j50000004BBOmAAO',
     },
   },
+  {
+    testCase: 11,
+    expectedFields: ['Name','expr0'],
+    query: {
+      fields: [
+        {
+          type: "FieldRelationship",
+          field: "Name",
+          relationships: [
+            "Account"
+          ],
+          rawValue: "Account.Name"
+        },
+        {
+          type: "FieldFunctionExpression",
+          functionName: "AVG",
+          parameters: [
+            "Amount"
+          ],
+          isAggregateFn: true,
+          rawValue: "AVG(Amount)"
+        }
+      ],
+      sObject: "Opportunity",
+      groupBy: {
+        field: "Account.Name"
+      },
+      limit: 5
+    },
+    sfdcObj: {
+      expr0: 1,
+      Name: 'Test',
+      SBQQ__Quote__c: 'a1j50000004BBOmAAO',
+    },
+  },
 ];