(req: MonkeyRequest)
| 258 | } |
| 259 | |
| 260 | export async function deleteUser(req: MonkeyRequest): Promise<MonkeyResponse> { |
| 261 | const { uid } = req.ctx.decodedToken; |
| 262 | |
| 263 | const { data: userInfo, error } = await tryCatch( |
| 264 | UserDAL.getPartialUser(uid, "delete user", [ |
| 265 | "banned", |
| 266 | "name", |
| 267 | "email", |
| 268 | "discordId", |
| 269 | ]), |
| 270 | ); |
| 271 | |
| 272 | if (error) { |
| 273 | if (error instanceof MonkeyError && error.status === 404) { |
| 274 | //userinfo was already deleted. We ignore this and still try to remove the other data |
| 275 | } else { |
| 276 | throw error; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | if (userInfo?.banned === true) { |
| 281 | await BlocklistDal.add(userInfo); |
| 282 | } |
| 283 | |
| 284 | //cleanup database |
| 285 | await Promise.all([ |
| 286 | UserDAL.deleteUser(uid), |
| 287 | deleteUserLogs(uid), |
| 288 | deleteAllApeKeys(uid), |
| 289 | deleteAllPresets(uid), |
| 290 | deleteConfig(uid), |
| 291 | deleteAllResults(uid), |
| 292 | purgeUserFromDailyLeaderboards( |
| 293 | uid, |
| 294 | req.ctx.configuration.dailyLeaderboards, |
| 295 | ), |
| 296 | purgeUserFromXpLeaderboards( |
| 297 | uid, |
| 298 | req.ctx.configuration.leaderboards.weeklyXp, |
| 299 | ), |
| 300 | ConnectionsDal.deleteByUid(uid), |
| 301 | ]); |
| 302 | |
| 303 | try { |
| 304 | //delete user from firebase |
| 305 | await AuthUtil.deleteUser(uid); |
| 306 | } catch (e) { |
| 307 | if (isFirebaseError(e) && e.errorInfo.code === "auth/user-not-found") { |
| 308 | //user was already deleted, ok to ignore |
| 309 | } else { |
| 310 | throw e; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | void addImportantLog( |
| 315 | "user_deleted", |
| 316 | `${userInfo?.email} ${userInfo?.name}`, |
| 317 | uid, |
nothing calls this directly
no test coverage detected