Skip to content

Commit 6f551df

Browse files
committed
critical bugfixes in in references after last commit
1 parent 628fbee commit 6f551df

1 file changed

Lines changed: 33 additions & 24 deletions

File tree

‎src/routes/references/index.js‎

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,40 @@ const wholeReferenceResponse = handler({
2121
if (!reference) return {
2222
headerError: NOT_FOUND,
2323
error: `Unknown reference "${referenceName}". Available references: ${availableReferences}`
24-
};
25-
// Get a list with all reference ids which are to be returned according to the available projects
26-
// Note that less references are to be returned if this is the production API
27-
const projectQuery = database.getProjectQuery();
28-
const distinctResult = await database.projects.distinct(
29-
reference.projectIdsField, projectQuery);
30-
let availableReferenceIds = new Set(distinctResult);
31-
// Get the number of references to be matched with the current query
32-
const referencesCount = availableReferenceIds.length;
24+
};
3325
// Set the target mongo collection
34-
const collection = database[referenceName];
26+
const referenceCollection = database[referenceName];
27+
// Set the reference query
28+
let referenceQuery = {}
3529
// Get the requested query, if any
3630
const query = request.query.query;
37-
// If a query was passed then filter references and get the remaining reference ids
38-
if (query) {
39-
// Parse the query
40-
const parsedQuery = JSON.parse(query);
41-
// Query all references and get only their reference ids
42-
const cursor = await collection.find(parsedQuery)
43-
.project({ _id: false, [reference.idField]: true });
44-
const filteredReferenceIds = new Set(cursor.map(ref => ref[reference.idField]));
45-
// Keep only reference ids which are both available and filtered
46-
availableReferenceIds = availableReferenceIds.intersection(filteredReferenceIds);
47-
}
31+
// If a query was passed then parse it and overwrite the reference query
32+
if (query) referenceQuery = JSON.parse(query);
33+
// Query references and get only their reference ids
34+
const referenceCursor = await referenceCollection
35+
.find(referenceQuery)
36+
.project({ _id: false, [reference.idField]: true })
37+
.map(ref => ref[reference.idField]);
38+
const referenceIds = await referenceCursor.toArray();
39+
// Get reference ids alone
40+
// WARNING: We must always start from these ids collected from references
41+
// WARNING: Note that projects may have reference ids for references which do not exist
42+
// WARNING: Although they shouldn't
43+
let availableReferenceIds = new Set(referenceIds);
44+
// Get a list with all reference ids which are to be returned according to the available projects
45+
// Note that less references are to be returned if this is the production API
46+
const projectsQuery = database.getBaseFilter();
47+
const distinctResult = await database.projects.distinct(
48+
reference.projectIdsField, projectsQuery);
49+
const coveredReferenceIds = new Set(distinctResult);
50+
// Filter away references which are not covered by the available projects
51+
availableReferenceIds = new Set(
52+
[...availableReferenceIds].filter(id => coveredReferenceIds.has(id))
53+
);
4854
// Sort the available reference ids
4955
const sortedReferenceIds = Array.from(availableReferenceIds).sort();
56+
// Get the number of references to be matched after all
57+
const referencesCount = sortedReferenceIds.length;
5058
// Check if we are to return only the available ids
5159
// If so then there is no need for pagination or projections
5260
// LORE: This was the original behaviour of this endpoint
@@ -82,9 +90,10 @@ const wholeReferenceResponse = handler({
8290
const skip = request.skip;
8391
const limit = request.query.limit; // The limit will never be greater than 100
8492
const paginatedReferenceIds = sortedReferenceIds.splice(skip, limit);
93+
//console.log(paginatedReferenceIds);
8594
const paginatedReferencesQuery = { [reference.idField]: { $in: paginatedReferenceIds } };
8695
// Finally, perform the final mongo query
87-
const cursor = await collection.find(paginatedReferencesQuery).project(projector);
96+
const cursor = await referenceCollection.find(paginatedReferencesQuery).project(projector);
8897
// Finally consume the cursor
8998
const references = await cursor.toArray();
9099
return { referencesCount, references };
@@ -105,11 +114,11 @@ const specificReferenceResponse = handler({
105114
error: `Unknown reference "${referenceName}". Available references: ${availableReferences}`
106115
};
107116
// Set the target mongo collection
108-
const collection = database[referenceName];
117+
const referenceCollection = database[referenceName];
109118
// Get the requested id
110119
const referenceId = request.params.id;
111120
// Return the reference which matches the request id
112-
const result = await collection.findOne(
121+
const result = await referenceCollection.findOne(
113122
// Set the query
114123
{ [reference.idField]: referenceId },
115124
// Set the projection

0 commit comments

Comments
 (0)