upd: api mobile

Deskripsi:
- update load divisi
- tambah anggota
- hapus anggota
- update status admin divisi
- info divisi

- detail divisi

No Issues
This commit is contained in:
amel
2025-05-21 15:58:27 +08:00
parent 13ec913984
commit 589fa01bf6
3 changed files with 59 additions and 47 deletions

View File

@@ -1,6 +1,6 @@
import { prisma } from "@/module/_global";
import { funGetUserByCookies } from "@/module/auth";
import { createLogUser } from "@/module/user";
import { funGetUserById } from "@/module/auth";
import { createLogUserMobile } from "@/module/user";
import _ from "lodash";
import moment from "moment";
import "moment/locale/id";
@@ -12,12 +12,13 @@ export async function GET(request: Request, context: { params: { id: string } })
try {
let allData
const { id } = context.params;
const user = await funGetUserByCookies()
const { searchParams } = new URL(request.url);
const kategori = searchParams.get("cat");
const user = searchParams.get("user");
const userMobile = await funGetUserById({ id: String(user) })
if (user.id == undefined) {
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
if (userMobile.id == "null" || userMobile.id == undefined || userMobile.id == "") {
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 });
}
const data = await prisma.division.findUnique({
@@ -28,7 +29,7 @@ export async function GET(request: Request, context: { params: { id: string } })
});
if (!data) {
return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, data tidak ditemukan", }, { status: 404 });
return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, data tidak ditemukan", }, { status: 200 });
}
if (kategori == "jumlah") {
@@ -244,18 +245,17 @@ export async function GET(request: Request, context: { params: { id: string } })
}
// MENGELUARKAN ANGGOTA DARI DIVISI
export async function DELETE(request: Request, context: { params: { id: string } }) {
try {
const user = await funGetUserByCookies()
if (user.id == undefined) {
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
}
const idDivision = context.params.id
const { id, user } = (await request.json())
const idDivision = context.params.id;
const { id } = (await request.json());
const userMobile = await funGetUserById({ id: String(user) })
if (userMobile.id == "null" || userMobile.id == undefined || userMobile.id == "") {
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 });
}
const data = await prisma.division.count({
where: {
@@ -270,7 +270,7 @@ export async function DELETE(request: Request, context: { params: { id: string }
success: false,
message: "Hapus anggota divisi gagal, data tidak ditemukan",
},
{ status: 404 }
{ status: 200 }
);
}
@@ -281,7 +281,7 @@ export async function DELETE(request: Request, context: { params: { id: string }
});
// create log user
const log = await createLogUser({ act: 'DELETE', desc: 'User mengeluarkan anggota divisi', table: 'division', data: idDivision })
const log = await createLogUserMobile({ act: 'DELETE', desc: 'User mengeluarkan anggota divisi', table: 'division', data: idDivision, user: userMobile.id })
return NextResponse.json(
{
@@ -300,13 +300,13 @@ export async function DELETE(request: Request, context: { params: { id: string }
// MENGGANTI STATUS ADMIN DIVISI
export async function PUT(request: Request, context: { params: { id: string } }) {
try {
const user = await funGetUserByCookies()
if (user.id == undefined) {
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
}
const idDivision = context.params.id;
const { id, isAdmin } = (await request.json());
const { id, isAdmin, user } = (await request.json());
const userMobile = await funGetUserById({ id: String(user) })
if (userMobile.id == "null" || userMobile.id == undefined || userMobile.id == "") {
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 });
}
const data = await prisma.division.count({
where: {
@@ -321,7 +321,7 @@ export async function PUT(request: Request, context: { params: { id: string } })
success: false,
message: "Perubahan status admin gagal, data tidak ditemukan",
},
{ status: 404 }
{ status: 200 }
);
}
@@ -335,7 +335,7 @@ export async function PUT(request: Request, context: { params: { id: string } })
});
// create log user
const log = await createLogUser({ act: 'UPDATE', desc: 'User mengupdate status anggota divisi', table: 'division', data: idDivision })
const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User mengupdate status anggota divisi', table: 'division', data: idDivision, user: userMobile.id })
return NextResponse.json(
{
@@ -354,13 +354,13 @@ export async function PUT(request: Request, context: { params: { id: string } })
// TAMBAH ANGGOTA DIVISI
export async function POST(request: Request, context: { params: { id: string } }) {
try {
const user = await funGetUserByCookies()
if (user.id == undefined) {
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
}
const { member, user } = await request.json()
const idDivision = context.params.id
const userMobile = await funGetUserById({ id: String(user) })
const member = await request.json();
const idDivision = context.params.id;
if (userMobile.id == "null" || userMobile.id == undefined || userMobile.id == "") {
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 });
}
const data = await prisma.division.count({
@@ -376,7 +376,7 @@ export async function POST(request: Request, context: { params: { id: string } }
success: false,
message: "Tambah anggota divisi gagal, data tidak ditemukan",
},
{ status: 404 }
{ status: 200 }
);
}
@@ -392,7 +392,7 @@ export async function POST(request: Request, context: { params: { id: string } }
})
// create log user
const log = await createLogUser({ act: 'CREATE', desc: 'User menambah anggota divisi', table: 'division', data: idDivision })
const log = await createLogUserMobile({ act: 'CREATE', desc: 'User menambah anggota divisi', table: 'division', data: idDivision, user: userMobile.id })
return NextResponse.json({ success: true, message: "Berhasil menambahkan anggota divisi" }, { status: 200 });
} catch (error) {

View File

@@ -1,5 +1,5 @@
import { prisma } from "@/module/_global";
import { funGetUserByCookies } from "@/module/auth";
import { funGetUserByCookies, funGetUserById } from "@/module/auth";
import { createLogUser } from "@/module/user";
import _ from "lodash";
import { NextResponse } from "next/server";
@@ -9,9 +9,12 @@ import { NextResponse } from "next/server";
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 { searchParams } = new URL(request.url);
const user = searchParams.get("user");
const userMobile = await funGetUserById({ id: String(user) })
if (userMobile.id == "null" || userMobile.id == undefined || userMobile.id == "") {
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 });
}
const data = await prisma.division.findUnique({
@@ -22,7 +25,7 @@ export async function GET(request: Request, context: { params: { id: string } })
});
if (!data) {
return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, data tidak ditemukan", }, { status: 404 });
return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, data tidak ditemukan", }, { status: 200 });
}
const member = await prisma.divisionMember.findMany({
@@ -42,9 +45,16 @@ export async function GET(request: Request, context: { params: { id: string } })
}
}
},
orderBy: {
isAdmin: 'desc',
}
orderBy: [
{
isAdmin: 'desc',
},
{
User: {
name: 'asc'
}
}
]
})
const fixMember = member.map((v: any) => ({

View File

@@ -1,26 +1,28 @@
import { prisma } from "@/module/_global";
import { funGetUserByCookies } from "@/module/auth";
import { funGetUserById } from "@/module/auth";
import _, { ceil } from "lodash";
import moment from "moment";
import { NextResponse } from "next/server";
export async function GET(request: Request) {
try {
const user = await funGetUserByCookies()
const { searchParams } = new URL(request.url)
const idGroup = searchParams.get("group")
const division = searchParams.get("division")
const date = searchParams.get("date")
const dateAkhir = searchParams.get("date-end")
const kat = searchParams.get("cat")
const user = searchParams.get("user")
let grup
if (user.id == undefined) {
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 })
const userMobile = await funGetUserById({ id: String(user) })
if (userMobile.id == "null" || userMobile.id == undefined || userMobile.id == "") {
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 });
}
if (idGroup == "null" || idGroup == undefined || idGroup == "") {
grup = user.idGroup
if (idGroup == "null" || idGroup == undefined || idGroup == "" || idGroup == null || idGroup == "undefined") {
grup = userMobile.idGroup
} else {
grup = idGroup
}