Skip to content

Commit

Permalink
add designation metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
wheelsandcogs committed Nov 5, 2024
1 parent d9a647b commit 28cb0fb
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/controllers/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { generateViewErrors } from '../utils/generate-view-errors';
import {
collectionValidator,
descriptionValidator,
designationValidator,
hasError,
qualityValidator,
roundingAppliedValidator,
Expand All @@ -24,6 +25,7 @@ import { NotFoundException } from '../exceptions/not-found.exception';
import { statusToColour } from '../utils/status-to-colour';
import { singleLangDataset } from '../utils/single-lang-dataset';
import { updateSourceTypes } from '../utils/update-source-types';
import { Designation } from '../enums/designation';

export const start = (req: Request, res: Response, next: NextFunction) => {
res.render('publish/start');
Expand Down Expand Up @@ -318,3 +320,31 @@ export const provideQuality = async (req: Request, res: Response, next: NextFunc

res.render('publish/quality', { ...datasetInfo, errors });
};

export const provideDesignation = async (req: Request, res: Response, next: NextFunction) => {
let errors: ViewErrDTO | undefined;
const dataset = singleLangDataset(res.locals.dataset, req.language);
const designationOptions = Object.values(Designation);
let designation = dataset?.datasetInfo?.designation;

if (req.method === 'POST') {
try {
const designationError = await hasError(designationValidator(), req);
if (designationError) {
res.status(400);
throw new Error('errors.designation.missing');
}

designation = req.body.designation;

await req.swapi.updateDatasetInfo(dataset.id, { designation, language: req.language });
res.redirect(req.buildUrl(`/publish/${dataset.id}/tasklist`, req.language));
return;
} catch (err) {
const error: ViewError = { field: 'designation', tag: { name: 'errors.designation.missing' } };
errors = generateViewErrors(undefined, 400, [error]);
}
}

res.render('publish/designation', { designation, designationOptions, errors });
};
3 changes: 3 additions & 0 deletions src/dtos/dataset-info.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Designation } from '../enums/designation';

export interface DatasetInfoDTO {
language?: string;
title?: string;
Expand All @@ -6,4 +8,5 @@ export interface DatasetInfoDTO {
quality?: string;
rounding_applied?: boolean;
rounding_description?: string;
designation?: Designation;
}
6 changes: 6 additions & 0 deletions src/enums/designation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum Designation {
Official = 'official',
Accredited = 'accredited',
InDevelopment = 'in_development',
None = 'none'
}
20 changes: 20 additions & 0 deletions src/middleware/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,23 @@
}
}
},
"designation": {
"heading": "How is this dataset designated?",
"options": {
"official": {
"label": "Official statistics"
},
"accredited": {
"label": "Accredited official statistics"
},
"in_development": {
"label": "Official statistics in development"
},
"none": {
"label": "No designation"
}
}
},
"upload": {
"title": "Upload the data table",
"note": "The file should be in a CSV format"
Expand Down Expand Up @@ -284,6 +301,9 @@
"rounding_description": {
"missing": "You need to provide a description of the rounding applied"
},
"designation": {
"missing": "You need to provide the designation of the dataset"
},
"upload": {
"no_csv": "No CSV data provided",
"no_csv_data": "No CSV data available"
Expand Down
6 changes: 5 additions & 1 deletion src/routes/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
redirectToTasklist,
provideSummary,
provideCollection,
provideQuality
provideQuality,
provideDesignation
} from '../controllers/publish';

export const publish = Router();
Expand Down Expand Up @@ -52,3 +53,6 @@ publish.post('/:datasetId/collection', fetchDataset, upload.none(), provideColle

publish.get('/:datasetId/quality', fetchDataset, provideQuality);
publish.post('/:datasetId/quality', fetchDataset, upload.none(), provideQuality);

publish.get('/:datasetId/designation', fetchDataset, provideDesignation);
publish.post('/:datasetId/designation', fetchDataset, upload.none(), provideDesignation);
4 changes: 4 additions & 0 deletions src/validators/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Request } from 'express';
import { body, param, ValidationChain } from 'express-validator';

import { Designation } from '../enums/designation';

export const hasError = async (validator: ValidationChain, req: Request) => {
return !(await validator.run(req)).isEmpty();
};
Expand All @@ -17,3 +19,5 @@ export const qualityValidator = () => body('quality').trim().notEmpty();
export const roundingAppliedValidator = () => body('rounding_applied').notEmpty().isBoolean();
export const roundingDescriptionValidator = () =>
body('rounding_description').if(body('rounding_applied').equals('true')).trim().notEmpty();

export const designationValidator = () => body('designation').trim().isIn(Object.values(Designation));
43 changes: 43 additions & 0 deletions src/views/publish/designation.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<%- include("../partials/header"); %>
<div class="govuk-width-container app-width-container">
<!-- <a href="#" class="govuk-back-link">Back</a> -->
<main class="govuk-main-wrapper" id="main-content" role="main">

<div class="top-links">
<div class="govuk-width-container">
<a href="javascript:history.back()" class="govuk-back-link"><%= t('buttons.back') %></a>
<a href="<%= buildUrl(`/publish/${locals.datasetId}/tasklist`, i18n.language) %>" class="govuk-link return-link"><%= t('publish.header.overview') %></a>
</div>
</div>

<div class="govuk-width-container">
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-xl"><%= t('publish.designation.heading') %></h1>

<form enctype="multipart/form-data" method="post">
<%- include("../partials/error-handler"); %>

<div class="govuk-form-group">
<fieldset class="govuk-fieldset" aria-describedby="designation">
<div class="govuk-radios" data-module="govuk-radios">
<% locals.designationOptions.forEach(option => { %>
<div class="govuk-radios__item">
<input class="govuk-radios__input" id="<%= `designation_${option}` %>" name="designation" type="radio" value="<%= option %>" <%= locals.designation === option ? 'checked' : '' %> />
<label class="govuk-label govuk-radios__label" for="<%= `designation_${option}` %>"><%= t(`publish.designation.options.${option}.label`) %></label>
</div>
<% }) %>
</div>
</fieldset>
</div>

<button type="submit" class="govuk-button" data-module="govuk-button"><%= t('buttons.continue') %></button>
<a class="govuk-button govuk-button--secondary" href="javascript:history.back()"><%= t('buttons.cancel')%></a>
</form>
</div>
</div>
</div>
</main>
</div>

<%- include("../partials/footer"); %>

0 comments on commit 28cb0fb

Please sign in to comment.