From 9f37f359267466eb514a0b2e97d730a8634d90d7 Mon Sep 17 00:00:00 2001 From: Cody Seibert Date: Wed, 30 Oct 2024 09:25:22 -0400 Subject: [PATCH] improve delete logic --- convex/users.ts | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/convex/users.ts b/convex/users.ts index 932b42e..8943cc6 100644 --- a/convex/users.ts +++ b/convex/users.ts @@ -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); @@ -132,27 +131,30 @@ export const deleteUserById = authenticatedMutation({ .filter((q) => q.eq(q.field("userId"), userId)) .collect(); - const promises: Promise[] = []; + const promises: Promise[] = []; 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)) @@ -164,6 +166,6 @@ export const deleteUserById = authenticatedMutation({ promises.push(ctx.db.delete(userId)); - await Promise.all(promises); + await Promise.all(promises); }, });