upd: notifikasi
Deskripsi: - jumlah notifikasi - nama desa di page home - masang notifikasi pada project dan pengumuman No Issues
This commit is contained in:
@@ -117,6 +117,7 @@ export async function POST(request: Request) {
|
||||
const { title, desc, groups } = (await request.json());
|
||||
const villaId = user.idVillage
|
||||
const userId = user.id
|
||||
const userRoleLogin = user.idUserRole
|
||||
|
||||
const data = await prisma.announcement.create({
|
||||
data: {
|
||||
@@ -177,27 +178,43 @@ export async function POST(request: Request) {
|
||||
desc: 'Anda memiliki pengumuman baru. Silahkan periksa detailnya.'
|
||||
}))
|
||||
|
||||
if (userRoleLogin != "supadmin") {
|
||||
const perbekel = await prisma.user.findFirst({
|
||||
where: {
|
||||
isActive: true,
|
||||
idUserRole: "supadmin",
|
||||
idVillage: user.idVillage
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
dataNotif.push({
|
||||
idUserTo: perbekel?.id,
|
||||
idUserFrom: userId,
|
||||
category: 'announcement',
|
||||
idContent: data.id,
|
||||
title: 'Pengumuman Baru',
|
||||
desc: 'Anda memiliki pengumuman baru. Silahkan periksa detailnya.'
|
||||
})
|
||||
}
|
||||
|
||||
const insertNotif = await prisma.notifications.createMany({
|
||||
data: dataNotif
|
||||
})
|
||||
|
||||
for (let index = 0; index < dataNotif.length; index++) {
|
||||
// for (let index = 0; index < dataNotif.length; index++) {
|
||||
|
||||
const user = dataNotif[index].idUserTo
|
||||
const title = dataNotif[index].title
|
||||
const desc = dataNotif[index].desc
|
||||
// const user = dataNotif[index].idUserTo
|
||||
// const title = dataNotif[index].title
|
||||
// const desc = dataNotif[index].desc
|
||||
|
||||
|
||||
mtqq_client.publish("app_SDM", JSON.stringify({
|
||||
"user": "clzm6swhg000tfgbhm3bau9ti",
|
||||
"title": title,
|
||||
"category": "announcement",
|
||||
"description": desc
|
||||
}))
|
||||
}
|
||||
// mtqq_client.publish("app_SDM", JSON.stringify({
|
||||
// "user": "clzm6swhg000tfgbhm3bau9ti",
|
||||
// "title": title,
|
||||
// "category": "announcement",
|
||||
// "description": desc
|
||||
// }))
|
||||
// }
|
||||
|
||||
|
||||
// create log user
|
||||
|
||||
94
src/app/api/home/notification/route.ts
Normal file
94
src/app/api/home/notification/route.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
import { prisma } from "@/module/_global";
|
||||
import { funGetUserByCookies } from "@/module/auth";
|
||||
import { createLogUser } from "@/module/user";
|
||||
import _ from "lodash";
|
||||
import moment from "moment";
|
||||
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 page = searchParams.get('page');
|
||||
const dataSkip = Number(page) * 10 - 10;
|
||||
|
||||
const announcements = await prisma.notifications.findMany({
|
||||
skip: dataSkip,
|
||||
take: 10,
|
||||
where: {
|
||||
isActive: true,
|
||||
idUserTo: user.id
|
||||
},
|
||||
orderBy: [
|
||||
{
|
||||
isRead: 'asc'
|
||||
},
|
||||
{
|
||||
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", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 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 } = await request.json();
|
||||
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: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
const result = await prisma.notifications.update({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
data: {
|
||||
isActive: false,
|
||||
},
|
||||
});
|
||||
|
||||
// create log user
|
||||
const log = await createLogUser({ act: 'UPDATE', desc: 'User membaca notifikasi', table: 'notifications', data: id })
|
||||
|
||||
return NextResponse.json( { success: true, message: "Berhasil mendapatkan notifikasi", }, { status: 200 } );
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal mendapatkan notifikasi, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -358,6 +358,25 @@ export async function GET(request: Request) {
|
||||
date: moment(v.dateStart).format("ll"),
|
||||
user: v.User.name
|
||||
}))
|
||||
} else if (kategori == "header") {
|
||||
const total = await prisma.notifications.count({
|
||||
where: {
|
||||
isActive: true,
|
||||
isRead: false,
|
||||
idUserTo: user.id
|
||||
}
|
||||
})
|
||||
|
||||
const desa = await prisma.village.findUnique({
|
||||
where: {
|
||||
id: idVillage
|
||||
}
|
||||
})
|
||||
|
||||
allData = {
|
||||
totalNotif: total,
|
||||
village: desa?.name
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true, message: "Berhasil mendapatkan data", data: allData }, { status: 200 });
|
||||
|
||||
@@ -137,7 +137,7 @@ export async function POST(request: Request) {
|
||||
|
||||
const { idGroup, title, task, member } = JSON.parse(dataBody as string)
|
||||
const userId = user.id
|
||||
|
||||
const userRoleLogin = user.idUserRole
|
||||
|
||||
const data = await prisma.project.create({
|
||||
data: {
|
||||
@@ -198,6 +198,77 @@ export async function POST(request: Request) {
|
||||
}
|
||||
}
|
||||
|
||||
const memberNotif = await prisma.projectMember.findMany({
|
||||
where: {
|
||||
idProject: data.id
|
||||
},
|
||||
select: {
|
||||
idUser: true
|
||||
}
|
||||
})
|
||||
|
||||
const dataNotif = memberNotif.map((v: any) => ({
|
||||
..._.omit(v, ["idUser"]),
|
||||
idUserTo: v.idUser,
|
||||
idUserFrom: userId,
|
||||
category: 'project',
|
||||
idContent: data.id,
|
||||
title: 'Kegiatan Baru',
|
||||
desc: 'Terdapat kegiatan baru. Silahkan periksa detailnya.'
|
||||
}))
|
||||
|
||||
if (userRoleLogin != "supadmin") {
|
||||
const perbekel = await prisma.user.findFirst({
|
||||
where: {
|
||||
isActive: true,
|
||||
idUserRole: "supadmin",
|
||||
idVillage: user.idVillage
|
||||
}
|
||||
})
|
||||
|
||||
dataNotif.push({
|
||||
idUserTo: perbekel?.id,
|
||||
idUserFrom: userId,
|
||||
category: 'project',
|
||||
idContent: data.id,
|
||||
title: 'Kegiatan Baru',
|
||||
desc: 'Terdapat kegiatan baru. Silahkan periksa detailnya.'
|
||||
})
|
||||
} else {
|
||||
const atasanGroup = await prisma.user.findMany({
|
||||
where: {
|
||||
isActive: true,
|
||||
idGroup: idGroup,
|
||||
AND: {
|
||||
OR: [
|
||||
{ idUserRole: 'cosupadmin' },
|
||||
{ idUserRole: 'admin' },
|
||||
]
|
||||
}
|
||||
},
|
||||
select:{
|
||||
id: true
|
||||
}
|
||||
})
|
||||
|
||||
const omitData = atasanGroup.map((v: any) => ({
|
||||
..._.omit(v, ["id"]),
|
||||
idUserTo: v.id,
|
||||
idUserFrom: userId,
|
||||
category: 'project',
|
||||
idContent: data.id,
|
||||
title: 'Kegiatan Baru',
|
||||
desc: 'Terdapat kegiatan baru. Silahkan periksa detailnya.'
|
||||
}))
|
||||
|
||||
dataNotif.push(...omitData)
|
||||
|
||||
}
|
||||
|
||||
const insertNotif = await prisma.notifications.createMany({
|
||||
data: dataNotif
|
||||
})
|
||||
|
||||
|
||||
// create log user
|
||||
const log = await createLogUser({ act: 'CREATE', desc: 'User membuat data kegiatan', table: 'project', data: data.id })
|
||||
|
||||
Reference in New Issue
Block a user