Hook up teamMemberDelete and teamDelete.

This commit is contained in:
Brian Cao 2023-03-09 23:21:19 -08:00
parent c3426a67ee
commit 701bde53b7
7 changed files with 150 additions and 21 deletions

View file

@ -1,5 +1,6 @@
import { Prisma, Team } from '@prisma/client';
import cache from 'lib/cache';
import { ROLES } from 'lib/constants';
import prisma from 'lib/prisma';
import { Website, User, Roles } from 'lib/types';
@ -134,6 +135,19 @@ export async function deleteUser(
websiteIds = websites.map(a => a.id);
}
const teams = await client.team.findMany({
where: {
teamUser: {
some: {
userId,
role: ROLES.teamOwner,
},
},
},
});
const teamIds = teams.map(a => a.id);
return prisma
.transaction([
client.websiteEvent.deleteMany({
@ -144,21 +158,39 @@ export async function deleteUser(
}),
client.teamWebsite.deleteMany({
where: {
website: {
userId,
OR: [
{
websiteId: {
in: websiteIds,
},
},
{
teamId: {
in: teamIds,
},
},
],
},
}),
client.teamWebsite.deleteMany({
where: {
teamId: {
in: teamIds,
},
},
}),
client.teamUser.deleteMany({
where: {
team: {
userId,
teamId: {
in: teamIds,
},
},
}),
client.team.deleteMany({
where: {
userId,
id: {
in: teamIds,
},
},
}),
cloudMode