-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.mjs
More file actions
276 lines (257 loc) · 7.21 KB
/
models.mjs
File metadata and controls
276 lines (257 loc) · 7.21 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
import { DataTypes, Model } from 'sequelize';
import sequelize from './sequelize.mjs';
//import logger from './logger.mjs';
//
// USER MODEL
//
class User extends Model {
//Custom methods go here
}
User.init(
{ // FIELDS
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
firstName: {
type: DataTypes.STRING(35),
allowNull: false,
},
lastName: {
type: DataTypes.STRING(70),
allowNull: false,
},
email: {
type: DataTypes.STRING,
unique: true,
allowNull: false,
isEmail: true,
},
phone: {
type: DataTypes.STRING(15),
},
role: {
type: DataTypes.ENUM('Admin','Leader','Participant'),
allowNull: false,
},
lotteryWeight: {
type: DataTypes.FLOAT,
allowNull: false,
defaultValue: 1,
},
hasWaiver: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
tripsLead: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 0,
},
tripsParticipated: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 0,
},
joinedListserv: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
}
},
{ // OPTIONS
sequelize,
//Preserves snake_case notation
tableName: 'users',
underscored: true,
}
);
//
// TRIP MODEL
//
class Trip extends Model {
//Custom methods go here
}
//Create default planning checklist
const tasks = ['Add to Google Calendar', 'Event Registration Request', 'Event Plan', 'Trip Description/Blurb', 'Lottery', 'Acceptance/Rejectance Emails', 'Pre-Trip Email', 'SAO Pre-Trip Email', 'Grab Medkit', 'Attendance', 'Follow Up Email', 'SAO Post-Trip Email', 'Impact Tracker'];
const taskObj = {
responsible: '',
complete: false,
};
const defaultPlanningChecklist = tasks.reduce((pcList, task) => {
pcList[task] = { ...taskObj };
return pcList;
}, {});
Trip.init(
{ // FIELDS
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
tripName: {
type: DataTypes.STRING(50),
allowNull: false,
},
category : {
type: DataTypes.ENUM('Hiking', 'Camping', 'Backpacking', 'Biking', 'Climbing', 'Skiing', 'Water', 'Event', 'Running', 'Exploration', 'Local', 'Special'),
allowNull: false,
defaultValue: 'Special',
},
plannedDate: {
type: DataTypes.DATEONLY,
allowNull: false,
},
plannedEndDate : {
type: DataTypes.DATEONLY,
},
maxSize: {
type: DataTypes.INTEGER,
allowNull: false,
},
class: {
type: DataTypes.STRING(1),
validate: { //Ensures only class or priceOverride is defined, not both
classXorPriceOverride(val) {
if (!(!val ^ !this.priceOverride)) { //nots coerce to booleans lol
throw new Error('Either both class and priceOverride are null or both are not null');
}
}
}
},
priceOverride: {
type: DataTypes.FLOAT,
},
sentenceDesc: {
type: DataTypes.STRING(1000), //We might want to play with this number at some point
},
blurb: {
type: DataTypes.TEXT,
},
image: {
type: DataTypes.STRING(512), //No URL will be longer than this... right?
},
status: {
type: DataTypes.ENUM('Staging','Open','Pre-Trip','Post-Trip','Complete'),
allowNull: false,
defaultValue: 'Staging',
},
planningChecklist: {
type: DataTypes.JSON,
allowNull: false,
defaultValue: JSON.stringify(defaultPlanningChecklist),
},
},
{ // OPTIONS
sequelize,
indexes: [{ //Combination of trip_name and planned_date must be unique
unique: true,
fields: ['trip_name', 'planned_date'],
}],
//Preserves snake_case notation
tableName: 'trips',
underscored: true,
}
);
//
// TRIP_USER_MAP MODEL
//
class TripSignUp extends Model {
//Custom methods go here
}
TripSignUp.init(
{ // FIELDS
tripId: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
},
userId: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
},
tripRole: {
type: DataTypes.ENUM('Leader','Participant'),
allowNull: false,
},
status: {
type: DataTypes.ENUM('Signed Up','Selected','Waitlisted','Not Selected','Attended','No Show'),
defaultValue: 'Signed Up',
},
needPaperwork: {
type: DataTypes.BOOLEAN,
defaultValue: false,
},
confirmed: {
type: DataTypes.BOOLEAN,
defaultValue: false,
},
paid: {
type: DataTypes.BOOLEAN,
defaultValue: false,
},
},
{ // OPTIONS
sequelize,
//Preserves snake_case notation
tableName: 'trip_signups',
underscored: true,
}
);
//Overrides default values of status, needPaperwork, confirmed, and paid to null for leaders
TripSignUp.beforeValidate((inst) => {
if (inst.tripRole === 'Leader') {
inst.status = null;
inst.needPaperwork = null;
inst.confirmed = null;
inst.paid = null;
}
});
//
// TRIP_CLASS MODEL
//
class TripClass extends Model {
//Custom methods go here
}
TripClass.init(
{ // FIELDS
tripClass: {
type: DataTypes.STRING(1),
allowNull: false,
primaryKey: true,
},
link: {
type: DataTypes.STRING,
},
price: {
type: DataTypes.FLOAT,
allowNull: false,
},
},
{ // OPTIONS
sequelize,
//Preserves snake_case notation
tableName: 'trip_classes',
underscored: true,
}
);
//
// ASSOCIATIONS AND SYNCHRONIZATION
//
//trip_classes and trips association
TripClass.hasMany(Trip, { foreignKey: 'class' });
Trip.belongsTo(TripClass, { foreignKey: 'class' });
//users and trips association - special "Super Many-to-Many" association
User.belongsToMany(Trip, { through: TripSignUp, foreignKey: 'userId' });
Trip.belongsToMany(User, { through: TripSignUp, foreignKey: 'tripId' });
User.hasMany(TripSignUp, { foreignKey: 'userId' });
TripSignUp.belongsTo(User, { foreignKey: 'userId' });
Trip.hasMany(TripSignUp, { foreignKey: 'tripId' });
TripSignUp.belongsTo(Trip, { foreignKey: 'tripId' });
//Sync models with database
// await sequelize.sync({force: true});
//logger.log('Models successfully synced with database');
export default { User, Trip, TripSignUp, TripClass };