Skip to content

Commit

Permalink
fix(stage-wizard): remove field selector from $count (#5097)
Browse files Browse the repository at this point in the history
* fix(stage-wizard): remove field selector from $count

* clean up value when count is selected

* test description
  • Loading branch information
mcasimir authored Nov 13, 2023
1 parent afed5c3 commit e86fc71
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,33 @@ describe('group with statistics', function () {
expect(onChange.lastCall.args[1]).to.be.null;
});
});

context('$count', function () {
it('adds a "count" field with the $count accumulator to the generated stage', function () {
setSelectValue(/select accumulator/i, 'count');
expect(onChange.lastCall.args[0]).to.equal(
JSON.stringify({
_id: null,
count: { $count: {} },
})
);
});

it('clears the field selection', function () {
setSelectValue(/select accumulator/i, 'sum');
setComboboxValue(new RegExp(SINGLE_SELECT_LABEL, 'i'), 'orders');
setSelectValue(/select accumulator/i, 'count');

// re-select sum, we expect the field to be gone, and the new accumulator to
// be invalid (not present in the result)
setSelectValue(/select accumulator/i, 'sum');

expect(onChange.lastCall.args[0]).to.equal(
JSON.stringify({
_id: null,
})
);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ type GroupWithStatisticsFormData = {
const _getGroupAccumulatorKey = ({ field, accumulator }: GroupAccumulators) => {
// we will always prepend an accumulator to the key as user
// can choose to calculate values of the same field in a document.
if (accumulator === '$count') {
return 'count';
}

const prefix = accumulator.replace(/\$/g, '');
const propertyName = mapFieldToPropertyName(field);
const underscore = propertyName.startsWith('_') ? '' : '_';
Expand All @@ -110,7 +114,7 @@ const mapGroupFormStateToStageValue = (
): Document => {
const values = Object.fromEntries(
data.groupAccumulators
.filter((x) => x.accumulator && x.field)
.filter((x) => x.accumulator && (x.accumulator === '$count' || x.field))
.map((x) => [_getGroupAccumulatorKey(x), _getGroupAccumulatorValue(x)])
);
return {
Expand Down Expand Up @@ -138,8 +142,13 @@ const GroupAccumulatorForm = ({
if (!value) {
return;
}

const newData = [...data];
newData[index][key] = value;
if (key === 'accumulator' && value === '$count') {
newData[index].field = '';
}

onChange(newData);
};

Expand Down Expand Up @@ -203,15 +212,19 @@ const GroupAccumulatorForm = ({
);
})}
</Select>
<Body>of</Body>
<FieldCombobox
className={accumulatorFieldcomboboxStyles}
value={item.field}
onChange={(value: string | null) =>
onChangeGroup(index, 'field', value)
}
fields={fields}
/>
{item.accumulator !== '$count' && (
<>
<Body>of</Body>
<FieldCombobox
className={accumulatorFieldcomboboxStyles}
value={item.field}
onChange={(value: string | null) =>
onChangeGroup(index, 'field', value)
}
fields={fields}
/>
</>
)}
</div>
);
}}
Expand Down Expand Up @@ -247,8 +260,10 @@ export const GroupWithStatistics = ({
setFormData(newData);

const isValuesEmpty =
newData.groupAccumulators.filter((x) => x.accumulator && x.field)
.length === 0;
newData.groupAccumulators.filter(
(x) => x.accumulator && (x.accumulator === '$count' || x.field)
).length === 0;

onChange(
JSON.stringify(mapGroupFormStateToStageValue(newData)),
isValuesEmpty ? new Error('Select one group accumulator') : null
Expand Down

0 comments on commit e86fc71

Please sign in to comment.