Skip to content

Commit cc57f2f

Browse files
Add offline session export to exports list
1 parent 5f5c00f commit cc57f2f

8 files changed

Lines changed: 54 additions & 46 deletions

File tree

app/controllers/download.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,21 +102,32 @@ export const downloadController = {
102102

103103
create(request, response) {
104104
const { account } = request.app.locals
105+
const { programmeType, type } = request.body.download
105106
const { data } = request.session
106107

107-
const { programmeType } = request.body.download
108-
const programme_id = programmesData[programmeType].id
109-
const programme = Programme.findOne(programme_id, data)
110-
111-
const createdDownload = Download.create(
112-
{
113-
...request.body.download,
114-
programme_id,
115-
vaccination_uuids: programme.vaccinations.map(({ uuid }) => uuid),
116-
createdBy_uid: account.uid
117-
},
118-
data
119-
)
108+
let createdDownload
109+
if (type) {
110+
createdDownload = Download.create(
111+
{
112+
createdBy_uid: account.uid,
113+
type
114+
},
115+
data
116+
)
117+
} else {
118+
const programme_id = programmesData[programmeType].id
119+
const programme = Programme.findOne(programme_id, data)
120+
121+
createdDownload = Download.create(
122+
{
123+
...request.body.download,
124+
programme_id,
125+
vaccination_uuids: programme.vaccinations.map(({ uuid }) => uuid),
126+
createdBy_uid: account.uid
127+
},
128+
data
129+
)
130+
}
120131

121132
const download = new Download(createdDownload, data)
122133

app/controllers/session.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -636,18 +636,6 @@ export const sessionController = {
636636
response.redirect(paths.next)
637637
},
638638

639-
downloadFile(request, response) {
640-
const { data } = request.session
641-
const { session } = response.locals
642-
643-
const { buffer, fileName, mimetype } = session.createFile(data)
644-
645-
response.header('Content-Type', mimetype)
646-
response.header('Content-disposition', `attachment; filename=${fileName}`)
647-
648-
response.end(buffer)
649-
},
650-
651639
giveInstructions(request, response) {
652640
const { account } = request.app.locals
653641
const { __, session } = response.locals

app/enums.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ export const DownloadFormat = {
111111
* @enum {string}
112112
*/
113113
export const DownloadType = {
114-
Report: 'Vaccination records'
114+
Report: 'Vaccination records',
115+
Session: 'Offline session'
115116
}
116117

117118
/**

app/models/download.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { fakerEN_GB as faker } from '@faker-js/faker'
22
import xlsx from 'json-as-xlsx'
33

44
import { DownloadFormat, DownloadType } from '../enums.js'
5-
import { Programme, Team, Vaccination, User } from '../models.js'
5+
import { Programme, Session, Team, Vaccination, User } from '../models.js'
66
import {
77
convertIsoDateToObject,
88
convertObjectToIsoDate,
@@ -44,6 +44,7 @@ export class Download {
4444
this.format = options?.format || DownloadFormat.CSV
4545
this.type = options?.type || DownloadType.Report
4646
this.programme_id = options?.programme_id
47+
this.session_id = options?.session_id
4748
this.team_ids = options?.team_ids
4849
this.vaccination_uuids = options?.vaccination_uuids || []
4950
}
@@ -112,6 +113,8 @@ export class Download {
112113
switch (true) {
113114
case this.type === DownloadType.Report:
114115
return `${this.programme.name} vaccination records`
116+
case this.type === DownloadType.Session:
117+
return `Offline spreadsheet for ${this.session.name}`
115118
default:
116119
return 'Download'
117120
}
@@ -133,6 +136,22 @@ export class Download {
133136
}
134137
}
135138

139+
/**
140+
* Get session
141+
*
142+
* @returns {Session} Session
143+
*/
144+
get session() {
145+
try {
146+
const session = this.context?.sessions[this.session_id]
147+
if (session) {
148+
return new Session(session)
149+
}
150+
} catch (error) {
151+
console.error('Download.session', error.message)
152+
}
153+
}
154+
136155
/**
137156
* Get teams
138157
*

app/models/session.js

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -826,23 +826,6 @@ export class Session {
826826
return createdSession
827827
}
828828

829-
/**
830-
* Create file
831-
*
832-
* @param {object} context - Context
833-
* @returns {object} File buffer, name and mime type
834-
* @todo Create download using Mavis offline XLSX schema
835-
*/
836-
createFile(context) {
837-
const { name } = new Session(this, context)
838-
839-
return {
840-
buffer: Buffer.from(''),
841-
fileName: `${name}.csv`,
842-
mimetype: 'text/csv'
843-
}
844-
}
845-
846829
/**
847830
* Update
848831
*

app/routes/session.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ router.post('/:session_id/edit/:view', session.updateForm)
2525

2626
router.post('/:session_id/invite-to-clinic', session.inviteToClinic)
2727
router.post('/:session_id/instructions', session.giveInstructions)
28-
router.post('/:session_id/offline', session.downloadFile)
2928
router.post('/:session_id/reminders', session.sendReminders)
3029

3130
router.all('/:session_id/:view', session.readPatientSessions)

app/views/_layouts/form.njk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
{% block content %}
66
{{ super() }}
77

8-
<form class="nhsuk-grid-row" method="post" novalidate data-validate>
8+
<form class="nhsuk-grid-row" action="{{ formAction }}" method="post" novalidate data-validate>
99
<div class="nhsuk-grid-column-{{ gridColumns }}">
1010
{% block form %}
1111
{% endblock %}

app/views/session/offline.njk

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
{% set title = __("session.offline.title") %}
44
{% set confirmButtonText = __("session.offline.confirm") %}
5+
{% set formAction = "/downloads/new" %}
56

67
{% block beforeContent %}
78
{{ breadcrumb({
@@ -46,5 +47,11 @@
4647
decorate: "lastName",
4748
value: account.lastName
4849
}) }}
50+
51+
{{ input({
52+
type: "hidden",
53+
decorate: "download.type",
54+
value: DownloadType.Session
55+
}) }}
4956
{% endcall %}
5057
{% endblock %}

0 commit comments

Comments
 (0)