1- const Follower = require ( "../../models/auth/followerModel" )
21const User = require ( "../../models/auth/userModel" )
3- const ChatMessage = require ( "../../models/chat/chatMessageModel" )
4- const Chat = require ( "../../models/chat/chatModel" )
5- const Comment = require ( "../../models/post/commentModel" )
6- const LikeDislike = require ( "../../models/post/likeDislikeModel" )
7- const Post = require ( "../../models/post/postModel" )
8- const { Op } = require ( "sequelize" )
9- const Story = require ( "../../models/Story/StoryModal" )
102const crypto = require ( "crypto" )
113const OTP = require ( "../../models/auth/otpModel" )
124const { otpForDeleteMailSender, accountDeletedMailSender } = require ( "../../utils/resend" )
135const sequelize = require ( "../../utils/sequelize" )
14- const Playlist = require ( "../../models/music/playlist" )
156
167const generateSixDigitOTP = ( ) => {
178 return crypto . randomInt ( 100000 , 999999 )
@@ -50,21 +41,16 @@ const deleteUser = async (req, res) => {
5041 const { userid } = req . user
5142 const { otp } = req . body
5243
53- // Uncomment if guest user deletion is restricted
5444 if ( req . user . role === "guest" ) {
5545 return res . status ( 403 ) . json ( { message : "Guest account cannot be deleted" } )
5646 }
5747
58- const transaction = await sequelize . transaction ( )
59-
6048 try {
6149 const user = await User . findByPk ( userid )
6250 if ( ! user ) {
6351 return res . status ( 404 ) . json ( { message : "User not found" } )
6452 }
6553
66- console . log ( user . email , otp )
67-
6854 const otpEntry = await OTP . findOne ( { where : { email : user . email , otp } } )
6955
7056 if ( ! otpEntry ) {
@@ -76,39 +62,22 @@ const deleteUser = async (req, res) => {
7662 return res . status ( 400 ) . json ( { message : "OTP expired" } )
7763 }
7864
79- // Perform deletions within the transaction
80- await Follower . destroy ( { where : { followerid : userid } , transaction } )
81- await Follower . destroy ( { where : { followid : userid } , transaction } )
82- await ChatMessage . destroy ( { where : { senderid : userid } , transaction } )
83- await Comment . destroy ( { where : { createdby : userid } , transaction } )
84- await LikeDislike . destroy ( { where : { userid : userid } , transaction } )
85- await Post . destroy ( { where : { createdby : userid } , transaction } )
86- await Story . destroy ( { where : { createdby : userid } , transaction } )
87- await OTP . destroy ( { where : { email : user . email } , transaction } )
88- await Playlist . destroy ( { where : { userId : userid } , transaction } )
89- // await PlaylistSong.destroy({ where: { userId: userid }, transaction });
90-
91- await Chat . destroy ( {
92- where : {
93- participants : {
94- [ Op . contains ] : [ userid ] ,
95- } ,
96- } ,
65+ const userEmail = user . email
66+ const tableName = User . getTableName ( )
67+
68+ const transaction = await sequelize . transaction ( )
69+ await sequelize . query ( `ALTER TABLE "${ tableName } " DISABLE TRIGGER ALL` , { transaction } )
70+ await sequelize . query ( `DELETE FROM "${ tableName } " WHERE userid = :userid` , {
71+ replacements : { userid } ,
9772 transaction,
9873 } )
99-
100- await User . destroy ( { where : { userid } , transaction } )
101-
102- // Commit transaction
74+ await sequelize . query ( `ALTER TABLE "${ tableName } " ENABLE TRIGGER ALL` , { transaction } )
10375 await transaction . commit ( )
10476
105- await accountDeletedMailSender ( user . email )
77+ await accountDeletedMailSender ( userEmail )
10678
10779 return res . status ( 200 ) . json ( { message : "Success" } )
10880 } catch ( error ) {
109- if ( transaction ) {
110- await transaction . rollback ( )
111- }
11281 console . error ( "Error deleting user:" , error )
11382 return res . status ( 500 ) . json ( { message : "Internal server error" } )
11483 }
0 commit comments