Clean up teams on user delete.

This commit is contained in:
Brian Cao 2023-03-02 22:48:30 -08:00
parent 82f0bc3d2b
commit 8684781624
8 changed files with 90 additions and 14 deletions

View file

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

View file

@ -54,3 +54,15 @@ export async function deleteTeamUser(teamUserId: string): Promise<TeamUser> {
},
});
}
export async function deleteTeamUserByUserId(
userId: string,
teamId: string,
): Promise<Prisma.BatchPayload> {
return prisma.client.teamUser.deleteMany({
where: {
userId,
teamId,
},
});
}

View file

@ -37,3 +37,11 @@ export async function createTeamWebsite(
},
});
}
export async function deleteTeamWebsite(teamWebsiteId: string): Promise<TeamWebsite> {
return prisma.client.teamWebsite.delete({
where: {
id: teamWebsiteId,
},
});
}

View file

@ -1,7 +1,7 @@
import { Prisma, Team } from '@prisma/client';
import cache from 'lib/cache';
import prisma from 'lib/prisma';
import { Website, User } from 'lib/types';
import { Website, User, Roles } from 'lib/types';
export async function getUser(
where: Prisma.UserWhereInput | Prisma.UserWhereUniqueInput,
@ -76,7 +76,7 @@ export async function createUser(data: {
id: string;
username: string;
password: string;
role: string;
role: Roles;
}): Promise<{
id: string;
username: string;
@ -110,7 +110,17 @@ export async function updateUser(
export async function deleteUser(
userId: string,
): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Prisma.BatchPayload, User]> {
): Promise<
[
Prisma.BatchPayload,
Prisma.BatchPayload,
Prisma.BatchPayload,
Prisma.BatchPayload,
Prisma.BatchPayload,
Prisma.BatchPayload,
User,
]
> {
const { client } = prisma;
const cloudMode = process.env.CLOUD_MODE;
@ -132,6 +142,25 @@ export async function deleteUser(
client.session.deleteMany({
where: { websiteId: { in: websiteIds } },
}),
client.teamWebsite.deleteMany({
where: {
website: {
userId,
},
},
}),
client.teamUser.deleteMany({
where: {
team: {
userId,
},
},
}),
client.team.deleteMany({
where: {
userId,
},
}),
cloudMode
? client.website.updateMany({
data: {

View file

@ -81,6 +81,11 @@ export async function deleteWebsite(
client.session.deleteMany({
where: { websiteId },
}),
client.teamWebsite.deleteMany({
where: {
websiteId,
},
}),
cloudMode
? prisma.client.website.update({
data: {