Skip to content

Commit

Permalink
FEATURE: Radars can be republished (same cutoff date as before)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdrescher committed Nov 4, 2020
1 parent 70925ce commit 54500f3
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 18 deletions.
21 changes: 4 additions & 17 deletions src/server/controllers/radarController.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,28 +83,15 @@ exports.getEditions = async () => {
//
// publish the radar
//
// Once checked for any mistakes after rendering, an admin (or other
// authorised user) can publish the radar for the public to analyse
exports.publishRadar = async (slug, date) => {
const cutOffDate = moment(date) // creates a Date.now() if date param is missing

// 1) Obtain the radar
const radar = await this.getRadarBySlug(slug)
if (!radar) {
throw new AppError(`No radar found for id ${slug}.`, 404)
}
// radar state change check
if (!['created'].includes(radar.status)) {
throw new AppError(`Radar ${radar.name} is not in state created.`, 500)
}

// 2) Render the radar based on the data received
// Fetches the (transient) radar data and renders it into an SVG and a set of tables.
exports.publishRadar = async (radar, cutOffDate) => {
// 1) Render the radar based on the data received
const data = await compileRadarPopulation(cutOffDate)
const rendering = renderer.renderRadar(data)
rendering.radar = radar._id
await rendering.save()

// 3) save radar state
// 2) save radar state
radar.status = 'published'
radar.publicationDate = Date.now()
radar.rendering = rendering._id
Expand Down
50 changes: 49 additions & 1 deletion src/server/handlers/radarHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
// IMPORTS
//
// libraries
const moment = require('moment')
// app modules
const AppError = require('../utils/AppError')
const catchAsync = require('../utils/catchAsync')
const handlerFactory = require('./handlerFactory')
const httpResponses = require('../utils/httpResponses')
// const logger = require('./../utils/logger')
const Radar = require('../models/radarModel')
const { RadarData, RadarRendering } = require('../models/radarDataModel')
const radarController = require('./../controllers/radarController')

exports.createRadar = handlerFactory.createOne(
Expand Down Expand Up @@ -96,12 +98,58 @@ exports.renderRadar = catchAsync(async (req, res, next) => {
// publish the radar
exports.publishRadar = catchAsync(async (req, res, next) => {
const { slug, date } = req.params
const cutOffDate = moment(date) // creates a Date.now() if date param is missing

const radar = await radarController.publishRadar(slug, date)
// 1) Obtain the radar
let radar = await radarController.getRadarBySlug(slug)
if (!radar) {
throw new AppError(`No radar found for id ${slug}.`, 404)
}
// radar state change check
if (!['created'].includes(radar.status)) {
throw new AppError(`Radar ${radar.name} is not in state created.`, 400)
}

// 2) Let the radar controller publish the radar
radar = await radarController.publishRadar(radar, cutOffDate)
if (!radar || radar.length === 0) {
return next(new AppError(`Error while publishing the radar.`, 500))
}

// 3) Return successful result
res.status(200).json({
status: 'success',
message: 'Radar published.',
})
})

// publish the radar
exports.republishRadar = catchAsync(async (req, res, next) => {
const { slug } = req.params

// 1) Obtain the radar
let radar = await radarController.getRadarBySlug(slug)
if (!radar) {
return next(new AppError(`No radar found for id ${slug}.`, 404))
}
// radar state change check
if (!['published'].includes(radar.status)) {
return next(new AppError(`Radar ${radar.name} is not in state created.`, 400))
}

// 2) Remove any data or rendering from the radar
await RadarData.findByIdAndDelete(radar.data)
await RadarRendering.findByIdAndDelete(radar.rendering)
radar.data = undefined
radar.rendering = undefined

// 3) render the radar with the cutoff date already set in the radar
radar = await radarController.publishRadar(radar, moment(radar.referenceDate))
if (!radar || radar.length === 0) {
return next(new AppError(`Error while publishing the radar.`, 500))
}

// 4) Return success
res.status(200).json({
status: 'success',
message: 'Radar published.',
Expand Down
6 changes: 6 additions & 0 deletions src/server/routers/radarRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ router.patch(
authC.restrictTo('admin', 'manager'),
handler.publishRadar
)
router.patch(
'/:slug/re-publish',
authC.protect,
authC.restrictTo('admin', 'manager'),
handler.republishRadar
)
router.patch(
'/:slug/archive',
authC.protect,
Expand Down
1 change: 1 addition & 0 deletions src/server/views/admin/editRadar.pug
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ block content
h2 Administer Radar
.populate-radar-form
+administerForm(radar.slug, 'Publish', radar.status === 'created')
+administerForm(radar.slug, 'Re-publish', radar.status === 'published')
+administerForm(radar.slug, 'Archive', radar.status === 'published')
+administerForm(radar.slug, 'Reset', true)

0 comments on commit 54500f3

Please sign in to comment.