26 lines
764 B
TypeScript
Raw Normal View History

2022-04-06 20:34:22 +02:00
import type { Team, Permission } from '@prisma/client';
import { prisma } from './common';
2022-02-10 15:47:44 +01:00
2022-04-06 20:34:22 +02:00
export async function listTeams(): Promise<Team[]> {
2022-02-10 15:47:44 +01:00
return await prisma.team.findMany();
}
2022-04-06 20:34:22 +02:00
export async function newTeam({ name, userId }: { name: string; userId: string }): Promise<Team> {
2022-02-10 15:47:44 +01:00
return await prisma.team.create({
data: {
name,
permissions: { create: { user: { connect: { id: userId } }, permission: 'owner' } },
users: { connect: { id: userId } }
}
});
}
2022-04-06 20:34:22 +02:00
export async function getMyTeams({
userId
}: {
userId: string;
}): Promise<(Permission & { team: Team & { _count: { users: number } } })[]> {
2022-02-10 15:47:44 +01:00
return await prisma.permission.findMany({
where: { userId },
include: { team: { include: { _count: { select: { users: true } } } } }
});
}