From f335a771d80b39b668c997be5820b4344900b6a8 Mon Sep 17 00:00:00 2001 From: RenzoPrats Date: Fri, 24 Nov 2023 13:01:49 -0300 Subject: [PATCH 1/6] fix: calculated fields not working in parent form --- src/utils/schema/resolvers/Query/all.ts | 49 ++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/src/utils/schema/resolvers/Query/all.ts b/src/utils/schema/resolvers/Query/all.ts index 84e123efa..6bd1ab0a0 100644 --- a/src/utils/schema/resolvers/Query/all.ts +++ b/src/utils/schema/resolvers/Query/all.ts @@ -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'; @@ -587,14 +587,51 @@ 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([ + { + $match: { + $or: [ + { _id: { $in: relatedIds.map((x) => new mongoose.Types.ObjectId(x)), } }, + ...relatedFilters + ], + archived: { $ne: true }, + }, + }, + ...resourceCalculatedFields, { - $or: [{ _id: { $in: relatedIds } }, ...relatedFilters], - archived: { $ne: true }, + $project: projectionObject }, - projection - ); + ]) // Update items for (const item of itemsToUpdate) { if (item.record) { From f9f0c226cec07b92d9c953bc9ecd247f9e09affa Mon Sep 17 00:00:00 2001 From: RenzoPrats Date: Fri, 24 Nov 2023 13:06:40 -0300 Subject: [PATCH 2/6] fix: lint --- src/utils/schema/resolvers/Query/all.ts | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/utils/schema/resolvers/Query/all.ts b/src/utils/schema/resolvers/Query/all.ts index 6bd1ab0a0..fa0a72268 100644 --- a/src/utils/schema/resolvers/Query/all.ts +++ b/src/utils/schema/resolvers/Query/all.ts @@ -601,7 +601,10 @@ export default (entityName: string, fieldsByName: any, idsByName: any) => // 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) { + if ( + resource.fields.includes(rdField.name) && + rdField.expression + ) { // add it to resource fields to be calculated resourceFieldsToCalculate.push(rdField); } @@ -611,27 +614,31 @@ export default (entityName: string, fieldsByName: any, idsByName: any) => await Promise.all(promises); // get the resource calculated fields const resourceCalculatedFields = []; - resourceFieldsToCalculate.forEach((f) =>{ + resourceFieldsToCalculate.forEach((f) => { resourceCalculatedFields.push( ...buildCalculatedFieldPipeline(f.expression, f.name) - ) + ); }); // Fetch records const relatedRecords = await Record.aggregate([ { $match: { $or: [ - { _id: { $in: relatedIds.map((x) => new mongoose.Types.ObjectId(x)), } }, - ...relatedFilters + { + _id: { + $in: relatedIds.map((x) => new mongoose.Types.ObjectId(x)), + }, + }, + ...relatedFilters, ], archived: { $ne: true }, }, }, ...resourceCalculatedFields, { - $project: projectionObject + $project: projectionObject, }, - ]) + ]); // Update items for (const item of itemsToUpdate) { if (item.record) { From ab3ab2f609e38d89b7cbc19a4a3034ff89c494b9 Mon Sep 17 00:00:00 2001 From: RenzoPrats Date: Fri, 24 Nov 2023 16:31:19 -0300 Subject: [PATCH 3/6] fix: started to fix recordsAggregation --- src/schema/query/recordsAggregation.query.ts | 29 ++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/schema/query/recordsAggregation.query.ts b/src/schema/query/recordsAggregation.query.ts index b5fe42f44..15cf4406e 100644 --- a/src/schema/query/recordsAggregation.query.ts +++ b/src/schema/query/recordsAggregation.query.ts @@ -278,6 +278,32 @@ export default { }, }, }); + // get aggregated fields from resource + 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 in resource.fields + if ( + values.some((item: any) => item.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 + resourceFieldsToCalculate.forEach((f) => { + pipeline.unshift( + ...buildCalculatedFieldPipeline(f.expression, f.name) + ); + }); // Loop on fields to apply lookups for special fields for (const fieldName of aggregation.sourceFields) { const field = resource.fields.find((x) => x.name === fieldName); @@ -495,9 +521,12 @@ export default { }, }); } + console.log("PIPELINE = ", pipeline); // Get aggregated data const recordAggregation = await RecordModel.aggregate(pipeline); + console.log(recordAggregation); + let items; let totalCount; if (args.mapping) { From 43dc093f6647a77763e8c70dd16e52338d03b443 Mon Sep 17 00:00:00 2001 From: RenzoPrats Date: Fri, 24 Nov 2023 16:32:09 -0300 Subject: [PATCH 4/6] fix: lint --- src/schema/query/recordsAggregation.query.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/schema/query/recordsAggregation.query.ts b/src/schema/query/recordsAggregation.query.ts index 15cf4406e..d6263fcdd 100644 --- a/src/schema/query/recordsAggregation.query.ts +++ b/src/schema/query/recordsAggregation.query.ts @@ -521,7 +521,7 @@ export default { }, }); } - console.log("PIPELINE = ", pipeline); + console.log('PIPELINE = ', pipeline); // Get aggregated data const recordAggregation = await RecordModel.aggregate(pipeline); From dec9b29dfc53a7f523ec6d3e91316847f35857ef Mon Sep 17 00:00:00 2001 From: RenzoPrats Date: Mon, 27 Nov 2023 12:20:56 -0300 Subject: [PATCH 5/6] fix: finished fixing recordsAggregation --- src/schema/query/recordsAggregation.query.ts | 59 ++++++++++---------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/src/schema/query/recordsAggregation.query.ts b/src/schema/query/recordsAggregation.query.ts index d6263fcdd..05572471c 100644 --- a/src/schema/query/recordsAggregation.query.ts +++ b/src/schema/query/recordsAggregation.query.ts @@ -278,32 +278,6 @@ export default { }, }, }); - // get aggregated fields from resource - 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 in resource.fields - if ( - values.some((item: any) => item.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 - resourceFieldsToCalculate.forEach((f) => { - pipeline.unshift( - ...buildCalculatedFieldPipeline(f.expression, f.name) - ); - }); // Loop on fields to apply lookups for special fields for (const fieldName of aggregation.sourceFields) { const field = resource.fields.find((x) => x.name === fieldName); @@ -447,6 +421,36 @@ export default { pipeline.push(...referenceDataAggregation); } } + const resourceFieldsToCalculate = []; + aggregation.sourceFields + 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( @@ -521,12 +525,9 @@ export default { }, }); } - console.log('PIPELINE = ', pipeline); // Get aggregated data const recordAggregation = await RecordModel.aggregate(pipeline); - console.log(recordAggregation); - let items; let totalCount; if (args.mapping) { From c95adc288b82722b5f359506b5e30208611f527f Mon Sep 17 00:00:00 2001 From: RenzoPrats Date: Mon, 27 Nov 2023 12:25:08 -0300 Subject: [PATCH 6/6] fix: lint --- src/schema/query/recordsAggregation.query.ts | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/schema/query/recordsAggregation.query.ts b/src/schema/query/recordsAggregation.query.ts index 05572471c..59c0c6385 100644 --- a/src/schema/query/recordsAggregation.query.ts +++ b/src/schema/query/recordsAggregation.query.ts @@ -422,7 +422,6 @@ export default { } } const resourceFieldsToCalculate = []; - aggregation.sourceFields const promises = resource.fields.map(async (resourceField: any) => { const resourceData = await Resource.findById(resourceField.resource); if (resourceData) { @@ -432,12 +431,10 @@ export default { // 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, - } - ); + resourceFieldsToCalculate.push({ + field: rdField, + name: resourceData.name, + }); } }); }