From 866f0144d7f7a063cc53aad81fdda4361279bbe3 Mon Sep 17 00:00:00 2001 From: Sebastian Webster Date: Sun, 10 Sep 2023 20:53:53 +1200 Subject: [PATCH] temp/deleteaccount API now aborts transactions on error and ends session --- controllers/Temp.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/controllers/Temp.js b/controllers/Temp.js index 813a4d1a..c8b8502f 100644 --- a/controllers/Temp.js +++ b/controllers/Temp.js @@ -5437,10 +5437,22 @@ class TempController { User.deleteOne({_id: {$eq: userId}}) ]).then(() => session.commitTransaction()).then(() => { console.log('User with id:', userId, 'has been successfully deleted along with all associated data.') - return resolve(HTTPWTHandler.OK('Successfully deleted account and all associated data.')) + session.endSession().catch(error => { + console.error('An error occurred while ending session after deleting account. The error was:', error) + }).finally(() => { + return resolve(HTTPWTHandler.OK('Successfully deleted account and all associated data.')) + }) }).catch(error => { - console.error('An error occured while deleting account data for user with id:', userId, '. The error was:', error) - return resolve(HTTPWTHandler.serverError('An error occurred while deleting data. Please try again.')) + session.abortTransaction().catch(error => { + console.error('An error occurred while aborting transaction after deleting account. The error was:', error) + }).finally(() => { + session.endSession().catch(error => { + console.error('An error occurred while ending session after deleting account data. The error was:', error) + }).finally(() => { + console.error('An error occured while deleting account data for user with id:', userId, '. The error was:', error) + return resolve(HTTPWTHandler.serverError('An error occurred while deleting data. Please try again.')) + }) + }) }) }).catch(error => { console.log('An error occurred while starting Mongoose session. The error was:', error)