Skip to content

Commit

Permalink
user invite
Browse files Browse the repository at this point in the history
  • Loading branch information
karthikscale3 committed Apr 1, 2024
1 parent ef1ce1f commit e77b9f0
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 22 deletions.
10 changes: 4 additions & 6 deletions app/(protected)/settings/members/page-client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -217,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 @@ -256,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
35 changes: 34 additions & 1 deletion 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 Down Expand Up @@ -119,3 +134,21 @@ export async function POST(req: NextRequest) {
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: 16 additions & 14 deletions lib/middleware/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +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,
role: "owner",
status: "active",
}),
});
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 e77b9f0

Please sign in to comment.