Skip to content

Commit

Permalink
Improve user deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mohr committed Jul 13, 2024
1 parent 3a7a8a5 commit 0c4f15a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
25 changes: 22 additions & 3 deletions src/deluser.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,32 @@ const stop = (code) => {

const username = await rl.question('Enter a username: ');
const exists = await users.exists(username);
if (!exists) {

let onlyData = false;
if (exists) {
const keepAccount = await rl.question('Keep account (y/n): ');
const onlyDataMap = {
y: true,
n: false
};
if (!(keepAccount in onlyDataMap)) {
console.error('Invalid input, must be y or n');
stop(1);
}
onlyData = onlyDataMap[keepAccount];
}
else {
console.warn('User might be a Google User, trying to delete the corresponding data');
}

try {
await users.remove(username);
console.log('User (data) deleted!');
await users.remove(username, onlyData);
if (onlyData) {
console.log('Workspace cleared!');
}
else {
console.log('User deleted and workspace cleared!');
}
stop(0);
} catch (err) {
console.error(err);
Expand Down
10 changes: 6 additions & 4 deletions src/models/userstore.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,15 @@ export default class UserStore {
return await this.db.findAsync({}).sort({ name: 1 });
}

async remove(name) {
async remove(name, onlyData = false) {
let user = await this.get(name);
let user_id;
if (user !== null) {
user_id = user._id;
await this.db.removeAsync({ _id: user_id});
console.log('- Removed user from database');
if (!onlyData) {
await this.db.removeAsync({ _id: user_id});
console.log('- Removed user from database');
}
}
else {
user_id = name;
Expand Down Expand Up @@ -155,7 +157,7 @@ export default class UserStore {
await jobDb.removeAsync({user_id});
console.log('- Removed batch jobs from database');

await Promise.all(jobs.map(job => job.removeResults(job._id)));
await Promise.all(jobs.map(job => jobModel.removeResults(job._id)));
console.log('- Removed batch job results and logs from file system');
} catch (err) {
console.error('- Error removing jobs:', err);
Expand Down

0 comments on commit 0c4f15a

Please sign in to comment.