1- import mongoose from 'mongoose' ;
1+ import mongoose , { Schema } from 'mongoose' ;
22import bcrypt from 'bcryptjs' ;
3+ import {
4+ UserDocument ,
5+ UserModel ,
6+ CookieConsentOptions ,
7+ EmailConfirmationStates
8+ } from '../types' ;
39import { apiKeySchema } from './apiKey' ;
410
5- const EmailConfirmationStates = {
6- Verified : 'verified' ,
7- Sent : 'sent' ,
8- Resent : 'resent'
9- } ;
10-
11- const { Schema } = mongoose ;
11+ // const EmailConfirmationStates = {
12+ // Verified: 'verified',
13+ // Sent: 'sent',
14+ // Resent: 'resent'
15+ // };
1216
13- const userSchema = new Schema (
17+ const userSchema = new Schema < UserDocument , UserModel > (
1418 {
1519 name : { type : String , default : '' } ,
1620 username : { type : String , required : true , unique : true } ,
@@ -44,13 +48,13 @@ const userSchema = new Schema(
4448 totalSize : { type : Number , default : 0 } ,
4549 cookieConsent : {
4650 type : String ,
47- enum : [ 'none' , 'essential' , 'all' ] ,
48- default : 'none'
51+ enum : Object . values ( CookieConsentOptions ) ,
52+ default : CookieConsentOptions . NONE
4953 } ,
5054 banned : { type : Boolean , default : false } ,
5155 lastLoginTimestamp : { type : Date }
5256 } ,
53- { timestamps : true , usePushEach : true }
57+ { timestamps : true }
5458) ;
5559
5660/**
@@ -67,6 +71,10 @@ userSchema.pre('save', function checkPassword(next) {
6771 next ( err ) ;
6872 return ;
6973 }
74+ if ( ! user . password ) {
75+ next ( new Error ( 'Password is missing' ) ) ;
76+ return ;
77+ }
7078 bcrypt . hash ( user . password , salt , ( innerErr , hash ) => {
7179 if ( innerErr ) {
7280 next ( innerErr ) ;
@@ -92,7 +100,7 @@ userSchema.pre('save', function checkApiKey(next) {
92100 let pendingTasks = 0 ;
93101 let nextCalled = false ;
94102
95- const done = ( err ) => {
103+ const done = ( err ?: mongoose . CallbackError ) => {
96104 if ( nextCalled ) return ;
97105 if ( err ) {
98106 nextCalled = true ;
@@ -144,7 +152,7 @@ userSchema.set('toJSON', {
144152 * @return {Promise<boolean> }
145153 */
146154userSchema . methods . comparePassword = async function comparePassword (
147- candidatePassword
155+ candidatePassword : string
148156) {
149157 if ( ! this . password ) {
150158 return false ;
@@ -162,7 +170,7 @@ userSchema.methods.comparePassword = async function comparePassword(
162170 * Helper method for validating a user's api key
163171 */
164172userSchema . methods . findMatchingKey = async function findMatchingKey (
165- candidateKey
173+ candidateKey : string
166174) {
167175 let keyObj = { isMatch : false , keyDocument : null } ;
168176 /* eslint-disable no-restricted-syntax */
@@ -332,9 +340,11 @@ userSchema.statics.findByEmailAndUsername = async function findByEmailAndUsernam
332340 return foundUser ;
333341} ;
334342
335- userSchema . statics . EmailConfirmation = EmailConfirmationStates ;
343+ // userSchema.statics.EmailConfirmation = EmailConfirmationStates;
336344
337345userSchema . index ( { username : 1 } , { collation : { locale : 'en' , strength : 2 } } ) ;
338346userSchema . index ( { email : 1 } , { collation : { locale : 'en' , strength : 2 } } ) ;
339347
340- export const User = mongoose . models . User || mongoose . model ( 'User' , userSchema ) ;
348+ export const User =
349+ ( mongoose . models . User as UserModel ) ||
350+ mongoose . model < UserDocument , UserModel > ( 'User' , userSchema ) ;
0 commit comments