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

RN-1060: (task) Added dashboards/:project/:entity/:dashboard/export route to tupaia-web-server #5125

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions packages/tupaia-web-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@tupaia/auth": "1.0.0",
"@tupaia/database": "1.0.0",
"@tupaia/server-boilerplate": "1.0.0",
"@tupaia/server-utils": "1.0.0",
"@tupaia/tsutils": "1.0.0",
"@tupaia/types": "1.0.0",
"@tupaia/utils": "1.0.0",
Expand Down
4 changes: 4 additions & 0 deletions packages/tupaia-web-server/src/app/createApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ export function createApp(db: TupaiaDatabase = new TupaiaDatabase()) {
'dashboards/:projectCode/:entityCode',
handleWith(routes.DashboardsRoute),
)
.post<routes.ExportDashboardRequest>(
'dashboards/:projectCode/:entityCode/:dashboardName/export',
handleWith(routes.ExportDashboardRoute),
)
.get<routes.CountryAccessListRequest>(
'countryAccessList',
handleWith(routes.CountryAccessListRoute),
Expand Down
36 changes: 36 additions & 0 deletions packages/tupaia-web-server/src/routes/ExportDashboardRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Tupaia
* Copyright (c) 2017 - 2023 Beyond Essential Systems Pty Ltd
*
*/

import { Request } from 'express';
import { Route } from '@tupaia/server-boilerplate';
import { TupaiaWebExportDashboardRequest } from '@tupaia/types';
import { downloadPageAsPDF } from '@tupaia/server-utils';
import { stringifyQuery } from '@tupaia/utils';

export type ExportDashboardRequest = Request<
TupaiaWebExportDashboardRequest.Params,
TupaiaWebExportDashboardRequest.ResBody,
TupaiaWebExportDashboardRequest.ReqBody,
TupaiaWebExportDashboardRequest.ReqQuery
>;

export class ExportDashboardRoute extends Route<ExportDashboardRequest> {
protected type = 'download' as const;

public async buildResponse() {
const { projectCode, entityCode, dashboardName } = this.req.params;
const { baseUrl, selectedDashboardItems, cookieDomain } = this.req.body;
const { cookie } = this.req.headers;

const endpoint = `${projectCode}/${entityCode}/${dashboardName}/pdf-export`;
const pdfPageUrl = stringifyQuery(baseUrl, endpoint, {
selectedDashboardItems: selectedDashboardItems?.join(','),
});

const buffer = await downloadPageAsPDF(pdfPageUrl, cookie, cookieDomain);
return { contents: buffer, type: 'application/pdf' };
}
}
1 change: 1 addition & 0 deletions packages/tupaia-web-server/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export {
} from './LegacyMapOverlayReportRoute';
export { MapOverlaysRequest, MapOverlaysRoute } from './MapOverlaysRoute';
export { UserRequest, UserRoute } from './UserRoute';
export { ExportDashboardRequest, ExportDashboardRoute } from './ExportDashboardRoute';
export { ProjectRequest, ProjectRoute } from './ProjectRoute';
export { CountryAccessListRequest, CountryAccessListRoute } from './CountryAccessListRoute';
export {
Expand Down
14 changes: 6 additions & 8 deletions packages/tupaia-web/src/api/mutations/useExportDashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* Copyright (c) 2017 - 2023 Beyond Essential Systems Pty Ltd
*/
import { useMutation } from 'react-query';
import { stringifyQuery } from '@tupaia/utils';
import { API_URL, post } from '../api';
import { DashboardItem, DashboardName, EntityCode, ProjectCode } from '../../types';

Expand All @@ -18,18 +17,17 @@ type ExportDashboardBody = {
export const useExportDashboard = ({ onSuccess }: { onSuccess?: (data: Blob) => void }) => {
return useMutation<any, Error, ExportDashboardBody, unknown>(
({ projectCode, entityCode, dashboardName, selectedDashboardItems }: ExportDashboardBody) => {
const hostname = `${window.location.protocol}/${window.location.host}`;
const endpoint = `${projectCode}/${entityCode}/${dashboardName}/pdf-export`;
const pdfPageUrl = stringifyQuery(hostname, endpoint, {
selectedDashboardItems: selectedDashboardItems?.join(','),
});
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('pdf', {

return post(`dashboards/${projectCode}/${entityCode}/${dashboardName}/export`, {
responseType: 'blob',
data: {
cookieDomain,
pdfPageUrl,
baseUrl,
selectedDashboardItems,
},
});
},
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/types/requests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export {
TupaiaWebChangePasswordRequest,
TupaiaWebCountryAccessListRequest,
TupaiaWebDashboardsRequest,
TupaiaWebExportDashboardRequest,
TupaiaWebEntitiesRequest,
TupaiaWebEntityRequest,
TupaiaWebEntitySearchRequest,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Tupaia
* Copyright (c) 2017 - 2023 Beyond Essential Systems Pty Ltd
*/

export interface Params {
projectCode: string;
entityCode: string;
dashboardName: string;
}
export interface ResBody {
contents: Buffer;
filePath?: string;
type: string;
}
export type ReqBody = {
cookieDomain: string;
baseUrl: string;
selectedDashboardItems?: string[];
};
export type ReqQuery = Record<string, string>;
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
export * as TupaiaWebChangePasswordRequest from './ChangePasswordRequest';
export * as TupaiaWebCountryAccessListRequest from './CountryAccessListRequest';
export * as TupaiaWebDashboardsRequest from './DashboardsRequest';
export * as TupaiaWebExportDashboardRequest from './ExportDashboardRequest';
export * as TupaiaWebEntitiesRequest from './EntitiesRequest';
export * as TupaiaWebEntityRequest from './EntityRequest';
export * as TupaiaWebEntitySearchRequest from './EntitySearchRequest';
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10491,6 +10491,7 @@ __metadata:
"@tupaia/auth": 1.0.0
"@tupaia/database": 1.0.0
"@tupaia/server-boilerplate": 1.0.0
"@tupaia/server-utils": 1.0.0
"@tupaia/tsutils": 1.0.0
"@tupaia/types": 1.0.0
"@tupaia/utils": 1.0.0
Expand Down