|
1 | | -import { IUserGroup } from "@codrjs/models"; |
| 1 | +import { IUser, IUserGroup } from "@codrjs/models"; |
2 | 2 | import { Schema, SchemaTypes } from "mongoose"; |
3 | 3 | import { |
4 | 4 | AccessibleFieldsModel, |
5 | 5 | AccessibleModel, |
6 | 6 | accessibleFieldsPlugin, |
7 | 7 | accessibleRecordsPlugin, |
8 | 8 | } from "@casl/mongoose"; |
9 | | -import { UserDocument } from "./User"; |
10 | 9 |
|
11 | 10 | export type UserGroupDocument = IUserGroup & AccessibleFieldsModel<IUserGroup>; |
12 | 11 |
|
13 | | -export function createUserGroupModel(userModel?: AccessibleModel<UserDocument>) { |
14 | | - if (userModel) { |
15 | | - const UserGroupSchema = new Schema<UserGroupDocument>( |
16 | | - { |
17 | | - createdBy: { |
18 | | - required: true, |
19 | | - index: true, |
| 12 | +export function createUserGroupModel(userModel: AccessibleModel<IUser>) { |
| 13 | + const UserGroupSchema = new Schema<UserGroupDocument>( |
| 14 | + { |
| 15 | + createdBy: { |
| 16 | + required: true, |
| 17 | + index: true, |
| 18 | + type: SchemaTypes.ObjectId, |
| 19 | + ref: "User", |
| 20 | + }, |
| 21 | + members: { |
| 22 | + items: { |
20 | 23 | type: SchemaTypes.ObjectId, |
21 | 24 | ref: "User", |
22 | 25 | }, |
23 | | - members: { |
24 | | - items: { |
25 | | - type: SchemaTypes.ObjectId, |
26 | | - ref: "User", |
27 | | - }, |
28 | | - }, |
29 | | - teams: { |
30 | | - items: { |
31 | | - type: SchemaTypes.ObjectId, |
32 | | - ref: "UserGroup", |
33 | | - }, |
| 26 | + }, |
| 27 | + teams: { |
| 28 | + items: { |
| 29 | + type: SchemaTypes.ObjectId, |
| 30 | + ref: "UserGroup", |
34 | 31 | }, |
35 | | - name: { |
36 | | - type: "String", |
37 | | - required: true, |
38 | | - index: true, |
39 | | - default: "Unnamed Group", |
| 32 | + }, |
| 33 | + name: { |
| 34 | + type: "String", |
| 35 | + required: true, |
| 36 | + index: true, |
| 37 | + default: "Unnamed Group", |
| 38 | + }, |
| 39 | + flags: { |
| 40 | + type: { |
| 41 | + isAnonymous: Boolean, |
| 42 | + isDeleted: Boolean, |
| 43 | + isJoinable: Boolean, |
| 44 | + isPrivate: Boolean, |
40 | 45 | }, |
41 | | - flags: { |
42 | | - type: { |
43 | | - isAnonymous: Boolean, |
44 | | - isDeleted: Boolean, |
45 | | - isJoinable: Boolean, |
46 | | - isPrivate: Boolean, |
47 | | - }, |
48 | | - required: true, |
49 | | - default: { |
50 | | - isAnonymous: false, |
51 | | - isDeleted: false, |
52 | | - isJoinable: false, |
53 | | - isPrivate: false, |
54 | | - }, |
| 46 | + required: true, |
| 47 | + default: { |
| 48 | + isAnonymous: false, |
| 49 | + isDeleted: false, |
| 50 | + isJoinable: false, |
| 51 | + isPrivate: false, |
55 | 52 | }, |
56 | | - createdAt: { type: String }, |
57 | | - updatedAt: { type: String }, |
58 | | - }, |
59 | | - { |
60 | | - timestamps: true, |
61 | 53 | }, |
62 | | - ); |
| 54 | + createdAt: { type: String }, |
| 55 | + updatedAt: { type: String }, |
| 56 | + }, |
| 57 | + { |
| 58 | + timestamps: true, |
| 59 | + }, |
| 60 | + ); |
63 | 61 |
|
64 | | - UserGroupSchema.virtual("user", { |
65 | | - ref: userModel, |
66 | | - localField: "userId", |
67 | | - foreignField: "_id", |
68 | | - }); |
| 62 | + UserGroupSchema.virtual("user", { |
| 63 | + ref: userModel, |
| 64 | + localField: "userId", |
| 65 | + foreignField: "_id", |
| 66 | + }); |
69 | 67 |
|
70 | | - // exports UserGroup model. |
71 | | - UserGroupSchema.plugin(accessibleFieldsPlugin); |
72 | | - UserGroupSchema.plugin(accessibleRecordsPlugin); |
| 68 | + // exports UserGroup model. |
| 69 | + UserGroupSchema.plugin(accessibleFieldsPlugin); |
| 70 | + UserGroupSchema.plugin(accessibleRecordsPlugin); |
73 | 71 |
|
74 | | - return UserGroupSchema; |
75 | | - } else { |
76 | | - throw "User model is not defined."; |
77 | | - } |
| 72 | + return UserGroupSchema; |
78 | 73 | } |
0 commit comments