-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuser.mgmodel.ts
More file actions
157 lines (143 loc) · 3.34 KB
/
user.mgmodel.ts
File metadata and controls
157 lines (143 loc) · 3.34 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
import mongoose, { Document, ObjectId, Schema } from "mongoose";
import { Role, Status } from "../types/userTypes";
export interface User extends Document {
id: ObjectId;
firstName: string;
lastName: string;
authId: string;
role: Role;
status: Status;
email: string;
profilePicture?: string;
bookmarks: Bookmark[];
}
export interface Bookmark {
id: mongoose.Types.ObjectId;
title: string;
type: string;
unitId: mongoose.Types.ObjectId;
moduleId: mongoose.Types.ObjectId;
pageId: mongoose.Types.ObjectId;
}
export interface Learner extends User {
facilitator: ObjectId;
activitiesCompleted: Map<string, Map<string, Array<ObjectId>>>;
nextPage?: ObjectId;
}
export interface Facilitator extends User {
learners: Array<ObjectId>;
bio?: string;
}
const options = {
discriminatorKey: "role",
timestamps: true,
toObject: {
virtuals: true,
versionKey: false,
transform: (_doc: Document, ret: Record<string, unknown>) => {
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
delete ret._id;
// eslint-disable-next-line no-param-reassign
delete ret.createdAt;
// eslint-disable-next-line no-param-reassign
delete ret.updatedAt;
},
},
};
export const BookmarkSchema: Schema = new Schema({
id: { type: mongoose.Schema.Types.ObjectId },
title: { type: String },
type: { type: String },
unitId: { type: mongoose.Types.ObjectId },
pageId: { type: mongoose.Types.ObjectId },
moduleId: { type: mongoose.Types.ObjectId },
});
export const UserSchema: Schema = new Schema(
{
firstName: {
type: String,
required: true,
},
lastName: {
type: String,
required: true,
},
authId: {
type: String,
required: true,
},
role: {
type: String,
required: true,
enum: ["Administrator", "Facilitator", "Learner"],
},
status: {
type: String,
required: true,
enum: ["Invited", "Active"],
},
email: {
type: String,
required: true,
},
profilePicture: {
type: String,
},
bookmarks: {
type: [BookmarkSchema],
default: [],
required: true,
},
},
options,
);
const UserModel = mongoose.model<User>("User", UserSchema);
const AdministratorSchema = new Schema({}, options);
const FacilitatorSchema = new Schema(
{
learners: {
type: [{ type: mongoose.Schema.Types.ObjectId, ref: "User" }],
default: [],
},
bio: {
type: String,
required: false,
},
},
options,
);
const LearnerSchema = new Schema(
{
facilitator: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
required: true,
},
activitiesCompleted: {
type: Map,
of: [
{
type: Map,
of: [{ type: mongoose.Schema.Types.ObjectId, ref: "Activity" }],
},
],
default: {},
},
nextPage: {
type: mongoose.Schema.Types.ObjectId,
ref: "CoursePage",
},
},
options,
);
const AdministratorModel = UserModel.discriminator(
"Administrator",
AdministratorSchema,
);
const FacilitatorModel = UserModel.discriminator<Facilitator>(
"Facilitator",
FacilitatorSchema,
);
const LearnerModel = UserModel.discriminator<Learner>("Learner", LearnerSchema);
export { AdministratorModel, FacilitatorModel, LearnerModel };
export default UserModel;