upd: api mobile
Deskriipsi: - api announcement get all - api announcement create - api division get - api announcement get one No Issues
This commit is contained in:
225
src/app/api/mobile/announcement/[id]/route.ts
Normal file
225
src/app/api/mobile/announcement/[id]/route.ts
Normal file
@@ -0,0 +1,225 @@
|
||||
import { prisma } from "@/module/_global";
|
||||
import { funGetUserByCookies, funGetUserById } from "@/module/auth";
|
||||
import { createLogUser } from "@/module/user";
|
||||
import _ from "lodash";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
|
||||
// GET ONE PENGUMUMAN, UNTUK TAMPIL DETAIL PENGUMUMAN
|
||||
export async function GET(request: Request, context: { params: { id: string } }) {
|
||||
try {
|
||||
const { id } = context.params;
|
||||
const { searchParams } = new URL(request.url);
|
||||
const user = searchParams.get('user');
|
||||
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.announcement.count({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
});
|
||||
|
||||
if (data == 0) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: "Gagal mendapatkan pengumuman, data tidak ditemukan",
|
||||
},
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
const announcement = await prisma.announcement.findUnique({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
desc: true,
|
||||
},
|
||||
});
|
||||
|
||||
const announcementMember = await prisma.announcementMember.findMany({
|
||||
where: {
|
||||
idAnnouncement: id,
|
||||
},
|
||||
select: {
|
||||
idGroup: true,
|
||||
idDivision: true,
|
||||
Group: {
|
||||
select: {
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
Division: {
|
||||
select: {
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const formatMember = announcementMember.map((v: any) => ({
|
||||
..._.omit(v, ["Group", "Division"]),
|
||||
idGroup: v.idGroup,
|
||||
idDivision: v.idDivision,
|
||||
group: v.Group.name,
|
||||
division: v.Division.name
|
||||
}))
|
||||
|
||||
// const fixMember = Object.groupBy(formatMember, ({ group }) => group);
|
||||
const fixMember = _.groupBy(formatMember, ({ group }) => group);
|
||||
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
message: "Berhasil mendapatkan pengumuman",
|
||||
data: announcement,
|
||||
member: fixMember
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
|
||||
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal mendapatkan pengumuman, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// HAPUS PENGUMUMAN
|
||||
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 data = await prisma.announcement.count({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
});
|
||||
|
||||
if (data == 0) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: "Hapus pengumuman gagal, data tidak ditemukan",
|
||||
},
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
const update = await prisma.announcement.update({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
data: {
|
||||
isActive: false,
|
||||
},
|
||||
});
|
||||
|
||||
// create log user
|
||||
const log = await createLogUser({ act: 'DELETE', desc: 'User menghapus data pengumuman', table: 'announcement', data: id })
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
message: "Pengumuman berhasil dihapus",
|
||||
data,
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal mendapatkan pengumuman, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// EDIT PENGUMUMAN
|
||||
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 { title, desc, groups } = (await request.json());
|
||||
const { id } = context.params;
|
||||
|
||||
const data = await prisma.announcement.count({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
});
|
||||
|
||||
if (data == 0) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: "Edit pengumuman gagal, data tidak ditemukan",
|
||||
},
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
const update = await prisma.announcement.update({
|
||||
where: {
|
||||
id: id
|
||||
},
|
||||
data: {
|
||||
title,
|
||||
desc,
|
||||
},
|
||||
});
|
||||
|
||||
// hapus semua member divisi pengumuman
|
||||
const hapus = await prisma.announcementMember.deleteMany({
|
||||
where: {
|
||||
idAnnouncement: id
|
||||
}
|
||||
})
|
||||
|
||||
let memberDivision = []
|
||||
|
||||
for (var i = 0, l = groups.length; i < l; i++) {
|
||||
var obj = groups[i].Division;
|
||||
for (let index = 0; index < obj.length; index++) {
|
||||
const element = obj[index];
|
||||
const fix = {
|
||||
idAnnouncement: id,
|
||||
idGroup: groups[i].id,
|
||||
idDivision: element.id
|
||||
}
|
||||
memberDivision.push(fix)
|
||||
}
|
||||
}
|
||||
|
||||
const announcementMember = await prisma.announcementMember.createMany({
|
||||
data: memberDivision,
|
||||
});
|
||||
|
||||
// create log user
|
||||
const log = await createLogUser({ act: 'UPDATE', desc: 'User mengupdate data pengumuman', table: 'announcement', data: id })
|
||||
|
||||
return NextResponse.json({ success: true, message: "Berhasil mengupdate pengumuman" }, { status: 200 });
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal mengeupdate pengumuman, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
251
src/app/api/mobile/announcement/route.ts
Normal file
251
src/app/api/mobile/announcement/route.ts
Normal file
@@ -0,0 +1,251 @@
|
||||
import { funSendWebPush, 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";
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
|
||||
|
||||
// GET ALL PENGUMUMAN
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const name = searchParams.get('search');
|
||||
const page = searchParams.get('page');
|
||||
const user = searchParams.get('user');
|
||||
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 villageId = userMobile.idVillage
|
||||
const roleUser = userMobile.idUserRole
|
||||
const groupId = userMobile.idGroup
|
||||
|
||||
const dataSkip = Number(page) * 10 - 10;
|
||||
|
||||
let kondisi: any = {
|
||||
idVillage: String(villageId),
|
||||
isActive: true,
|
||||
title: {
|
||||
contains: (name == undefined || name == null) ? "" : name,
|
||||
mode: "insensitive"
|
||||
}
|
||||
}
|
||||
|
||||
if (roleUser != "supadmin" && roleUser != "developer") {
|
||||
if (roleUser == "cosupadmin" || roleUser == "admin") {
|
||||
kondisi = {
|
||||
idVillage: String(villageId),
|
||||
isActive: true,
|
||||
title: {
|
||||
contains: (name == undefined || name == null) ? "" : name,
|
||||
mode: "insensitive"
|
||||
},
|
||||
AnnouncementMember: {
|
||||
some: {
|
||||
idGroup: String(groupId)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
} else {
|
||||
kondisi = {
|
||||
idVillage: String(villageId),
|
||||
isActive: true,
|
||||
title: {
|
||||
contains: (name == undefined || name == null) ? "" : name,
|
||||
mode: "insensitive"
|
||||
},
|
||||
AnnouncementMember: {
|
||||
some: {
|
||||
idGroup: String(groupId),
|
||||
Division: {
|
||||
DivisionMember: {
|
||||
some: {
|
||||
idUser: String(userMobile.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const announcements = await prisma.announcement.findMany({
|
||||
// skip: dataSkip,
|
||||
// take: 10,
|
||||
where: kondisi,
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
desc: true,
|
||||
createdAt: true,
|
||||
},
|
||||
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 pengumuman", data: allData, }, { status: 200 });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal mendapatkan pengumuman, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// CREATE PENGUMUMAN
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const { title, desc, groups, 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 villaId = userMobile.idVillage
|
||||
const userId = userMobile.id
|
||||
const userRoleLogin = userMobile.idUserRole
|
||||
|
||||
const data = await prisma.announcement.create({
|
||||
data: {
|
||||
title,
|
||||
desc,
|
||||
idVillage: String(villaId),
|
||||
createdBy: String(userId),
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
}
|
||||
});
|
||||
|
||||
let memberDivision = []
|
||||
|
||||
for (var i = 0, l = groups.length; i < l; i++) {
|
||||
2
|
||||
var obj = groups[i].Division;
|
||||
for (let index = 0; index < obj.length; index++) {
|
||||
const element = obj[index];
|
||||
const fix = {
|
||||
idAnnouncement: data.id,
|
||||
idGroup: groups[i].id,
|
||||
idDivision: element.id
|
||||
}
|
||||
memberDivision.push(fix)
|
||||
}
|
||||
}
|
||||
|
||||
const announcementMember = await prisma.announcementMember.createMany({
|
||||
data: memberDivision,
|
||||
});
|
||||
|
||||
const memberNotif = await prisma.divisionMember.findMany({
|
||||
where: {
|
||||
Division: {
|
||||
AnnouncementMember: {
|
||||
some: {
|
||||
idAnnouncement: data.id
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
select: {
|
||||
idUser: true,
|
||||
User: {
|
||||
select: {
|
||||
Subscribe: {
|
||||
select: {
|
||||
subscription: true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
// mengirim notifikasi
|
||||
// datanotif untuk realtime notifikasi
|
||||
// datapush untuk web push notifikasi ketika aplikasi tidak aktif
|
||||
const dataNotif = memberNotif.map((v: any) => ({
|
||||
..._.omit(v, ["idUser", "User", "Subscribe"]),
|
||||
idUserTo: v.idUser,
|
||||
idUserFrom: userId,
|
||||
category: 'announcement',
|
||||
idContent: data.id,
|
||||
title: 'Pengumuman Baru',
|
||||
desc: 'Anda memiliki pengumuman baru. Silahkan periksa detailnya.'
|
||||
}))
|
||||
|
||||
const dataPush = memberNotif.map((v: any) => ({
|
||||
..._.omit(v, ["idUser", "User", "Subscribe"]),
|
||||
idUser: v.idUser,
|
||||
subscription: v.User.Subscribe?.subscription,
|
||||
}))
|
||||
|
||||
if (userRoleLogin != "supadmin") {
|
||||
const perbekel = await prisma.user.findFirst({
|
||||
where: {
|
||||
isActive: true,
|
||||
idUserRole: "supadmin",
|
||||
idVillage: user.idVillage
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
Subscribe: {
|
||||
select: {
|
||||
subscription: true
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
dataNotif.push({
|
||||
idUserTo: perbekel?.id,
|
||||
idUserFrom: userId,
|
||||
category: 'announcement',
|
||||
idContent: data.id,
|
||||
title: 'Pengumuman Baru',
|
||||
desc: 'Anda memiliki pengumuman baru. Silahkan periksa detailnya.'
|
||||
})
|
||||
|
||||
dataPush.push({
|
||||
idUser: perbekel?.id,
|
||||
subscription: perbekel?.Subscribe?.subscription
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
const pushNotif = dataPush.filter((item) => item.subscription != undefined)
|
||||
|
||||
const sendWebPush = await funSendWebPush({ sub: pushNotif, message: { title: 'Pengumuman Baru', body: 'Anda memiliki pengumuman baru. Silahkan periksa detailnya.' } })
|
||||
const insertNotif = await prisma.notifications.createMany({
|
||||
data: dataNotif
|
||||
})
|
||||
|
||||
// create log user
|
||||
const log = await createLogUserMobile({ act: 'CREATE', desc: 'User membuat data pengumuman baru', table: 'announcement', data: data.id, user: userMobile.id })
|
||||
|
||||
return NextResponse.json({ success: true, message: "Berhasil membuat pengumuman" }, { status: 200 });
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal membuat pengumuman, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { prisma } from "@/module/_global";
|
||||
import { funGetUserByCookies } from "@/module/auth";
|
||||
import { funGetUserById } from "@/module/auth";
|
||||
import { NextResponse } from "next/server";
|
||||
export const dynamic = 'force-dynamic'
|
||||
export const revalidate = true
|
||||
@@ -7,19 +7,23 @@ export const revalidate = true
|
||||
|
||||
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 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 role = user.idUserRole
|
||||
const villaId = user.idVillage
|
||||
const group = user.idGroup
|
||||
|
||||
const role = userMobile.idUserRole
|
||||
const villaId = userMobile.idVillage
|
||||
const group = userMobile.idGroup
|
||||
let kondisi: any = {
|
||||
isActive: true,
|
||||
idVillage: String(villaId)
|
||||
}
|
||||
|
||||
if (role != "supadmin") {
|
||||
if (role != "supadmin" && role != "developer") {
|
||||
kondisi = {
|
||||
isActive: true,
|
||||
idVillage: String(villaId),
|
||||
|
||||
Reference in New Issue
Block a user