Spaces:
Sleeping
Sleeping
Commit
·
f4fe35f
1
Parent(s):
1562ed3
update(models): add schema middleware
Browse files
src/common/models/user.model.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
import mongoose from "mongoose";
|
2 |
import bcrypt from "bcrypt";
|
3 |
import { AuthenticatableType } from "@common/enums/authenticatable-type.enum";
|
4 |
import { FitnessGoal } from "@common/enums/fitness-goal.enum";
|
@@ -93,10 +93,33 @@ const userSchema = new Schema({
|
|
93 |
});
|
94 |
|
95 |
userSchema.pre("save", async function (next) {
|
96 |
-
|
|
|
|
|
|
|
|
|
|
|
97 |
next();
|
98 |
});
|
99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
export type UserDocument = IUser & mongoose.Document;
|
101 |
|
102 |
export const User = mongoose.model<UserDocument>("users", userSchema);
|
|
|
1 |
+
import mongoose, { UpdateQuery } from "mongoose";
|
2 |
import bcrypt from "bcrypt";
|
3 |
import { AuthenticatableType } from "@common/enums/authenticatable-type.enum";
|
4 |
import { FitnessGoal } from "@common/enums/fitness-goal.enum";
|
|
|
93 |
});
|
94 |
|
95 |
userSchema.pre("save", async function (next) {
|
96 |
+
if (this.isModified("password")) {
|
97 |
+
this.password = await bcrypt.hash(this.password, saltrounds);
|
98 |
+
}
|
99 |
+
if (this.isModified("email")) {
|
100 |
+
this.email = this.email.toLowerCase();
|
101 |
+
}
|
102 |
next();
|
103 |
});
|
104 |
|
105 |
+
userSchema.pre(["updateOne", "findOneAndUpdate"], async function () {
|
106 |
+
const data = this.getUpdate() as UpdateQuery<IUser>;
|
107 |
+
if (data.password) {
|
108 |
+
data.password = await bcrypt.hash(data.password, saltrounds);
|
109 |
+
}
|
110 |
+
if (data.email) {
|
111 |
+
data.email = data.email.toLowerCase();
|
112 |
+
}
|
113 |
+
});
|
114 |
+
|
115 |
+
// pre find make email case insensitive
|
116 |
+
userSchema.pre(["find", "findOne"], function () {
|
117 |
+
const query = this.getQuery();
|
118 |
+
if (query.email) {
|
119 |
+
query.email = query.email.toLowerCase();
|
120 |
+
}
|
121 |
+
});
|
122 |
+
|
123 |
export type UserDocument = IUser & mongoose.Document;
|
124 |
|
125 |
export const User = mongoose.model<UserDocument>("users", userSchema);
|
src/modules/console/common/models/admin.model.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
import mongoose from "mongoose";
|
2 |
import bcrypt from "bcrypt";
|
3 |
import { config } from "../../../../configs/config";
|
4 |
import { Role } from "@common/enums/role.enum";
|
@@ -29,10 +29,33 @@ const AdminSchema = new Schema({
|
|
29 |
});
|
30 |
|
31 |
AdminSchema.pre("save", async function (next) {
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
33 |
next();
|
34 |
});
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
export type AdminDocument = mongoose.Document & IAdmin;
|
37 |
|
38 |
export const Admin = mongoose.model<AdminDocument>("admins", AdminSchema);
|
|
|
1 |
+
import mongoose, { UpdateQuery } from "mongoose";
|
2 |
import bcrypt from "bcrypt";
|
3 |
import { config } from "../../../../configs/config";
|
4 |
import { Role } from "@common/enums/role.enum";
|
|
|
29 |
});
|
30 |
|
31 |
AdminSchema.pre("save", async function (next) {
|
32 |
+
if (this.isModified("password")) {
|
33 |
+
this.password = await bcrypt.hash(this.password, saltrounds);
|
34 |
+
}
|
35 |
+
if (this.isModified("email")) {
|
36 |
+
this.email = this.email.toLowerCase();
|
37 |
+
}
|
38 |
next();
|
39 |
});
|
40 |
|
41 |
+
AdminSchema.pre(["updateOne", "findOneAndUpdate"], async function () {
|
42 |
+
const data = this.getUpdate() as UpdateQuery<IAdmin>;
|
43 |
+
if (data.password) {
|
44 |
+
data.password = await bcrypt.hash(data.password, saltrounds);
|
45 |
+
}
|
46 |
+
if (data.email) {
|
47 |
+
data.email = data.email.toLowerCase();
|
48 |
+
}
|
49 |
+
});
|
50 |
+
|
51 |
+
// pre find make email case insensitive
|
52 |
+
AdminSchema.pre(["find", "findOne"], function () {
|
53 |
+
const query = this.getQuery();
|
54 |
+
if (query.email) {
|
55 |
+
query.email = query.email.toLowerCase();
|
56 |
+
}
|
57 |
+
});
|
58 |
+
|
59 |
export type AdminDocument = mongoose.Document & IAdmin;
|
60 |
|
61 |
export const Admin = mongoose.model<AdminDocument>("admins", AdminSchema);
|