From 4377468b8a9c6094a6d113747e3d1e3c52c40659 Mon Sep 17 00:00:00 2001 From: himanshu goyal Date: Sat, 15 Feb 2025 14:53:45 +0530 Subject: [PATCH 1/2] add deketeuser mutation --- graphql/resolvers.js | 66 ++++++++++++++++++++++++++++++++++++++++++++ graphql/schema.js | 3 +- 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/graphql/resolvers.js b/graphql/resolvers.js index 0bca239..4523d66 100644 --- a/graphql/resolvers.js +++ b/graphql/resolvers.js @@ -628,6 +628,72 @@ const resolvers = { return user; }, + + deleteUser: async (_, { credentials }) => { + try { + const userToDelete = await User.findOne({ email: credentials.email }); + console.log("User to delete:", userToDelete); + if (!userToDelete) { + throw new UserInputError("User not found"); + } + + // Verify email matches + if (userToDelete.email !== credentials.email) { + throw new AuthenticationError("Email does not match"); + } + + // Verify password + const validPassword = await bcrypt.compare(credentials.password, userToDelete.password); + if (!validPassword) { + throw new AuthenticationError("Invalid password"); + } + + // Handle groups + const userGroups = await Group.find({ + $or: [{ leader: userToDelete.id }, { members: userToDelete.id }], + }); + + for (const group of userGroups) { + if (group.leader.toString() === userToDelete.id) { + // If user is leader, delete the group and all its beacons + const groupBeacons = await Beacon.find({ group: group._id }); + for (const beacon of groupBeacons) { + // Delete associated landmarks + await Landmark.deleteMany({ _id: { $in: beacon.landmarks } }); + // Remove beacon reference from all followers + await User.updateMany( + { _id: { $in: beacon.followers } }, + { $pull: { beacons: beacon._id } } + ); + } + // Delete all beacons in the group + await Beacon.deleteMany({ group: group._id }); + // Delete the group + await Group.findByIdAndDelete(group._id); + } else { + // If user is member, remove from group + await Group.findByIdAndUpdate(group._id, { $pull: { members: userToDelete.id } }); + } + } + + // Handle beacons where user is a follower + await Beacon.updateMany({ followers: userToDelete.id }, { $pull: { followers: userToDelete.id } }); + + // Delete landmarks created by user + await Landmark.deleteMany({ createdBy: userToDelete.id }); + + // Finally delete the user + await User.findByIdAndDelete(userToDelete.id); + + return true; + } catch (error) { + console.error("Error deleting user:", error); + if (error instanceof AuthenticationError || error instanceof UserInputError) { + throw error; + } + return false; + } + }, }, ...(process.env._HANDLER == null && { Subscription: { diff --git a/graphql/schema.js b/graphql/schema.js index 16e5b0f..14304a8 100644 --- a/graphql/schema.js +++ b/graphql/schema.js @@ -158,6 +158,7 @@ const typeDefs = gql` joinGroup(shortcode: String!): Group! deleteBeacon(id: ID!): Boolean! sos(id: ID!): User! + deleteUser(credentials: AuthPayload!): Boolean! } type Subscription { @@ -173,4 +174,4 @@ const typeDefs = gql` } `; -module.exports = typeDefs; +module.exports = typeDefs; \ No newline at end of file From 35f522337df5a3884cdf7ab9cffac609807c64b5 Mon Sep 17 00:00:00 2001 From: himanshu goyal Date: Sat, 15 Feb 2025 14:58:06 +0530 Subject: [PATCH 2/2] Format graphql/schema.js with Prettier --- graphql/schema.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphql/schema.js b/graphql/schema.js index 14304a8..efb7bb2 100644 --- a/graphql/schema.js +++ b/graphql/schema.js @@ -174,4 +174,4 @@ const typeDefs = gql` } `; -module.exports = typeDefs; \ No newline at end of file +module.exports = typeDefs;