Skip to content

Commit

Permalink
Merge pull request #3109 from ONSdigital/EAR-2252-folder-import
Browse files Browse the repository at this point in the history
EAR 2252 folder import
  • Loading branch information
farres1 authored May 20, 2024
2 parents 297cd4d + 1639ca7 commit 1acaa32
Show file tree
Hide file tree
Showing 27 changed files with 2,791 additions and 67 deletions.
65 changes: 65 additions & 0 deletions eq-author-api/schema/resolvers/importing.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const {
getPagesByIds,
getFolderById,
getFoldersByIds,
getSectionById,
getSectionByFolderId,
stripQCodes,
Expand Down Expand Up @@ -98,6 +99,70 @@ module.exports = {
return section;
}
),
importFolders: createMutation(
async (_, { input: { questionnaireId, folderIds, position } }, ctx) => {
const { sectionId, index: insertionIndex } = position;

if (!sectionId) {
throw new UserInputError("Target section ID must be provided.");
}

const sourceQuestionnaire = await getQuestionnaire(questionnaireId);
if (!sourceQuestionnaire) {
throw new UserInputError(
`Questionnaire with ID ${questionnaireId} does not exist.`
);
}

const sourceFolders = getFoldersByIds(
{ questionnaire: sourceQuestionnaire },
folderIds
);

if (sourceFolders.length !== folderIds.length) {
throw new UserInputError(
`Not all folder IDs in [${folderIds}] exist in source questionnaire ${questionnaireId}.`
);
}

sourceFolders.forEach((folder) => {
remapAllNestedIds(folder);
removeExtraSpaces(folder);
folder.skipConditions = null;
if (folder.listId) {
folder.listId = "";
}
folder.pages.forEach((page) => {
page.routing = null;
page.skipConditions = null;
if (page.answers !== undefined) {
page.answers.forEach((answer) => {
return stripQCodes(answer);
});
}

if (page.answers?.length === 1) {
if (page.answers[0].repeatingLabelAndInputListId) {
page.answers[0].repeatingLabelAndInputListId = "";
}
}
});
});

const section = getSectionById(ctx, sectionId);

if (!section) {
throw new UserInputError(
`Section with ID ${sectionId} doesn't exist in target questionnaire.`
);
}

section.folders.splice(insertionIndex, 0, ...sourceFolders);
setDataVersion(ctx);

return section;
}
),
importSections: createMutation(
async (_, { input: { questionnaireId, sectionIds, position } }, ctx) => {
const { sectionId, index: insertionIndex } = position;
Expand Down
4 changes: 4 additions & 0 deletions eq-author-api/schema/resolvers/utils/folderGetters.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ const getFolderByAnswerId = (ctx, id) =>
pages && some(pages, ({ answers }) => answers && some(answers, { id }))
);

const getFoldersByIds = (ctx, ids) =>
getFolders(ctx).filter(({ id }) => ids.includes(id));

module.exports = {
getFolders,
getFoldersBySectionId,
getFolderById,
getFolderByPageId,
getFolderByAnswerId,
getFoldersByIds,
};
Loading

0 comments on commit 1acaa32

Please sign in to comment.