11import  {  EmailRegex  }  from  "../classes/Email" ; 
22import  {  Schema ,  model  }  from  "mongoose" ; 
33
4- const  UserProvider  =  new  Schema ( { 
4+ interface  IUserProvider  { 
5+   photo ?: string ; 
6+   phone ?: string ; 
7+   email : string ; 
8+   uid : string ; 
9+ } 
10+ 
11+ export  interface  IUser  { 
12+   name ?: string ; 
13+   email : string ; 
14+   accessToken : string ; 
15+   refreshToken : string ; 
16+   providers ?: IUserProvider ; 
17+   isAdmin : boolean ; 
18+ } 
19+ 
20+ const  UserProvider  =  new  Schema < IUserProvider > ( { 
521  photo : {  type : String  } , 
622  phone : {  type : String  } , 
723  email : {  type : String ,  required : true  } , 
824  uid : {  type : String ,  required : [ true ,  "Provider's user id is required" ]  } , 
925} ) ; 
1026
1127/* UserSchema will correspond to a collection in your MongoDB database. */ 
12- const  UserSchema  =  new  Schema ( 
28+ const  UserSchema  =  new  Schema < IUser > ( 
1329  { 
1430    name : { 
1531      type : String , 
@@ -43,60 +59,6 @@ const UserSchema = new Schema(
4359  } , 
4460  { 
4561    timestamps : true , 
46-     virtuals : { 
47-       firstName : { 
48-         get ( )  { 
49-           const  displayNameRegex  = 
50-             / (?< lastName > \w + ) , (?< firstName > \w + ) < ? (?< preferredName > \w + ) ? > ? / gm; 
51-           const  result  =  displayNameRegex . exec ( < string > this . name ) ; 
52- 
53-           // set name data 
54-           if  ( result ?. groups )  { 
55-             const  {  firstName }  =  result . groups ; 
56-             return  firstName ; 
57-           } 
58-         } , 
59-       } , 
60-       lastName : { 
61-         get ( )  { 
62-           const  displayNameRegex  = 
63-             / (?< lastName > \w + ) , (?< firstName > \w + ) < ? (?< preferredName > \w + ) ? > ? / gm; 
64-           const  result  =  displayNameRegex . exec ( < string > this . name ) ; 
65- 
66-           // set name data 
67-           if  ( result ?. groups )  { 
68-             const  {  lastName }  =  result . groups ; 
69-             return  lastName ; 
70-           } 
71-         } , 
72-       } , 
73-       preferredName : { 
74-         get ( )  { 
75-           const  displayNameRegex  = 
76-             / (?< lastName > \w + ) , (?< firstName > \w + ) < ? (?< preferredName > \w + ) ? > ? / gm; 
77-           const  result  =  displayNameRegex . exec ( < string > this . name ) ; 
78- 
79-           // set name data 
80-           if  ( result ?. groups )  { 
81-             const  {  preferredName }  =  result . groups ; 
82-             return  preferredName ; 
83-           } 
84-         } , 
85-       } , 
86-       fullname : { 
87-         get ( )  { 
88-           const  displayNameRegex  = 
89-             / (?< lastName > \w + ) , (?< firstName > \w + ) < ? (?< preferredName > \w + ) ? > ? / gm; 
90-           const  result  =  displayNameRegex . exec ( < string > this . name ) ; 
91- 
92-           // set name data 
93-           if  ( result ?. groups )  { 
94-             const  {  firstName,  lastName }  =  result . groups ; 
95-             return  firstName  +  " "  +  lastName ; 
96-           } 
97-         } , 
98-       } , 
99-     } , 
10062    methods : { 
10163      // generateJWT: { 
10264      //   get() { 
@@ -117,6 +79,54 @@ const UserSchema = new Schema(
11779  } , 
11880) ; 
11981
82+ UserSchema . virtual ( "firstName" ) . get ( function  get ( )  { 
83+   const  displayNameRegex  = 
84+     / (?< lastName > \w + ) , (?< firstName > \w + ) < ? (?< preferredName > \w + ) ? > ? / gm; 
85+   const  result  =  displayNameRegex . exec ( < string > this . name ) ; 
86+ 
87+   // set name data 
88+   if  ( result ?. groups )  { 
89+     const  {  firstName }  =  result . groups ; 
90+     return  firstName ; 
91+   } 
92+ } ) ; 
93+ 
94+ UserSchema . virtual ( "lastName" ) . get ( function  get ( )  { 
95+   const  displayNameRegex  = 
96+     / (?< lastName > \w + ) , (?< firstName > \w + ) < ? (?< preferredName > \w + ) ? > ? / gm; 
97+   const  result  =  displayNameRegex . exec ( < string > this . name ) ; 
98+ 
99+   // set name data 
100+   if  ( result ?. groups )  { 
101+     const  {  lastName }  =  result . groups ; 
102+     return  lastName ; 
103+   } 
104+ } ) ; 
105+ 
106+ UserSchema . virtual ( "perferredName" ) . get ( function  get ( )  { 
107+   const  displayNameRegex  = 
108+     / (?< lastName > \w + ) , (?< firstName > \w + ) < ? (?< preferredName > \w + ) ? > ? / gm; 
109+   const  result  =  displayNameRegex . exec ( < string > this . name ) ; 
110+ 
111+   // set name data 
112+   if  ( result ?. groups )  { 
113+     const  {  preferredName }  =  result . groups ; 
114+     return  preferredName ; 
115+   } 
116+ } ) ; 
117+ 
118+ UserSchema . virtual ( "fullName" ) . get ( function  get ( )  { 
119+   const  displayNameRegex  = 
120+     / (?< lastName > \w + ) , (?< firstName > \w + ) < ? (?< preferredName > \w + ) ? > ? / gm; 
121+   const  result  =  displayNameRegex . exec ( < string > this . name ) ; 
122+ 
123+   // set name data 
124+   if  ( result ?. groups )  { 
125+     const  {  firstName,  lastName }  =  result . groups ; 
126+     return  firstName  +  " "  +  lastName ; 
127+   } 
128+ } ) ; 
129+ 
120130// exports User model. 
121- const  User  =  model ( "User" ,  UserSchema ) ; 
131+ const  User  =  model < IUser > ( "User" ,  UserSchema ) ; 
122132export  default  User ; 
0 commit comments