Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JUP-86 add admin #254

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:8080",
"webRoot": "${workspaceFolder}"
}
]
}
9 changes: 9 additions & 0 deletions src/app/admin/users/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import AddAdmin from '@src/components/admin/AddAdmin';
export default function Page() {
return (
<div className="m-5 md:pl-72">
<h1 className="text-center text-4xl font-bold text-black">Admin</h1>
<AddAdmin />
</div>
);
}
58 changes: 58 additions & 0 deletions src/components/admin/AddAdmin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use client';

import { api } from '@src/trpc/react';
import { UserSearchBar } from '../searchBar/UserSearchBar';
import { useRouter } from 'next/navigation';
import { useState } from 'react';

type AdminState = {
name: string;
userId: string;
};

export default function AddAdmin() {
const router = useRouter();
const { mutate } = api.admin.addAdmin.useMutation({
onSuccess: () => {
router.refresh();
setPerson(null);
},
});

const [toAdd, setPerson] = useState<AdminState | null>(null);

return (
<div className="container">
<h1>Add Admin</h1>
<div className="flex p-3">
<UserSearchBar
passUser={(user) =>
setPerson({
name: user.name,
userId: user.id, // Set selected role when user is selected
})
}
/>
</div>
<div className="flex items-center p-3">
<button
className="rounded bg-blue-500 px-4 py-2 font-bold text-white hover:bg-blue-700 disabled:cursor-not-allowed disabled:opacity-50"
onClick={() => {
if (!toAdd) return;
mutate({
userId: toAdd.userId,
});
}}
disabled={!toAdd || !toAdd.name}
>
Confirm
</button>
{toAdd && toAdd.name && (
<span className="ml-3">
Adding <strong>{toAdd.name}</strong> as admin
</span>
)}
</div>
</div>
);
}
21 changes: 20 additions & 1 deletion src/server/api/routers/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { and, eq, gt } from 'drizzle-orm';
import { club } from '@src/server/db/schema/club';
import { userMetadataToClubs } from '@src/server/db/schema/users';
import { type DateRange } from 'react-day-picker';
import { carousel } from '@src/server/db/schema/admin';
import { admin, carousel } from '@src/server/db/schema/admin';

function isDateRange(value: unknown): value is DateRange {
return Boolean(value && typeof value === 'object' && 'from' in value);
Expand All @@ -20,6 +20,10 @@ const updateOfficer = z.object({
role: z.enum(['President', 'Officer', 'Member']),
});

const updateAdmin = z.object({
userId: z.string(),
});

const carouselSchema = z.object({
orgId: z.string(),
range: z.custom<DateRange>((val) => isDateRange(val)),
Expand Down Expand Up @@ -62,6 +66,21 @@ export const adminRouter = createTRPCRouter({
),
);
}),
addAdmin: adminProcedure
.input(updateAdmin)
.mutation(async ({ ctx, input }) => {
// Check if the user is already an admin
const exists = await ctx.db.query.admin.findFirst({
where: (userMetadataToAdmin) =>
eq(userMetadataToAdmin.userId, input.userId), // Corrected the condition to check userId
});
if (!exists) {
await ctx.db.insert(admin).values({
userId: input.userId,
});
}
return;
}),
addOfficer: adminProcedure
.input(updateOfficer)
.mutation(async ({ ctx, input }) => {
Expand Down
2 changes: 2 additions & 0 deletions src/server/db/schema/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export const clubRoleEnum = pgEnum('member_type', [
'Member',
]);

export const adminRoleEnum = pgEnum('admin_type', ['Admin', 'Not Admin']);

export const users = pgTable('user', {
id: text('id').notNull().primaryKey(),
name: text('name'),
Expand Down
12 changes: 12 additions & 0 deletions src/utils/formSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,15 @@ export const feedbackFormSchema = z.object({
features: z.string().default(''),
submit_on: z.date().default(new Date()),
});

export const editAdminSchema = z.object({
admin: z
.object({
userId: z.string(),
name: z.string(),
locked: z.boolean(),
title: z.string().min(1),
position: z.enum(['Admin']),
})
.array(),
});