Skip to content

Commit

Permalink
working export
Browse files Browse the repository at this point in the history
  • Loading branch information
wheelsandcogs committed Dec 11, 2024
1 parent 7756f73 commit 8192bfa
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 18 deletions.
7 changes: 7 additions & 0 deletions src/dtos/translations-dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class TranslationDTO {
type: string;
key: string;
id?: string;
english?: string;
cymraeg?: string;
}
74 changes: 56 additions & 18 deletions src/route/translation.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,77 @@
import { Request, Response, NextFunction, Router } from 'express';
import { stringify } from 'csv';

import { logger } from '../utils/logger';
import { UnknownException } from '../exceptions/unknown.exception';
import { Dataset } from '../entities/dataset/dataset';
import { DatasetInfo } from '../entities/dataset/dataset-info';
import { TranslationDTO } from '../dtos/translations-dto';

export const translationRouter = Router();
import { loadDataset } from './dataset';

translationRouter.get('/preview', async (req: Request, res: Response, next: NextFunction) => {
try {
logger.info('Previewing translations for export...');
const collectTranslations = (dataset: Dataset): TranslationDTO[] => {
const metadataEN = dataset.datasetInfo?.find((info) => info.language.includes('en'));
const metadataCY = dataset.datasetInfo?.find((info) => info.language.includes('cy'));

// collect all the things needing translation
// return it as a json object
const metadataProps: (keyof DatasetInfo)[] = [
'title',
'description',
'collection',
'quality',
'roundingDescription'
];

res.json({});
} catch (error) {
logger.error('Error previewing translations', error);
next(new UnknownException());
const translations: TranslationDTO[] = [
...dataset.dimensions?.map((dim) => ({
type: 'dimension',
id: dim.id,
key: dim.factTableColumn,
english: dim.dimensionInfo?.find((info) => info.language.includes('en'))?.name,
welsh: dim.dimensionInfo?.find((info) => info.language.includes('cy'))?.name
})),
...metadataProps.map((prop) => ({
type: 'metadata',
key: prop,
english: metadataEN?.[prop] as string,
welsh: metadataCY?.[prop] as string
}))
];

return translations;
};

export const translationRouter = Router();

translationRouter.get(
'/:dataset_id/preview',
loadDataset(),
async (req: Request, res: Response, next: NextFunction) => {
try {
logger.info('Previewing translations for export...');
const dataset: Dataset = res.locals.dataset;
const translations = collectTranslations(dataset);
res.json(translations);
} catch (error) {
logger.error('Error previewing translations', error);
next(new UnknownException());
}
}
});
);

translationRouter.get('/export', async (req: Request, res: Response, next: NextFunction) => {
translationRouter.get('/:dataset_id/export', loadDataset(), async (req: Request, res: Response, next: NextFunction) => {
try {
logger.info('Exporting translations to CSV...');

// collect all the things needing translation
// return it as a CSV file

res.json({});
const dataset: Dataset = res.locals.dataset;
const translations = collectTranslations(dataset);
res.setHeader('Content-Type', 'text/csv');
stringify(translations, { bom: true, header: true }).pipe(res);
} catch (error) {
logger.error('Error exporting translations', error);
next(new UnknownException());
}
});

translationRouter.get('/import', async (req: Request, res: Response, next: NextFunction) => {
translationRouter.get('/:dataset_id/import', loadDataset(), async (req: Request, res: Response, next: NextFunction) => {
try {
logger.info('Importing translations from CSV...');

Expand Down

0 comments on commit 8192bfa

Please sign in to comment.