-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserviceRegistration.js
More file actions
121 lines (112 loc) · 6.04 KB
/
serviceRegistration.js
File metadata and controls
121 lines (112 loc) · 6.04 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
117
118
119
120
121
const {getPredefinedProperty} = require("./helperFunctions");
const {GDBInternalTypeModel} = require("../../models/internalType");
const {SPARQL} = require("graphdb-utils");
const {GDBServiceOccurrenceModel} = require("../../models/service/serviceOccurrence");
const {getIndividualsInClass} = require("../dynamicForm");
const {PredefinedCharacteristics, PredefinedInternalTypes} = require("../characteristics");
const FORMTYPE = 'serviceRegistration'
const serviceRegistrationInternalTypeCreateTreater = async (internalType, instanceData, value) => {
const property = getPredefinedProperty(FORMTYPE, internalType);
if (property === 'client' || property === 'referral' || property === 'appointment' ||
property === 'serviceOccurrence' || property === 'needOccurrence') {
instanceData[property] = value;
}
};
const serviceRegistrationInternalTypeFetchTreater = async (data) => {
const result = {};
const schema = data.schema;
for (const property in data) {
if (property === 'client' || property === 'referral' || property === 'appointment' ||
property === 'serviceOccurrence' || property === 'needOccurrence') {
const internalType = await GDBInternalTypeModel.findOne({
predefinedProperty: schema[property].internalKey,
formType: FORMTYPE
});
result['internalType_' + internalType._id] = SPARQL.ensureFullURI(data[property]);
}
}
return result;
};
const serviceRegistrationInternalTypeUpdateTreater = async (internalType, value, result) => {
await serviceRegistrationInternalTypeCreateTreater(internalType, result, value);
}
const updateOccurrenceOccupancyOnServiceRegistrationCreate = async function (characteristics, questions, fields) {
const statuses = await getIndividualsInClass(':RegistrationStatus');
const status = statuses[fields[`characteristic_${PredefinedCharacteristics['Registration Status']._id}`]];
const occUri = fields['internalType_' + PredefinedInternalTypes['serviceOccurrenceForServiceRegistration']._id];
if (!occUri) return;
const occ = await GDBServiceOccurrenceModel.findOne({_uri: occUri}, {populates: ['characteristicOccurrences']});
if (status === 'Registered') {
if (!occ.capacity || (occ.occupancy < occ.capacity)) {
occ.occupancy += 1;
const occupancyC = PredefinedCharacteristics['Occupancy'];
const occupancyCO = occ.characteristicOccurrences.find(co => co.occurrenceOf === occupancyC._uri);
occupancyCO.dataNumberValue += 1;
occ.save();
} else {
throw new Error('The requested service occurrence is now at capacity. Please refresh the form to review your current options.');
}
} else if (status === 'Waitlisted') {
// TODO: Push new registration to waitlist (probably after create instead of before)
}
}
const checkServiceOccurrenceUnchanged = async function (instanceData, internalTypes, fields, oldGeneric) {
const newOccUri = fields['internalType_' + PredefinedInternalTypes['serviceOccurrenceForServiceRegistration']._id];
const oldOccUri = oldGeneric.serviceOccurrence;
if (newOccUri !== oldOccUri)
throw new Error('A service registration\'s service occurrence cannot change.');
}
const updateOccurrenceOccupancyOnServiceRegistrationUpdate = async function (instanceData, internalTypes, fields, oldGeneric) {
// checkServiceOccurrenceUnchanged must be called before this
const statuses = await getIndividualsInClass(':RegistrationStatus');
const oldStatus = statuses[oldGeneric.status];
const newStatus = statuses[fields[`characteristic_${PredefinedCharacteristics['Registration Status']._id}`]];
const occUri = fields['internalType_' + PredefinedInternalTypes['serviceOccurrenceForServiceRegistration']._id];
if (!occUri) return;
const occ = await GDBServiceOccurrenceModel.findOne({_uri: occUri}, {populates: ['characteristicOccurrences']});
const occupancyC = PredefinedCharacteristics['Occupancy'];
const occupancyCO = occ.characteristicOccurrences.find(co => co.occurrenceOf === occupancyC._uri);
if (oldStatus === 'Not Registered' && newStatus === 'Registered') {
if (!occ.capacity || (occ.occupancy < occ.capacity)) {
occ.occupancy += 1;
occupancyCO.dataNumberValue += 1;
occ.save();
} else {
throw new Error('The requested service occurrence is now at capacity. Please refresh the form to review your current options.');
}
} else if (oldStatus === 'Registered' && newStatus === 'Not Registered') {
occ.occupancy -= 1;
occupancyCO.dataNumberValue -= 1;
occ.save();
// TODO: Pop one registration from waitlist, if any, and change its status to registered
} else if (oldStatus === 'Waitlisted' && newStatus === 'Not Registered') { // TODO: Remove this registration from waitlist
} else if (oldStatus === 'Not Registered' && newStatus === 'Waitlisted') { // TODO: Push this registration to waitlist
} else {
throw new Error('Invalid registration status chosen.');
}
}
const updateOccurrenceOccupancyOnServiceRegistrationDelete = async function (oldGeneric) {
const statuses = await getIndividualsInClass(':RegistrationStatus');
const status = statuses[oldGeneric.status];
const occUri = oldGeneric.serviceOccurrence;
if (!occUri) return;
const occ = await GDBServiceOccurrenceModel.findOne({_uri: occUri}, {populates: ['characteristicOccurrences']});
if (status === 'Registered') {
occ.occupancy -= 1;
const occupancyC = PredefinedCharacteristics['Occupancy'];
const occupancyCO = occ.characteristicOccurrences.find(co => co.occurrenceOf === occupancyC._uri);
occupancyCO.dataNumberValue -= 1;
occ.save();
// TODO: Pop one registration from waitlist, if any, and change its status to registered
} else if (status === 'Waitlisted') { // TODO: Remove this registration from waitlist
}
}
module.exports = {
serviceRegistrationInternalTypeCreateTreater,
serviceRegistrationInternalTypeFetchTreater,
serviceRegistrationInternalTypeUpdateTreater,
checkServiceOccurrenceUnchanged,
updateOccurrenceOccupancyOnServiceRegistrationCreate,
updateOccurrenceOccupancyOnServiceRegistrationUpdate,
updateOccurrenceOccupancyOnServiceRegistrationDelete,
}