File tree Expand file tree Collapse file tree 1 file changed +9
-6
lines changed Expand file tree Collapse file tree 1 file changed +9
-6
lines changed Original file line number Diff line number Diff line change 1
1
import UserModel from "../models/User.js" ;
2
- import bcrypt from ' bcrypt' ;
2
+ import bcrypt from " bcrypt" ;
3
3
import {
4
4
handleServerError ,
5
5
handleSuccess ,
6
6
handleNotFound ,
7
7
handleBadRequest ,
8
8
} from "../utils/handlers.js" ;
9
+ import MessageModel from "../models/Message.js" ;
9
10
10
11
export const getUserById = async ( req , res ) => {
11
12
try {
@@ -137,20 +138,22 @@ export const deleteUser = async (req, res) => {
137
138
try {
138
139
const { userId } = req . params ;
139
140
140
- const result = await UserModel . findByIdAndDelete ( userId ) ;
141
+ const user = await UserModel . findById ( userId ) ;
141
142
142
- if ( ! result ) {
143
+ if ( ! user ) {
143
144
return handleNotFound ( res , "User not found" ) ;
144
145
}
145
146
146
- // await user.remove();
147
+ await MessageModel . deleteMany ( { _id : { $in : user . messages } } ) ;
148
+ await UserModel . findByIdAndDelete ( userId ) ;
147
149
148
- handleSuccess ( res , { message : "User successfully deleted" } ) ;
150
+ handleSuccess ( res , {
151
+ message : "User and their messages successfully deleted" ,
152
+ } ) ;
149
153
} catch ( err ) {
150
154
handleServerError ( res , err ) ;
151
155
}
152
156
} ;
153
-
154
157
export const toggleNotifyFriends = async ( req , res ) => {
155
158
try {
156
159
const { userId } = req . params ;
You can’t perform that action at this time.
0 commit comments