Skip to content

Commit bf60b72

Browse files
Misc updates and refactoring
1 parent ec18fcd commit bf60b72

File tree

10 files changed

+341
-330
lines changed

10 files changed

+341
-330
lines changed

app/lib/generate-seed-data.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ const generateData = async () => {
109109
console.log('Generating clinics and events...');
110110
const today = dayjs().startOf('day');
111111
const snapshots = [
112-
today.subtract(9, 'year'),
113-
today.subtract(6, 'year'),
114-
today.subtract(3, 'year'),
112+
today.subtract(9, 'year').add(3, 'month'),
113+
today.subtract(6, 'year').add(2, 'month'),
114+
today.subtract(3, 'year').add(1, 'month'),
115115
today.subtract(3, 'days')
116116
];
117117

app/lib/utils/dates.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ const formatRelativeDate = (dateString, withoutSuffix = false) => {
100100
*/
101101
const isPast = (dateString) => {
102102
if (!dateString) return false;
103-
return dayjs(dateString).isBefore(dayjs());
103+
return dayjs(dateString).isBefore(dayjs(), 'day');
104104
};
105105

106106
/**

app/lib/utils/participants.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,42 @@ const sortBySurname = (participants) => {
6868
});
6969
};
7070

71+
/**
72+
* Get clinic history for a participant
73+
* @param {Object} data - Session data containing clinics and events
74+
* @param {string} participantId - Participant ID to get history for
75+
* @returns {Array} Array of clinic/event pairs
76+
*/
77+
const getParticipantClinicHistory = (data, participantId) => {
78+
if (!data?.events || !data?.clinics || !participantId) return []
79+
80+
const participantEvents = data.events.filter(event =>
81+
event.participantId === participantId
82+
)
83+
84+
// Get clinic details for each event
85+
return participantEvents.map(event => {
86+
const clinic = data.clinics.find(clinic => clinic.id === event.clinicId)
87+
const unit = data.breastScreeningUnits.find(unit => unit.id === clinic.breastScreeningUnitId);
88+
const location = unit.locations.find(location => location.id === clinic.locationId);
89+
// console.log({location})
90+
91+
return {
92+
clinic,
93+
unit,
94+
location,
95+
event
96+
}
97+
}).filter(history => history.clinic) // Remove any with missing clinics
98+
}
99+
100+
71101
module.exports = {
72102
getFullName,
73103
getFullNameReversed,
74104
getShortName,
75105
findBySXNumber,
76106
getAge,
77-
sortBySurname
107+
sortBySurname,
108+
getParticipantClinicHistory
78109
};

app/routes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ router.use(async (req, res, next) => {
2121
require('./routes/settings')(router);
2222
require('./routes/clinics')(router);
2323
require('./routes/participants')(router);
24+
require('./routes/events')(router);
2425

2526
// Add your routes here - above the module.exports line
2627

app/routes/clinics.js

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -35,33 +35,6 @@ function getClinicData(data, clinicId) {
3535
};
3636
}
3737

38-
/**
39-
* Get single event and its related data
40-
*/
41-
function getEventData(data, clinicId, eventId) {
42-
const clinic = data.clinics.find(c => c.id === clinicId)
43-
44-
if (!clinic) {
45-
return null
46-
}
47-
48-
const event = data.events.find(e => e.id === eventId && e.clinicId === clinicId)
49-
50-
if (!event) {
51-
return null
52-
}
53-
54-
const participant = data.participants.find(p => p.id === event.participantId)
55-
const unit = data.breastScreeningUnits.find(u => u.id === clinic.breastScreeningUnitId)
56-
57-
return {
58-
clinic,
59-
event,
60-
participant,
61-
unit
62-
}
63-
}
64-
6538
module.exports = router => {
6639

6740
// Set clinics to active in nav for all urls starting with /clinics
@@ -114,24 +87,7 @@ module.exports = router => {
11487

11588
});
11689

117-
// Event within clinic context
118-
router.get('/clinics/:clinicId/events/:eventId', (req, res) => {
119-
const eventData = getEventData(req.session.data, req.params.clinicId, req.params.eventId)
120-
121-
if (!eventData) {
122-
res.redirect('/clinics/' + req.params.clinicId)
123-
return
124-
}
12590

126-
res.render('clinics/event', {
127-
clinic: eventData.clinic,
128-
event: eventData.event,
129-
participant: eventData.participant,
130-
unit: eventData.unit,
131-
clinicId: req.params.clinicId,
132-
eventId: req.params.eventId
133-
})
134-
})
13591

13692
const QUESTIONNAIRE_SECTIONS = ['health-status', 'medical-history', 'current-symptoms'];
13793

app/routes/events.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// app/routes/events.js
2+
3+
4+
/**
5+
* Get single event and its related data
6+
*/
7+
function getEventData(data, clinicId, eventId) {
8+
const clinic = data.clinics.find(c => c.id === clinicId)
9+
10+
if (!clinic) {
11+
return null
12+
}
13+
14+
const event = data.events.find(e => e.id === eventId && e.clinicId === clinicId)
15+
16+
if (!event) {
17+
return null
18+
}
19+
20+
const participant = data.participants.find(p => p.id === event.participantId)
21+
const unit = data.breastScreeningUnits.find(u => u.id === clinic.breastScreeningUnitId)
22+
23+
return {
24+
clinic,
25+
event,
26+
participant,
27+
unit
28+
}
29+
}
30+
31+
module.exports = router => {
32+
33+
// Event within clinic context
34+
router.get('/clinics/:clinicId/events/:eventId', (req, res) => {
35+
const eventData = getEventData(req.session.data, req.params.clinicId, req.params.eventId)
36+
37+
if (!eventData) {
38+
res.redirect('/clinics/' + req.params.clinicId)
39+
return
40+
}
41+
42+
res.render('events/show', {
43+
clinic: eventData.clinic,
44+
event: eventData.event,
45+
participant: eventData.participant,
46+
unit: eventData.unit,
47+
clinicId: req.params.clinicId,
48+
eventId: req.params.eventId
49+
})
50+
})
51+
52+
};

app/routes/participants.js

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// app/routes/participants.js
22

3-
const { sortBySurname } = require('../lib/utils/participants');
3+
const { sortBySurname, getParticipantClinicHistory } = require('../lib/utils/participants');
44
const { findById } = require('../lib/utils/arrays');
55

66

@@ -59,35 +59,24 @@ module.exports = router => {
5959
});
6060

6161

62+
// In the show route:
6263
router.get('/participants/:participantId', (req, res) => {
63-
const data = req.session.data;
64+
const data = req.session.data
6465
const participantId = req.params.participantId
65-
const participant = findById(data.participants, participantId);
66+
const participant = findById(data.participants, participantId)
6667

6768
if (!participant) {
68-
res.redirect('/participants');
69-
return;
69+
res.redirect('/participants')
70+
return
7071
}
7172

72-
// Find all events for this participant
73-
const participantEvents = data.events.filter(e =>
74-
e.participantId === participant.id
75-
);
76-
77-
// Get clinic details for each event
78-
const participantClinics = participantEvents.map(event => {
79-
const clinic = findById(data.clinics, event.clinicId);
80-
return {
81-
clinic,
82-
event
83-
};
84-
});
73+
const clinicHistory = getParticipantClinicHistory(data, participant.id)
8574

8675
res.render('participants/show', {
8776
participant,
8877
participantId,
89-
clinicHistory: participantClinics
90-
});
91-
});
78+
clinicHistory
79+
})
80+
})
9281

9382
};
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
{% set today = "" | today %}
2+
{% set upcomingAppointments = [] %}
3+
{% set pastAppointments = [] %}
4+
5+
{% for record in clinicHistory %}
6+
{% if record.event.timing.startTime | isPast %}
7+
{% set pastAppointments = (pastAppointments.push(record), pastAppointments) %}
8+
{% else %}
9+
{% set upcomingAppointments = (upcomingAppointments.push(record), upcomingAppointments) %}
10+
{% endif %}
11+
{% endfor %}
12+
{% if not hideUpcoming %}
13+
{% if upcomingAppointments.length > 0 %}
14+
<h3>Upcoming appointments</h3>
15+
<table class="nhsuk-table">
16+
<thead>
17+
<tr>
18+
<th>Date</th>
19+
<th>Type</th>
20+
<th>Location</th>
21+
<th>Status</th>
22+
<th>Details</th>
23+
</tr>
24+
</thead>
25+
<tbody>
26+
{% for record in upcomingAppointments | sort(false, false, 'event.slot.dateTime') %}
27+
{% set event = record.event %}
28+
{% set clinic = record.clinic %}
29+
{% set unit = record.unit %}
30+
<tr>
31+
<td>{{ event.timing.startTime | formatDate }}</td>
32+
<td>{{ clinic.clinicType | formatWords | sentenceCase }}</td>
33+
<td>{{ unit.name }}</td>
34+
<td>
35+
{{ tag({
36+
text: event.status | formatWords | sentenceCase,
37+
classes: "nhsuk-tag--" + event.status | getStatusTagColour
38+
}) }}
39+
</td>
40+
<td>
41+
<a href="/clinics/{{ clinic.id }}/events/{{ event.id }}">
42+
View details
43+
</a>
44+
</td>
45+
</tr>
46+
{% endfor %}
47+
</tbody>
48+
</table>
49+
{% endif %}
50+
{% endif %}
51+
52+
53+
{% if pastAppointments.length > 0 %}
54+
<h3>Screening history</h3>
55+
<table class="nhsuk-table">
56+
<thead>
57+
<tr>
58+
<th>Date</th>
59+
<th>Type</th>
60+
<th>Location</th>
61+
<th>Status</th>
62+
{# <th>Outcome</th> #}
63+
<th>Details</th>
64+
</tr>
65+
</thead>
66+
<tbody>
67+
{% for record in pastAppointments | sort(true, false, 'event.timing.startTime') %}
68+
{% set event = record.event %}
69+
{% set clinic = record.clinic %}
70+
{% set unit = record.unit %}
71+
<tr>
72+
<td>{{ event.timing.startTime | formatDate }}</td>
73+
<td>{{ clinic.clinicType | formatWords | sentenceCase }}</td>
74+
<td>{{ unit.name }}</td>
75+
<td>
76+
{{ tag({
77+
text: event.status | formatWords | sentenceCase,
78+
classes: "nhsuk-tag--" + event.status | getStatusTagColour
79+
}) }}
80+
</td>
81+
{# <td>
82+
{% if event.outcome %}
83+
{{ event.outcome.status | formatWords | sentenceCase }}
84+
{% endif %}
85+
</td> #}
86+
<td>
87+
<a href="/clinics/{{ clinic.id }}/events/{{ event.id }}">
88+
View details
89+
</a>
90+
</td>
91+
</tr>
92+
{% endfor %}
93+
</tbody>
94+
</table>
95+
{% endif %}
96+
97+
{% if upcomingAppointments.length === 0 and pastAppointments.length === 0 %}
98+
<p>No screening history found</p>
99+
{% endif %}

0 commit comments

Comments
 (0)