Skip to content

Commit

Permalink
improve delete logic
Browse files Browse the repository at this point in the history
  • Loading branch information
webdevcody committed Oct 30, 2024
1 parent 144d7cc commit 9f37f35
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions convex/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ export const authenticatedMutation = customMutation(
);

export const deleteUserById = authenticatedMutation({
handler: async (ctx, args) => {

handler: async (ctx) => {
const userId = ctx.userId;
if (userId === null) {
throw new ConvexError(SIGN_IN_ERROR_MESSAGE);
Expand All @@ -132,27 +131,30 @@ export const deleteUserById = authenticatedMutation({
.filter((q) => q.eq(q.field("userId"), userId))
.collect();

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

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

const maps = await ctx.db
.query("maps")
.filter((q) => q.eq(q.field("submittedBy"), userId))
const authAccounts = await ctx.db
.query("authAccounts")
.filter((q) => q.eq(q.field("userId"), userId))
.collect();
for (const map of maps) {
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) {
promises.push(ctx.db.delete(account._id));
}

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

for (const session of authSessions) {
promises.push(ctx.db.delete(session._id));
}

const admin = await ctx.db
.query("admins")
.withIndex("by_userId", (q) => q.eq("userId", userId))
Expand All @@ -164,6 +166,6 @@ export const deleteUserById = authenticatedMutation({

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

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

0 comments on commit 9f37f35

Please sign in to comment.