Cal 262 refactor edit teams according to the design reference (#516)
* refactored settings/team landing page
* changed team edit flow, WIP
* merge conflict fix for teams.tsx
* minor fixes to edit team, WIP
* invite-member and disband team APIs attached inside edit-team page
* added remove-member API in edit-team page, minor fixes
* minor code fix, WIP
* WIP
* add logo, bio, branding to team schema
* bio, logo, branding, slug patch API and minor code fix-- WIP
* fn to Disband team directly from the dropdown menu in settings/teams page, removed debug remnants --WIP
* Pull latest data after an action in settings/teams-edit page
* added slug conflict check at Patch time
* code clean-up
* initial change request fixes --WIP
* prop type fix and add warn button color theme --WIP
* added warn Button to Dialog
* remaining change request fixes
* added noop from react-query
* updated invited team-list design
* prettier fix for api/teams/profile
* removed noop import and added custom noop
* minor Button fix
* requested changes addressed
2021-09-06 13:22:22 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
import { getSession } from "next-auth/client";
|
|
|
|
|
2021-09-22 19:52:38 +00:00
|
|
|
import prisma from "@lib/prisma";
|
|
|
|
|
Cal 262 refactor edit teams according to the design reference (#516)
* refactored settings/team landing page
* changed team edit flow, WIP
* merge conflict fix for teams.tsx
* minor fixes to edit team, WIP
* invite-member and disband team APIs attached inside edit-team page
* added remove-member API in edit-team page, minor fixes
* minor code fix, WIP
* WIP
* add logo, bio, branding to team schema
* bio, logo, branding, slug patch API and minor code fix-- WIP
* fn to Disband team directly from the dropdown menu in settings/teams page, removed debug remnants --WIP
* Pull latest data after an action in settings/teams-edit page
* added slug conflict check at Patch time
* code clean-up
* initial change request fixes --WIP
* prop type fix and add warn button color theme --WIP
* added warn Button to Dialog
* remaining change request fixes
* added noop from react-query
* updated invited team-list design
* prettier fix for api/teams/profile
* removed noop import and added custom noop
* minor Button fix
* requested changes addressed
2021-09-06 13:22:22 +00:00
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
const session = await getSession({ req: req });
|
|
|
|
if (!session) {
|
|
|
|
return res.status(401).json({ message: "Not authenticated" });
|
|
|
|
}
|
|
|
|
|
|
|
|
const isTeamOwner = !!(await prisma.membership.findFirst({
|
|
|
|
where: {
|
|
|
|
userId: session.user.id,
|
|
|
|
teamId: parseInt(req.query.team as string),
|
|
|
|
role: "OWNER",
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
|
|
|
|
if (!isTeamOwner) {
|
|
|
|
res.status(403).json({ message: "You are not authorized to manage this team" });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// PATCH /api/teams/profile/{team}
|
|
|
|
if (req.method === "PATCH") {
|
|
|
|
const team = await prisma.team.findFirst({
|
|
|
|
where: {
|
|
|
|
id: parseInt(req.query.team as string),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!team) {
|
|
|
|
return res.status(404).json({ message: "Invalid team" });
|
|
|
|
}
|
|
|
|
|
|
|
|
const username = req.body.username;
|
|
|
|
const userConflict = await prisma.team.findMany({
|
|
|
|
where: {
|
|
|
|
slug: username,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const teamId = Number(req.query.team);
|
|
|
|
if (userConflict.some((team) => team.id !== teamId)) {
|
|
|
|
return res.status(409).json({ message: "Team username already taken" });
|
|
|
|
}
|
|
|
|
|
|
|
|
const name = req.body.name;
|
|
|
|
const slug = req.body.username;
|
|
|
|
const bio = req.body.description;
|
|
|
|
const logo = req.body.logo;
|
|
|
|
const hideBranding = req.body.hideBranding;
|
|
|
|
|
|
|
|
await prisma.team.update({
|
|
|
|
where: {
|
|
|
|
id: team.id,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
name,
|
|
|
|
slug,
|
|
|
|
logo,
|
|
|
|
bio,
|
|
|
|
hideBranding,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return res.status(200).json({ message: "Team updated successfully" });
|
|
|
|
}
|
|
|
|
}
|