Skip to content

Commit

Permalink
Merge pull request #3131 from ONSdigital/EAR-2470-questionnaire-picker
Browse files Browse the repository at this point in the history
EAR 2470 questionnaire picker
  • Loading branch information
farres1 authored Nov 4, 2024
2 parents f3fdfdc + 6cdd7a8 commit 220ad87
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 39 deletions.
7 changes: 7 additions & 0 deletions eq-author-api/db/datastore/datastore-mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ const getMatchQuery = async (input = {}, ctx) => {
createdOnOrBefore,
access,
myQuestionnaires,
questionnairesToExclude,
} = input;

const { id: userId } = ctx.user;
Expand Down Expand Up @@ -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(
Expand Down
152 changes: 113 additions & 39 deletions eq-author-api/db/datastore/datastore-mongodb.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
});
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -1001,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", () => {
Expand Down
3 changes: 3 additions & 0 deletions eq-author-api/schema/typeDefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,7 @@ input FilteredQuestionnairesInput {
access: Access!
myQuestionnaires: Boolean
sortBy: String
questionnairesToExclude: [ID!]
}
input TotalFilteredQuestionnairesInput {
Expand All @@ -965,6 +966,7 @@ input TotalFilteredQuestionnairesInput {
createdOnOrBefore: DateTime
access: Access!
myQuestionnaires: Boolean
questionnairesToExclude: [ID!]
}
input TotalPagesInput {
Expand All @@ -975,6 +977,7 @@ input TotalPagesInput {
createdOnOrBefore: DateTime
access: Access!
myQuestionnaires: Boolean
questionnairesToExclude: [ID!]
}
input QueryInput {
Expand Down

0 comments on commit 220ad87

Please sign in to comment.