Skip to content
Merged
108 changes: 94 additions & 14 deletions app/controllers/download.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,66 @@
import programmesData from '../datasets/programmes.js'
import { AcademicYear, DownloadFormat, ProgrammeType } from '../enums.js'
import { Download, Programme, Team } from '../models.js'
import { getDateValueDifference } from '../utils/date.js'
import { getResults, getPagination } from '../utils/pagination.js'

export const downloadController = {
read(request, response, next, download_id) {
response.locals.download = Download.findOne(
download_id,
request.session.data
)

next()
},

readAll(request, response, next) {
response.locals.downloads = Download.findAll(request.session.data)

next()
},

list(request, response) {
const { type } = request.query
const { data } = request.session
const { downloads } = response.locals

let results = downloads

// Filter by type
if (type && type !== 'none') {
results = results.filter((download) => download.type === type)
}

// Sort
results = results.sort((a, b) =>
getDateValueDifference(b.createdAt, a.createdAt)
)

// Results
response.locals.results = getResults(results, request.query, 40)
response.locals.pages = getPagination(results, request.query, 40)

// Clean up session data
delete data.type

response.render(`download/list`)
},

filterList(request, response) {
const params = new URLSearchParams()

// Radios and text inputs
for (const key of ['type']) {
const value = request.body[key]
if (value) {
params.append(key, String(value))
}
}

response.redirect(`/downloads?${params}`)
},

form(request, response) {
const { data } = request.session

Expand Down Expand Up @@ -44,24 +102,46 @@ export const downloadController = {

create(request, response) {
const { account } = request.app.locals
const { data } = request.session
const { programmeType, session_id, type } = request.body.download
const { data, referrer } = request.session
const { __ } = response.locals

const { type } = request.body.download
const programme_id = programmesData[type].id
const programme = Programme.findOne(programme_id, data)

const createdDownload = Download.create(
{
...request.body.download,
programme_id,
vaccination_uuids: programme.vaccinations.map(({ uuid }) => uuid),
createdBy_uid: account.uid
},
data
)
let createdDownload
if (type) {
createdDownload = Download.create(
{
createdBy_uid: account.uid,
session_id,
type
},
data
)
} else {
const programme_id = programmesData[programmeType].id
const programme = Programme.findOne(programme_id, data)

createdDownload = Download.create(
{
...request.body.download,
programme_id,
vaccination_uuids: programme.vaccinations.map(({ uuid }) => uuid),
createdBy_uid: account.uid
},
data
)
}

const download = new Download(createdDownload, data)

request.flash('success', __(`download.new.success`, { download }))

response.redirect(referrer)
},

download(request, response) {
const { data } = request.session
const { download } = response.locals

// Generate and return file
const { buffer, fileName, mimetype } = download.createFile(data)

Expand Down
5 changes: 5 additions & 0 deletions app/controllers/interchange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const interchangeController = {
list(request, response) {
response.redirect('/uploads')
}
}
12 changes: 0 additions & 12 deletions app/controllers/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -636,18 +636,6 @@ export const sessionController = {
response.redirect(paths.next)
},

downloadFile(request, response) {
const { data } = request.session
const { session } = response.locals

const { buffer, fileName, mimetype } = session.createFile(data)

response.header('Content-Type', mimetype)
response.header('Content-disposition', `attachment; filename=${fileName}`)

response.end(buffer)
},

giveInstructions(request, response) {
const { account } = request.app.locals
const { __, session } = response.locals
Expand Down
1 change: 1 addition & 0 deletions app/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const data = {
clinics,
counts: {},
defaultBatches: {},
downloads: {},
features: {},
instructions,
moves,
Expand Down
19 changes: 19 additions & 0 deletions app/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ export const DownloadFormat = {
SystmOne: 'XLSX for SystmOne (TPP)'
}

/**
* @readonly
* @enum {string}
*/
export const DownloadStatus = {
Processing: 'Processing',
Ready: 'Ready'
}

/**
* @readonly
* @enum {string}
*/
export const DownloadType = {
Report: 'Vaccination records',
Moves: 'School moves',
Session: 'Offline session'
}

/**
* @readonly
* @enum {string}
Expand Down
38 changes: 35 additions & 3 deletions app/locales/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -712,9 +712,27 @@ export const en = {
}
},
download: {
label: 'Exports',
list: {
label: 'Exports',
title: 'Exports'
},
search: {
label: 'Find export'
},
results:
'{count, plural, =0 {No exports matching your search criteria were found} one {Showing <b>{from}</b> to <b>{to}</b> of <b>{count}</b> export} other {Showing <b>{from}</b> to <b>{to}</b> of <b>{count}</b> exports}}',
new: {
label: 'Download vaccination report',
confirm: 'Download vaccination data'
confirm: 'Download vaccination data',
success:
'It will take some time to prepare the records. You’ll be able to download them soon in [Exports](/downloads)'
},
createdAt: {
label: 'Requested at'
},
createdBy: {
label: 'Requested by'
},
startAt: {
label: 'Get vaccination data from'
Expand All @@ -726,10 +744,16 @@ export const en = {
title: 'Select file format',
label: 'File format'
},
status: {
label: 'Status'
},
teams: {
title: 'Select providers',
label: 'Providers'
},
type: {
label: 'Type'
},
vaccinations: {
label: 'Records'
}
Expand Down Expand Up @@ -860,6 +884,14 @@ export const en = {
title: 'Home'
}
},
interchange: {
label: 'Manage data',
list: {
label: 'Manage data',
title: 'Manage data',
description: 'Import and export child and vaccination records'
}
},
notice: {
label: 'Notice',
list: {
Expand Down Expand Up @@ -1918,7 +1950,7 @@ export const en = {
title: 'Record offline',
description:
'If the internet connection at the vaccination session is unreliable, you can record offline using a spreadsheet.\n\nYou need to download the blank spreadsheet ahead of the session while you still have internet access.\n\nTo upload a completed spreadsheet, go to the ‘Vaccinations’ area. You also need an internet connection to upload the spreadsheet.',
confirm: 'Download spreadsheet',
confirm: 'Download offline spreadsheet',
vaccinator: {
label: 'Vaccinator',
firstName: 'First name',
Expand Down Expand Up @@ -2142,7 +2174,7 @@ export const en = {
'Use this page to upload and import child, class list and vaccination records.\n\nUpload times can vary. Refresh the page to see the latest status.'
},
search: {
label: 'Find upload'
label: 'Find import'
},
results:
'{count, plural, =0 {No imports matching your search criteria were found} one {Showing <b>{from}</b> to <b>{to}</b> of <b>{count}</b> import} other {Showing <b>{from}</b> to <b>{to}</b> of <b>{count}</b> imports}}',
Expand Down
Loading