Skip to content

Fix/ab#76765 calculated fields not working in aggregation parent form #841

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: 2.x.x
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions src/schema/query/recordsAggregation.query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,33 @@ export default {
pipeline.push(...referenceDataAggregation);
}
}
const resourceFieldsToCalculate = [];
const promises = resource.fields.map(async (resourceField: any) => {
const resourceData = await Resource.findById(resourceField.resource);
if (resourceData) {
// const values = Object.values(args.mapping).flat();
// get each field of resourceData
resourceData.fields.forEach((rdField: any) => {
// if have the resourceDataField is a expression
if (rdField.expression) {
// add it to resource fields to be calculated
resourceFieldsToCalculate.push({
field: rdField,
name: resourceData.name,
});
}
});
}
});
await Promise.all(promises);
resourceFieldsToCalculate.forEach((obj) => {
pipeline.push(
...buildCalculatedFieldPipeline(
obj.field.expression,
`${obj.name}.${obj.field.name}`
)
);
});
pipeline.push({
$project: {
...(aggregation.sourceFields as any[]).reduce(
Expand Down
56 changes: 50 additions & 6 deletions src/utils/schema/resolvers/Query/all.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GraphQLError } from 'graphql';
import { Form, Record, ReferenceData, User } from '@models';
import { Form, Record, ReferenceData, User, Resource } from '@models';
import extendAbilityForRecords from '@security/extendAbilityForRecords';
import { decodeCursor, encodeCursor } from '@schema/types';
import getReversedFields from '../../introspection/getReversedFields';
Expand Down Expand Up @@ -587,14 +587,58 @@ export default (entityName: string, fieldsByName: any, idsByName: any) =>
})
)
);
const projectionObject = projection.reduce((acc, field) => {
acc[field] = 1;
return acc;
}, {});
// get aggregated fields from resource
const resourceFieldsToCalculate = [];
// get each resource in resourceFields
const promises = resourcesFields.map(async (resource: any) => {
// filter resource by resource id
const resourceData = await Resource.findById(resource.resource);
if (resourceData) {
// get each field of resourceData
resourceData.fields.forEach((rdField: any) => {
// if have the resourceDataField in resource.fields
if (
resource.fields.includes(rdField.name) &&
rdField.expression
) {
// add it to resource fields to be calculated
resourceFieldsToCalculate.push(rdField);
}
});
}
});
await Promise.all(promises);
// get the resource calculated fields
const resourceCalculatedFields = [];
resourceFieldsToCalculate.forEach((f) => {
resourceCalculatedFields.push(
...buildCalculatedFieldPipeline(f.expression, f.name)
);
});
// Fetch records
const relatedRecords = await Record.find(
const relatedRecords = await Record.aggregate([
{
$or: [{ _id: { $in: relatedIds } }, ...relatedFilters],
archived: { $ne: true },
$match: {
$or: [
{
_id: {
$in: relatedIds.map((x) => new mongoose.Types.ObjectId(x)),
},
},
...relatedFilters,
],
archived: { $ne: true },
},
},
projection
);
...resourceCalculatedFields,
{
$project: projectionObject,
},
]);
// Update items
for (const item of itemsToUpdate) {
if (item.record) {
Expand Down