I am writing a API endpoint in NodeJs, The code is roughly like this:
function myApi(myUserId, userIdToDelete){
if ( checkIfIAmAdmin(myUserId) ) {
deleteUser(userIdToDelete);
}
}
Now, checkIfIAmAdmin() and deletUser() perform database operations, but as you can see, not atomically. This means that between the executions of the two functions, the current user may change its permission to a lower one, hence deleting a user while not being admin.
Thi is conceptually erroneous (in this simple example) but it may be acceptable since all of this happens in few milliseconds or we can just accept the fact that "if the user was an administrator in a time not too long ago (milliseconds), it's still okay to consider them admin"
I'd like to develop this API in the most clean and right way (I would like to know the best practices). What do you suggest me to do?