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

@ -52,17 +52,17 @@ export async function updateTeam(
export async function deleteTeam(
teamId: string,
): Promise<Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Team]>> {
const { client } = prisma;
const { client, transaction } = prisma;
return prisma.transaction([
return transaction([
client.teamWebsite.deleteMany({
where: {
id: teamId,
teamId,
},
}),
client.teamUser.deleteMany({
where: {
id: teamId,
teamId,
},
}),
client.team.delete({

View file

@ -2,6 +2,14 @@ import { Prisma, TeamUser } from '@prisma/client';
import { uuid } from 'lib/crypto';
import prisma from 'lib/prisma';
export async function getTeamUserById(teamUserId: string): Promise<TeamUser> {
return prisma.client.teamUser.findUnique({
where: {
id: teamUserId,
},
});
}
export async function getTeamUser(teamId: string, userId: string): Promise<TeamUser> {
return prisma.client.teamUser.findFirst({
where: {
@ -48,11 +56,25 @@ export async function updateTeamUser(
}
export async function deleteTeamUser(teamUserId: string): Promise<TeamUser> {
return prisma.client.teamUser.delete({
where: {
id: teamUserId,
},
});
const { client, transaction } = prisma;
const teamUser = await getTeamUserById(teamUserId);
return transaction([
client.teamWebsite.deleteMany({
where: {
teamId: teamUser.teamId,
website: {
userId: teamUser.userId,
},
},
}),
client.teamUser.deleteMany({
where: {
id: teamUserId,
},
}),
]);
}
export async function deleteTeamUserByUserId(

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