From a013bd4ccec37c76ed6cc393c1b32ca1f74d64bc Mon Sep 17 00:00:00 2001 From: farres1 Date: Fri, 18 Oct 2024 08:47:55 +0100 Subject: [PATCH 1/3] Filter "questionnaires to exclude" from questionnaires list --- eq-author-api/db/datastore/datastore-mongodb.js | 7 +++++++ eq-author-api/schema/typeDefs.js | 3 +++ 2 files changed, 10 insertions(+) diff --git a/eq-author-api/db/datastore/datastore-mongodb.js b/eq-author-api/db/datastore/datastore-mongodb.js index 7297802a82..b81044adb4 100644 --- a/eq-author-api/db/datastore/datastore-mongodb.js +++ b/eq-author-api/db/datastore/datastore-mongodb.js @@ -299,6 +299,7 @@ const getMatchQuery = async (input = {}, ctx) => { createdOnOrBefore, access, myQuestionnaires, + questionnairesToExclude, } = input; const { id: userId } = ctx.user; @@ -395,6 +396,12 @@ const getMatchQuery = async (input = {}, ctx) => { }); } + // Excludes questionnaires with IDs in `questionnairesToExclude` array + if (questionnairesToExclude?.length) { + matchQuery.$and = matchQuery.$and || []; + matchQuery.$and.push({ id: { $nin: questionnairesToExclude } }); + } + return matchQuery; } catch (error) { logger.error( diff --git a/eq-author-api/schema/typeDefs.js b/eq-author-api/schema/typeDefs.js index 62ffce5eae..c73165f42e 100644 --- a/eq-author-api/schema/typeDefs.js +++ b/eq-author-api/schema/typeDefs.js @@ -956,6 +956,7 @@ input FilteredQuestionnairesInput { access: Access! myQuestionnaires: Boolean sortBy: String + questionnairesToExclude: [ID!] } input TotalFilteredQuestionnairesInput { @@ -965,6 +966,7 @@ input TotalFilteredQuestionnairesInput { createdOnOrBefore: DateTime access: Access! myQuestionnaires: Boolean + questionnairesToExclude: [ID!] } input TotalPagesInput { @@ -975,6 +977,7 @@ input TotalPagesInput { createdOnOrBefore: DateTime access: Access! myQuestionnaires: Boolean + questionnairesToExclude: [ID!] } input QueryInput { From 5d0296849b9b7e868107f55d16eaba51c3f2a0a0 Mon Sep 17 00:00:00 2001 From: farres1 Date: Fri, 18 Oct 2024 09:21:47 +0100 Subject: [PATCH 2/3] Add test for questionnaires to exclude --- .../db/datastore/datastore-mongodb.test.js | 134 +++++++++++++----- 1 file changed, 95 insertions(+), 39 deletions(-) diff --git a/eq-author-api/db/datastore/datastore-mongodb.test.js b/eq-author-api/db/datastore/datastore-mongodb.test.js index eb23f7c130..d0f06390a2 100644 --- a/eq-author-api/db/datastore/datastore-mongodb.test.js +++ b/eq-author-api/db/datastore/datastore-mongodb.test.js @@ -414,64 +414,85 @@ describe("MongoDB Datastore", () => { }); await mongoDB.createQuestionnaire( - mockQuestionnaire({ - title: "Test questionnaire 1", - ownerId: "user-1", - shortTitle: "Alias 1", - createdAt: new Date(2021, 2, 5, 5, 0, 0, 0), - }), + { + id: "test-questionnaire-1", + ...mockQuestionnaire({ + title: "Test questionnaire 1", + ownerId: "user-1", + shortTitle: "Alias 1", + createdAt: new Date(2021, 2, 5, 5, 0, 0, 0), + }), + }, ctx ); await mongoDB.createQuestionnaire( - mockQuestionnaire({ - title: "Test questionnaire 2", - ownerId: "user-1", - createdAt: new Date(2021, 2, 10, 5, 0, 0, 0), - }), + { + id: "test-questionnaire-2", + ...mockQuestionnaire({ + title: "Test questionnaire 2", + ownerId: "user-1", + createdAt: new Date(2021, 2, 10, 5, 0, 0, 0), + }), + }, ctx ); await mongoDB.createQuestionnaire( - mockQuestionnaire({ - title: "Test questionnaire 3", - ownerId: "user-2", - editors: ["user-1"], - createdAt: new Date(2021, 2, 15, 5, 0, 0, 0), - }), + { + id: "test-questionnaire-3", + ...mockQuestionnaire({ + title: "Test questionnaire 3", + ownerId: "user-2", + editors: ["user-1"], + createdAt: new Date(2021, 2, 15, 5, 0, 0, 0), + }), + }, ctx ); await mongoDB.createQuestionnaire( - mockQuestionnaire({ - title: "Test questionnaire 4", - ownerId: "user-2", - createdAt: new Date(2021, 2, 20, 5, 0, 0, 0), - }), + { + id: "test-questionnaire-4", + ...mockQuestionnaire({ + title: "Test questionnaire 4", + ownerId: "user-2", + createdAt: new Date(2021, 2, 20, 5, 0, 0, 0), + }), + }, ctx ); // ** "Test questionnaire 5" is not included in several test assertions as it is not public and `ctx.user` is not owner/editor await mongoDB.createQuestionnaire( - mockQuestionnaire({ - title: "Test questionnaire 5", - ownerId: "user-2", - createdAt: new Date(2021, 2, 25, 5, 0, 0, 0), - isPublic: false, - }), + { + id: "test-questionnaire-5", + ...mockQuestionnaire({ + title: "Test questionnaire 5", + ownerId: "user-2", + createdAt: new Date(2021, 2, 25, 5, 0, 0, 0), + isPublic: false, + }), + }, ctx ); await mongoDB.createQuestionnaire( - mockQuestionnaire({ - title: "Test questionnaire 6", - ownerId: "user-1", - createdAt: new Date(2021, 2, 30, 5, 0, 0, 0), - isPublic: false, - }), + { + id: "test-questionnaire-6", + ...mockQuestionnaire({ + title: "Test questionnaire 6", + ownerId: "user-1", + createdAt: new Date(2021, 2, 30, 5, 0, 0, 0), + isPublic: false, + }), + }, ctx ); await mongoDB.createQuestionnaire( - mockQuestionnaire({ - title: "Test questionnaire 7", - ownerId: "user-4", - createdAt: new Date(2021, 4, 10, 5, 0, 0, 0), - }), + { + id: "test-questionnaire-7", + ...mockQuestionnaire({ + title: "Test questionnaire 7", + ownerId: "user-4", + createdAt: new Date(2021, 4, 10, 5, 0, 0, 0), + }), + }, ctx ); }); @@ -728,6 +749,41 @@ describe("MongoDB Datastore", () => { expect(listOfQuestionnaires[3].title).toEqual("Test questionnaire 1"); }); + it("should not return questionnaires in `questionnairesToExclude` array", async () => { + const listOfQuestionnaires = await mongoDB.listFilteredQuestionnaires( + { + searchByTitleOrShortCode: "", + owner: "", + access: "All", + resultsPerPage: 10, + questionnairesToExclude: [ + "test-questionnaire-1", + "test-questionnaire-2", + ], + }, + ctx + ); + + expect(listOfQuestionnaires.length).toBe(7); + /* + Questionnaires with titles "Default questionnaire title" are created in previous tests. + These appear first when sorted by newest to oldest as their `createdAt` dates are most recent. + */ + expect(listOfQuestionnaires[0].title).toEqual( + "Default questionnaire title" + ); + expect(listOfQuestionnaires[1].title).toEqual( + "Default questionnaire title" + ); + expect(listOfQuestionnaires[2].title).toEqual( + "Default questionnaire title" + ); + expect(listOfQuestionnaires[3].title).toEqual("Test questionnaire 7"); + expect(listOfQuestionnaires[4].title).toEqual("Test questionnaire 6"); + expect(listOfQuestionnaires[5].title).toEqual("Test questionnaire 4"); + expect(listOfQuestionnaires[6].title).toEqual("Test questionnaire 3"); + }); + it("should return questionnaires on previous page when `firstQuestionnaireIdOnPage` is provided without `lastQuestionnaireIdOnPage`", async () => { // Gets questionnaires with "All" access to get a questionnaire ID to use as `firstQuestionnaireIdOnPage` const allQuestionnaires = await mongoDB.listFilteredQuestionnaires( From 20daaef8989af64efdf759ab363b8c0f62bc057e Mon Sep 17 00:00:00 2001 From: farres1 Date: Fri, 18 Oct 2024 09:35:38 +0100 Subject: [PATCH 3/3] Add "questionnaires to exclude" total questionnaires test --- .../db/datastore/datastore-mongodb.test.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/eq-author-api/db/datastore/datastore-mongodb.test.js b/eq-author-api/db/datastore/datastore-mongodb.test.js index d0f06390a2..f352ba998d 100644 --- a/eq-author-api/db/datastore/datastore-mongodb.test.js +++ b/eq-author-api/db/datastore/datastore-mongodb.test.js @@ -1057,6 +1057,24 @@ describe("MongoDB Datastore", () => { expect(totalFilteredQuestionnaires).toBe(2); }); + + it("should get the total number of questionnaires when `questionnairesToExclude` filter is applied", async () => { + const totalFilteredQuestionnaires = + await mongoDB.getTotalFilteredQuestionnaires( + { + searchByTitleOrShortCode: "", + owner: "", + access: "All", + questionnairesToExclude: [ + "test-questionnaire-3", + "test-questionnaire-4", + ], + }, + ctx + ); + + expect(totalFilteredQuestionnaires).toBe(7); + }); }); describe("Getting total page count", () => {