Merge pull request #135 from bipproduction/lukman/21-agustus-2024
feat : update project get all
This commit is contained in:
41
src/app/api/project/[id]/route.ts
Normal file
41
src/app/api/project/[id]/route.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { prisma } from "@/module/_global";
|
||||
import { funGetUserByCookies } from "@/module/auth";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
|
||||
// GET ONE PROJECT
|
||||
export async function GET(request: Request, context: { params: { id: string } }) {
|
||||
try {
|
||||
const { id } = context.params;
|
||||
const user = await funGetUserByCookies()
|
||||
if (user.id == undefined) {
|
||||
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
|
||||
}
|
||||
|
||||
const data = await prisma.project.findFirst({
|
||||
where: {
|
||||
id: id
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
desc: true,
|
||||
status: true,
|
||||
ProjectMember: {
|
||||
where: {
|
||||
isActive: true
|
||||
},
|
||||
select: {
|
||||
idUser: true
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json({ success: true, message: "Berhasil mendapatkan project", data: data, }, { status: 200 });
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal mendapatkan project, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
65
src/app/api/project/route.ts
Normal file
65
src/app/api/project/route.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { prisma } from "@/module/_global";
|
||||
import { funGetUserByCookies } from "@/module/auth";
|
||||
import _ from "lodash";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
|
||||
|
||||
// GET ALL DATA PROJECT
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
const user = await funGetUserByCookies()
|
||||
if (user.id == undefined) {
|
||||
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(request.url);
|
||||
|
||||
const name = searchParams.get('search');
|
||||
const status = searchParams.get('status');
|
||||
const villageId = user.idVillage
|
||||
const groupId = user.idGroup
|
||||
const userId = user.id
|
||||
|
||||
|
||||
const data = await prisma.project.findMany({
|
||||
where: {
|
||||
isActive: true,
|
||||
idVillage: String(villageId),
|
||||
idGroup: String(groupId),
|
||||
createdBy: String(userId),
|
||||
name: {
|
||||
contains: (name == undefined || name == "null") ? "" : name,
|
||||
mode: "insensitive"
|
||||
},
|
||||
status: (status == "0" || status == "1" || status == "2" || status == "3") ? Number(status) : 0
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
desc: true,
|
||||
status: true,
|
||||
ProjectMember: {
|
||||
where: {
|
||||
isActive: true
|
||||
},
|
||||
select: {
|
||||
idUser: true
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const omitData = data.map((v: any) => ({
|
||||
..._.omit(v, ["ProjectMember"]),
|
||||
member: v.ProjectMember.length
|
||||
}))
|
||||
|
||||
|
||||
return NextResponse.json({ success: true, message: "Berhasil mendapatkan project", data: omitData, }, { status: 200 });
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal mendapatkan project, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user