Mobile API: Admin Voting
Add: - src/app/api/mobile/admin/voting/ Fix: - src/app/api/admin/vote/status/[name]/route.ts ### No Issue
This commit is contained in:
@@ -70,7 +70,7 @@ export async function GET(
|
||||
|
||||
for (let i of getAllData) {
|
||||
if (moment(i.akhirVote).diff(moment(), "minutes") < 0) {
|
||||
await prisma.event.update({
|
||||
await prisma.voting.update({
|
||||
where: {
|
||||
id: i.id,
|
||||
},
|
||||
|
||||
38
src/app/api/mobile/admin/voting/[id]/route.ts
Normal file
38
src/app/api/mobile/admin/voting/[id]/route.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib";
|
||||
|
||||
export { GET };
|
||||
|
||||
async function GET(request: Request, { params }: { params: { id: string } }) {
|
||||
const { id } = params;
|
||||
try {
|
||||
const data = await prisma.voting.findUnique({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
include: {
|
||||
Author: true,
|
||||
Voting_Status: true,
|
||||
Voting_DaftarNamaVote: true,
|
||||
},
|
||||
});
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
message: "Success get data voting",
|
||||
data: data,
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
} catch (error) {
|
||||
console.log("[ERROR GET DATA VOTING]", error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: "Error get data voting",
|
||||
reason: (error as Error).message,
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
215
src/app/api/mobile/admin/voting/route.ts
Normal file
215
src/app/api/mobile/admin/voting/route.ts
Normal file
@@ -0,0 +1,215 @@
|
||||
import _ from "lodash";
|
||||
import moment from "moment";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
export { GET };
|
||||
|
||||
async function GET(request: Request) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const category = searchParams.get("category");
|
||||
const fixToStatus = _.startCase(category || "");
|
||||
|
||||
const search = searchParams.get("search");
|
||||
const page = searchParams.get("page");
|
||||
const takeData = 10;
|
||||
const skipData = Number(page) * takeData - takeData;
|
||||
let fixData;
|
||||
|
||||
console.log("CATEGORY", category);
|
||||
console.log("FIX TO STATUS", fixToStatus);
|
||||
|
||||
try {
|
||||
if (category === "dashboard") {
|
||||
const publish = await prisma.voting.count({
|
||||
where: {
|
||||
Voting_Status: {
|
||||
name: "Publish",
|
||||
},
|
||||
isActive: true,
|
||||
isArsip: false,
|
||||
akhirVote: {
|
||||
gte: new Date(),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const review = await prisma.voting.count({
|
||||
where: {
|
||||
Voting_Status: {
|
||||
name: "Review",
|
||||
},
|
||||
isActive: true,
|
||||
isArsip: false,
|
||||
akhirVote: {
|
||||
gte: new Date(),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const reject = await prisma.voting.count({
|
||||
where: {
|
||||
Voting_Status: {
|
||||
name: "Reject",
|
||||
},
|
||||
isActive: true,
|
||||
isArsip: false,
|
||||
akhirVote: {
|
||||
gte: new Date(),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const history = await prisma.voting.count({
|
||||
where: {
|
||||
Voting_Status: {
|
||||
name: "Publish",
|
||||
},
|
||||
isActive: true,
|
||||
isArsip: false,
|
||||
akhirVote: {
|
||||
lte: new Date(),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
fixData = {
|
||||
publish,
|
||||
review,
|
||||
reject,
|
||||
history,
|
||||
};
|
||||
} else if (category === "history") {
|
||||
} else {
|
||||
|
||||
// ====== Status Publish Start ====== //
|
||||
if (fixToStatus === "Publish") {
|
||||
const getAllData = await prisma.voting.findMany({
|
||||
where: {
|
||||
Voting_Status: {
|
||||
name: "Publish",
|
||||
},
|
||||
isActive: true,
|
||||
isArsip: false,
|
||||
akhirVote: {
|
||||
gte: new Date(),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
for (let i of getAllData) {
|
||||
if (moment(i.akhirVote).diff(moment(), "minutes") < 0) {
|
||||
await prisma.voting.update({
|
||||
where: {
|
||||
id: i.id,
|
||||
},
|
||||
data: {
|
||||
isArsip: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const data = await prisma.voting.findMany({
|
||||
take: page ? takeData : undefined,
|
||||
skip: page ? skipData : undefined,
|
||||
orderBy: {
|
||||
createdAt: "desc",
|
||||
},
|
||||
where: {
|
||||
Voting_Status: {
|
||||
name: "Publish",
|
||||
},
|
||||
isActive: true,
|
||||
title: {
|
||||
contains: search ? search : "",
|
||||
mode: "insensitive",
|
||||
},
|
||||
akhirVote: {
|
||||
gte: new Date(),
|
||||
},
|
||||
isArsip: false,
|
||||
},
|
||||
include: {
|
||||
Author: {
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
Profile: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Voting_Kontributor: true,
|
||||
Voting_DaftarNamaVote: true,
|
||||
},
|
||||
});
|
||||
|
||||
fixData = data;
|
||||
|
||||
// ====== Status Publish End ====== //
|
||||
} else {
|
||||
// ====== Status Other Start ====== //
|
||||
const data = await prisma.voting.findMany({
|
||||
take: page ? takeData : undefined,
|
||||
skip: page ? skipData : undefined,
|
||||
orderBy: {
|
||||
createdAt: "desc",
|
||||
},
|
||||
where: {
|
||||
Voting_Status: {
|
||||
name: fixToStatus,
|
||||
},
|
||||
isActive: true,
|
||||
title: {
|
||||
contains: search ? search : "",
|
||||
mode: "insensitive",
|
||||
},
|
||||
isArsip: false,
|
||||
},
|
||||
include: {
|
||||
Author: {
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
Profile: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Voting_Kontributor: true,
|
||||
Voting_DaftarNamaVote: true,
|
||||
},
|
||||
});
|
||||
|
||||
fixData = data;
|
||||
|
||||
// ====== Status Other End ====== //
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
message: `Success get data voting ${category}`,
|
||||
data: fixData,
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
} catch (error) {
|
||||
console.log(`[ERROR GET DATA VOTING: ${category}]`, error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: `Error get data voting ${category}`,
|
||||
reason: (error as Error).message,
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user