@@ -4,56 +4,34 @@ import {
44 AccessibleModel ,
55 accessibleRecordsPlugin ,
66} from "@casl/mongoose" ;
7- import { Schema , model , Document } from "mongoose" ;
7+ import { Schema , model , Document , ObjectId } from "mongoose" ;
88
99type Role = "admin" | "researcher" | "annotator" ;
1010export type UserRoleType = `codr:${Role } `;
1111
12- interface IUserProvider {
13- photo ?: string ;
14- phone ?: string ;
15- email : string ;
16- uid : string ;
17- }
18-
19- export interface IUserName {
20- first : string ;
21- last : string ;
22- preferred : string ;
23- }
24-
25- export interface IUser extends Document {
26- name ?: IUserName ;
12+ interface User {
13+ name ?: {
14+ first : string ;
15+ last : string ;
16+ preferred ?: string ;
17+ } ;
2718 email : string ;
2819 accessToken : string ;
2920 refreshToken : string ;
30- providers ?: IUserProvider ;
31- isAdmin : boolean ;
3221 role : UserRoleType ;
33- disabled : boolean ;
34- anonymous : boolean ;
22+ flags : {
23+ isDisabled : boolean ;
24+ isAnonymous : boolean ;
25+ } ;
3526}
3627
37- const UserProvider = new Schema < IUserProvider > ( {
38- photo : { type : String } ,
39- phone : { type : String } ,
40- email : { type : String , required : true } ,
41- uid : {
42- type : String ,
43- required : [ true , "Provider's unique identifier is required" ] ,
44- } ,
45- } ) ;
46-
47- const UserName = new Schema < IUserName > ( {
48- first : { type : String } ,
49- last : { type : String } ,
50- preferred : { type : String } ,
51- } ) ;
28+ type IUserSchema = User & Document ;
29+ export type IUser = User & { _id : ObjectId }
5230
53- const UserSchema = new Schema < IUser > (
31+ const UserSchema = new Schema < User > (
5432 {
5533 name : {
56- type : UserName ,
34+ type : Object ,
5735 } ,
5836 email : {
5937 type : String ,
@@ -69,33 +47,23 @@ const UserSchema = new Schema<IUser>(
6947 } ,
7048 accessToken : { type : String } ,
7149 refreshToken : { type : String } ,
72- providers : {
73- type : [ UserProvider ] ,
74- } ,
7550 role : {
7651 type : String ,
7752 required : [ true , "Please specify the user's role." ] ,
7853 } ,
79- disabled : {
80- type : Boolean ,
81- default : false ,
82- } ,
83- anonymous : {
84- type : Boolean ,
85- default : false ,
54+ flags : {
55+ type : Object ,
56+ required : true ,
57+ default : { isAnonymous : false , isDisabled : false } ,
8658 } ,
8759 } ,
8860 {
8961 timestamps : true ,
9062 } ,
9163) ;
9264
93- UserSchema . virtual ( "fullName" ) . get ( function get ( ) {
94- return this . name ?. preferred + " " + this . name ?. last ;
95- } ) ;
96-
9765// exports User model.
9866UserSchema . plugin ( accessibleFieldsPlugin ) ;
9967UserSchema . plugin ( accessibleRecordsPlugin ) ;
100- const User = model < IUser , AccessibleModel < IUser > > ( "User" , UserSchema ) ;
68+ const User = model < IUserSchema , AccessibleModel < IUserSchema > > ( "User" , UserSchema ) ;
10169export default User ;
0 commit comments