|
7 | 7 | */ |
8 | 8 |
|
9 | 9 | import User from "../models/user.model.js"; |
| 10 | +import Claim from "../models/claim.model.js"; |
| 11 | +import Report from "../models/report.model.js"; |
| 12 | +import { deleteFile } from "../utils/s3.utils.js"; |
10 | 13 | import { withQueryTimeout } from "../middlewares/queryTimeout.middleware.js"; |
| 14 | +import { clearCachePattern } from "../utils/redisClient.js"; |
11 | 15 | import { paginationMeta } from "../utils/helpers.js"; |
12 | 16 |
|
13 | 17 | /** |
@@ -106,3 +110,73 @@ export const toggleBlacklist = async (req, res) => { |
106 | 110 | return res.status(500).json({ message: "Internal server error" }); |
107 | 111 | } |
108 | 112 | }; |
| 113 | + |
| 114 | +/** |
| 115 | + * Permanently delete a user account and cascade-remove all their data. |
| 116 | + * |
| 117 | + * Cascade order: |
| 118 | + * 1. Delete all reports by the user (best-effort ImageKit photo cleanup per report). |
| 119 | + * 2. Delete all claims by the user. |
| 120 | + * 3. Clear per-user Redis caches. |
| 121 | + * 4. Delete the User document. |
| 122 | + * |
| 123 | + * Admins cannot delete other admin accounts. |
| 124 | + * An admin cannot delete their own account via this endpoint. |
| 125 | + * |
| 126 | + * @route DELETE /admin/users/:id |
| 127 | + * @access Protected — admins only |
| 128 | + */ |
| 129 | +export const deleteUser = async (req, res) => { |
| 130 | + try { |
| 131 | + const { id } = req.params; |
| 132 | + |
| 133 | + if (id === req.user.id) { |
| 134 | + return res |
| 135 | + .status(403) |
| 136 | + .json({ message: "You cannot delete your own account." }); |
| 137 | + } |
| 138 | + |
| 139 | + const user = await withQueryTimeout(User.findById(id)); |
| 140 | + if (!user) return res.status(404).json({ message: "User not found" }); |
| 141 | + |
| 142 | + if (user.isAdmin) { |
| 143 | + return res |
| 144 | + .status(403) |
| 145 | + .json({ message: "Cannot delete an admin account." }); |
| 146 | + } |
| 147 | + |
| 148 | + // 1. Delete all reports owned by this user, cleaning up ImageKit photos. |
| 149 | + const reports = await withQueryTimeout( |
| 150 | + Report.find({ user: id }).select("photos").lean(), |
| 151 | + ); |
| 152 | + for (const report of reports) { |
| 153 | + for (const photo of report.photos || []) { |
| 154 | + if (photo.fileId) { |
| 155 | + try { |
| 156 | + await deleteFile(photo.fileId); |
| 157 | + } catch (err) { |
| 158 | + console.error("ImageKit photo cleanup failed:", err.message); |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + await Report.deleteMany({ user: id }); |
| 164 | + |
| 165 | + // 2. Delete all claims made by this user. |
| 166 | + await Claim.deleteMany({ claimant: id }); |
| 167 | + |
| 168 | + // 3. Clear caches. |
| 169 | + await clearCachePattern(`user:${id}:*`); |
| 170 | + await clearCachePattern(`user:profile:${id}`); |
| 171 | + |
| 172 | + // 4. Delete the user. |
| 173 | + await User.findByIdAndDelete(id); |
| 174 | + |
| 175 | + return res.status(200).json({ |
| 176 | + message: `User "${user.name}" and all associated data have been deleted.`, |
| 177 | + }); |
| 178 | + } catch (error) { |
| 179 | + console.error("deleteUser error:", error); |
| 180 | + return res.status(500).json({ message: "Internal server error" }); |
| 181 | + } |
| 182 | +}; |
0 commit comments