Skip to content

Commit

Permalink
feat: updated delete user to use promises rather than multiple awaits
Browse files Browse the repository at this point in the history
  • Loading branch information
edinstance committed Oct 27, 2024
1 parent b73c567 commit 03317aa
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions convex/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,23 +131,26 @@ export const deleteUserById = authenticatedMutation({
.query("userResults")
.filter((q) => q.eq(q.field("userId"), userId))
.collect();

const promises: Promise<void>[] = [];

for (const result of userResults) {
await ctx.db.delete(result._id);
promises.push(ctx.db.delete(result._id));
}

const maps = await ctx.db
.query("maps")
.filter((q) => q.eq(q.field("submittedBy"), userId))
.collect();
for (const map of maps) {
await ctx.db.patch(map._id, { submittedBy: undefined });
promises.push(ctx.db.patch(map._id, { submittedBy: undefined }));
}

const authAccounts = await ctx.db.query("authAccounts").filter(q => q.eq(q.field("userId"), userId)).collect();

// Iterate over the queried documents and delete each one
for (const account of authAccounts) {
await ctx.db.delete(account._id);
promises.push(ctx.db.delete(account._id));
}

const admin = await ctx.db
Expand All @@ -156,9 +159,11 @@ export const deleteUserById = authenticatedMutation({
.first();

if (admin) {
await ctx.db.delete(admin._id);
promises.push(ctx.db.delete(admin._id));
}

await ctx.db.delete(userId);
promises.push(ctx.db.delete(userId));

await Promise.all(promises);
},
});

0 comments on commit 03317aa

Please sign in to comment.