upd: api mobile

Deskripsi:
- api mobile load,  edit, cancel project
- api moblie update status task project

No Issues
This commit is contained in:
amel
2025-05-09 17:37:35 +08:00
parent 7149c88e5d
commit a597842566
2 changed files with 71 additions and 64 deletions

View File

@@ -1,8 +1,9 @@
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";
import { NextResponse } from "next/server";
@@ -11,12 +12,14 @@ 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 userMobile = searchParams.get("user");
if (user.id == undefined) {
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
const user = await funGetUserById({ id: String(userMobile) })
if (user.id == "null" || user.id == undefined || user.id == "") {
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 });
}
const data = await prisma.project.findUnique({
@@ -27,7 +30,7 @@ export async function GET(request: Request, context: { params: { id: string } })
});
if (!data) {
return NextResponse.json({ success: false, message: "Gagal mendapatkan kegiatan, data tidak ditemukan", }, { status: 404 });
return NextResponse.json({ success: false, message: "Gagal mendapatkan kegiatan, data tidak ditemukan", }, { status: 200 });
}
@@ -146,13 +149,14 @@ export async function GET(request: Request, context: { params: { id: string } })
//CREATE NEW DETAIL TASK PROJECT
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 { id } = context.params
const { name, dateStart, dateEnd, } = await request.json()
const { name, dateStart, dateEnd, 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.project.count({
where: {
@@ -165,7 +169,7 @@ export async function POST(request: Request, context: { params: { id: string } }
{
success: false, message: "Gagal mendapatkan kegiatan, data tidak ditemukan",
},
{ status: 404 }
{ status: 200 }
);
}
@@ -182,9 +186,9 @@ export async function POST(request: Request, context: { params: { id: string } }
})
// create log user
const log = await createLogUser({ act: 'CREATE', desc: 'User membuat data tahapan kegiatan', table: 'projectTask', data: String(dataCreate.id) })
const log = await createLogUserMobile({ act: 'CREATE', desc: 'User membuat data tahapan kegiatan', table: 'projectTask', data: String(dataCreate.id), user: userMobile.id })
return NextResponse.json({ success: true, message: "Detail tahapan kegiatan berhasil ditambahkan", data: dataCreate, }, { status: 200 });
return NextResponse.json({ success: true, message: "Detail tahapan kegiatan berhasil ditambahkan" }, { status: 200 });
} catch (error) {
console.error(error);
@@ -196,13 +200,15 @@ export async function POST(request: Request, context: { params: { id: string } }
// PEMBATALAN TASK PROJECT
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 { id } = context.params
const { reason, 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 { id } = context.params
const { reason } = await request.json()
const data = await prisma.project.count({
where: {
id: id
@@ -214,7 +220,7 @@ export async function DELETE(request: Request, context: { params: { id: string }
{
success: false, message: "Gagal mendapatkan kegiatan, data tidak ditemukan",
},
{ status: 404 }
{ status: 200 }
);
}
@@ -229,7 +235,7 @@ export async function DELETE(request: Request, context: { params: { id: string }
})
// create log user
const log = await createLogUser({ act: 'UPDATE', desc: 'User membatalkan data kegiatan', table: 'project', data: String(id) })
const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User membatalkan data kegiatan', table: 'project', data: String(id), user: userMobile.id })
return NextResponse.json({ success: true, message: "Kegiatan berhasil dibatalkan" }, { status: 200 });
} catch (error) {
@@ -241,13 +247,13 @@ export async function DELETE(request: Request, context: { params: { id: string }
// EDIT PROJECT
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 { id } = context.params
const { name } = await request.json()
const { name, 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.project.count({
where: {
@@ -260,7 +266,7 @@ export async function PUT(request: Request, context: { params: { id: string } })
{
success: false, message: "Gagal mendapatkan kegiatan, data tidak ditemukan",
},
{ status: 404 }
{ status: 200 }
);
}
@@ -274,7 +280,7 @@ export async function PUT(request: Request, context: { params: { id: string } })
})
// create log user
const log = await createLogUser({ act: 'UPDATE', desc: 'User mengupdate data kegiatan', table: 'project', data: String(id) })
const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User mengupdate data kegiatan', table: 'project', data: String(id), user: userMobile.id })
return NextResponse.json({ success: true, message: "Kegiatan berhasil diupdate" }, { status: 200 });

View File

@@ -1,18 +1,20 @@
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 { NextResponse } from "next/server";
// HAPUS DETAIL PROJECT
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 { id } = context.params;
const { idProject } = (await request.json());
const { idProject, 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.projectTask.count({
where: {
@@ -26,7 +28,7 @@ export async function DELETE(request: Request, context: { params: { id: string }
success: false,
message: "Hapus tahapan kegiatan gagal, data tidak ditemukan",
},
{ status: 404 }
{ status: 200 }
);
}
@@ -68,14 +70,13 @@ export async function DELETE(request: Request, context: { params: { id: string }
})
// create log user
const log = await createLogUser({ act: 'DELETE', desc: 'User menghapus tahapan kegiatan', table: 'projectTask', data: String(id) })
const log = await createLogUserMobile({ act: 'DELETE', desc: 'User menghapus tahapan kegiatan', table: 'projectTask', data: String(id), user: userMobile.id })
return NextResponse.json(
{
success: true,
message: "Tahapan kegiatan berhasil dihapus",
data,
},
{ status: 200 }
);
@@ -89,13 +90,13 @@ export async function DELETE(request: Request, context: { params: { id: string }
// EDIT STATUS DETAIL PROJECT
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 { id } = context.params;
const { status, idProject } = (await request.json());
const { status, idProject, 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.projectTask.count({
where: {
@@ -108,7 +109,7 @@ export async function PUT(request: Request, context: { params: { id: string } })
{
success: false, message: "Gagal mendapatkan kegiatan, data tidak ditemukan",
},
{ status: 404 }
{ status: 200 }
);
}
@@ -152,9 +153,9 @@ export async function PUT(request: Request, context: { params: { id: string } })
})
// create log user
const log = await createLogUser({ act: 'UPDATE', desc: 'User mengupdate status tahapan kegiatan', table: 'projectTask', data: String(id) })
const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User mengupdate status tahapan kegiatan', table: 'projectTask', data: String(id), user: userMobile.id })
return NextResponse.json({ success: true, message: "Status tahapan kegiatan berhasil diupdate", data }, { status: 200 });
return NextResponse.json({ success: true, message: "Status tahapan kegiatan berhasil diupdate" }, { status: 200 });
} catch (error) {
console.error(error);
@@ -166,12 +167,14 @@ export async function PUT(request: Request, context: { params: { id: string } })
// GET ONE DETAIL PROJECT
export async function GET(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 { id } = context.params;
const { 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 { id } = context.params;
const data = await prisma.projectTask.findUnique({
where: {
id: String(id),
@@ -184,7 +187,7 @@ export async function GET(request: Request, context: { params: { id: string } })
{
success: false, message: "Gagal mendapatkan kegiatan, data tidak ditemukan",
},
{ status: 404 }
{ status: 200 }
);
}
@@ -199,15 +202,13 @@ export async function GET(request: Request, context: { params: { id: string } })
// EDIT DETAIL PROJECT
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 { id } = context.params;
const { title, dateStart, dateEnd } = (await request.json());
const { title, dateStart, dateEnd, 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 dataTask = await prisma.projectTask.count({
where: {
@@ -220,7 +221,7 @@ export async function POST(request: Request, context: { params: { id: string } }
{
success: false, message: "Gagal mendapatkan kegiatan, data tidak ditemukan",
},
{ status: 404 }
{ status: 200 }
);
}
@@ -236,9 +237,9 @@ export async function POST(request: Request, context: { params: { id: string } }
})
// create log user
const log = await createLogUser({ act: 'UPDATE', desc: 'User mengupdate tahapan kegiatan', table: 'projectTask', data: String(id) })
const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User mengupdate tahapan kegiatan', table: 'projectTask', data: String(id), user: userMobile.id })
return NextResponse.json({ success: true, message: "Detail tahapan kegiatan berhasil diupdate", data }, { status: 200 });
return NextResponse.json({ success: true, message: "Detail tahapan kegiatan berhasil diupdate" }, { status: 200 });
} catch (error) {
console.error(error);