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 { 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";
@@ -9,21 +9,22 @@ import { NextResponse } from "next/server";
// GET ALL NOTIFIKASI
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 user = searchParams.get('user');
const page = searchParams.get('page');
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({
skip: dataSkip,
take: 10,
where: {
isActive: true,
idUserTo: user.id
idUserTo: userMobile.id
},
orderBy: [
{
@@ -53,11 +54,13 @@ export async function GET(request: Request) {
// UPDATE READ NOTIFIKASI
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, 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 } = await request.json();
const data = await prisma.notifications.count({
where: {
id: id,
@@ -70,7 +73,7 @@ export async function PUT(request: Request, context: { params: { id: string } })
success: false,
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
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) {
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
export async function POST(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 { 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 upd = await prisma.notifications.updateMany({
where: {
idUserTo: user.id,
idUserTo: userMobile.id,
isRead: false
},
data: {
@@ -114,7 +119,7 @@ export async function POST(request: Request) {
})
// 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 });

View File

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

View File

@@ -42,6 +42,11 @@ export async function GET(request: Request, context: { params: { id: string } })
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 phone = users?.phone.substr(2)
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"]);