-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathf_nested_schema.js
More file actions
99 lines (86 loc) · 2.75 KB
/
f_nested_schema.js
File metadata and controls
99 lines (86 loc) · 2.75 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
import mongoose from 'mongoose';
var Schema = mongoose.Schema;
var ExamsSchema = new Schema({
subject: String,
examDate: Date,
score: Number,
result: {type: String, enum: ['distinction', 'merit', 'pass', 'fail']},
grader: { type: Schema.Types.ObjectId, ref: 'b_using_options', form: {select2: {fngAjax: true}, label:'Marked by'}},
retakeDate: {type: Date, form: {showWhen: {lhs: '$exams.result', comp: 'eq', rhs: 'fail'}}}
}, {_id: false});
var FSchema = new Schema({
surname: {type: String, index: true, list: {}},
forename: {type: String, index: true, list: true},
aTest: { type: Schema.Types.ObjectId, ref: 'b_using_options'},
// exams: [ExamsSchema] // defaults to horizontal compact form
// or
exams: {type: [ExamsSchema], form: {formStyle: 'inline'}}
});
var F;
try {
F = mongoose.model('F');
} catch (e) {
F = mongoose.model('F', FSchema);
}
F.prototype.searchResultFormat = function (req) {
// You can set up a function to modify search result display and the
// ordering within a collection
let weighting;
weighting = this.forename === 'John' ? 2 : 3;
const retVal = {
resource: 'f_nested_schema',
resourceText: 'Exams',
id: this._id,
weighting: weighting,
text: this.surname + (this.forename ? ', ' + this.forename : '')
};
if (req.query && req.query.q && req.query.q.length > 0) {
if (req.query.q.slice(0, 6).toLowerCase() === 'exams:') {
retVal.resourceText = ' ';
} else if (req.query.q.slice(0, 8).toLowerCase() === 'retakes:') {
retVal.resourceText = 'Retakes';
}
}
return Promise.resolve(retVal);
};
FSchema.statics.form = function (layout) {
var formSchema = '';
switch (layout) {
case 'English' :
// Just the English exam from the array
formSchema = {
surname: {},
forename: {},
exams: {subkey: {keyList: {subject: 'English'}, containerType: 'well', title: 'English Exam'}}
};
break;
case 'EnglishAndMaths' :
// English and Maths exams from the array
formSchema = {
surname: {},
forename: {},
exams: {subkey: [
{keyList: {subject: 'English'}, containerType: 'well', title: 'English Exam'},
{keyList: {subject: 'Maths'}, containerType: 'well', title: 'Maths Exam'}
]}
};
break;
case 'ResultsOnly' :
// Demonstration of specifying fields within sub schemas in a form schema
formSchema = {
surname: {},
forename: {},
exams: {schema: {
subject: {},
result: {label: 'Outcome'}
}}
};
break;
}
return formSchema;
};
export default {
model: F,
synonyms: [{name:'exams'}, {name: 'retakes', filter:{'exams.result':'fail'}}],
searchResultFormat: F.prototype.searchResultFormat
};