import type { PrismaClient } from "./client.js"; type AccountDeleteDb = Pick; /** Spaces whose team sandboxes and homes may be torn down when this user deletes their account. */ export async function spaceIdsForSandboxTeardownAfterAccountDelete( prisma: AccountDeleteDb, input: { userId: string; spaceIds: Iterable }, ): Promise { const uniqueSpaceIds = [...new Set(input.spaceIds)]; if (!uniqueSpaceIds.length) return []; const eligible: string[] = []; for (const spaceId of uniqueSpaceIds) { const membership = await prisma.spaceMember.findUnique({ where: { spaceId_userId: { spaceId, userId: input.userId } }, select: { role: true }, }); if (membership?.role !== "owner") continue; const [otherMembers, otherBots] = await Promise.all([ prisma.spaceMember.count({ where: { spaceId, userId: { not: input.userId } }, }), prisma.bot.count({ where: { spaceId, userId: { not: input.userId } }, }), ]); if (otherMembers === 0 && otherBots === 0) eligible.push(spaceId); } return eligible; }