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

tweak(adminPanel): RN-1409: Inclusion of Additional Data Fields in Entity Hierarchy Export File #5852

Merged
merged 18 commits into from
Nov 27, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import { Request } from 'express';
import xlsx from 'xlsx';
import { keyBy } from 'lodash';
import { Route } from '@tupaia/server-boilerplate';

export type ExportEntityHierarchiesRequest = Request<
Expand All @@ -14,6 +15,15 @@ export type ExportEntityHierarchiesRequest = Request<
Record<string, any>
>;

type ExportEntityHierarchiesData = {
parent_name: string;
parent_code: string;
name: string;
code: string;
type: string;
attributes: Record<string, any>;
};

export class ExportEntityHierarchiesRoute extends Route<ExportEntityHierarchiesRequest> {
protected readonly type = 'download';

Expand All @@ -30,18 +40,59 @@ export class ExportEntityHierarchiesRoute extends Route<ExportEntityHierarchiesR
hierarchy,
hierarchy,
{
fields: ['name', 'code', 'parent_code'],
fields: ['parent_code', 'name', 'code', 'type', 'attributes'],
},
false,
true,
false,
);

const descendantsByCode = keyBy(descendants, 'code');

const data = descendants.map((row: ExportEntityHierarchiesData) => {
const record = {
'grandparent name': undefined,
'grandparent code': undefined,
'parent name': undefined,
'parent code': row.parent_code,
name: row.name,
code: row.code,
type: row.type,
attributes: Object.entries(row.attributes)
.map(([key, value]) => `${key}: ${value}`)
.join('\n'),
};

if (row.parent_code) {
const parent = descendantsByCode[row.parent_code];
record['parent name'] = parent?.name;

if (parent?.parent_code) {
const grandparent = descendantsByCode[parent.parent_code];
record['grandparent name'] = grandparent?.name;
record['grandparent code'] = grandparent?.code;
}
}

return record;
});

const projectEntity = await entityApi.getEntity(hierarchy, hierarchy, {
fields: ['name'],
});

const sheetName = projectEntity?.name || hierarchy;
const sheet = xlsx.utils.json_to_sheet(descendants);
const sheet = xlsx.utils.json_to_sheet(data, {
header: [
'grandparent name',
'grandparent code',
'parent name',
'parent code',
'name',
'code',
'type',
'attributes',
],
});
xlsx.utils.book_append_sheet(workbook, sheet, sheetName);
}

Expand Down