2021-06-05 22:53:33 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
|
|
import prisma from '../../lib/prisma';
|
|
|
|
import {getSession} from "next-auth/client";
|
|
|
|
|
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
|
|
|
|
const session = await getSession({req: req});
|
|
|
|
|
|
|
|
if (!session) {
|
|
|
|
res.status(401).json({message: "Not authenticated"});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.method === "POST") {
|
2021-06-07 15:12:00 +00:00
|
|
|
|
|
|
|
// TODO: Prevent creating a team with identical names?
|
|
|
|
|
2021-06-05 22:53:33 +00:00
|
|
|
const createTeam = await prisma.team.create({
|
|
|
|
data: {
|
|
|
|
name: req.body.name,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-07-01 09:47:12 +00:00
|
|
|
await prisma.membership.create({
|
2021-06-05 22:53:33 +00:00
|
|
|
data: {
|
|
|
|
teamId: createTeam.id,
|
|
|
|
userId: session.user.id,
|
|
|
|
role: 'OWNER',
|
|
|
|
accepted: true,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-06-30 13:48:34 +00:00
|
|
|
return res.status(201).json({ message: 'Team created' });
|
2021-06-05 22:53:33 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 09:47:12 +00:00
|
|
|
res.status(404).json({ message: 'Team not found' });
|
2021-06-05 22:53:33 +00:00
|
|
|
}
|