-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from TEDx-SJEC/admin-page
Admin page
- Loading branch information
Showing
27 changed files
with
939 additions
and
568 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,56 @@ | ||
"use server"; | ||
|
||
import prisma from "@/server/db"; | ||
import { getServerSideSession } from "@/lib/get-server-session"; | ||
import { revalidatePath } from "next/cache"; | ||
import getErrorMessage from "@/utils/getErrorMessage"; | ||
import { UserRole,ADMIN_USERS_PATH } from "@/constants"; | ||
|
||
async function updateUserRole(id: string, role: string) { | ||
async function updateUserRole(id: string, role: UserRole) { | ||
const VALID_ROLES = Object.values(UserRole); | ||
if (!VALID_ROLES.includes(role)) { | ||
throw new Error(`Invalid role: ${role}`); | ||
} | ||
const session = await getServerSideSession(); | ||
if (!session || session.user.role !== UserRole.ADMIN) { | ||
throw new Error(`Unauthorized Access...`); | ||
} | ||
try { | ||
const updatedUser = await prisma.user.update({ | ||
where: { id }, | ||
data: { role }, | ||
}); | ||
revalidatePath("/admin/users"); | ||
revalidatePath(ADMIN_USERS_PATH); | ||
return updatedUser; | ||
} catch (error) { | ||
console.error("Error updating user role:", error); | ||
return null; | ||
console.error("Error updating user role:", getErrorMessage(error)); | ||
throw new Error("Failed to update user role. Please try again later."); | ||
} | ||
} | ||
|
||
export const makeAdmin = async (userId: string) => { | ||
return await updateUserRole(userId, "ADMIN"); | ||
try { | ||
return await updateUserRole(userId, UserRole.ADMIN); | ||
} catch (error) { | ||
console.error("Failed to make user admin:", getErrorMessage(error)); | ||
return null; | ||
} | ||
}; | ||
|
||
export const makeParticipant = async (userId: string) => { | ||
return await updateUserRole(userId, "PARTICIPANT"); | ||
try { | ||
return await updateUserRole(userId, UserRole.PARTICIPANT); | ||
} catch (error) { | ||
console.error("Failed to make user participant:", getErrorMessage(error)); | ||
return null; | ||
} | ||
}; | ||
|
||
export const makeCoordinator = async (userId: string) => { | ||
try { | ||
return await updateUserRole(userId, UserRole.COORDINATOR); | ||
} catch (error) { | ||
console.error("Failed to make user coordinator:", getErrorMessage(error)); | ||
return null; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,46 @@ | ||
"use server"; | ||
import { generateCouponCode } from "@/lib/helper"; | ||
import prisma from "@/server/db"; | ||
import { couponSchema } from "@/utils/zod-schemas"; | ||
|
||
export const saveCoupon = async ( | ||
coupon: string, | ||
id: string, | ||
discount: string = "20", | ||
coupon: string, | ||
createdById: string, | ||
discount: number = 20, | ||
numberOfCoupons: number = 1 | ||
) => { | ||
const resp = await prisma.referral.create({ | ||
data: { | ||
code: coupon, | ||
isUsed: false, | ||
createdById: id, | ||
discountPercentage: discount, | ||
}, | ||
}); | ||
try { | ||
const validatCoupon = couponSchema.parse({ coupon, createdById, discount }); | ||
|
||
// Check if the coupon already exists | ||
const couponExists = await prisma.referral.findFirst({ | ||
where: { code: validatCoupon.coupon }, | ||
}); | ||
if (couponExists) { | ||
throw new Error("Coupon code already exists"); | ||
} | ||
|
||
// Create coupons | ||
const createCoupon = async (code: string) => { | ||
return prisma.referral.create({ | ||
data: { | ||
code, | ||
isUsed: false, | ||
createdById: validatCoupon.createdById, | ||
discountPercentage: validatCoupon.discount.toString(), | ||
}, | ||
}); | ||
}; | ||
|
||
const couponCodes = | ||
numberOfCoupons === 1 | ||
? [validatCoupon.coupon] | ||
: Array.from({ length: numberOfCoupons }, () => generateCouponCode(10)); | ||
|
||
const responses = await Promise.all(couponCodes.map(createCoupon)); | ||
return responses; | ||
} catch (error) { | ||
console.error("Error creating coupon:", error); | ||
throw new Error("Failed to create coupon. Please try again later."); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
"use server"; | ||
|
||
import { getServerSideSession } from "@/lib/get-server-session"; | ||
import prisma from "@/server/db"; | ||
|
||
export default async function getPaymentCount() { | ||
const session = await getServerSideSession(); | ||
if (!session) { | ||
return null; | ||
} | ||
|
||
const paymentCount = await prisma.payment.count(); | ||
|
||
return paymentCount; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,20 @@ | ||
import prisma from "@/server/db"; // Adjust the import based on your structure | ||
|
||
export async function getUserById(userId: string) { | ||
const user = await prisma.user.findUnique({ | ||
where: { id: userId }, | ||
select: { | ||
id: true, | ||
role: true, // Include any other fields you need | ||
}, | ||
}); | ||
|
||
return user; | ||
try { | ||
const user = await prisma.user.findUnique({ | ||
where: { id: userId }, | ||
select: { | ||
id: true, | ||
role: true, | ||
}, | ||
}); | ||
if (!user) { | ||
throw new Error(`User with ID ${userId} not found`); | ||
} | ||
return user; | ||
} catch (error) { | ||
console.error("Error getting user by id:", error); | ||
throw new Error("Failed to get user. Please try again later."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
"use server"; | ||
import prisma from "@/server/db"; | ||
import { getServerSideSession } from "@/lib/get-server-session"; | ||
|
||
export default async function getUserCount() { | ||
const session = await getServerSideSession(); | ||
if (!session) { | ||
return null; | ||
} | ||
|
||
const userCount = await prisma.user.count(); | ||
|
||
return userCount; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"use client" | ||
import React from "react"; | ||
|
||
const Loading: React.FC = () => { | ||
return ( | ||
<div className="flex items-center justify-center h-screen text-red-500"> | ||
<div className="loader"></div> | ||
<style jsx>{` | ||
.loader { | ||
border: 8px solid #f3f3f3; /* Light grey */ | ||
border-top: 8px solid #3498db; /* Blue */ | ||
border-radius: 50%; | ||
width: 60px; | ||
height: 60px; | ||
animation: spin 1s linear infinite; | ||
} | ||
@keyframes spin { | ||
0% { | ||
transform: rotate(0deg); | ||
} | ||
100% { | ||
transform: rotate(360deg); | ||
} | ||
} | ||
`}</style> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Loading; |
Oops, something went wrong.