Teams refactor: removed team websites.

This commit is contained in:
Mike Cao 2024-01-25 23:20:53 -08:00
parent 0d442b751d
commit f85393f8df
23 changed files with 190 additions and 351 deletions

View file

@ -10,8 +10,9 @@ export interface GetTeamOptions {
async function getTeam(where: Prisma.TeamWhereInput, options: GetTeamOptions = {}): Promise<Team> {
const { includeTeamUser = false } = options;
const { client } = prisma;
return prisma.client.team.findFirst({
return client.team.findFirst({
where,
include: {
teamUser: includeTeamUser,
@ -27,14 +28,15 @@ export function getTeamByAccessCode(accessCode: string, options: GetTeamOptions
return getTeam({ accessCode }, options);
}
export async function createTeam(data: Prisma.TeamCreateInput, userId: string): Promise<Team> {
export async function createTeam(data: Prisma.TeamCreateInput, userId: string): Promise<any> {
const { id } = data;
const { client, transaction } = prisma;
return prisma.transaction([
prisma.client.team.create({
return transaction([
client.team.create({
data,
}),
prisma.client.teamUser.create({
client.teamUser.create({
data: {
id: uuid(),
teamId: id,
@ -46,7 +48,9 @@ export async function createTeam(data: Prisma.TeamCreateInput, userId: string):
}
export async function updateTeam(teamId: string, data: Prisma.TeamUpdateInput): Promise<Team> {
return prisma.client.team.update({
const { client } = prisma;
return client.team.update({
where: {
id: teamId,
},
@ -61,13 +65,22 @@ export async function deleteTeam(
teamId: string,
): Promise<Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Team]>> {
const { client, transaction } = prisma;
const cloudMode = process.env.CLOUD_MODE;
if (cloudMode) {
return transaction([
client.team.update({
data: {
deletedAt: new Date(),
},
where: {
id: teamId,
},
}),
]);
}
return transaction([
client.teamWebsite.deleteMany({
where: {
teamId,
},
}),
client.teamUser.deleteMany({
where: {
teamId,
@ -87,6 +100,7 @@ export async function getTeams(
): Promise<FilterResult<Team[]>> {
const { userId, query } = filters;
const mode = prisma.getQueryMode();
const { client } = prisma;
const where: Prisma.TeamWhereInput = {
...(userId && {
@ -123,7 +137,7 @@ export async function getTeams(
...filters,
});
const teams = await prisma.client.team.findMany({
const teams = await client.team.findMany({
where: {
...where,
},
@ -131,7 +145,7 @@ export async function getTeams(
...(options?.include && { include: options?.include }),
});
const count = await prisma.client.team.count({ where });
const count = await client.team.count({ where });
return { data: teams, count, ...getParameters };
}
@ -154,6 +168,9 @@ export async function getTeamsByUserId(
},
},
},
_count: {
select: { website: true, teamUser: true },
},
},
},
);

View file

@ -68,14 +68,6 @@ export async function deleteTeamUser(teamId: string, userId: string): Promise<Te
const { client, transaction } = prisma;
return transaction([
client.teamWebsite.deleteMany({
where: {
teamId: teamId,
website: {
userId: userId,
},
},
}),
client.teamUser.deleteMany({
where: {
teamId,

View file

@ -1,127 +0,0 @@
import { Prisma, Team, TeamUser, TeamWebsite, Website } from '@prisma/client';
import { ROLES } from 'lib/constants';
import { uuid } from 'lib/crypto';
import prisma from 'lib/prisma';
export async function getTeamWebsite(
teamId: string,
websiteId: string,
): Promise<
TeamWebsite & {
website: Website;
}
> {
return prisma.client.teamWebsite.findFirst({
where: {
teamId,
websiteId,
},
include: {
website: true,
},
});
}
export async function findTeamWebsiteByUserId(
websiteId: string,
userId: string,
): Promise<TeamWebsite> {
return prisma.client.teamWebsite.findFirst({
where: {
websiteId,
team: {
teamUser: {
some: {
userId,
},
},
},
},
});
}
export async function getTeamWebsites(teamId: string): Promise<
(TeamWebsite & {
team: Team & { teamUser: TeamUser[] };
website: Website & {
user: { id: string; username: string };
};
})[]
> {
return prisma.client.teamWebsite.findMany({
where: {
teamId,
},
include: {
team: {
include: {
teamUser: {
where: {
role: ROLES.teamOwner,
},
},
},
},
website: {
include: {
user: {
select: {
id: true,
username: true,
},
},
},
},
},
orderBy: [
{
team: {
name: 'asc',
},
},
],
});
}
export async function createTeamWebsite(teamId: string, websiteId: string): Promise<TeamWebsite> {
return prisma.client.teamWebsite.create({
data: {
id: uuid(),
teamId,
websiteId,
},
});
}
export async function createTeamWebsites(teamId: string, websiteIds: string[]) {
const currentTeamWebsites = await getTeamWebsites(teamId);
// filter out websites that already exists on the team
const addWebsites = websiteIds.filter(
websiteId => !currentTeamWebsites.some(a => a.websiteId === websiteId),
);
const teamWebsites: Prisma.TeamWebsiteCreateManyInput[] = addWebsites.map(a => {
return {
id: uuid(),
teamId,
websiteId: a,
};
});
return prisma.client.teamWebsite.createMany({
data: teamWebsites,
});
}
export async function deleteTeamWebsite(
teamId: string,
websiteId: string,
): Promise<Prisma.BatchPayload> {
return prisma.client.teamWebsite.deleteMany({
where: {
teamId,
websiteId,
},
});
}

View file

@ -105,6 +105,7 @@ export async function getUsersByTeamId(teamId: string, filter?: UserSearchFilter
include: {
teamUser: {
select: {
teamId: true,
role: true,
},
},
@ -188,7 +189,27 @@ export async function deleteUser(
const teamIds = teams.map(a => a.id);
return prisma
if (cloudMode) {
return client.transaction([
client.website.updateMany({
data: {
deletedAt: new Date(),
},
where: { id: { in: websiteIds } },
}),
client.user.update({
data: {
username: getRandomChars(32),
deletedAt: new Date(),
},
where: {
id: userId,
},
}),
]);
}
return client
.transaction([
client.eventData.deleteMany({
where: { websiteId: { in: websiteIds } },
@ -199,29 +220,6 @@ export async function deleteUser(
client.session.deleteMany({
where: { websiteId: { in: websiteIds } },
}),
client.teamWebsite.deleteMany({
where: {
OR: [
{
websiteId: {
in: websiteIds,
},
},
{
teamId: {
in: teamIds,
},
},
],
},
}),
client.teamWebsite.deleteMany({
where: {
teamId: {
in: teamIds,
},
},
}),
client.teamUser.deleteMany({
where: {
OR: [
@ -257,33 +255,16 @@ export async function deleteUser(
],
},
}),
cloudMode
? client.website.updateMany({
data: {
deletedAt: new Date(),
},
where: { id: { in: websiteIds } },
})
: client.website.deleteMany({
where: { id: { in: websiteIds } },
}),
cloudMode
? client.user.update({
data: {
username: getRandomChars(32),
deletedAt: new Date(),
},
where: {
id: userId,
},
})
: client.user.delete({
where: {
id: userId,
},
}),
client.website.deleteMany({
where: { id: { in: websiteIds } },
}),
client.user.delete({
where: {
id: userId,
},
}),
])
.then(async data => {
.then(async (data: any) => {
if (cache.enabled) {
const ids = websites.map(a => a.id);

View file

@ -260,7 +260,7 @@ export async function deleteWebsite(
},
}),
cloudMode
? prisma.client.website.update({
? client.website.update({
data: {
deletedAt: new Date(),
},