127 lines
4.4 KiB
TypeScript
127 lines
4.4 KiB
TypeScript
import { prisma } from "@/module/_global";
|
|
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";
|
|
|
|
// GET ALL NOTIFIKASI
|
|
export async function GET(request: Request) {
|
|
try {
|
|
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: userMobile.id
|
|
},
|
|
orderBy: [
|
|
{
|
|
createdAt: 'desc'
|
|
}
|
|
]
|
|
|
|
});
|
|
|
|
const allData = announcements.map((v: any) => ({
|
|
..._.omit(v, ["createdAt"]),
|
|
createdAt: moment(v.createdAt).format("ll")
|
|
}))
|
|
|
|
return NextResponse.json({ success: true, message: "Berhasil mendapatkan notifikasi", data: allData, }, { 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 });
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// UPDATE READ NOTIFIKASI
|
|
export async function PUT(request: Request, context: { params: { id: string } }) {
|
|
try {
|
|
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 data = await prisma.notifications.count({
|
|
where: {
|
|
id: id,
|
|
},
|
|
});
|
|
|
|
if (data == 0) {
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
message: "Gagal mendapatkan data, data tidak ditemukan",
|
|
},
|
|
{ status: 200 }
|
|
);
|
|
}
|
|
|
|
const result = await prisma.notifications.update({
|
|
where: {
|
|
id: id,
|
|
},
|
|
data: {
|
|
isRead: true,
|
|
},
|
|
});
|
|
|
|
// create log user
|
|
const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User membaca notifikasi', table: 'notifications', data: id, user: userMobile.id })
|
|
|
|
return NextResponse.json({ success: true, message: "Berhasil membaca notifikasi", }, { status: 200 });
|
|
|
|
} catch (error) {
|
|
console.error(error);
|
|
return NextResponse.json({ success: false, message: "Gagal membaca notifikasi, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
|
|
// UPDATE READ ALL NOTIFICATION
|
|
export async function POST(request: Request) {
|
|
try {
|
|
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: userMobile.id,
|
|
isRead: false
|
|
},
|
|
data: {
|
|
isRead: true
|
|
}
|
|
})
|
|
|
|
// create log user
|
|
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 });
|
|
|
|
} catch (error) {
|
|
console.error(error);
|
|
return NextResponse.json({ success: false, message: "Gagal mengupdate notifikasi, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
|
|
}
|
|
} |