refactor(db/models): add notifyOnReviewReply flag to user schema
				
					
				
			This commit is contained in:
		
							parent
							
								
									2c282b6bcd
								
							
						
					
					
						commit
						6598dfd3d6
					
				
							
								
								
									
										216
									
								
								models/user.ts
									
									
									
									
									
								
							
							
						
						
									
										216
									
								
								models/user.ts
									
									
									
									
									
								
							| @ -1,11 +1,17 @@ | ||||
| import mongoose, { Schema, connect, PopulatedDoc, Document, Model } from "mongoose"; | ||||
| import mongoose, { | ||||
| 	Schema, | ||||
| 	connect, | ||||
| 	PopulatedDoc, | ||||
| 	Document, | ||||
| 	Model, | ||||
| } from "mongoose"; | ||||
| import SequenceFactory from "mongoose-sequence"; | ||||
| import bcrypt from "bcryptjs"; | ||||
| import md5 from "blueimp-md5"; | ||||
| import jwt from "jsonwebtoken" | ||||
| import { hasMigrated } from "../lib/dbconfig"; | ||||
| import jwt from "jsonwebtoken"; | ||||
| import { hasMigrated } from "~/lib/dbconfig"; | ||||
| import { IBand } from "./band"; | ||||
| import {IStory} from "./stories/index" | ||||
| import { IStory } from "./stories/index"; | ||||
| import { QuickMenuItem, QuickMenuSchema } from "./quickMenu"; | ||||
| 
 | ||||
| const AutoIncrement = SequenceFactory(mongoose); | ||||
| @ -23,11 +29,11 @@ export interface IUser extends Document { | ||||
| 		emailVerified: boolean; | ||||
| 		activationKey: string | null; | ||||
| 		passwordResetToken: string | null; | ||||
| 	} | ||||
| 	}; | ||||
| 	ts: { | ||||
| 		created: Date; | ||||
| 		updated: Date; | ||||
| 	} | ||||
| 	}; | ||||
| 	ipLog: IIPLogEntry[]; | ||||
| 	lastLogin: Date; | ||||
| 	lastVisit: Date; | ||||
| @ -45,20 +51,20 @@ export interface IUser extends Document { | ||||
| 		hidden: boolean; | ||||
| 		disclaimer: string; | ||||
| 		showEmail: boolean; | ||||
| 	} | ||||
| 	}; | ||||
| 	biffno: { | ||||
| 		years: string[]; | ||||
| 		wins: number; | ||||
| 	} | ||||
| 	}; | ||||
| 	favs: { | ||||
| 		authors: PopulatedDoc<IUser & Document>[]; | ||||
| 		stories: PopulatedDoc<IStory & Document>[]; | ||||
| 	} | ||||
| 	}; | ||||
| 	subscriptions: { | ||||
| 		authors: PopulatedDoc<IUser & Document>[]; | ||||
| 		bands: PopulatedDoc<IBand & Document>[]; | ||||
| 		stories: PopulatedDoc<IStory & Document>[]; | ||||
| 	} | ||||
| 	}; | ||||
| 	//@ts-ignore SHUT UP
 | ||||
| 	hiddenAuthors: PopulatedDoc<IUser & Document>[]; | ||||
| 	hiddenBands: PopulatedDoc<IBand & Document>[]; | ||||
| @ -66,7 +72,8 @@ export interface IUser extends Document { | ||||
| 	blocked: PopulatedDoc<IUser & Document>[]; | ||||
| 	sessionId: string | null; | ||||
| 	banned: boolean; | ||||
| 	quickMenuConfig: QuickMenuItem[] | ||||
| 	quickMenuConfig: QuickMenuItem[]; | ||||
| 	notifyOnReviewReply?: boolean; | ||||
| 	validPassword(password: string): boolean; | ||||
| 	generateToken(jwtSecret: string): string; | ||||
| } | ||||
| @ -75,183 +82,216 @@ interface UModel extends Model<IUser> { | ||||
| 	generateHash(pwd: string): string; | ||||
| } | ||||
| 
 | ||||
| const UserSchema = new mongoose.Schema<IUser, UModel>({ | ||||
| const UserSchema = new mongoose.Schema<IUser>({ | ||||
| 	_id: { | ||||
| 		type: Number | ||||
| 		type: Number, | ||||
| 	}, | ||||
| 	username: { | ||||
| 		type: String | ||||
| 		type: String, | ||||
| 	}, | ||||
| 	email: { | ||||
| 		type: String | ||||
| 		type: String, | ||||
| 	}, | ||||
| 	password: { | ||||
| 		type: String | ||||
| 		type: String, | ||||
| 	}, | ||||
| 	auth: { | ||||
| 		emailVerified: { | ||||
| 			type: Boolean, | ||||
| 			default: false | ||||
| 			default: false, | ||||
| 		}, | ||||
| 		activationKey: { | ||||
| 			type: String | ||||
| 			type: String, | ||||
| 		}, | ||||
| 		passwordResetToken: { | ||||
| 			type: String, | ||||
| 			default: null | ||||
| 		} | ||||
| 			default: null, | ||||
| 		}, | ||||
| 	}, | ||||
| 	ts: { | ||||
| 		created: { | ||||
| 			type: Date, | ||||
| 			default: new Date() | ||||
| 			default: new Date(), | ||||
| 		}, | ||||
| 		updated: { | ||||
| 			type: Date, | ||||
| 			default: new Date() | ||||
| 		} | ||||
| 			default: new Date(), | ||||
| 		}, | ||||
| 	ipLog: [{ | ||||
| 	}, | ||||
| 	ipLog: [ | ||||
| 		{ | ||||
| 			ip: { | ||||
| 			type: String | ||||
| 				type: String, | ||||
| 			}, | ||||
| 			lastAccess: { | ||||
| 				type: Date, | ||||
| 			default: new Date() | ||||
| 		} | ||||
| 	}], | ||||
| 				default: new Date(), | ||||
| 			}, | ||||
| 		}, | ||||
| 	], | ||||
| 	lastLogin: { | ||||
| 		type: Date, | ||||
| 		default: null | ||||
| 		default: null, | ||||
| 	}, | ||||
| 	lastVisit: { | ||||
| 		type: Date, | ||||
| 		default: null | ||||
| 		default: null, | ||||
| 	}, | ||||
| 	profile: { | ||||
| 		avatar: { | ||||
| 			type: String, | ||||
| 			default: "" | ||||
| 			default: "", | ||||
| 		}, | ||||
| 		isAdmin: { | ||||
| 			type: Boolean, | ||||
| 			default: false | ||||
| 			default: false, | ||||
| 		}, | ||||
| 		nightMode: { | ||||
| 			type: Boolean, | ||||
| 			default: false | ||||
| 			default: false, | ||||
| 		}, | ||||
| 		bio: { | ||||
| 			type: String, | ||||
| 			default: "" | ||||
| 			default: "", | ||||
| 		}, | ||||
| 		location: { | ||||
| 			type: String, | ||||
| 			default: "" | ||||
| 			default: "", | ||||
| 		}, | ||||
| 		occupation: { | ||||
| 			type: String, | ||||
| 			default: "" | ||||
| 			default: "", | ||||
| 		}, | ||||
| 		website: { | ||||
| 			type: String, | ||||
| 			default: "" | ||||
| 			default: "", | ||||
| 		}, | ||||
| 		blog: { | ||||
| 			type: String, | ||||
| 			default: "" | ||||
| 			default: "", | ||||
| 		}, | ||||
| 		views: { | ||||
| 			type: Number, | ||||
| 			min: 0, | ||||
| 			default: 0 | ||||
| 			default: 0, | ||||
| 		}, | ||||
| 		lastWhere: { | ||||
| 			type: String, | ||||
| 			default: null | ||||
| 			default: null, | ||||
| 		}, | ||||
| 		hidden: { | ||||
| 			type: Boolean, | ||||
| 			default: false | ||||
| 			default: false, | ||||
| 		}, | ||||
| 		disclaimer: { | ||||
| 			type: String, | ||||
| 			default: "" | ||||
| 			default: "", | ||||
| 		}, | ||||
| 		showEmail: { | ||||
| 			type: Boolean, | ||||
| 			default: false | ||||
| 		} | ||||
| 			default: false, | ||||
| 		}, | ||||
| 	}, | ||||
| 	biffno: { | ||||
| 		years: [{ | ||||
| 			type: String | ||||
| 		}], | ||||
| 		years: [ | ||||
| 			{ | ||||
| 				type: String, | ||||
| 			}, | ||||
| 		], | ||||
| 		wins: { | ||||
| 			type: Number, | ||||
| 			default: 0 | ||||
| 		} | ||||
| 			default: 0, | ||||
| 		}, | ||||
| 	}, | ||||
| 	favs: { | ||||
| 		authors: [{ | ||||
| 		authors: [ | ||||
| 			{ | ||||
| 				type: Number, | ||||
| 			ref: "User" | ||||
| 		}], | ||||
| 		stories: [{ | ||||
| 				ref: "User", | ||||
| 			}, | ||||
| 		], | ||||
| 		stories: [ | ||||
| 			{ | ||||
| 				type: Number, | ||||
| 			ref: "Story" | ||||
| 		}] | ||||
| 				ref: "Story", | ||||
| 			}, | ||||
| 		], | ||||
| 	}, | ||||
| 	subscriptions: { | ||||
| 		authors:  [{ | ||||
| 		authors: [ | ||||
| 			{ | ||||
| 				type: Number, | ||||
| 			ref: "User" | ||||
| 		}], | ||||
| 		stories: [{ | ||||
| 			type: Number, | ||||
| 			ref: "Story" | ||||
| 		}], | ||||
| 		bands: [{ | ||||
| 			type: Number, | ||||
| 			ref: "Band" | ||||
| 		}] | ||||
| 				ref: "User", | ||||
| 			}, | ||||
| 	hiddenBands: [{ | ||||
| 		], | ||||
| 		stories: [ | ||||
| 			{ | ||||
| 				type: Number, | ||||
| 		ref: "Band" | ||||
| 	}], | ||||
| 	hiddenAuthors: [{ | ||||
| 				ref: "Story", | ||||
| 			}, | ||||
| 		], | ||||
| 		bands: [ | ||||
| 			{ | ||||
| 				type: Number, | ||||
| 		ref: "User" | ||||
| 	}], | ||||
| 	blocked: [{ | ||||
| 				ref: "Band", | ||||
| 			}, | ||||
| 		], | ||||
| 	}, | ||||
| 	hiddenBands: [ | ||||
| 		{ | ||||
| 			type: Number, | ||||
| 		ref: "User" | ||||
| 	}], | ||||
| 			ref: "Band", | ||||
| 		}, | ||||
| 	], | ||||
| 	hiddenAuthors: [ | ||||
| 		{ | ||||
| 			type: Number, | ||||
| 			ref: "User", | ||||
| 		}, | ||||
| 	], | ||||
| 	notifyOnReviewReply: { | ||||
| 		type: Boolean, | ||||
| 		default: true, | ||||
| 	}, | ||||
| 	blocked: [ | ||||
| 		{ | ||||
| 			type: Number, | ||||
| 			ref: "User", | ||||
| 		}, | ||||
| 	], | ||||
| 	sessionId: { | ||||
| 		type: String, | ||||
| 		default: null | ||||
| 		default: null, | ||||
| 	}, | ||||
| 	banned: { | ||||
| 		type: Boolean, | ||||
| 		default: false | ||||
| 		default: false, | ||||
| 	}, | ||||
| 	quickMenuConfig: [QuickMenuSchema] | ||||
| }) | ||||
| 	quickMenuConfig: [QuickMenuSchema], | ||||
| }); | ||||
| 
 | ||||
| UserSchema.static("generateHash", function (password: string): string { | ||||
| 	return bcrypt.hashSync(password, bcrypt.genSaltSync(8)); | ||||
| }); | ||||
| UserSchema.methods.validPassword = function (password: string): boolean { | ||||
| 	return md5(password) === this.password || bcrypt.compareSync(password, this.password) || false; | ||||
| } | ||||
| 	return ( | ||||
| 		md5(password) === this.password || | ||||
| 		bcrypt.compareSync(password, this.password) || | ||||
| 		false | ||||
| 	); | ||||
| }; | ||||
| 
 | ||||
| UserSchema.methods.generateToken = function (jwtKey: string): string { | ||||
| 	let token = jwt.sign({id: this._id, isAdmin: this.profile.isAdmin}, jwtKey, { | ||||
| 		expiresIn: '14 days' | ||||
| 	}) | ||||
| 	return token | ||||
| } | ||||
| 	let token = jwt.sign( | ||||
| 		{ id: this._id, isAdmin: this.profile.isAdmin }, | ||||
| 		jwtKey, | ||||
| 		{ | ||||
| 			expiresIn: "14 days", | ||||
| 		}, | ||||
| 	); | ||||
| 	return token; | ||||
| }; | ||||
| 
 | ||||
| hasMigrated && UserSchema.plugin(AutoIncrement, {id: "userid"}) | ||||
| export const User = mongoose.model<IUser, UModel>("User", UserSchema, "users") | ||||
| hasMigrated && | ||||
| 	UserSchema.plugin(AutoIncrement, { id: "userid", inc_field: "_id" }); | ||||
| export const User = mongoose.model<IUser, UModel>("User", UserSchema, "users"); | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user