({
ctx,
input,
}: InferProcedureOpts<typeof baseProcedure>)
| 27 | export const deleteProcedure = once(() => baseProcedure.mutation(delete_)); |
| 28 | |
| 29 | export async function delete_({ |
| 30 | ctx, |
| 31 | input, |
| 32 | }: InferProcedureOpts<typeof baseProcedure>) { |
| 33 | return await ctx.usingLocks( |
| 34 | [[`user-lock:${ctx.userId}`]], |
| 35 | async (signals) => { |
| 36 | return await ctx.dataAbstraction.transaction(async (dtrx) => { |
| 37 | // Assert correct password |
| 38 | |
| 39 | await ctx.assertCorrectUserPassword({ |
| 40 | userId: ctx.userId, |
| 41 | loginHash: input.loginHash, |
| 42 | }); |
| 43 | |
| 44 | // Check if any group has more than one member |
| 45 | |
| 46 | const memberships = await GroupMemberModel.query(dtrx.trx) |
| 47 | .where('group_members.user_id', ctx.userId) |
| 48 | .leftJoin( |
| 49 | GroupMemberModel.query(dtrx.trx) |
| 50 | .groupBy('group_id') |
| 51 | .select('group_id') |
| 52 | .count('* as member_count') |
| 53 | .as('member_counts'), |
| 54 | 'group_members.group_id', |
| 55 | 'member_counts.group_id', |
| 56 | ) |
| 57 | .leftJoin( |
| 58 | GroupMemberModel.query(dtrx.trx) |
| 59 | .where('role', 'owner') |
| 60 | .groupBy('group_id') |
| 61 | .select('group_id') |
| 62 | .count('* as owner_count') |
| 63 | .as('owner_counts'), |
| 64 | 'group_members.group_id', |
| 65 | 'owner_counts.group_id', |
| 66 | ) |
| 67 | .select( |
| 68 | 'group_members.group_id', |
| 69 | raw('COALESCE(member_counts.member_count, 0) as member_count'), |
| 70 | raw('COALESCE(owner_counts.owner_count, 0) as owner_count'), |
| 71 | ); |
| 72 | |
| 73 | if ( |
| 74 | memberships.some( |
| 75 | (count) => |
| 76 | (count as any).member_count > 1 && |
| 77 | (count as any).owner_count <= 1, |
| 78 | ) |
| 79 | ) { |
| 80 | throw new TRPCError({ |
| 81 | message: |
| 82 | 'Some groups would be left without an owner. Transfer ownership before deleting your account.', |
| 83 | code: 'BAD_REQUEST', |
| 84 | }); |
| 85 | } |
| 86 |
nothing calls this directly
no test coverage detected