-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
1024 lines (881 loc) · 40.2 KB
/
index.js
File metadata and controls
1024 lines (881 loc) · 40.2 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const {
GDBClientModel,
GDBPhoneNumberModel,
GDBAddressModel,
GDBQOModel
} = require("../../models");
const {SPARQL, GraphDB} = require('graphdb-utils');
const {FieldTypes} = require("../characteristics");
const {MDBDynamicFormModel} = require("../../models/dynamicForm");
const {GDBQuestionModel} = require("../../models/ClientFunctionalities/question");
const {GDBNoteModel} = require("../../models/ClientFunctionalities/note");
const {GDBCOModel} = require("../../models/ClientFunctionalities/characteristicOccurrence");
const {MDBUsageModel} = require("../../models/usage");
const {parsePhoneNumber, combinePhoneNumber} = require("../../helpers/phoneNumber");
const {GDBServiceModel} = require("../../models/service/service");
const {GDBProgramModel} = require("../../models/program/program");
const {Server400Error} = require("../../utils");
const {GDBOrganizationModel} = require("../../models/organization");
const {GDBVolunteerModel} = require("../../models/volunteer");
const {GDBAppointmentModel} = require("../../models/appointment");
const {GDBPersonModel} = require("../../models/person");
const {GDBServiceOccurrenceModel} = require("../../models/service/serviceOccurrence");
const {GDBProgramOccurrenceModel} = require("../../models/program/programOccurrence");
const {GDBServiceWaitlistModel} = require("../../models/service/serviceWaitlist");
const{GDBWaitlistEntryModel} = require("../../models/service/waitlistEntry");
const {GDBInternalTypeModel} = require("../../models/internalType");
const {noQuestion, checkCapacity, setOccupancy, unsetOccupancy} = require('./checkers')
const {
serviceOccurrenceInternalTypeCreateTreater, serviceOccurrenceInternalTypeFetchTreater,
serviceOccurrenceInternalTypeUpdateTreater
} = require("./serviceOccurrenceInternalTypeTreater");
const {
programOccurrenceInternalTypeCreateTreater, programOccurrenceInternalTypeFetchTreater,
programOccurrenceInternalTypeUpdateTreater
} = require("./programOccurrenceInternalTypeTreater");
const {
fetchCharacteristicQuestionsInternalTypesBasedOnForms,
implementCharacteristicOccurrence,
getPredefinedProperty
} = require("./helperFunctions");
const {GDBReferralModel} = require("../../models/referral");
const {
serviceInternalTypeCreateTreater,
serviceInternalTypeFetchTreater,
serviceInternalTypeUpdateTreater,
afterCreateService,
afterUpdateService,
afterDeleteService
} = require("./serviceInternalTypeTreater");
const {
programInternalTypeCreateTreater,
programInternalTypeFetchTreater,
programInternalTypeUpdateTreater,
afterCreateProgram,
afterUpdateProgram,
afterDeleteProgram
} = require("./programInternalTypeTreater");
const {
referralInternalTypeCreateTreater,
referralInternalTypeFetchTreater,
referralInternalTypeUpdateTreater
} = require("./referralInternalTypeTreater");
const {GDBServiceRegistrationModel} = require("../../models/serviceRegistration");
const {GDBProgramRegistrationModel} = require("../../models/programRegistration");
const {
serviceRegistrationInternalTypeCreateTreater, serviceRegistrationInternalTypeFetchTreater,
serviceRegistrationInternalTypeUpdateTreater,
updateOccurrenceOccupancyOnServiceRegistrationCreate, updateOccurrenceOccupancyOnServiceRegistrationUpdate,
updateOccurrenceOccupancyOnServiceRegistrationDelete, checkServiceOccurrenceUnchanged,
} = require("./serviceRegistration");
const {
programRegistrationInternalTypeCreateTreater, programRegistrationInternalTypeFetchTreater,
programRegistrationInternalTypeUpdateTreater,
updateOccurrenceOccupancyOnProgramRegistrationCreate, updateOccurrenceOccupancyOnProgramRegistrationUpdate,
updateOccurrenceOccupancyOnProgramRegistrationDelete, checkProgramOccurrenceUnchanged,
} = require("./programRegistration");
const {
appointmentInternalTypeCreateTreater,
appointmentInternalTypeFetchTreater,
appointmentInternalTypeUpdateTreater
} = require("./appointment");
const {GDBServiceProvisionModel} = require("../../models/serviceProvision");
const {GDBProgramProvisionModel} = require("../../models/programProvision");
const {GDBNeedSatisfierOccurrenceModel} = require("../../models/needSatisfierOccurrence");
const {GDBNeedOccurrenceModel} = require("../../models/need/needOccurrence");
const {GDBOutcomeOccurrenceModel} = require("../../models/outcome/outcomeOccurrence");
const {GDBClientAssessmentModel} = require("../../models/clientAssessment");
const {
serviceProvisionInternalTypeCreateTreater, serviceProvisionInternalTypeFetchTreater,
serviceProvisionInternalTypeUpdateTreater
} = require("./serviceProvision");
const {
programProvisionInternalTypeCreateTreater, programProvisionInternalTypeFetchTreater,
programProvisionInternalTypeUpdateTreater
} = require("./programProvision");
const {
clientInternalTypeUpdateTreater, clientInternalTypeCreateTreater, clientInternalTypeFetchTreater
} = require("./clientInternalTypeTreater");
const {
needOccurrenceInternalTypeUpdateTreater,
needOccurrenceInternalTypeCreateTreater,
needOccurrenceInternalTypeFetchTreater,
beforeDeleteNeedOccurrence
} = require("./needOccurrenceInternalTypeTreater");
const {
outcomeOccurrenceInternalTypeUpdateTreater,
outcomeOccurrenceInternalTypeCreateTreater,
outcomeOccurrenceInternalTypeFetchTreater,
beforeDeleteOutcomeOccurrence
} = require("./outcomeOccurrenceInternalTypeTreater");
const {
clientAssessmentInternalTypeUpdateTreater,
clientAssessmentInternalTypeCreateTreater,
clientAssessmentInternalTypeFetchTreater,
beforeCreateClientAssessment,
beforeUpdateClientAssessment
} = require("./clientAssessmentInternalTypeTreater");
const {
personInternalTypeUpdateTreater,
personInternalTypeCreateTreater,
personInternalTypeFetchTreater
} = require("./person");
const {
volunteerInternalTypeCreateTreater,
volunteerInternalTypeFetchTreater,
volunteerInternalTypeUpdateTreater,
afterCreateVolunteer,
afterUpdateVolunteer,
afterDeleteVolunteer
} = require("./volunteerInternalTypeTreater");
const {GDBEligibilityModel} = require("../../models/eligibility");
const { serviceWaitlistInternalTypeCreateTreater, serviceWaitlistInternalTypeFetchTreater, serviceWaitlistInternalTypeUpdateTreater } = require("./serviceWaitlist");
const { waitlistEntryInternalTypeCreateTreater, waitlistEntryInternalTypeFetchTreater, waitlistEntryInternalTypeUpdateTreater } = require("./waitlistEntry");
const genericType2Model = {
'client': GDBClientModel,
'organization': GDBOrganizationModel,
'volunteer': GDBVolunteerModel,
'service': GDBServiceModel,
'program': GDBProgramModel,
'appointment': GDBAppointmentModel,
'serviceOccurrence': GDBServiceOccurrenceModel,
'serviceWaitlist': GDBServiceWaitlistModel,
'waitlistEntry' : GDBWaitlistEntryModel,
'programOccurrence': GDBProgramOccurrenceModel,
'referral': GDBReferralModel,
'serviceRegistration': GDBServiceRegistrationModel,
'serviceProvision': GDBServiceProvisionModel,
'programRegistration': GDBProgramRegistrationModel,
'programProvision': GDBProgramProvisionModel,
'needSatisfierOccurrence': GDBNeedSatisfierOccurrenceModel,
'needOccurrence': GDBNeedOccurrenceModel,
'outcomeOccurrence': GDBOutcomeOccurrenceModel,
'clientAssessment': GDBClientAssessmentModel,
'person': GDBPersonModel,
};
const genericType2Populates = {
'serviceProvision': ['address', 'needOccurrence', 'serviceOccurrence', 'needSatisfierOccurrence'],
'programProvision' : ['address', 'needOccurrence', 'programOccurrence', 'needSatisfierOccurrence'],
'serviceRegistration': ['address', 'needOccurrence', 'serviceOccurrence'],
'programRegistration' : ['address', 'needOccurrence', 'programOccurrence'],
'needOccurrence': ['address', 'occurrenceOf', 'client'],
'outcomeOccurrence': ['address', 'occurrenceOf', 'client'],
'needSatisfierOccurrence': ['address'],
'clientAssessment': ['address', 'client'],
'referral': ['address', 'client'],
'service': ['serviceProvider.organization.address', 'serviceProvider.volunteer.address'],
'program': ['serviceProvider.organization.address', 'serviceProvider.volunteer.address', 'serviceProvider.organization', 'serviceProvider.volunteer', 'manager'],
'serviceOccurrence': ['address', 'occurrenceOf'],
'serviceWaitlist': ['waitlist', 'serviceOccurrence'],
'waitlistEntry': ['serviceRegistration', 'priority', 'date'],
'programOccurrence': ['address', 'occurrenceOf'],
'client': ['address', 'needs'],
'appointment': ['address', 'referral'],
'person': ['address'],
'volunteer': ['partnerOrganizations', 'organization', 'address'],
};
const genericType2BeforeCreateChecker = {
'service': [noQuestion],
'serviceOccurrence': [noQuestion, checkCapacity, setOccupancy],
'serviceRegistration': [updateOccurrenceOccupancyOnServiceRegistrationCreate],
'program': [noQuestion],
'programOccurrence': [noQuestion, checkCapacity, setOccupancy],
'programRegistration': [updateOccurrenceOccupancyOnProgramRegistrationCreate],
'serviceWaitlist': [noQuestion],
'waitlistEntry': [noQuestion],
};
const genericType2BeforeUpdateChecker = {
'service': [noQuestion],
'serviceOccurrence': [noQuestion, checkCapacity, unsetOccupancy],
'serviceRegistration': [checkServiceOccurrenceUnchanged, updateOccurrenceOccupancyOnServiceRegistrationUpdate],
'program': [noQuestion],
'programOccurrence': [noQuestion, checkCapacity, unsetOccupancy],
'programRegistration': [checkProgramOccurrenceUnchanged, updateOccurrenceOccupancyOnProgramRegistrationUpdate],
'serviceWaitlist': [noQuestion],
'waitlistEntry' : [noQuestion],
};
const genericType2BeforeDeleteChecker = {
'serviceRegistration': [updateOccurrenceOccupancyOnServiceRegistrationDelete],
'programRegistration': [updateOccurrenceOccupancyOnProgramRegistrationDelete],
}
const genericType2BeforeCreateTreater = {
'clientAssessment': beforeCreateClientAssessment
}
const genericType2BeforeUpdateTreater = {
'clientAssessment': beforeUpdateClientAssessment
}
const genericType2BeforeDeleteTreater = {
'needOccurrence': beforeDeleteNeedOccurrence,
'outcomeOccurrence': beforeDeleteOutcomeOccurrence
}
// this dict will be shared by all generic types with internal types as their properties
// os going to be used by generic services/ serviceOccurrence/ appointment / referral ...
const genericType2InternalTypeCreateTreater = {
'serviceOccurrence': serviceOccurrenceInternalTypeCreateTreater,
'service': serviceInternalTypeCreateTreater,
'programOccurrence': programOccurrenceInternalTypeCreateTreater,
'program': programInternalTypeCreateTreater,
'referral': referralInternalTypeCreateTreater,
'serviceRegistration': serviceRegistrationInternalTypeCreateTreater,
'programRegistration': programRegistrationInternalTypeCreateTreater,
'appointment': appointmentInternalTypeCreateTreater,
'serviceProvision': serviceProvisionInternalTypeCreateTreater,
'programProvision': programProvisionInternalTypeCreateTreater,
'client': clientInternalTypeCreateTreater,
'needOccurrence': needOccurrenceInternalTypeCreateTreater,
'outcomeOccurrence': outcomeOccurrenceInternalTypeCreateTreater,
'clientAssessment': clientAssessmentInternalTypeCreateTreater,
'person': personInternalTypeCreateTreater,
'volunteer': volunteerInternalTypeCreateTreater,
'serviceWaitlist': serviceWaitlistInternalTypeCreateTreater,
'waitlistEntry': waitlistEntryInternalTypeCreateTreater
};
const genericType2InternalTypeFetchTreater = {
'serviceOccurrence': serviceOccurrenceInternalTypeFetchTreater,
'service': serviceInternalTypeFetchTreater,
'programOccurrence': programOccurrenceInternalTypeFetchTreater,
'program': programInternalTypeFetchTreater,
'referral': referralInternalTypeFetchTreater,
'serviceRegistration': serviceRegistrationInternalTypeFetchTreater,
'programRegistration': programRegistrationInternalTypeFetchTreater,
'appointment': appointmentInternalTypeFetchTreater,
'serviceProvision': serviceProvisionInternalTypeFetchTreater,
'programProvision': programProvisionInternalTypeFetchTreater,
'client': clientInternalTypeFetchTreater,
'needOccurrence': needOccurrenceInternalTypeFetchTreater,
'outcomeOccurrence': outcomeOccurrenceInternalTypeFetchTreater,
'clientAssessment': clientAssessmentInternalTypeFetchTreater,
'person': personInternalTypeFetchTreater,
'volunteer': volunteerInternalTypeFetchTreater,
'serviceWaitlist': serviceWaitlistInternalTypeFetchTreater,
'waitlistEntry': waitlistEntryInternalTypeFetchTreater
};
const genericType2InternalTypeUpdateTreater = {
'serviceOccurrence': serviceOccurrenceInternalTypeUpdateTreater,
'service': serviceInternalTypeUpdateTreater,
'programOccurrence': programOccurrenceInternalTypeUpdateTreater,
'program' : programInternalTypeUpdateTreater,
'referral': referralInternalTypeUpdateTreater,
'serviceRegistration': serviceRegistrationInternalTypeUpdateTreater,
'programRegistration': programRegistrationInternalTypeUpdateTreater,
'appointment': appointmentInternalTypeUpdateTreater,
'serviceProvision': serviceProvisionInternalTypeUpdateTreater,
'programProvision': programProvisionInternalTypeUpdateTreater,
'client': clientInternalTypeUpdateTreater,
'needOccurrence': needOccurrenceInternalTypeUpdateTreater,
'outcomeOccurrence': outcomeOccurrenceInternalTypeUpdateTreater,
'clientAssessment': clientAssessmentInternalTypeUpdateTreater,
'person': personInternalTypeUpdateTreater,
'volunteer': volunteerInternalTypeUpdateTreater,
'serviceWaitlist': serviceWaitlistInternalTypeUpdateTreater,
'waitlistEntry' : waitlistEntryInternalTypeUpdateTreater
};
const genericType2AfterCreateTreater = {
'program': afterCreateProgram,
'service': afterCreateService,
'volunteer': afterCreateVolunteer
}
const genericType2AfterUpdateTreater = {
'program': afterUpdateProgram,
'service': afterUpdateService,
'volunteer': afterUpdateVolunteer
}
const genericType2AfterDeleteTreater = {
'program': afterDeleteProgram,
'service': afterDeleteService,
'volunteer': afterDeleteVolunteer
}
const {graphdb} = require('../../config');
const specialField2Model = {
'address': GDBAddressModel,
'phoneNumber': GDBPhoneNumberModel
};
const {extractAllIndexes} = require('../../helpers/stringProcess');
async function fetchSingleGenericHelper(genericType, id) {
if (!genericType2Model[genericType]) {
throw new Server400Error('Invalid generic type.');
// return res.status(400).json({success: false, message: 'Invalid generic type.'});
}
const data = await genericType2Model[genericType].findOne({_id: id},
{populates: ['characteristicOccurrences.occurrenceOf.implementation', 'questionOccurrences']});
if (!data) {
throw new Server400Error('Generic not found.');
}
const inter = await GDBInternalTypeModel.findById(1);
let result = {}
if (genericType2InternalTypeFetchTreater[genericType])
result = await genericType2InternalTypeFetchTreater[genericType](data) || {};
// Copy the values to occurrence.value regardless of its type.
if (data.characteristicOccurrences)
for (const co of data.characteristicOccurrences) {
// Assign full URI
if (co.objectValue) {
// when object is a phoneNumber
await co.populate('occurrenceOf.implementation');
if (co.occurrenceOf.implementation.fieldType === FieldTypes.PhoneNumberField._uri) {
const id = co.objectValue.split('_')[1];
co.objectValue = combinePhoneNumber(await GDBPhoneNumberModel.findOne({_id: id}));
} else if (co.occurrenceOf.implementation.fieldType === FieldTypes.AddressField._uri) {
const id = co.objectValue.split('_')[1];
const address = (await GDBAddressModel.findOne({_id: id})).toJSON();
// Get full URI
if (address.streetType) address.streetType = SPARQL.ensureFullURI(address.streetType);
if (address.streetDirection) address.streetDirection = SPARQL.ensureFullURI(address.streetDirection);
if (address.state) address.state = SPARQL.ensureFullURI(address.state);
co.objectValue = address;
} else if (co.occurrenceOf.implementation.fieldType === FieldTypes.EligibilityField._uri) {
const id = co.objectValue.split('_')[1];
co.objectValue = (await GDBEligibilityModel.findOne({_id: id})).toJSON();
if (co.objectValue.formulaJSON) co.objectValue.formulaJSON = JSON.parse(co.objectValue.formulaJSON);
}
} else if (co.multipleObjectValues) {
co.multipleObjectValues = co.multipleObjectValues.map(value => SPARQL.ensureFullURI(value));
}
const coName = co.occurrenceOf.individualName
? co.occurrenceOf.individualName.replace(':', '')
: SPARQL.ensurePrefixedURI(co.occurrenceOf).replace(':', '');
result[coName] =
co.dataStringValue ?? co.dataNumberValue ?? co.dataBooleanValue ?? co.dataDateValue
?? co.objectValue ?? co.multipleObjectValues;
}
if (data.questionOccurrences)
for (const qo of data.questionOccurrences) {
result[SPARQL.ensurePrefixedURI(qo.occurrenceOf).replace(':', '')] = qo.stringValue;
}
return result;
}
async function fetchSingleGeneric(req, res, next) {
try {
const {genericType, id} = req.params;
const result = await fetchSingleGenericHelper(genericType, id);
if (result)
return res.status(200).json({data: result, success: true});
} catch (e) {
next(e);
}
}
async function addIdToUsage(option, genericType, id) {
let usage = await MDBUsageModel.findOne({option: option, genericType: genericType});
if (!usage)
usage = new MDBUsageModel({option: option, genericType: genericType, optionKeys: []});
// check whether the characteristic linked to this option
if (!usage.optionKeys.includes(id))
usage.optionKeys.push(id);
await usage.save();
}
async function deleteIdFromUsageAfterChecking(option, genericType, id) {
// check if this option's occurrence is linked with another instance of the genericType
const key = option + 'Occurrences';
const value = ':' + option + "_" + id;
const x = await genericType2Model[genericType].find({[key]: {occurrenceOf: value}});
if (x.length === 0) {
// then we have to remove the id from the usage
const usage = await MDBUsageModel.findOne({option: option, genericType: genericType});
if (usage) {
usage.optionKeys = usage.optionKeys.filter((key) => (key !== id));
await usage.save();
}
}
}
const createSingleGenericHelper = async (data, genericType) => {
// check the data package from frontend
// check if a formId was sent
if (!data.formId) {
throw new Server400Error('No form id is given');
// return res.status(400).json({success: false, message: 'No form id is given'})
}
const form = await MDBDynamicFormModel.findById(data.formId);
// check if the genericType is in genericType2Model
if (!genericType2Model[genericType])
throw new Server400Error('Invalid genericType');
// return res.status(400).json({success: false, message: 'Invalid genericType'})
// check if the form type is consist with request type(client, serviceProvider, ...)
if (form.formType !== genericType)
throw new Server400Error(`The form is not for ${genericType}`);
// return res.status(400).json({success: false, message: `The form is not for ${genericType}`})
// check if the form has a structure
if (!form.formStructure) {
throw new Server400Error('The form structure is not defined');
// return res.status(400).json({success: false, message: 'The form structure is not defined'})
}
// TODO: verify if questions and characteristics are in the form
// check if the fields are provided
if (!data.fields) {
throw new Server400Error('No fields provided');
// return res.status(400).json({success: false, message: 'No fields provided'})
}
const questions = {};
const characteristics = {};
const internalTypes = {};
// extract questions and characteristics based on fields from the database
await fetchCharacteristicQuestionsInternalTypesBasedOnForms(characteristics, questions, internalTypes, data.fields);
if (genericType2BeforeCreateChecker[genericType])
for (const checker of genericType2BeforeCreateChecker[genericType])
await checker(characteristics, questions, data.fields);
const instanceData = {characteristicOccurrences: [], questionOccurrences: []};
// iterating over all fields and create occurrences and store them into instanceData
for (const [key, value] of Object.entries(data.fields)) {
if (value == null)
continue;
const [type, id] = key.split('_');
if (type === 'characteristic') {
const characteristic = characteristics[id];
const occurrence = {occurrenceOf: characteristic};
await implementCharacteristicOccurrence(characteristic, occurrence, value);
// find the usage(given option and geneticType), create a new one if no such
await addIdToUsage('characteristic', genericType, id);
if (characteristic.isPredefined) {
const property = getPredefinedProperty(genericType, characteristic);
if (property) {
instanceData[property] = occurrence.dataStringValue ?? occurrence.dataNumberValue ?? occurrence.dataBooleanValue ??
occurrence.dataDateValue ?? occurrence.objectValue ?? occurrence.multipleObjectValues;
}
}
instanceData.characteristicOccurrences.push(occurrence);
} else if (type === 'question') {
await addIdToUsage('question', genericType, id);
const occurrence = {occurrenceOf: questions[id], stringValue: value};
instanceData.questionOccurrences.push(occurrence);
}
}
if (genericType2BeforeCreateTreater[genericType])
await genericType2BeforeCreateTreater[genericType](instanceData, internalTypes, data.fields);
for (const [key, value] of Object.entries(data.fields)) {
if (value == null)
continue;
const [type, id] = key.split('_');
if (type === 'internalType') {
await addIdToUsage('internalType', genericType, id);
const internalType = internalTypes[id];
await genericType2InternalTypeCreateTreater[genericType](internalType, instanceData, value);
}
}
if (instanceData.characteristicOccurrences.length === 0)
delete instanceData.characteristicOccurrences;
if (instanceData.questionOccurrences.length === 0)
delete instanceData.questionOccurrences
return instanceData;
};
const createSingleGeneric = async (req, res, next) => {
const data = req.body;
// genericType will be 'client', 'serviceProvider',...
const {genericType} = req.params;
try {
const instanceData = await createSingleGenericHelper(data, genericType);
// add createDate to person
if (genericType === 'person'){
instanceData['createDate'] = new Date();
}
if (instanceData) {
// the instance data is stored into graphdb
const newGeneric = genericType2Model[genericType](instanceData);
await newGeneric.save();
//create a new waitlist here that corresponds to the serviceOccurrence
if(genericType === 'serviceOccurrence'){
const occurrenceWaitlist = genericType2Model['serviceWaitllist']({'serviceOccurrence': newGeneric, 'waitlist':[]});
//pass in the serviceOccurrence that was just created ("newGeneric").
//and an empty list for the queue.
await occurrenceWaitlist.save();
}
if (genericType2AfterCreateTreater[genericType])
await genericType2AfterCreateTreater[genericType](data, req);
return res.status(202).json({success: true, message: `Successfully created a/an ${genericType}`, createdId: newGeneric._id});
}
} catch (e) {
next(e);
}
};
async function updateSingleGenericHelper(genericId, data, genericType) {
const generic = await genericType2Model[genericType].findOne({_id: genericId}, {
populates: ['characteristicOccurrences',
'questionOccurrences']
});
if (!generic) {
throw new Server400Error(`No such ${genericType}`);
}
if (!data.formId) {
throw new Server400Error('No form id was provided');
}
const form = await MDBDynamicFormModel.findById(data.formId);
for (let key of Object.keys(genericType2Model)) {
if (form.formType !== key && genericType === key) {
throw new Server400Error(`The form is not for ${key}`);
}
}
if (!form.formStructure) {
throw new Server400Error('The form structure is not defined');
}
// TODO: verify if questions and characteristics are in the form
if (!data.fields) {
throw new Server400Error('No fields provided');
}
// fetch characteristics and questions from GDB
const questions = {};
const characteristics = {};
const internalTypes = {};
await fetchCharacteristicQuestionsInternalTypesBasedOnForms(characteristics, questions, internalTypes, data.fields);
if (genericType2BeforeUpdateChecker[genericType])
for (const checker of genericType2BeforeUpdateChecker[genericType])
await checker(characteristics, questions, data.fields, generic);
// check should we update or create a characteristicOccurrence or questionOccurrence
// in other words, is there a characteristicOccurrence/questionOccurrence belong to this user,
// and related to the characteristic/question
for (const [key, value] of Object.entries(data.fields)) {
const [type, id] = key.split('_');
if (type === 'characteristic') {
// find out all possible COs related to this characteristic
let query = `
PREFIX : <http://snmi#>
select * where {
?co ?p :characteristic_${id}.
?co a :CharacteristicOccurrence.
}`;
const possibleCharacteristicOccurrencesIds = [];
await GraphDB.sendSelectQuery(query, false, ({co, p}) => {
possibleCharacteristicOccurrencesIds.push(co.value.split('_')[1]);
});
// check if there is a CO in possibleCharacteristicOccurrencesIds is related to this generic
if (!generic.characteristicOccurrences)
generic.characteristicOccurrences = [];
const existedCO = generic.characteristicOccurrences.filter((co) => {
return possibleCharacteristicOccurrencesIds.filter((id) => {
return id === co._id;
}).length > 0;
})[0];
const characteristic = characteristics[id];
if (!existedCO && value != null) { // have to create a new CO and add the characteristic's id to the usage
await addIdToUsage('characteristic', genericType, id);
const occurrence = {occurrenceOf: characteristic};
await implementCharacteristicOccurrence(characteristic, occurrence, value);
generic.characteristicOccurrences.push(occurrence);
// update the generic's property if needed
if (characteristic.isPredefined) {
const property = getPredefinedProperty(genericType, characteristic);
if (property) {
generic[property] = occurrence.dataStringValue ?? occurrence.dataNumberValue ?? occurrence.dataBooleanValue ?? occurrence.dataDateValue
?? occurrence.objectValue ?? occurrence.multipleObjectValues;
}
}
} else if (existedCO && value != null) { // just add the value on existedCO
await implementCharacteristicOccurrence(characteristic, existedCO, value);
if (characteristic.isPredefined) {
const property = getPredefinedProperty(genericType, characteristic);
if (property) {
generic[property] = existedCO.dataStringValue ?? existedCO.dataNumberValue ?? existedCO.dataBooleanValue ?? existedCO.dataDateValue ??
existedCO.objectValue ?? existedCO.multipleObjectValues;
}
}
} else if (existedCO && value == null) { // when the user wants to remove the occurrence
await existedCO.populate('occurrenceOf');
if (existedCO.objectValue) { // remove the objectValue if necessary
const [fieldType, id] = existedCO.objectValue.split('_');
await specialField2Model[fieldType]?.findByIdAndDelete(id);
}
await GDBCOModel.findByIdAndDelete(existedCO._id); // remove the occurrence
// Remove the CO
generic.characteristicOccurrences.splice(generic.characteristicOccurrences.indexOf(existedCO), 1)
generic.markModified('characteristicOccurrences');
// also have to remove from usage if necessary
await deleteIdFromUsageAfterChecking('characteristic', genericType, existedCO.occurrenceOf._id);
}
}
if (type === 'question') {
// find out all possible QOs related to this question
let query = `
PREFIX : <http://snmi#>
select * where {
?qo ?p :question_${id}.
?qo a :QuestionOccurrence.
}`;
const possibleQuestionOccurrencesIds = [];
await GraphDB.sendSelectQuery(query, false, ({qo, p}) => {
possibleQuestionOccurrencesIds.push(qo.value.split('_')[1]);
});
// check if there is a QO in possibleQuestionOccurrencesIds is related to this generic
if (!generic.questionOccurrences)
generic.questionOccurrences = [];
const existedQO = generic.questionOccurrences.filter((qo) => {
return possibleQuestionOccurrencesIds.filter((id) => {
return id === qo._id;
}).length > 0;
})[0];
if (!existedQO && value) { // create a new QO
await addIdToUsage('question', genericType, id);
const occurrence = {occurrenceOf: questions[id], stringValue: value};
generic.questionOccurrences.push(occurrence);
} else if (existedQO && value) { // update the question
existedQO.stringValue = value;
} else if (existedQO && !value) { // remove the occurrence
await GDBQOModel.findByIdAndDelete(existedQO._id);
await existedQO.populate('occurrenceOf');
await deleteIdFromUsageAfterChecking('question', genericType, existedQO.occurrenceOf._id);
}
}
}
if (genericType2BeforeUpdateTreater[genericType])
await genericType2BeforeUpdateTreater[genericType](generic, internalTypes, data.fields);
const oldGeneric = generic.toJSON();
// TODO: Fix the issue where the new value is undefined, it never triggers the genericType2InternalTypeUpdateTreater()
for (const [key, value] of Object.entries(data.fields)) {
const [type, id] = key.split('_');
if (type === 'internalType') {
if (genericType2InternalTypeUpdateTreater[genericType]) {
await genericType2InternalTypeUpdateTreater[genericType](internalTypes[id], value, generic);
} else {
throw Error(`Cannot find internal type ${genericType}`)
}
}
}
return { oldGeneric, generic };
}
// what should we do if the field is empty
async function updateSingleGeneric(req, res, next) {
const data = req.body;
const {id, genericType} = req.params;
try {
// check the data package from frontend
// checking if a generic id is provided
if (!id) {
return res.status(400).json({success: false, message: `No ${genericType} id is given`});
}
const { oldGeneric, generic } = await updateSingleGenericHelper(id, data, genericType);
await generic.save();
if (genericType2AfterUpdateTreater[genericType])
await genericType2AfterUpdateTreater[genericType](data, oldGeneric, req);
res.status(200).json({success: true});
} catch (e) {
next(e);
}
}
async function deleteSingleGenericHelper(genericType, id) {
if (!genericType || !id)
throw new Server400Error('genericType or id is not given');
const generic = await genericType2Model[genericType].findOne({_id: id},
{populates: ['characteristicOccurrences', 'questionOccurrences']});
if (!generic)
throw new Server400Error('Invalid genericType or id');
if (genericType2BeforeDeleteChecker[genericType])
for (const checker of genericType2BeforeDeleteChecker[genericType])
await checker(generic);
if (genericType2BeforeDeleteTreater[genericType])
genericType2BeforeDeleteTreater[genericType](generic);
const oldGeneric = generic.toJSON();
const characteristicsOccurrences = generic.characteristicOccurrences;
const questionsOccurrences = generic.questionOccurrences;
const note = generic.note;
// delete notes
await GDBNoteModel.findByIdAndDelete(note?._id);
// recursively delete characteristicsOccurrences, including phoneNumber and Address
// todo: other special fields
if (characteristicsOccurrences) {
for (let characteristicOccurrence of characteristicsOccurrences) {
await characteristicOccurrence.populate('occurrenceOf');
if (characteristicOccurrence.objectValue) {
const [fieldType, id] = characteristicOccurrence.objectValue.split('_');
await specialField2Model[fieldType]?.findByIdAndDelete(id);
}
await GDBCOModel.findByIdAndDelete(characteristicOccurrence._id);
await deleteIdFromUsageAfterChecking('characteristic', genericType, characteristicOccurrence.occurrenceOf._id);
}
}
// recursively delete questionOccurrences
if (questionsOccurrences) {
for (let questionOccurrence of questionsOccurrences) {
await GDBQOModel.findByIdAndDelete(questionOccurrence._id);
await questionOccurrence.populate('occurrenceOf');
await deleteIdFromUsageAfterChecking('question', genericType, questionOccurrence.occurrenceOf._id);
}
}
await genericType2Model[genericType].findByIdAndDelete(id);
return oldGeneric;
}
async function deleteSingleGeneric(req, res, next) {
try {
// check the package from frontend
const {genericType, id} = req.params;
const oldGeneric = await deleteSingleGenericHelper(genericType, id);
if (genericType2AfterDeleteTreater[genericType])
await genericType2AfterDeleteTreater[genericType](oldGeneric, req);
return res.status(200).json({success: true});
} catch (e) {
next(e);
}
}
async function fetchGenericDatasHelper(genericType) {
if (!genericType2Model[genericType])
return null;
const extraPopulates = genericType2Populates[genericType] || [];
const data = await genericType2Model[genericType].find({},
{populates: ['characteristicOccurrences.occurrenceOf', 'questionOccurrence', ...extraPopulates]});
return data;
}
const fetchGenericDatas = async (req, res, next) => {
const {genericType} = req.params;
try {
const result = await fetchGenericDatasHelper(genericType);
if (!result) {
return res.status(400).json({success: false, message: 'No such generic type'});
}
return res.status(200).json({data: result, success: true});
} catch (e) {
next(e);
}
};
const searchGenericDatas = async (req, res, next) => {
const {genericType} = req.params;
let connector_search_result;
let fts_search_result;
try {
if (!genericType2Model[genericType])
return res.status(400).json({success: false, message: 'No such generic type'})
const extraPopulates = genericType2Populates[genericType] || [];
let data = [];
if (req.query.searchitem === undefined || req.query.searchitem === "") {
// nothing is entered in the search bar
// or it's just requesting all the objects
data = await genericType2Model[genericType].find({},
{populates: ['characteristicOccurrences.occurrenceOf', 'questionOccurrence', ...extraPopulates]});
} else {
fts_search_result = await fts_search(genericType, req.query.searchitem + '*');
connector_search_result = await connector_search(genericType, req.query.searchitem + '*');
// merge the two arrays
let array = [...new Set([...fts_search_result, ...connector_search_result])];
if (array.length !== 0) {
let data_array = [];
for (let i = 0; i < array.length; i++) {
data_array.push(await genericType2Model[genericType].find({_uri: array[i]},
{populates: ['characteristicOccurrences.occurrenceOf', 'questionOccurrence', ...extraPopulates]}));
}
data = data_array.flat();
}
}
return res.status(200).json({data, success: true});
} catch (e) {
next(e);
}
};
async function fts_search(searchtype, searchitem) {
// The initial query sent to the database
const baseURI = graphdb.addr + "/repositories/snmi?query=";
let query = "";
search_type_in_string = ['service', 'program', 'serviceProvision', 'client']
search_type_in_object = [':Service', ':Program', ':ServiceProvision', ':Client']
let search_object = '';
// if search type is in search_type_in_string, set search_object to the corresponding object
for (let i = 0; i < search_type_in_string.length; i++) {
if (searchtype === search_type_in_string[i]) {
search_object = search_type_in_object[i];
break
}
}
// FTS search part
const sparqlQuery =
`
PREFIX onto: <http://www.ontotext.com/>
PREFIX : <http://snmi#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
select distinct ?e0
where
{
BIND("${searchitem}" AS ?searchitem)
# Search :Service objects
{
?e0 ?p0 ?o0 .
?e0 rdf:type ${search_object} .
}.
# Check if the object itself contains the search item
{
?o0 onto:fts ?searchitem .
}
UNION
# Check if the object contains object containing the search item
{
?o0 ?p1 ?o1 .
{
?o1 onto:fts ?searchitem .
}
UNION
{
?o1 ?p2 ?o2 .
?o2 onto:fts ?searchitem .
}
}
}
`;
query = baseURI + encodeURIComponent(sparqlQuery);
const response = await fetch(query);
const text = await response.text();
return extractAllIndexes(searchtype, text);
}
async function connector_search(searchtype, searchitem) {
// The initial query sent to the database
const baseURI = graphdb.addr + "/repositories/snmi?query=";
let query = "";
search_type_in_string = ['service', 'program', 'serviceProvision', 'client']
search_type_in_object = [':Service', ':Program', ':ServiceProvision', ':Client']
let search_object = '';
// if search type is in search_type_in_string, set search_object to the corresponding object
for (let i = 0; i < search_type_in_string.length; i++) {
if (searchtype === search_type_in_string[i]) {
search_object = search_type_in_object[i];
break
}
}
// FTS search part
const sparqlQuery =
`
PREFIX onto: <http://www.ontotext.com/>
PREFIX : <http://snmi#>
PREFIX luc: <http://www.ontotext.com/connectors/lucene#>
PREFIX luc-index: <http://www.ontotext.com/connectors/lucene/instance#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
select distinct ?e0
where
{
# Search :Service objects
{
?e0 ?p0 ?o0 .
?e0 rdf:type ${search_object} .
}.
{
?search a luc-index:service_connector ;
luc:query "${searchitem}" ;
luc:entities ?e0 .
}
UNION
{
?search a luc-index:program_connector ;
luc:query "${searchitem}" ;
luc:entities ?e0 .
}
UNION
{
?search a luc-index:client_connector ;
luc:query "${searchitem}" ;
luc:entities ?e0 .
}
UNION
{
?search a luc-index:characteristicoccurrence_connector ;
luc:query "${searchitem}" ;
luc:entities ?o0 .
}