Better error handling during team member invitation
Now tells you if you have already added this member / invite is pending. Behaviour a little bit more predictable during team editting.
This commit is contained in:
1 parent
9f12ccf5c1
commit
5d3e39ea6e
4 files changed
+50
-18
No files matched your search
+3
-1
@@ -1,7 +1,6 @@
|
||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||
import prisma from '../../lib/prisma';
|
||||
import {getSession} from "next-auth/client";
|
||||
import {create} from "domain";
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
|
||||
@@ -13,6 +12,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
}
|
||||
|
||||
if (req.method === "POST") {
|
||||
|
||||
// TODO: Prevent creating a team with identical names?
|
||||
|
||||
const createTeam = await prisma.team.create({
|
||||
data: {
|
||||
name: req.body.name,
|
||||
|
||||
@@ -21,7 +21,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
});
|
||||
|
||||
if (!team) {
|
||||
return res.status(404).json({message: "Unable to find team to invite user to."});
|
||||
return res.status(404).json({message: "Invalid team"});
|
||||
}
|
||||
|
||||
const invitee = await prisma.user.findFirst({
|
||||
@@ -34,17 +34,29 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
});
|
||||
|
||||
if (!invitee) {
|
||||
return res.status(404).json({message: "Missing user, currently unsupported."});
|
||||
return res.status(400).json({
|
||||
message: `Invite failed because there is no corresponding user for ${req.body.usernameOrEmail}`});
|
||||
}
|
||||
|
||||
// create provisional membership
|
||||
const createMembership = await prisma.membership.create({
|
||||
data: {
|
||||
teamId: parseInt(req.query.team),
|
||||
userId: invitee.id,
|
||||
role: req.body.role,
|
||||
},
|
||||
});
|
||||
try {
|
||||
const createMembership = await prisma.membership.create({
|
||||
data: {
|
||||
teamId: parseInt(req.query.team),
|
||||
userId: invitee.id,
|
||||
role: req.body.role,
|
||||
},
|
||||
});
|
||||
}
|
||||
catch (err) {
|
||||
if (err.code === "P2002") { // unique constraint violation
|
||||
return res.status(409).json({
|
||||
message: 'This user is a member of this team / has a pending invitation.',
|
||||
});
|
||||
} else {
|
||||
throw err; // rethrow
|
||||
}
|
||||
};
|
||||
|
||||
// inform user of membership by email
|
||||
if (req.body.sendEmailInvitation) {
|
||||
|
||||
Reference in new issue
Block a user