upd: pengumuman
deskripsiL : - update api ai pengumuman No Issues
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
import { prisma } from "@/module/_global";
|
import { prisma } from "@/module/_global";
|
||||||
import _ from "lodash";
|
import _ from "lodash";
|
||||||
import moment from "moment";
|
|
||||||
import "moment/locale/id";
|
import "moment/locale/id";
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
export const dynamic = 'force-dynamic'
|
export const dynamic = 'force-dynamic'
|
||||||
@@ -11,42 +10,41 @@ export const dynamic = 'force-dynamic'
|
|||||||
export async function GET(request: Request) {
|
export async function GET(request: Request) {
|
||||||
try {
|
try {
|
||||||
const { searchParams } = new URL(request.url);
|
const { searchParams } = new URL(request.url);
|
||||||
const judul = searchParams.get('q');
|
const judul = searchParams.get('search');
|
||||||
const page = searchParams.get('p');
|
const page = searchParams.get('page');
|
||||||
const get = searchParams.get('get');
|
const get = searchParams.get('get');
|
||||||
const villageId = searchParams.get('desa');
|
const villageId = searchParams.get('desa');
|
||||||
const dataSkip = page == null || page == undefined ? 0 : Number(page) * 10 - 10;
|
const active = searchParams.get('active');
|
||||||
|
|
||||||
|
let getFix = 0;
|
||||||
|
if (get == null || get == undefined || get == "" || _.isNaN(Number(get))) {
|
||||||
|
getFix = 10;
|
||||||
|
} else {
|
||||||
|
getFix = Number(get);
|
||||||
|
}
|
||||||
|
|
||||||
|
const dataSkip = page == null || page == undefined ? 0 : Number(page) * getFix - getFix;
|
||||||
|
|
||||||
|
|
||||||
let kondisi: any = {
|
let kondisi: any = {
|
||||||
idVillage: String(villageId),
|
idVillage: String(villageId),
|
||||||
isActive: true,
|
isActive: (active == "false" || active == undefined) ? false : true,
|
||||||
title: {
|
title: {
|
||||||
contains: (judul == undefined || judul == null) ? "" : judul,
|
contains: (judul == undefined || judul == null) ? "" : judul,
|
||||||
mode: "insensitive"
|
mode: "insensitive"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const announcements = await prisma.announcement.findMany({
|
const data = await prisma.announcement.findMany({
|
||||||
skip: dataSkip,
|
skip: dataSkip,
|
||||||
take: (get == null || get == undefined || get == "" || _.isNaN(Number(get))) ? 10 : Number(get),
|
take: getFix,
|
||||||
where: kondisi,
|
where: kondisi,
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
title: true,
|
|
||||||
desc: true,
|
|
||||||
createdAt: true,
|
|
||||||
},
|
|
||||||
orderBy: {
|
orderBy: {
|
||||||
createdAt: 'desc'
|
createdAt: 'desc'
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const allData = announcements.map((v: any) => ({
|
return NextResponse.json({ success: true, message: "Berhasil mendapatkan pengumuman", data, }, { status: 200 });
|
||||||
..._.omit(v, ["createdAt"]),
|
|
||||||
createdAt: moment(v.createdAt).format("ll")
|
|
||||||
}))
|
|
||||||
|
|
||||||
return NextResponse.json({ success: true, message: "Berhasil mendapatkan pengumuman", data: allData, }, { status: 200 });
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
return NextResponse.json({ success: false, message: "Gagal mendapatkan pengumuman, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
|
return NextResponse.json({ success: false, message: "Gagal mendapatkan pengumuman, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
|
||||||
|
|||||||
Reference in New Issue
Block a user