Skip to content

Commit

Permalink
RN-1060: (task) Added dashboards/:project/:entity/:dashboard/export r…
Browse files Browse the repository at this point in the history
…oute to tupaia-web (#5125)
  • Loading branch information
rohan-bes authored Oct 31, 2023
1 parent fdcc053 commit 33195c9
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 8 deletions.
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 @@ -10492,6 +10492,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

0 comments on commit 33195c9

Please sign in to comment.