Skip to content

Commit

Permalink
Merge pull request #7 from Scale3-Labs/karthik/teams-bug-fixes
Browse files Browse the repository at this point in the history
Bug fixes
  • Loading branch information
karthikscale3 authored Apr 1, 2024
2 parents 47981a0 + e77b9f0 commit 6f9f679
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 24 deletions.
4 changes: 3 additions & 1 deletion app/(protected)/projects/page-client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ export default function PageClient({ email }: { email: string }) {
}

function ProjectCard({
key,
project,
teamId,
}: {
key: number;
project: Project;
teamId: string;
}) {
Expand All @@ -111,7 +113,7 @@ function ProjectCard({
return <div>Loading...</div>;
}
return (
<div className="relative">
<div className="relative" key={key}>
<div className="flex items-center flex-row gap-2 absolute top-2 right-2 z-10">
{!fetchProjecStats.isLoading &&
fetchProjecStats.data &&
Expand Down
12 changes: 6 additions & 6 deletions app/(protected)/settings/members/page-client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export function InviteMember({ user }: { user: any }) {
email: data.email,
name: data.name,
team_id: user.teamId,
role: "member",
status: "invited",
}),
});

Expand Down Expand Up @@ -215,11 +217,11 @@ function ManageRole({ member }: { member: any }) {
defaultValue={type}
>
<div className="flex items-center space-x-2 py-4">
<RadioGroupItem value="user" id="r2" />
<RadioGroupItem value="member" id="r2" />
<div className="flex flex-col gap-1">
<Label htmlFor="r2">Member</Label>
<p className="text-sm text-muted-foreground">
Create projects and deploy AI models.
Standard-level permissions. Can view and edit projects.
</p>
</div>
</div>
Expand Down Expand Up @@ -254,21 +256,19 @@ function ManageRole({ member }: { member: any }) {

function RemoveMemberDialog({ member }: { member: any }) {
const [busy, setBusy] = useState(false);
const [type, setType] = useState(member.role);
const [open, setOpen] = useState(false);
const queryClient = useQueryClient();

const removeUser = async (member: any) => {
try {
setBusy(true);
await fetch(`/api/user?id=${member.id}`, {
method: "PUT",
await fetch("/api/user", {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
id: member.id,
teamId: null,
}),
});
queryClient.invalidateQueries({ queryKey: ["getUsers"] }).then(() => {
Expand Down
40 changes: 37 additions & 3 deletions app/api/user/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export async function PUT(req: NextRequest) {
}

const data = await req.json();
const { id, name, teamId, role } = data;
const { id, name, teamId, role, status } = data;

if ("teamId" in data) {
const user = await prisma.user.update({
Expand Down Expand Up @@ -82,6 +82,21 @@ export async function PUT(req: NextRequest) {
});
}

if ("status" in data) {
console.log("updating status");
const user = await prisma.user.update({
where: {
id,
},
data: {
status,
},
});
return NextResponse.json({
data: user,
});
}

const user = await prisma.user.update({
where: {
id,
Expand All @@ -96,25 +111,44 @@ export async function PUT(req: NextRequest) {
});
}

// may not work / be necessary if users only get created through google auth
export async function POST(req: NextRequest) {
const session = await getServerSession(authOptions);
if (!session || !session.user) {
redirect("/login");
}

const data = await req.json();
const { email, name, team_id } = data;
const { email, name, team_id, status, role } = data;

const user = await prisma.user.create({
data: {
email,
name,
teamId: team_id,
status,
role,
},
});

return NextResponse.json({
data: user,
});
}

export async function DELETE(req: NextRequest) {
const session = await getServerSession(authOptions);
if (!session || !session.user) {
redirect("/login");
}

const data = await req.json();
const { id } = data;

const user = await prisma.user.delete({
where: {
id,
},
});

return NextResponse.json({});
}
2 changes: 2 additions & 0 deletions lib/auth/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export const authOptions: NextAuthOptions = {
email: process.env.ADMIN_EMAIL,
name: "Admin",
image: null,
role: "owner",
status: "active",
},
});
}
Expand Down
30 changes: 17 additions & 13 deletions lib/middleware/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default async function AppMiddleware(req: NextRequest) {
email?: string;
user?: User;
};
if (session && path === "/login") {
if (session && (path === "/login" || path === "/signup")) {
const userReq = await fetch(
`${process.env.NEXT_PUBLIC_HOST}/api/user?email=${session?.email}`,
{
Expand All @@ -28,18 +28,22 @@ export default async function AppMiddleware(req: NextRequest) {
);
const response = await userReq.json();
const user = response.data;
if (user && !user.teamId) {
// create a team
await fetch(`${process.env.NEXT_PUBLIC_HOST}/api/team`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
name: "My Team",
userId: user.id,
}),
});
if (user) {
if (!user.teamId) {
// create a team
await fetch(`${process.env.NEXT_PUBLIC_HOST}/api/team`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
name: "My Team",
userId: user.id,
role: "owner",
status: "active",
}),
});
}
}

// if there's a session
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "User" ALTER COLUMN "role" SET DEFAULT 'owner';
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ model User {
teamId String?
Team Team? @relation(fields: [teamId], references: [id], onDelete: Cascade)
status String? @default("active") // invited, active
role Role? @default(member)
role Role? @default(owner)
createdAt DateTime @default(now())
Evaluation Evaluation[]
}
Expand Down

0 comments on commit 6f9f679

Please sign in to comment.