Skip to content

Commit

Permalink
fix all the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wheelsandcogs committed Oct 29, 2024
1 parent 8ce7528 commit e0502df
Show file tree
Hide file tree
Showing 7 changed files with 524 additions and 916 deletions.
43 changes: 20 additions & 23 deletions src/controllers/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const provideTitle = async (req: Request, res: Response, next: NextFuncti
try {
const titleError = await hasError(titleValidator(), req);
if (titleError) {
res.status(400);
throw new Error('errors.title.missing');
}

Expand Down Expand Up @@ -68,6 +69,7 @@ export const uploadFile = async (req: Request, res: Response, next: NextFunction
res.redirect(req.buildUrl(`/publish/${dataset.id}/preview`, req.language));
return;
} catch (err) {
res.status(400);
const error: ViewError = { field: 'csv', tag: { name: 'errors.upload.no_csv_data' } };
errors = generateViewErrors(undefined, 400, [error]);
}
Expand All @@ -83,43 +85,38 @@ export const importPreview = async (req: Request, res: Response, next: NextFunct
let previewData: ViewDTO | undefined;
let ignoredCount = 0;

try {
if (!dataset || !revision || !fileImport) {
logger.error('Import not found');
throw new Error('errors.preview.import_missing');
if (!dataset || !revision || !fileImport) {
logger.error('Import not found');
next(new UnknownException('errors.preview.import_missing'));
return;
}

if (req.method === 'POST') {
try {
await req.swapi.confirmFileImport(dataset.id, revision.id, fileImport.id);
res.redirect(req.buildUrl(`/publish/${dataset.id}/sources`, req.language));
return;
} catch (err: any) {
res.status(500);
const error: ViewError = { field: 'confirm', tag: { name: 'errors.preview.confirm_error' } };
errors = generateViewErrors(dataset.id, 500, [error]);
}
}

try {
const pageNumber = Number.parseInt(req.query.page_number as string, 10) || 1;
const pageSize = Number.parseInt(req.query.page_size as string, 10) || 10;
previewData = await req.swapi.getImportPreview(dataset.id, revision.id, fileImport.id, pageNumber, pageSize);
ignoredCount = previewData.headers.filter((header) => header.source_type === SourceType.Ignore).length;
} catch (err: any) {
res.status(400);
const error: ViewError = { field: 'preview', tag: { name: 'errors.preview.failed_to_get_preview' } };
errors = generateViewErrors(undefined, 400, [error]);
}

res.render('publish/preview', { ...previewData, ignoredCount, revisit, errors });
};

export const confirm = async (req: Request, res: Response, next: NextFunction) => {
const { dataset, revision, fileImport } = res.locals;

try {
if (!dataset || !revision || !fileImport) {
logger.error('Import not found');
throw new Error('errors.preview.import_missing');
}

await req.swapi.confirmFileImport(dataset.id, revision.id, fileImport.id);
res.redirect(req.buildUrl(`/publish/${dataset.id}/sources`, req.language));
} catch (err: any) {
const error: ViewError = { field: 'confirm', tag: { name: 'errors.preview.confirm_error' } };
req.session.errors = generateViewErrors(dataset.id, 500, [error]);
req.session.save();
res.redirect(req.buildUrl(`/publish/${dataset.id}/preview`, req.language));
}
};

export const sources = async (req: Request, res: Response, next: NextFunction) => {
const { dataset, revision, fileImport } = res.locals;
const revisit = dataset.dimensions?.length > 0;
Expand Down
7 changes: 2 additions & 5 deletions src/routes/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ import { Router } from 'express';
import multer from 'multer';

import { fetchDataset } from '../middleware/fetch-dataset';
import { flashErrors } from '../middleware/flash-errors';
import {
start,
provideTitle,
uploadFile,
importPreview,
confirm,
sources,
taskList,
changeData,
Expand All @@ -32,9 +30,8 @@ publish.post('/:datasetId/title', fetchDataset, upload.none(), provideTitle);
publish.get('/:datasetId/upload', fetchDataset, uploadFile);
publish.post('/:datasetId/upload', fetchDataset, upload.single('csv'), uploadFile);

publish.get('/:datasetId/preview', fetchDataset, flashErrors, importPreview);

publish.get('/:datasetId/confirm', fetchDataset, confirm);
publish.get('/:datasetId/preview', fetchDataset, importPreview);
publish.post('/:datasetId/preview', fetchDataset, upload.none(), importPreview);

publish.get('/:datasetId/sources', fetchDataset, sources);
publish.post('/:datasetId/sources', fetchDataset, upload.none(), sources);
Expand Down
19 changes: 12 additions & 7 deletions src/views/publish/preview.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,18 @@
</div>
<div class="govuk-grid-row">
<div class="govuk-grid-column-one-half">
<% if (locals.revisit) { %>
<a href="<%= buildUrl(`/publish/${locals.datasetId}/change`, i18n.language) %>" class="govuk-button govuk-!-display-inline"><%= t('publish.preview.buttons.change_datatable') %></a>
<% } else { %>
<p class="govuk-body"><%= t('publish.preview.confirm_correct') %></p>
<a href="<%= buildUrl(`/publish/${locals.datasetId}/confirm`, i18n.language) %>" class="govuk-button govuk-!-display-inline"><%= t('buttons.continue') %></a>
<a href="<%= buildUrl(`/publish/${locals.datasetId}/upload`, i18n.language) %>" class="govuk-button govuk-button--secondary govuk-!-display-inline"><%= t('publish.preview.buttons.choose_different') %></a>
<% } %>
<form method="post">
<% if (locals.revisit) { %>
<a href="<%= buildUrl(`/publish/${locals.datasetId}/change`, i18n.language) %>" class="govuk-button govuk-!-display-inline"><%= t('publish.preview.buttons.change_datatable') %></a>
<% } else { %>
<p class="govuk-body"><%= t('publish.preview.confirm_correct') %></p>
<!-- <a href="<%= buildUrl(`/publish/${locals.datasetId}/confirm`, i18n.language) %>" class="govuk-button govuk-!-display-inline"><%= t('buttons.continue') %></a> -->
<button type="submit" name="confirm" value="true" class="govuk-button govuk-!-display-inline" data-module="govuk-button">
<%= t('buttons.continue') %>
</button>
<a href="<%= buildUrl(`/publish/${locals.datasetId}/upload`, i18n.language) %>" class="govuk-button govuk-button--secondary govuk-!-display-inline"><%= t('publish.preview.buttons.choose_different') %></a>
<% } %>
</form>
</div>
</div>
<% } %> <!-- Do I have data? -->
Expand Down
Loading

0 comments on commit e0502df

Please sign in to comment.