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

#119 [FIX]: TableAnswers retrieval in getApplicationWithAnswers endpoint #120

Merged
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
43 changes: 40 additions & 3 deletions src/controllers/applicationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ export const getApplicationWithAnswers = async (req: Request, res: Response): Pr
for (const itemGroup of page.itemGroups) {
for (const item of itemGroup.items) {
item.itemAnswers = {};
// For each item in the application, get all itemAnswers associated with some applicationAnswer in the application
const itemAnswers = await prismaClient.itemAnswer.findMany({
where: {
group: {
Expand All @@ -486,21 +487,57 @@ export const getApplicationWithAnswers = async (req: Request, res: Response): Pr
},
},
});

// For each itemAnswer, group them by applicationAnswerId and answerGroupId
for (const answer of itemAnswers) {
// Initialize the groupAnswers of that applicationAnswer if it doesn't exist
if (!item.itemAnswers[answer.group.applicationAnswerId]) {
item.itemAnswers[answer.group.applicationAnswerId] = {};
}

// Initialize the itemAnswers of that groupAnswer if it doesn't exist
if (!item.itemAnswers[answer.group.applicationAnswerId][answer.group.id]) {
item.itemAnswers[answer.group.applicationAnswerId][answer.group.id] = [];
}

// Push the itemAnswer to the itemAnswers of that ApplicationAnswer/GroupAnswer
item.itemAnswers[answer.group.applicationAnswerId][answer.group.id].push({
text: answer.text,
files: answer.files,
});
}
// For each item in the application, get all tableAnswers associated with some applicationAnswer in the application
item.tableAnswers = {};
const tableAnswers = await prismaClient.tableAnswer.findMany({
where: {
group: {
applicationAnswerId: {
in: applicationWithAnswers.answers.map((answer: any) => answer.id),
},
},
itemId: item.id,
},
select: {
text: true,
group: {
select: {
id: true,
applicationAnswerId: true,
},
},
columnId: true,
},
});
// For each tableAnswer, group them by applicationAnswerId and answerGroupId
for (const answer of tableAnswers) {
// Initialize the groupAnswers of that applicationAnswer if it doesn't exist
if (!item.tableAnswers[answer.group.applicationAnswerId]) {
item.tableAnswers[answer.group.applicationAnswerId] = {};
}
// Initialize the columnAnswer of that groupAnswer if it doesn't exist
if (!item.tableAnswers[answer.group.applicationAnswerId][answer.group.id]) {
item.tableAnswers[answer.group.applicationAnswerId][answer.group.id] = {};
}
// Define the columnAnswer of that applicationAnswer/GroupAnswer/ColumnAnswer
item.tableAnswers[answer.group.applicationAnswerId][answer.group.id][answer.columnId] = answer.text;
}

for (const option of item.itemOptions) {
option.optionAnswers = {};
Expand Down
Loading