Skip to content

Commit

Permalink
Merge branch 'master' into revert-3054-revert-3034-ear-1948-option-va…
Browse files Browse the repository at this point in the history
…lues-for-rad-chk-sel-ans
  • Loading branch information
farres1 authored Oct 25, 2024
2 parents a98fedd + ecb9a8f commit f90b8ba
Show file tree
Hide file tree
Showing 55 changed files with 1,881 additions and 163 deletions.
2 changes: 2 additions & 0 deletions eq-author-api/constants/validationErrorCodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const ERR_COUNT_OF_GREATER_THAN_AVAILABLE_OPTIONS =
const ERR_VALID_PIPED_ANSWER_REQUIRED = "ERR_VALID_PIPED_ANSWER_REQUIRED";
const ERR_UNIQUE_PAGE_DESCRIPTION = "ERR_UNIQUE_PAGE_DESCRIPTION";
const ERR_NO_ANSWERS = "ERR_NO_ANSWERS";
const ERR_SURVEY_ID_MISMATCH = "ERR_SURVEY_ID_MISMATCH";

module.exports = {
ERR_INVALID,
Expand Down Expand Up @@ -76,4 +77,5 @@ module.exports = {
ERR_VALID_PIPED_ANSWER_REQUIRED,
ERR_UNIQUE_PAGE_DESCRIPTION,
ERR_NO_ANSWERS,
ERR_SURVEY_ID_MISMATCH,
};
74 changes: 74 additions & 0 deletions eq-author-api/db/datastore/datastore-firestore.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,79 @@ const listQuestionnaires = async () => {
}
};

const listFilteredQuestionnaires = async (input) => {
try {
const {
resultsPerPage,
firstQuestionnaireIdOnPage,
lastQuestionnaireIdOnPage,
} = input;

// Orders questionnaires by when they were created, starting with the newest
let questionnairesQuery = db
.collection("questionnaires")
.orderBy("createdAt", "desc");

// Gets questionnaires on first page when firstQuestionnaireIdOnPage and lastQuestionnaireIdOnPage are not provided
if (!firstQuestionnaireIdOnPage && !lastQuestionnaireIdOnPage) {
questionnairesQuery = questionnairesQuery.limit(resultsPerPage);
}
// Gets questionnaires on previous page when firstQuestionnaireIdOnPage is provided without lastQuestionnaireIdOnPage
else if (firstQuestionnaireIdOnPage && !lastQuestionnaireIdOnPage) {
// Gets first questionnaire on current page based on firstQuestionnaireIdOnPage
const firstQuestionnaireOnPage = await db
.collection("questionnaires")
.doc(firstQuestionnaireIdOnPage)
.get();

// Gets previous questionnaires before firstQuestionnaireOnPage, limiting the number of questionnaires to `resultsPerPage`
questionnairesQuery = questionnairesQuery
.endBefore(firstQuestionnaireOnPage)
.limitToLast(resultsPerPage);
}
// Gets questionnaires on next page when lastQuestionnaireIdOnPage is provided without firstQuestionnaireIdOnPage
else if (lastQuestionnaireIdOnPage && !firstQuestionnaireIdOnPage) {
// Gets last questionnaire on current page based on lastQuestionnaireIdOnPage
const lastQuestionnaireOnPage = await db
.collection("questionnaires")
.doc(lastQuestionnaireIdOnPage)
.get();

// Gets next questionnaires after lastQuestionnaireOnPage, limiting the number of questionnaires to `resultsPerPage`
questionnairesQuery = questionnairesQuery
.startAfter(lastQuestionnaireOnPage)
.limit(resultsPerPage);
}
// Throws an error when both firstQuestionnaireIdOnPage and lastQuestionnaireIdOnPage are provided
else {
logger.error(
"Invalid input - both firstQuestionnaireIdOnPage and lastQuestionnaireIdOnPage have been provided (from listFilteredQuestionnaires)"
);
}

const questionnairesSnapshot = await questionnairesQuery.get();

if (questionnairesSnapshot.empty) {
logger.info("No questionnaires found (from listFilteredQuestionnaires)");
return [];
}

const questionnaires = questionnairesSnapshot.docs.map((doc) => ({
...doc.data(),
editors: doc.data().editors || [],
createdAt: doc.data().createdAt.toDate(),
updatedAt: doc.data().updatedAt.toDate(),
}));
return questionnaires || [];
} catch (error) {
logger.error(
error,
"Unable to retrieve questionnaires (from listFilteredQuestionnaires)"
);
return;
}
};

const deleteQuestionnaire = async (id) => {
try {
await db.collection("questionnaires").doc(id).delete();
Expand Down Expand Up @@ -666,6 +739,7 @@ module.exports = {
saveQuestionnaire,
deleteQuestionnaire,
listQuestionnaires,
listFilteredQuestionnaires,
getQuestionnaire,
getQuestionnaireMetaById,
getQuestionnaireByVersionId,
Expand Down
Loading

0 comments on commit f90b8ba

Please sign in to comment.