Skip to content

Ab#90670 filtering from resources question works #1025

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 1 commit 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
104 changes: 72 additions & 32 deletions src/utils/schema/resolvers/Query/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,39 +256,61 @@ export default (entityName: string, fieldsByName: any, idsByName: any) =>
}

// Get list of needed resources for the aggregation
const resourcesToQuery = [
const resourceToQuery = [
...new Set(usedFields.map((x) => x.split('.')[0])),
].filter((x) =>
fields.find((f) => f.name === x && f.type === 'resource')
);
const resourcesToQuery = [
...new Set(usedFields.map((x) => x.split('.')[0])),
].filter((x) =>
fields.find((f) => f.name === x && f.type === 'resources')
);

const resourceFieldsById = resourcesToQuery.reduce((o, x) => {
const resourceId = fields.find((f) => f.name === x).resource;
const resourceName = Object.keys(idsByName).find(
(key) => idsByName[key] == resourceId
);
const resourceFields = fieldsByName[resourceName];
return {
...o,
[resourceId]: resourceFields,
};
}, {});

const resourceFieldsById = resourceToQuery
.concat(resourcesToQuery)
.reduce((o, x) => {
const resourceId = fields.find((f) => f.name === x).resource;
const resourceName = Object.keys(idsByName).find(
(key) => idsByName[key] == resourceId
);
const resourceFields = fieldsByName[resourceName];
return {
...o,
[resourceId]: resourceFields,
};
}, {});
context = { ...context, resourceFieldsById };

let linkedRecordsAggregation = [];
for (const resource of resourcesToQuery) {
// Build linked records aggregations
const handleLinkedRecords = (
resource: string,
isOfTypeResource: boolean // Build linked records aggregations
) => {
linkedRecordsAggregation = linkedRecordsAggregation.concat([
{
$addFields: {
[`data.${resource}_id`]: {
$convert: {
input: `$data.${resource}`,
to: 'objectId',
onError: null,
},
},
[`data.${resource}_id`]: isOfTypeResource
? {
$convert: {
input: `$data.${resource}`,
to: 'objectId',
onError: null,
},
}
: {
$map: {
input: `$data.${resource}`,
as: 'resource',
in: {
$convert: {
input: '$$resource',
to: 'objectId',
onError: null,
},
},
},
},
},
},
{
Expand All @@ -299,18 +321,28 @@ export default (entityName: string, fieldsByName: any, idsByName: any) =>
as: `_${resource}`,
},
},
{
]);
if (isOfTypeResource) {
linkedRecordsAggregation.push({
$unwind: {
path: `$_${resource}`,
preserveNullAndEmptyArrays: true,
},
});
}
linkedRecordsAggregation.push({
$addFields: {
[`_${resource}.id`]: isOfTypeResource
? { $toString: `$_${resource}._id` }
: {
$map: {
input: `$_${resource}`,
as: 'resource',
in: { $toString: '$$resource._id' },
},
},
},
{
$addFields: {
[`_${resource}.id`]: { $toString: `$_${resource}._id` },
},
},
]);
});

// Build linked records filter
const resourceId = fields.find((f) => f.name === resource).resource;
Expand All @@ -329,6 +361,13 @@ export default (entityName: string, fieldsByName: any, idsByName: any) =>
...{ name: `${resource}.${x.name}` },
})
);
};

for (const resource of resourceToQuery) {
handleLinkedRecords(resource, true);
}
for (const resources of resourcesToQuery) {
handleLinkedRecords(resources, false);
}

// Get list of reference data fields to query
Expand Down Expand Up @@ -435,7 +474,7 @@ export default (entityName: string, fieldsByName: any, idsByName: any) =>

// Finally putting all filters together
const filters = {
$and: [basicFilters, mongooseFilter, permissionFilters],
$and: [mongooseFilter, permissionFilters],
};

const searchFilter = getSearchFilter(filter, fields, context);
Expand All @@ -453,16 +492,17 @@ export default (entityName: string, fieldsByName: any, idsByName: any) =>
context
);
const pipeline = [
{ $match: basicFilters },
...(searchFilter ? [searchFilter] : []),
...linkedRecordsAggregation,
...linkedReferenceDataAggregation,
...calculatedFieldsAggregation,
...defaultRecordAggregation,
{ $match: filters },
...(at ? getAtAggregation(new Date(at)) : []),
];
const aggregation = await Record.aggregate(pipeline).facet({
items: [
...linkedRecordsAggregation,
...linkedReferenceDataAggregation,
...sort,
...projectAggregation,
{ $skip: skip },
Expand Down
8 changes: 6 additions & 2 deletions src/utils/schema/resolvers/Query/getFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ const buildMongoFilter = (
)?.type || '';

// If type is resource and refers to a nested field, get the type of the nested field
if (type === 'resource' && context.resourceFieldsById) {
if (
(type === 'resource' || type === 'resources') &&
context.resourceFieldsById
) {
const resourceField = fields.find(
(x) => x.name === filter.field.split('.')[0]
);
Expand Down Expand Up @@ -165,7 +168,8 @@ const buildMongoFilter = (
if (
!fields.find(
(x) =>
x.name === filter.field.split('.')[0] && x.type === 'resource'
x.name === filter.field.split('.')[0] &&
(x.type === 'resource' || x.type === 'resources')
)
) {
// Prevent createdBy / lastUpdatedBy to return, as they should be in the filter
Expand Down
Loading