Skip to content

Commit

Permalink
csv numbering fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
AttePeramaki committed Oct 29, 2024
1 parent d01ef47 commit 8de896d
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions server/src/application/answer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ interface SectionHeader {
groupIndex: number;
pageIndex: number;
sectionIndex: number;
questionIndex: number;
}

/**
Expand Down Expand Up @@ -594,8 +595,10 @@ async function getGeometryDBEntries(surveyId: number): Promise<AnswerEntry[]> {
* @param surveyId
* @returns
*/
const getSectionHeaders = async (surveyId: number) =>
getDb().manyOrNone<SectionHeader>(
async function getSectionHeaders(surveyId: number) {
const res = await getDb().manyOrNone<
SectionHeader & { questionOrderIndex: number }
>(
`
SELECT
opt.id as "optionId",
Expand All @@ -611,7 +614,8 @@ const getSectionHeaders = async (surveyId: number) =>
ps.predecessor_section as "predecessorSection",
og.name as "groupName",
og.idx as "groupIndex",
sp.idx as "pageIndex"
sp.idx as "pageIndex",
coalesce(ps2.idx, ps.idx) as "questionOrderIndex"
FROM data.page_section ps
LEFT JOIN data.option opt ON ps.id = opt.section_id
LEFT JOIN data.option_group og ON opt.group_id = og.id
Expand All @@ -625,11 +629,31 @@ const getSectionHeaders = async (surveyId: number) =>
AND ps.type <> 'text'
AND ps.type <> 'image'
AND ps.parent_section IS NULL
ORDER BY "pageIndex", "predecessorSectionIndex" nulls first, ps.idx, og.idx NULLS FIRST, opt.idx NULLS first;
ORDER BY "pageIndex", "questionOrderIndex", "predecessorSectionIndex" nulls first, ps.idx, og.idx NULLS FIRST, opt.idx NULLS first;
`,
[surveyId],
);

let questionIndex = 0;
let lastSectionIndex = -1;
let lastHandledPage = -1;
return res.map<SectionHeader>((section) => {
if (lastHandledPage !== section.pageIndex) {
questionIndex = 0;
lastHandledPage = section.pageIndex;
lastSectionIndex = section.sectionIndex;
} else if (
section.predecessorSection === null &&
lastSectionIndex !== section.questionOrderIndex
) {
questionIndex++;
lastSectionIndex = section.sectionIndex;
}

return { ...section, questionIndex };
});
}

/**
* Create key for CSV headers and submissions
* @pageIndex pageIndex
Expand Down Expand Up @@ -662,12 +686,12 @@ function getSectionDetailsForHeader(section, predecessorIndexes) {
if (section.predecessorSection) {
const [pageIndex, sectionIndex] =
predecessorIndexes[section.predecessorSection].split('-');
return `s${Number(pageIndex) + 1}k${Number(sectionIndex) + 1}${indexToAlpha(
section.sectionIndex,
)}`;
return `s${Number(pageIndex) + 1}k${
Number(section.questionIndex) + 1
}${indexToAlpha(section.sectionIndex)}`;
}

return `s${section.pageIndex + 1}k${section.sectionIndex + 1}`;
return `s${section.pageIndex + 1}k${section.questionIndex + 1}`;
}

/**
Expand Down

0 comments on commit 8de896d

Please sign in to comment.