@@ -44,95 +44,113 @@ const locationSchema = new Schema({
44
44
} ) ;
45
45
46
46
//senseBox schema
47
- const boxSchema = new Schema ( {
48
- name : {
49
- type : String ,
50
- required : true ,
51
- trim : true
52
- } ,
53
- locations : {
54
- type : [ locationSchema ] ,
55
- required : true ,
56
- } ,
57
- currentLocation : {
58
- type : locationSchema ,
59
- required : true ,
60
- } ,
61
- exposure : {
62
- type : String ,
63
- trim : true ,
64
- required : true ,
65
- enum : [ 'unknown' , 'indoor' , 'outdoor' , 'mobile' ]
66
- } ,
67
- grouptag : {
68
- type : [ String ] , // Default value for array is [] (empty array)
69
- trim : true ,
70
- required : false
71
- } ,
72
- model : {
73
- type : String ,
74
- required : true ,
75
- trim : true ,
76
- default : 'custom' ,
77
- enum : [ 'custom' , ...sensorLayouts . models ]
78
- } ,
79
- weblink : {
80
- type : String ,
81
- trim : true ,
82
- required : false
83
- } ,
84
- description : {
85
- type : String ,
86
- trim : true ,
87
- required : false
88
- } ,
89
- image : {
90
- type : String ,
91
- trim : true ,
92
- required : false ,
93
- /* eslint-disable func-name-matching */
94
- set : function imageSetter ( { type, data, deleteImage } ) {
95
- /* eslint-enable func-name-matching */
96
- if ( type && data ) {
97
- const filename = `${ this . _id } _${ Math . round ( Date . now ( ) / 1000 ) . toString ( 36 ) } .${ type } ` ;
98
- try {
99
- fs . writeFileSync ( `${ imageFolder } ${ filename } ` , data ) ;
100
- } catch ( err ) {
101
- log . warn ( err ) ;
102
-
103
- return ;
104
- }
47
+ const boxSchema = new Schema (
48
+ {
49
+ name : {
50
+ type : String ,
51
+ required : true ,
52
+ trim : true
53
+ } ,
54
+ locations : {
55
+ type : [ locationSchema ] ,
56
+ required : true
57
+ } ,
58
+ currentLocation : {
59
+ type : locationSchema ,
60
+ required : true
61
+ } ,
62
+ exposure : {
63
+ type : String ,
64
+ trim : true ,
65
+ required : true ,
66
+ enum : [ 'unknown' , 'indoor' , 'outdoor' , 'mobile' ]
67
+ } ,
68
+ grouptag : {
69
+ type : [ String ] , // Default value for array is [] (empty array)
70
+ trim : true ,
71
+ required : false
72
+ } ,
73
+ model : {
74
+ type : String ,
75
+ required : true ,
76
+ trim : true ,
77
+ default : 'custom' ,
78
+ enum : [ 'custom' , ...sensorLayouts . models ]
79
+ } ,
80
+ weblink : {
81
+ type : String ,
82
+ trim : true ,
83
+ required : false
84
+ } ,
85
+ description : {
86
+ type : String ,
87
+ trim : true ,
88
+ required : false
89
+ } ,
90
+ image : {
91
+ type : String ,
92
+ trim : true ,
93
+ required : false ,
94
+ /* eslint-disable func-name-matching */
95
+ set : function imageSetter ( { type, data, deleteImage } ) {
96
+ /* eslint-enable func-name-matching */
97
+ if ( type && data ) {
98
+ const filename = `${ this . _id } _${ Math . round (
99
+ Date . now ( ) / 1000
100
+ ) . toString ( 36 ) } .${ type } `;
101
+ try {
102
+ fs . writeFileSync ( `${ imageFolder } ${ filename } ` , data ) ;
103
+ } catch ( err ) {
104
+ log . warn ( err ) ;
105
+
106
+ return ;
107
+ }
105
108
106
- return filename ;
107
- } else if ( deleteImage === true ) {
108
- if ( this . image ) {
109
- const oldFilename = `${ imageFolder } ${ this . image } ` ;
110
- const extensionToUse = this . image . slice ( this . image . lastIndexOf ( '.' ) ) ;
111
- const newFilename = `${ imageFolder } ${ Buffer . from ( ( Buffer . from ( `${ Math . random ( ) . toString ( 36 )
112
- . slice ( 2 ) } ${ Date . now ( ) } `) . toString ( 'base64' ) ) ) . toString ( 'hex' ) } ${ extensionToUse } `;
113
- fs . rename ( oldFilename , newFilename , ( ) => { } ) ;
109
+ return filename ;
110
+ } else if ( deleteImage === true ) {
111
+ if ( this . image ) {
112
+ const oldFilename = `${ imageFolder } ${ this . image } ` ;
113
+ const extensionToUse = this . image . slice (
114
+ this . image . lastIndexOf ( '.' )
115
+ ) ;
116
+ const newFilename = `${ imageFolder } ${ Buffer . from (
117
+ Buffer . from (
118
+ `${ Math . random ( ) . toString ( 36 )
119
+ . slice ( 2 ) } ${ Date . now ( ) } `
120
+ ) . toString ( 'base64' )
121
+ ) . toString ( 'hex' ) } ${ extensionToUse } `;
122
+ fs . rename ( oldFilename , newFilename , ( ) => { } ) ;
123
+ }
114
124
}
115
125
}
126
+ } ,
127
+ sensors : {
128
+ type : [ sensorSchema ] ,
129
+ // required: [true, 'sensors are required if model is invalid or missing.'],
130
+ // https://github.com/Automattic/mongoose/issues/5139#issuecomment-429990002
131
+ validate : {
132
+ validator : ( v ) => {
133
+ return v === null || v . length > 0 ;
134
+ } ,
135
+ message : ( ) => 'sensors are required if model is invalid or missing.'
136
+ }
137
+ } ,
138
+ lastMeasurementAt : {
139
+ type : Date ,
140
+ required : false
141
+ } ,
142
+ access_token : {
143
+ type : String ,
144
+ required : true
145
+ } ,
146
+ useAuth : {
147
+ type : Boolean ,
148
+ required : true ,
149
+ default : false
116
150
}
117
151
} ,
118
- sensors : {
119
- type : [ sensorSchema ] ,
120
- required : [ true , 'sensors are required if model is invalid or missing.' ] ,
121
- } ,
122
- lastMeasurementAt : {
123
- type : Date ,
124
- required : false
125
- } ,
126
- access_token : {
127
- type : String ,
128
- required : true
129
- } ,
130
- useAuth : {
131
- type : Boolean ,
132
- required : true ,
133
- default : false ,
134
- }
135
- } , { usePushEach : true } ) ;
152
+ { usePushEach : true }
153
+ ) ;
136
154
boxSchema . plugin ( timestamp ) ;
137
155
138
156
const BOX_PROPS_FOR_POPULATION = {
0 commit comments