Skip to content
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

EAR 2470 questionnaire picker #3131

Merged
merged 4 commits into from
Nov 4, 2024
Merged
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
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