umami/queries/admin/teamWebsite.ts

128 lines
2.5 KiB
TypeScript
Raw Normal View History

2023-04-10 01:04:28 +02:00
import { Prisma, Team, TeamUser, TeamWebsite, Website } from '@prisma/client';
import { ROLES } from 'lib/constants';
import { uuid } from 'lib/crypto';
import prisma from 'lib/prisma';
2023-04-10 01:04:28 +02:00
export async function getTeamWebsite(
teamId: string,
websiteId: string,
): Promise<
TeamWebsite & {
website: Website;
}
> {
2023-02-28 01:01:34 +01:00
return prisma.client.teamWebsite.findFirst({
where: {
2023-04-10 01:04:28 +02:00
teamId,
websiteId,
},
include: {
website: true,
},
});
}
export async function getTeamWebsiteByTeamMemberId(
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 & {
2023-03-30 05:54:54 +02:00
team: Team & { teamUser: TeamUser[] };
website: Website & {
2023-03-30 05:54:54 +02:00
user: { id: string; username: string };
};
})[]
> {
2023-02-28 01:01:34 +01:00
return prisma.client.teamWebsite.findMany({
where: {
teamId,
},
include: {
team: {
include: {
teamUser: {
where: {
role: ROLES.teamOwner,
},
},
},
},
website: {
include: {
2023-03-30 05:54:54 +02:00
user: {
select: {
id: true,
username: true,
},
},
},
},
},
orderBy: [
{
team: {
name: 'asc',
},
},
],
});
}
export async function createTeamWebsite(teamId: string, websiteId: string): Promise<TeamWebsite> {
2023-02-28 01:01:34 +01:00
return prisma.client.teamWebsite.create({
data: {
id: uuid(),
teamId,
websiteId,
},
});
}
2023-03-03 07:48:30 +01:00
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,
});
}
2023-04-10 01:04:28 +02:00
export async function deleteTeamWebsite(
teamId: string,
websiteId: string,
): Promise<Prisma.BatchPayload> {
return prisma.client.teamWebsite.deleteMany({
2023-03-03 07:48:30 +01:00
where: {
2023-04-10 01:04:28 +02:00
teamId,
websiteId,
2023-03-03 07:48:30 +01:00
},
});
}