upd: api mobile

deskripsi:
- list notifikasi
- read notifikasi

No Issues
This commit is contained in:
amel
2025-06-26 14:34:07 +08:00
parent 0b0ecc7791
commit 4e4fc7d1cf
3 changed files with 37 additions and 25 deletions

View File

@@ -1,6 +1,6 @@
import { prisma } from "@/module/_global"; import { prisma } from "@/module/_global";
import { funGetUserByCookies } from "@/module/auth"; import { funGetUserById } from "@/module/auth";
import { createLogUser } from "@/module/user"; import { createLogUserMobile } from "@/module/user";
import _ from "lodash"; import _ from "lodash";
import moment from "moment"; import moment from "moment";
import "moment/locale/id"; import "moment/locale/id";
@@ -9,21 +9,22 @@ import { NextResponse } from "next/server";
// GET ALL NOTIFIKASI // GET ALL NOTIFIKASI
export async function GET(request: Request) { export async function GET(request: Request) {
try { 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 { searchParams } = new URL(request.url);
const user = searchParams.get('user');
const page = searchParams.get('page'); const page = searchParams.get('page');
const dataSkip = Number(page) * 10 - 10; const dataSkip = Number(page) * 10 - 10;
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 announcements = await prisma.notifications.findMany({ const announcements = await prisma.notifications.findMany({
skip: dataSkip, skip: dataSkip,
take: 10, take: 10,
where: { where: {
isActive: true, isActive: true,
idUserTo: user.id idUserTo: userMobile.id
}, },
orderBy: [ orderBy: [
{ {
@@ -53,11 +54,13 @@ export async function GET(request: Request) {
// UPDATE READ NOTIFIKASI // UPDATE READ NOTIFIKASI
export async function PUT(request: Request, context: { params: { id: string } }) { export async function PUT(request: Request, context: { params: { id: string } }) {
try { try {
const user = await funGetUserByCookies() const { id, user } = await request.json();
if (user.id == undefined) { const userMobile = await funGetUserById({ id: String(user) });
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 { id } = await request.json();
const data = await prisma.notifications.count({ const data = await prisma.notifications.count({
where: { where: {
id: id, id: id,
@@ -70,7 +73,7 @@ export async function PUT(request: Request, context: { params: { id: string } })
success: false, success: false,
message: "Gagal mendapatkan data, data tidak ditemukan", message: "Gagal mendapatkan data, data tidak ditemukan",
}, },
{ status: 404 } { status: 200 }
); );
} }
@@ -84,13 +87,13 @@ export async function PUT(request: Request, context: { params: { id: string } })
}); });
// create log user // create log user
const log = await createLogUser({ act: 'UPDATE', desc: 'User membaca notifikasi', table: 'notifications', data: id }) const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User membaca notifikasi', table: 'notifications', data: id, user: userMobile.id })
return NextResponse.json({ success: true, message: "Berhasil mendapatkan notifikasi", }, { status: 200 }); return NextResponse.json({ success: true, message: "Berhasil membaca notifikasi", }, { status: 200 });
} catch (error) { } catch (error) {
console.error(error); console.error(error);
return NextResponse.json({ success: false, message: "Gagal mendapatkan notifikasi, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); return NextResponse.json({ success: false, message: "Gagal membaca notifikasi, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
} }
} }
@@ -98,14 +101,16 @@ export async function PUT(request: Request, context: { params: { id: string } })
// UPDATE READ ALL NOTIFICATION // UPDATE READ ALL NOTIFICATION
export async function POST(request: Request) { export async function POST(request: Request) {
try { try {
const user = await funGetUserByCookies() const { user } = await request.json();
if (user.id == undefined) { const userMobile = await funGetUserById({ id: String(user) });
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 upd = await prisma.notifications.updateMany({ const upd = await prisma.notifications.updateMany({
where: { where: {
idUserTo: user.id, idUserTo: userMobile.id,
isRead: false isRead: false
}, },
data: { data: {
@@ -114,7 +119,7 @@ export async function POST(request: Request) {
}) })
// create log user // create log user
const log = await createLogUser({ act: 'UPDATE', desc: 'User menandai semua notifikasi', table: 'notifications', data: '' }) const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User menandai semua notifikasi', table: 'notifications', data: '', user: userMobile.id })
return NextResponse.json({ success: true, message: "Berhasil mengupdate notifikasi", }, { status: 200 }); return NextResponse.json({ success: true, message: "Berhasil mengupdate notifikasi", }, { status: 200 });

View File

@@ -24,8 +24,9 @@ export async function GET(request: Request) {
body: 'Check out the latest features in our app.', body: 'Check out the latest features in our app.',
}, },
data: { data: {
category: 'value1', id: 'cm1eqwkwj00067j2bzejyh9u7',
content: 'value2', category: 'division',
content: 'cm1eqwkvu00017j2b8qnisp9g',
}, },
}; };

View File

@@ -42,6 +42,11 @@ export async function GET(request: Request, context: { params: { id: string } })
id: true id: true
}, },
}, },
Village:{
select:{
name:true
}
}
}, },
}); });
@@ -51,10 +56,11 @@ export async function GET(request: Request, context: { params: { id: string } })
const idUserRole = users?.UserRole.id const idUserRole = users?.UserRole.id
const phone = users?.phone.substr(2) const phone = users?.phone.substr(2)
const role = users?.UserRole.name const role = users?.UserRole.name
const village = users?.Village.name
const result = { ...userData, group, position, idUserRole, phone, role }; const result = { ...userData, group, position, idUserRole, phone, role, village };
const omitData = _.omit(result, ["Group", "Position", "UserRole"]); const omitData = _.omit(result, ["Group", "Position", "UserRole", "Village"]);