Skip to content

Commit 587247e

Browse files
committed
chore: use column names in preview if no commas
1 parent 0b57665 commit 587247e

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

packages/backend/src/apps/formsg/auth/decrypt-form-response.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ export async function decryptFormResponse(
130130
row.map((column) => column.replaceAll('\u0000', '')),
131131
)
132132

133-
rest.answer = convertTableAnswerArrayToTableObject(rest.answerArray)
133+
rest.answer = convertTableAnswerArrayToTableObject(
134+
rest.question,
135+
rest.answerArray,
136+
)
134137
} else {
135138
rest.answerArray = (rest.answerArray as string[]).map((answer) =>
136139
answer.replaceAll('\u0000', ''),

packages/backend/src/apps/formsg/common/process-table-field.ts

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
11
import { FOR_EACH_INPUT_SOURCE } from '@/apps/toolbox/common/constants'
22

3+
import { extractLastTopLevelBracketContent } from '../triggers/new-submission/get-data-out-metadata'
4+
5+
type TableColumn = {
6+
id: string
7+
label: string
8+
name: string
9+
value: string
10+
}
11+
12+
const createColumn = (label: string): TableColumn => {
13+
const id = Buffer.from(label).toString('hex')
14+
return {
15+
id,
16+
label,
17+
name: label,
18+
value: `data.rows.*.data.${id}`,
19+
}
20+
}
21+
322
export default function convertTableAnswerArrayToTableObject(
23+
question: string,
424
answerArray: string[][],
525
) {
6-
const columns = (answerArray[0] as string[]).map(
7-
(_: string, index: number) => {
8-
const label = `Column ${index + 1}`
9-
return {
10-
id: Buffer.from(label).toString('hex'),
11-
label,
12-
name: label,
13-
value: `data.rows.*.data.${Buffer.from(label).toString('hex')}`,
14-
}
15-
},
16-
)
26+
const { content: columnNames } = extractLastTopLevelBracketContent(question)
27+
const columnNamesArray = columnNames.split(',').map((name) => name.trim())
28+
29+
const columns =
30+
// make sure that column names do not contain commas
31+
columnNamesArray.length === answerArray[0].length
32+
? columnNamesArray.map(createColumn)
33+
: answerArray[0].map((_, index) => createColumn(`Column ${index + 1}`))
1734

1835
/**
1936
* NOTE: we do not show table rows that do not have any data

packages/backend/src/apps/formsg/triggers/new-submission/get-data-out-metadata.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ function buildAnswerArrayForCheckbox(
9999
}
100100
}
101101

102-
function extractLastTopLevelBracketContent(questionText: string): {
102+
export function extractLastTopLevelBracketContent(questionText: string): {
103103
content: string
104104
prefix: string
105105
} {

packages/backend/src/apps/formsg/triggers/new-submission/get-mock-data.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,9 @@ async function getMockData($: IGlobalVariable) {
149149
if (data.responses[formFields[i]._id].fieldType === 'table') {
150150
const answerArray = data.responses[formFields[i]._id]
151151
.answerArray as string[][]
152+
const question = data.responses[formFields[i]._id].question
152153
data.responses[formFields[i]._id].answer =
153-
convertTableAnswerArrayToTableObject(answerArray)
154+
convertTableAnswerArrayToTableObject(question, answerArray)
154155
}
155156

156157
data.responses[formFields[i]._id].order = i + 1

0 commit comments

Comments
 (0)