Skip to content

Commit

Permalink
Merge branch 'master' into EAR-2244-Adjust-the-submission-page
Browse files Browse the repository at this point in the history
  • Loading branch information
Farhanam76 authored Sep 24, 2024
2 parents ebddaa0 + 086f70d commit 31c48a4
Show file tree
Hide file tree
Showing 129 changed files with 5,993 additions and 477 deletions.
2 changes: 1 addition & 1 deletion cloudbuild-author-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ steps:
exit $(( 1 - $result ))
id: unit_tests
entrypoint: /bin/ash
timeout: 1200s
timeout: 1800s
options:
machineType: E2_HIGHCPU_8
34 changes: 17 additions & 17 deletions cloudbuild.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
steps:
- name: 'node:$_NODE_VERSION'
- name: "node:$_NODE_VERSION"
id: yarn_install_and_build
dir: eq-author
entrypoint: /bin/bash
args:
- '-c'
- "-c"
- |
if [ $_ENV = "staging" ]; then
yarn install
Expand All @@ -21,8 +21,8 @@ steps:
entrypoint: sh
waitFor:
- yarn_install_and_build
args:
- '-c'
args:
- "-c"
- |
if [ $_ENV = "staging" ]; then
docker build -t "eu.gcr.io/ons-eqbs-images/eq-author:$SHORT_SHA" .
Expand All @@ -38,9 +38,9 @@ steps:
dir: eq-author-api
entrypoint: sh
waitFor:
- '-'
args:
- '-c'
- "-"
args:
- "-c"
- |
if [ $_ENV = "staging" ]; then
docker build -t "eu.gcr.io/ons-eqbs-images/eq-author-api:$SHORT_SHA" .
Expand All @@ -51,11 +51,11 @@ steps:
echo "*************************************************************"
fi
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:alpine'
- name: "gcr.io/google.com/cloudsdktool/cloud-sdk:alpine"
id: tag_author_release
entrypoint: /bin/bash
args:
- '-c'
- "-c"
- |
if [ $_ENV = "preprod" ]; then
gcloud container images add-tag \
Expand All @@ -67,11 +67,11 @@ steps:
echo "*************************************************************"
fi
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:alpine'
- name: "gcr.io/google.com/cloudsdktool/cloud-sdk:alpine"
id: tag_author_api_release
entrypoint: /bin/bash
args:
- '-c'
- "-c"
- |
if [ $_ENV = "preprod" ]; then
gcloud container images add-tag \
Expand All @@ -83,11 +83,11 @@ steps:
echo "*************************************************************"
fi
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:alpine'
- name: "gcr.io/google.com/cloudsdktool/cloud-sdk:alpine"
id: deploy_author_api
entrypoint: sh
args:
- '-c'
- "-c"
- |
if [ $_ENV = "staging" ]; then
gcloud run deploy eq-author-api \
Expand All @@ -100,12 +100,12 @@ steps:
--region europe-west2 \
--platform managed
fi
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:alpine'
- name: "gcr.io/google.com/cloudsdktool/cloud-sdk:alpine"
id: deploy_author
entrypoint: sh
args:
- '-c'
- "-c"
- |
if [ $_ENV = "staging" ]; then
gcloud run deploy eq-author \
Expand All @@ -118,4 +118,4 @@ steps:
--region europe-west2 \
--platform managed
fi
timeout: 1200s
timeout: 1800s
1 change: 1 addition & 0 deletions eq-author-api/constants/themes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const THEME_SHORT_NAMES = [
"ukhsa-ons",
"desnz",
"desnz-ni",
"ons-nhs",
];

module.exports = { THEME_SHORT_NAMES };
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,
};
13 changes: 13 additions & 0 deletions eq-author-api/db/baseQuestionnaireSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,20 @@ const baseQuestionnaireFields = {
locked,
};

const saveQuestionnaireFields = {
id,
isPublic,
title,
type,
shortTitle,
publishStatus,
introduction,
editors,
locked,
};

module.exports = {
baseQuestionnaireFields,
...baseQuestionnaireFields,
saveQuestionnaireFields,
};
92 changes: 88 additions & 4 deletions eq-author-api/db/datastore/datastore-firestore.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ const { logger } = require("../../utils/logger");
const { pick } = require("lodash/fp");
const { omit } = require("lodash");
const { removeEmpty } = require("../../utils/removeEmpty");
const { baseQuestionnaireFields } = require("../baseQuestionnaireSchema");
const {
baseQuestionnaireFields,
saveQuestionnaireFields,
} = require("../baseQuestionnaireSchema");
const {
questionnaireCreationEvent,
historyCreationForImport,
Expand Down Expand Up @@ -32,6 +35,14 @@ const BASE_FIELDS = [

const justListFields = pick(BASE_FIELDS);

const SAVE_FIELDS = [
...Object.keys(saveQuestionnaireFields),
"updatedAt",
"history",
];

const justListSaveFields = pick(SAVE_FIELDS);

const saveSections = (parentDoc, sections) =>
Promise.all(
sections.map((section, position) =>
Expand Down Expand Up @@ -271,8 +282,7 @@ const saveQuestionnaire = async (changedQuestionnaire) => {
"Unable to save questionnaire; cannot find required field: ID (from saveQuestionnaire)"
);
}
const createdAt = new Date();
const updatedAt = createdAt;
const updatedAt = new Date();

const originalQuestionnaire = await getQuestionnaire(id);

Expand All @@ -297,7 +307,7 @@ const saveQuestionnaire = async (changedQuestionnaire) => {

const baseDoc = db.collection("questionnaires").doc(id);
await baseDoc.update({
...justListFields(updatedQuestionnaire),
...justListSaveFields(updatedQuestionnaire),
updatedAt,
});

Expand Down Expand Up @@ -364,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 @@ -656,6 +739,7 @@ module.exports = {
saveQuestionnaire,
deleteQuestionnaire,
listQuestionnaires,
listFilteredQuestionnaires,
getQuestionnaire,
getQuestionnaireMetaById,
getQuestionnaireByVersionId,
Expand Down
10 changes: 10 additions & 0 deletions eq-author-api/db/datastore/datastore-firestore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,16 @@ describe("Firestore Datastore", () => {
});
expect(updatedAt !== savedQuestionnaire.updatedAt).toBeTruthy();
});

it("Should not update the 'createdAt' property", async () => {
const createdAt = questionnaire.createdAt;
const savedQuestionnaire = await saveQuestionnaire({
id: "123",
title: "Updated questionnaire title",
...questionnaire,
});
expect(createdAt === savedQuestionnaire.createdAt).toBeTruthy();
});
});

describe("Getting a list of questionnaires", () => {
Expand Down
Loading

0 comments on commit 31c48a4

Please sign in to comment.