From f010b62f8552c60bf611dff554f9b067b666b6b3 Mon Sep 17 00:00:00 2001 From: ckat-1 Date: Tue, 22 Oct 2024 18:38:04 -0500 Subject: [PATCH 1/4] git checkout -b JUP-86-add-Admin --- .vscode/launch.json | 15 ++++++++ src/app/admin/users/page.tsx | 11 ++++++ src/components/admin/AddAdmin.tsx | 58 +++++++++++++++++++++++++++++++ src/server/api/routers/admin.ts | 22 +++++++++++- src/server/db/schema/users.ts | 5 +++ src/utils/formSchemas.ts | 13 +++++++ 6 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 .vscode/launch.json create mode 100644 src/app/admin/users/page.tsx create mode 100644 src/components/admin/AddAdmin.tsx diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..2ba986f6 --- /dev/null +++ b/.vscode/launch.json @@ -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}" + } + ] +} \ No newline at end of file diff --git a/src/app/admin/users/page.tsx b/src/app/admin/users/page.tsx new file mode 100644 index 00000000..77b4dc5d --- /dev/null +++ b/src/app/admin/users/page.tsx @@ -0,0 +1,11 @@ +import AddAdmin from '@src/components/admin/AddAdmin'; +export default function Page() { + return ( +
+

+ Admin +

+ +
+ ); +} \ No newline at end of file diff --git a/src/components/admin/AddAdmin.tsx b/src/components/admin/AddAdmin.tsx new file mode 100644 index 00000000..90e53f15 --- /dev/null +++ b/src/components/admin/AddAdmin.tsx @@ -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(null); + + return ( +
+

Add Admin

+
+ + setPerson({ + name: user.name, + userId:user.id, // Set selected role when user is selected + }) + } + /> +
+
+ + {toAdd && toAdd.name && ( + + Adding {toAdd.name} as admin + + )} +
+
+ ); +} diff --git a/src/server/api/routers/admin.ts b/src/server/api/routers/admin.ts index 86eac7b1..4fc849c6 100644 --- a/src/server/api/routers/admin.ts +++ b/src/server/api/routers/admin.ts @@ -4,7 +4,8 @@ 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'; +import role from '@src/models/role'; function isDateRange(value: unknown): value is DateRange { return Boolean(value && typeof value === 'object' && 'from' in value); @@ -20,6 +21,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((val) => isDateRange(val)), @@ -62,6 +67,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 }) => { diff --git a/src/server/db/schema/users.ts b/src/server/db/schema/users.ts index 1da47684..70f26c3a 100644 --- a/src/server/db/schema/users.ts +++ b/src/server/db/schema/users.ts @@ -40,6 +40,11 @@ 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'), diff --git a/src/utils/formSchemas.ts b/src/utils/formSchemas.ts index 3df635cd..1953cf0b 100644 --- a/src/utils/formSchemas.ts +++ b/src/utils/formSchemas.ts @@ -1,4 +1,5 @@ import { selectContact } from '@src/server/db/models'; +import { admin } from '@src/server/db/schema/admin'; import { z } from 'zod'; export const createClubSchema = z.object({ @@ -58,3 +59,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(), +}); \ No newline at end of file From 7ee5682e1d02e20fb5bbb2f99e94d453a7ab1d0c Mon Sep 17 00:00:00 2001 From: ckat-1 Date: Tue, 29 Oct 2024 18:46:09 -0500 Subject: [PATCH 2/4] Fixing for test case --- src/server/api/routers/admin.ts | 2 +- src/utils/formSchemas.ts | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/server/api/routers/admin.ts b/src/server/api/routers/admin.ts index 4fc849c6..7a8163c6 100644 --- a/src/server/api/routers/admin.ts +++ b/src/server/api/routers/admin.ts @@ -5,7 +5,7 @@ import { club } from '@src/server/db/schema/club'; import { userMetadataToClubs } from '@src/server/db/schema/users'; import { type DateRange } from 'react-day-picker'; import { admin, carousel } from '@src/server/db/schema/admin'; -import role from '@src/models/role'; + function isDateRange(value: unknown): value is DateRange { return Boolean(value && typeof value === 'object' && 'from' in value); diff --git a/src/utils/formSchemas.ts b/src/utils/formSchemas.ts index 988a51bd..bffa8244 100644 --- a/src/utils/formSchemas.ts +++ b/src/utils/formSchemas.ts @@ -1,5 +1,4 @@ -import { selectContact } from '@src/server/db/models'; -import { admin } from '@src/server/db/schema/admin'; + import { z } from 'zod'; import { contactSchema } from './contact'; From 8d4a9dd0b0f3e94a89733e1ba1d0b916843661e0 Mon Sep 17 00:00:00 2001 From: ckat-1 Date: Tue, 12 Nov 2024 18:12:10 -0600 Subject: [PATCH 3/4] Run prettier --- src/app/admin/users/page.tsx | 8 +++----- src/components/admin/AddAdmin.tsx | 6 +++--- src/server/api/routers/admin.ts | 5 ++--- src/server/db/schema/users.ts | 5 +---- src/utils/formSchemas.ts | 3 +-- 5 files changed, 10 insertions(+), 17 deletions(-) diff --git a/src/app/admin/users/page.tsx b/src/app/admin/users/page.tsx index 77b4dc5d..fea45ace 100644 --- a/src/app/admin/users/page.tsx +++ b/src/app/admin/users/page.tsx @@ -2,10 +2,8 @@ import AddAdmin from '@src/components/admin/AddAdmin'; export default function Page() { return (
-

- Admin -

- +

Admin

+
); -} \ No newline at end of file +} diff --git a/src/components/admin/AddAdmin.tsx b/src/components/admin/AddAdmin.tsx index 90e53f15..5ee69d11 100644 --- a/src/components/admin/AddAdmin.tsx +++ b/src/components/admin/AddAdmin.tsx @@ -7,7 +7,7 @@ import { useState } from 'react'; type AdminState = { name: string; - userId:string; + userId: string; }; export default function AddAdmin() { @@ -29,7 +29,7 @@ export default function AddAdmin() { passUser={(user) => setPerson({ name: user.name, - userId:user.id, // Set selected role when user is selected + userId: user.id, // Set selected role when user is selected }) } /> @@ -40,7 +40,7 @@ export default function AddAdmin() { onClick={() => { if (!toAdd) return; mutate({ - userId:toAdd.userId + userId: toAdd.userId, }); }} disabled={!toAdd || !toAdd.name} diff --git a/src/server/api/routers/admin.ts b/src/server/api/routers/admin.ts index 7a8163c6..076f3be8 100644 --- a/src/server/api/routers/admin.ts +++ b/src/server/api/routers/admin.ts @@ -6,7 +6,6 @@ import { userMetadataToClubs } from '@src/server/db/schema/users'; import { type DateRange } from 'react-day-picker'; 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); } @@ -67,7 +66,7 @@ export const adminRouter = createTRPCRouter({ ), ); }), - addAdmin: adminProcedure + addAdmin: adminProcedure .input(updateAdmin) .mutation(async ({ ctx, input }) => { // Check if the user is already an admin @@ -77,7 +76,7 @@ export const adminRouter = createTRPCRouter({ }); if (!exists) { await ctx.db.insert(admin).values({ - userId: input.userId + userId: input.userId, }); } return; diff --git a/src/server/db/schema/users.ts b/src/server/db/schema/users.ts index 70f26c3a..25d77bd2 100644 --- a/src/server/db/schema/users.ts +++ b/src/server/db/schema/users.ts @@ -40,10 +40,7 @@ export const clubRoleEnum = pgEnum('member_type', [ 'Member', ]); -export const adminRoleEnum = pgEnum('admin_type', [ - 'Admin', - 'Not Admin', -]); +export const adminRoleEnum = pgEnum('admin_type', ['Admin', 'Not Admin']); export const users = pgTable('user', { id: text('id').notNull().primaryKey(), diff --git a/src/utils/formSchemas.ts b/src/utils/formSchemas.ts index bffa8244..cd8bcad3 100644 --- a/src/utils/formSchemas.ts +++ b/src/utils/formSchemas.ts @@ -1,4 +1,3 @@ - import { z } from 'zod'; import { contactSchema } from './contact'; @@ -65,4 +64,4 @@ export const editAdminSchema = z.object({ position: z.enum(['Admin']), }) .array(), -}); \ No newline at end of file +}); From c01420a916727d986f5505da5a58655f3f801755 Mon Sep 17 00:00:00 2001 From: ckat-1 Date: Tue, 12 Nov 2024 18:14:23 -0600 Subject: [PATCH 4/4] Update launch.json --- .vscode/launch.json | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 2ba986f6..8f30e65a 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,15 +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}" - } - ] -} \ No newline at end of file + // 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}" + } + ] +}