-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
139 additions
and
8 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
46 changes: 46 additions & 0 deletions
46
packages/datatrak-web-server/src/routes/ExportSurveyResponseRoute.ts
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,46 @@ | ||
/* | ||
* Tupaia | ||
* Copyright (c) 2017 - 2024 Beyond Essential Systems Pty Ltd | ||
* | ||
*/ | ||
|
||
import { Request } from 'express'; | ||
import { Route } from '@tupaia/server-boilerplate'; | ||
import { downloadPageAsPDF } from '@tupaia/server-utils'; | ||
|
||
export type ExportSurveyResponseRequest = Request< | ||
{ | ||
surveyResponseId: string; | ||
}, | ||
{ | ||
contents: Buffer; | ||
type: string; | ||
}, | ||
{ | ||
baseUrl: string; | ||
cookieDomain: string; | ||
}, | ||
Record<string, unknown> | ||
>; | ||
|
||
export class ExportSurveyResponseRoute extends Route<ExportSurveyResponseRequest> { | ||
protected type = 'download' as const; | ||
|
||
public async buildResponse() { | ||
const { surveyResponseId } = this.req.params; | ||
const { baseUrl, cookieDomain } = this.req.body; | ||
const { cookie } = this.req.headers; | ||
|
||
if (!cookie) { | ||
throw new Error(`Must have a valid session to export a dashboard`); | ||
} | ||
|
||
const pdfPageUrl = `${baseUrl}/export/${surveyResponseId}`; | ||
const buffer = await downloadPageAsPDF(pdfPageUrl, cookie, cookieDomain, false, true); | ||
|
||
return { | ||
contents: buffer, | ||
type: 'application/pdf', | ||
}; | ||
} | ||
} |
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
32 changes: 32 additions & 0 deletions
32
packages/datatrak-web/src/api/mutations/useExportSurveyResponse.ts
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,32 @@ | ||
/* | ||
* Tupaia | ||
* Copyright (c) 2017 - 2024 Beyond Essential Systems Pty Ltd | ||
*/ | ||
import { useMutation } from '@tanstack/react-query'; | ||
import { API_URL, post } from '../api'; | ||
import download from 'downloadjs'; | ||
|
||
// Requests a survey response PDF export from the server, and returns the response | ||
export const useExportSurveyResponse = (surveyResponseId: string) => { | ||
return useMutation<any, Error, unknown, unknown>( | ||
() => { | ||
const baseUrl = `${window.location.protocol}//${window.location.host}`; | ||
|
||
// Auth cookies are saved against this domain. Pass this to server, so that when it pretends to be us, it can do the same. | ||
const cookieDomain = new URL(API_URL).hostname; | ||
|
||
return post(`export/${surveyResponseId}`, { | ||
responseType: 'blob', | ||
data: { | ||
cookieDomain, | ||
baseUrl, | ||
}, | ||
}); | ||
}, | ||
{ | ||
onSuccess: data => { | ||
download(data, `survey_response_${surveyResponseId}.pdf`); | ||
}, | ||
}, | ||
); | ||
}; |
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
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