-
Notifications
You must be signed in to change notification settings - Fork 0
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 #76 from MinaFoundation/feature/admin-proposal-status
Feature/admin proposal status
- Loading branch information
Showing
15 changed files
with
722 additions
and
35 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
This file was deleted.
Oops, something went wrong.
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,11 @@ | ||
import { ManageProposalsComponent } from "@/components/admin/ManageProposals"; | ||
import { Metadata } from "next"; | ||
|
||
export const metadata: Metadata = { | ||
title: "Manage Proposals | MEF Admin", | ||
description: "Manage proposal statuses and funding round assignments", | ||
}; | ||
|
||
export default function ManageProposalsPage() { | ||
return <ManageProposalsComponent />; | ||
} |
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,78 @@ | ||
import { NextResponse } from "next/server"; | ||
import prisma from "@/lib/prisma"; | ||
import { getOrCreateUserFromRequest } from "@/lib/auth"; | ||
import { AdminService } from "@/services/AdminService"; | ||
import { ApiResponse } from "@/lib/api-response"; | ||
import { AppError } from "@/lib/errors"; | ||
import { AuthErrors } from "@/constants/errors"; | ||
import { UserMetadata } from "@/services"; | ||
|
||
const adminService = new AdminService(prisma); | ||
|
||
export async function GET( | ||
request: Request, | ||
{ params }: { params: Promise<{ id: string }> } | ||
) { | ||
try { | ||
const user = await getOrCreateUserFromRequest(request); | ||
if (!user) { | ||
throw AppError.unauthorized(AuthErrors.UNAUTHORIZED); | ||
} | ||
|
||
// Check if user is admin | ||
const isAdmin = await adminService.checkAdminStatus(user.id, user.linkId); | ||
if (!isAdmin) { | ||
throw AppError.forbidden(AuthErrors.FORBIDDEN); | ||
} | ||
|
||
// Get funding round ID from params | ||
const fundingRoundId = (await params).id; | ||
|
||
// Verify funding round exists | ||
const fundingRound = await prisma.fundingRound.findUnique({ | ||
where: { id: fundingRoundId }, | ||
}); | ||
|
||
if (!fundingRound) { | ||
throw AppError.notFound("Funding round not found"); | ||
} | ||
|
||
// Get proposals for the funding round | ||
const proposals = await prisma.proposal.findMany({ | ||
where: { | ||
fundingRoundId, | ||
}, | ||
include: { | ||
user: { | ||
select: { | ||
metadata: true, | ||
}, | ||
}, | ||
fundingRound: { | ||
select: { | ||
name: true, | ||
}, | ||
}, | ||
}, | ||
orderBy: [ | ||
{ status: "asc" }, | ||
{ createdAt: "desc" }, | ||
], | ||
}); | ||
|
||
// Transform the data for the frontend | ||
const transformedProposals = proposals.map(proposal => ({ | ||
id: proposal.id, | ||
proposalName: proposal.proposalName, | ||
status: proposal.status, | ||
budgetRequest: proposal.budgetRequest, | ||
createdAt: proposal.createdAt, | ||
submitter: (proposal.user?.metadata as UserMetadata)?.username || "Unknown", | ||
fundingRound: proposal.fundingRound?.name, | ||
})); | ||
|
||
return ApiResponse.success(transformedProposals); | ||
} catch (error) { | ||
return ApiResponse.error(error); | ||
} | ||
} |
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,72 @@ | ||
import { NextResponse } from "next/server"; | ||
import prisma from "@/lib/prisma"; | ||
import { getOrCreateUserFromRequest } from "@/lib/auth"; | ||
import { AdminService } from "@/services/AdminService"; | ||
import { ApiResponse } from "@/lib/api-response"; | ||
import { AppError } from "@/lib/errors"; | ||
import { AuthErrors } from "@/constants/errors"; | ||
import { ProposalStatus } from "@prisma/client"; | ||
import { z } from "zod"; | ||
import { UserMetadata } from "@/services"; | ||
|
||
const adminService = new AdminService(prisma); | ||
|
||
// Validation schema for status update | ||
const updateStatusSchema = z.object({ | ||
status: z.nativeEnum(ProposalStatus), | ||
}); | ||
|
||
export async function PATCH( | ||
request: Request, | ||
{ params }: { params: Promise<{ id: string }> } | ||
) { | ||
try { | ||
const user = await getOrCreateUserFromRequest(request); | ||
if (!user) { | ||
throw AppError.unauthorized(AuthErrors.UNAUTHORIZED); | ||
} | ||
|
||
// Check if user is admin | ||
const isAdmin = await adminService.checkAdminStatus(user.id, user.linkId); | ||
if (!isAdmin) { | ||
throw AppError.forbidden(AuthErrors.FORBIDDEN); | ||
} | ||
|
||
// Validate request body | ||
const body = await request.json(); | ||
const { status } = updateStatusSchema.parse(body); | ||
|
||
// Update proposal status | ||
const updatedProposal = await prisma.proposal.update({ | ||
where: { id: parseInt((await params).id) }, | ||
data: { status }, | ||
include: { | ||
user: { | ||
select: { | ||
metadata: true, | ||
}, | ||
}, | ||
fundingRound: { | ||
select: { | ||
name: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
return ApiResponse.success({ | ||
id: updatedProposal.id, | ||
proposalName: updatedProposal.proposalName, | ||
status: updatedProposal.status, | ||
budgetRequest: updatedProposal.budgetRequest, | ||
createdAt: updatedProposal.createdAt, | ||
submitter: (updatedProposal.user?.metadata as UserMetadata)?.username || "Unknown", | ||
fundingRound: updatedProposal.fundingRound?.name, | ||
}); | ||
} catch (error) { | ||
if (error instanceof z.ZodError) { | ||
throw AppError.badRequest("Invalid status value"); | ||
} | ||
return ApiResponse.error(error); | ||
} | ||
} |
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,59 @@ | ||
import { NextResponse } from "next/server"; | ||
import prisma from "@/lib/prisma"; | ||
import { getOrCreateUserFromRequest } from "@/lib/auth"; | ||
import { AdminService } from "@/services/AdminService"; | ||
import { ApiResponse } from "@/lib/api-response"; | ||
import { AppError } from "@/lib/errors"; | ||
import { AuthErrors } from "@/constants/errors"; | ||
import { UserMetadata } from "@/services"; | ||
|
||
const adminService = new AdminService(prisma); | ||
|
||
export async function GET(request: Request) { | ||
try { | ||
const user = await getOrCreateUserFromRequest(request); | ||
if (!user) { | ||
throw AppError.unauthorized(AuthErrors.UNAUTHORIZED); | ||
} | ||
|
||
// Check if user is admin | ||
const isAdmin = await adminService.checkAdminStatus(user.id, user.linkId); | ||
if (!isAdmin) { | ||
throw AppError.forbidden(AuthErrors.FORBIDDEN); | ||
} | ||
|
||
const proposals = await prisma.proposal.findMany({ | ||
include: { | ||
user: { | ||
select: { | ||
metadata: true, | ||
}, | ||
}, | ||
fundingRound: { | ||
select: { | ||
name: true, | ||
}, | ||
}, | ||
}, | ||
orderBy: [ | ||
{ status: "asc" }, | ||
{ createdAt: "desc" }, | ||
], | ||
}); | ||
|
||
// Transform the data for the frontend | ||
const transformedProposals = proposals.map(proposal => ({ | ||
id: proposal.id, | ||
proposalName: proposal.proposalName, | ||
status: proposal.status, | ||
budgetRequest: proposal.budgetRequest, | ||
createdAt: proposal.createdAt, | ||
submitter: (proposal.user?.metadata as UserMetadata)?.username || "Unknown", | ||
fundingRound: proposal.fundingRound?.name, | ||
})); | ||
|
||
return ApiResponse.success(transformedProposals); | ||
} catch (error) { | ||
return ApiResponse.error(error); | ||
} | ||
} |
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
Oops, something went wrong.