-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsession.js
More file actions
116 lines (101 loc) · 3.18 KB
/
session.js
File metadata and controls
116 lines (101 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { isAfter, isBefore } from 'date-fns'
import _ from 'lodash'
import programmesData from '../datasets/programmes.js'
import schoolsData from '../datasets/schools.js'
import {
ConsentWindow,
SessionPresetName,
SessionStatus,
SessionType
} from '../enums.js'
import { today } from './date.js'
/**
* Get consent window (is it open, opening or closed)
*
* @param {import('../models.js').Session} session - Session
* @returns {string} Consent window key and value
*/
export const getConsentWindow = (session) => {
const nowAt = today()
switch (true) {
// Opening (open date is after today)
case isAfter(session.openAt, nowAt):
return ConsentWindow.Opening
// Open (open date is before today, and close date after today)
case isBefore(session.openAt, nowAt) && isAfter(session.closeAt, nowAt):
return ConsentWindow.Open
// Closed (close date is before today)
case isBefore(session.closeAt, nowAt):
return ConsentWindow.Closed
default:
return ConsentWindow.None
}
}
/**
* Get consent URL
*
* @param {import('../models.js').Session[]} sessions - Sessions
* @param {string} [presetName] - Session preset name
* @param {boolean} [isSchool] - Get school session
* @returns {object|undefined} Consent window key and value
*/
export const getSessionConsentUrl = (
sessions,
presetName = SessionPresetName.Flu,
isSchool = true
) => {
const sessionType = isSchool ? SessionType.School : SessionType.Clinic
const session = Object.values(sessions)
.filter((session) => session?.presetNames.includes(presetName))
.filter((session) => session.type === sessionType)
.find((session) => session.status !== SessionStatus.Unplanned)
if (session) {
return session.consentUrl
}
}
/**
* Filter array where key has a value
*
* @param {import('../models.js').Session} session - Session
* @param {Array} filters - Filters
* @returns {number} Number
*/
export const getSessionActivityCount = (session, filters) => {
let patientSessions = session.patientSessions
for (const filter of filters) {
for (const [key, value] of Object.entries(filter)) {
if (value) {
patientSessions = patientSessions.filter(
(patientSession) => _.get(patientSession, key) === value
)
}
}
}
if (patientSessions) {
const uniquePatientSessions = _.uniqBy(patientSessions, 'patient.nhsn')
return uniquePatientSessions.length
}
return 0
}
/**
* Get year groups based on intersection of school phase and programme
*
* @param {string} school_id - School ID
* @param {Array<import('../enums.js').SessionPreset>} sessionPresets - Session presets
* @returns {Array<number>} Year groups
*/
export const getSessionYearGroups = (school_id, sessionPresets) => {
const programmeYearGroups = new Set()
for (const preset of sessionPresets) {
for (const programmeType of preset.programmeTypes) {
const programme = programmesData[programmeType]
for (const yearGroup of programme.yearGroups) {
programmeYearGroups.add(yearGroup)
}
}
}
const school = schoolsData[school_id]
return school.yearGroups.filter((yearGroup) =>
[...programmeYearGroups].includes(yearGroup)
)
}