-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app): finalize survey entries download (#190)
feat(app): finalize survey entries CSV download
- Loading branch information
1 parent
33b9afd
commit 65643e8
Showing
6 changed files
with
125 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
import styled from 'styled-components' | ||
|
||
export const AdminHeader = styled.div` | ||
align-items: center; | ||
align-items: flex-start; | ||
display: flex; | ||
justify-content: space-between; | ||
height: 2.5rem; | ||
margin-bottom: 1.5rem; | ||
min-height: 2.5rem; | ||
h1 { | ||
line-height: 1; | ||
line-height: 1.5; | ||
padding: 0 1.5rem 0 0.5rem; | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { ApiError } from '@api/libs/ApiError' | ||
import { prisma } from '@api/libs/prisma' | ||
import { handleAuth } from '@api/middlewares/withAuth/handleAuth' | ||
import { handleApiEndpointError } from '@common/helpers/handleApiEndpointError' | ||
import { UserRole } from '@prisma/client' | ||
import { TellMe } from '@schemas/1.0.0/TellMe' | ||
import { stringify } from 'csv-stringify' | ||
import { keys, uniq } from 'ramda' | ||
|
||
import type { NextApiRequest, NextApiResponse } from 'next' | ||
|
||
const ERROR_PATH = 'pages/api/surveys/[surveyId]/entries/download.ts' | ||
|
||
async function generateCsvFromObject(data: Array<Record<string, number | string | undefined>>): Promise<string> { | ||
return new Promise((resolve, reject) => { | ||
stringify( | ||
data, | ||
{ | ||
header: true, | ||
quoted_empty: true, | ||
quoted_string: true, | ||
}, | ||
(err, dataAsCsv) => { | ||
if (err) { | ||
reject(err) | ||
|
||
return | ||
} | ||
|
||
resolve(dataAsCsv) | ||
}, | ||
) | ||
}) | ||
} | ||
|
||
export default async function SurveyEntriesDownloadEndpoint(req: NextApiRequest, res: NextApiResponse) { | ||
switch (req.method) { | ||
case 'GET': | ||
try { | ||
await handleAuth(req, res, [UserRole.ADMINISTRATOR, UserRole.MANAGER], true) | ||
|
||
const { surveyId } = req.query | ||
if (typeof surveyId !== 'string') { | ||
throw new ApiError('`surveyId` must be a string.', 422, true) | ||
} | ||
|
||
const maybeSurvey = await prisma.survey.findUnique({ | ||
where: { | ||
id: surveyId, | ||
}, | ||
}) | ||
if (maybeSurvey === null) { | ||
throw new ApiError('Not found.', 404, true) | ||
} | ||
|
||
const data = maybeSurvey.data as TellMe.Data | ||
const dataAsFlatObject = data.entries.map(({ answers, id, openedAt, submittedAt }) => | ||
answers | ||
.filter(({ type }) => type !== 'file') | ||
.reduce( | ||
(prev, { question, rawValue }) => ({ | ||
...prev, | ||
[question.value]: rawValue, | ||
}), | ||
{ | ||
/* eslint-disable sort-keys-fix/sort-keys-fix */ | ||
ID: id, | ||
'Opened At': openedAt, | ||
'Submitted At': submittedAt, | ||
/* eslint-enable sort-keys-fix/sort-keys-fix */ | ||
} as Record<string, number | string | undefined>, | ||
), | ||
) | ||
const dataKeys = uniq(dataAsFlatObject.map(keys).flat()) | ||
const consolidatedDataAsFlatObject = dataAsFlatObject.map(record => | ||
dataKeys.reduce((_record, dataKey) => { | ||
if (_record[dataKey] === undefined) { | ||
return { | ||
..._record, | ||
[dataKey]: undefined, | ||
} | ||
} | ||
|
||
return _record | ||
}, record), | ||
) | ||
const dataAsCsv = await generateCsvFromObject(consolidatedDataAsFlatObject) | ||
|
||
res.setHeader('Content-Type', 'text/csv') | ||
res.status(200).send(dataAsCsv) | ||
} catch (err) { | ||
handleApiEndpointError(err, ERROR_PATH, res, true) | ||
} | ||
|
||
return undefined | ||
|
||
default: | ||
handleApiEndpointError(new ApiError('Method not allowed.', 405, true), ERROR_PATH, res, true) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.