mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-15 17:55:08 +01:00
8a9532f213
* Link up teams UI. * Fix auth order. * PR touchups.
75 lines
1.5 KiB
TypeScript
75 lines
1.5 KiB
TypeScript
import { Prisma, Team, TeamWebsite } from '@prisma/client';
|
|
import prisma from 'lib/prisma';
|
|
import { uuid } from 'lib/crypto';
|
|
import { ROLES } from 'lib/constants';
|
|
|
|
export async function getTeam(where: Prisma.TeamWhereInput): Promise<Team> {
|
|
return prisma.client.team.findFirst({
|
|
where,
|
|
include: {
|
|
teamUser: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function getTeams(where: Prisma.TeamWhereInput): Promise<Team[]> {
|
|
return prisma.client.team.findMany({
|
|
where,
|
|
});
|
|
}
|
|
|
|
export async function createTeam(data: Prisma.TeamCreateInput, userId: string): Promise<Team> {
|
|
const { id } = data;
|
|
|
|
return prisma.transaction([
|
|
prisma.client.team.create({
|
|
data,
|
|
}),
|
|
prisma.client.teamUser.create({
|
|
data: {
|
|
id: uuid(),
|
|
teamId: id,
|
|
userId,
|
|
role: ROLES.teamOwner,
|
|
},
|
|
}),
|
|
]);
|
|
}
|
|
|
|
export async function updateTeam(
|
|
data: Prisma.TeamUpdateInput,
|
|
where: Prisma.TeamWhereUniqueInput,
|
|
): Promise<Team> {
|
|
return prisma.client.team.update({
|
|
data: {
|
|
...data,
|
|
updatedAt: new Date(),
|
|
},
|
|
where,
|
|
});
|
|
}
|
|
|
|
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,
|
|
},
|
|
}),
|
|
]);
|
|
}
|