Merge pull request #450 from bipproduction/amalia/22-mei-25

upd: api mobile
This commit is contained in:
Amalia
2025-05-22 17:24:36 +08:00
committed by GitHub
4 changed files with 565 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
import { prisma } from "@/module/_global";
import { funGetUserById } from "@/module/auth";
import { createLogUserMobile } from "@/module/user";
import { NextResponse } from "next/server";
// CREATE COMENT BY ID KOMENTAR
export async function POST(request: Request, context: { params: { id: string } }) {
try {
const { id } = context.params
const { comment, 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: "User tidak ditemukan" }, { status: 200 });
}
const cek = await prisma.divisionDisscussion.count({
where: {
id: id
}
})
if (cek == 0) {
return NextResponse.json(
{
success: false,
message: "Tambah komentar gagal, data tidak ditemukan",
},
{ status: 200 }
);
}
const data = await prisma.divisionDisscussionComment.create({
data: {
comment: comment,
idDisscussion: id,
createdBy: userMobile.id
},
select: {
id: true
}
})
// create log user
const log = await createLogUserMobile({ act: 'CREATE', desc: 'User menambah komentar pada diskusi', table: 'divisionDisscussionComment', data: data.id, user: userMobile.id })
return NextResponse.json({ success: true, message: "Berhasil menambah komentar" }, { status: 200 });
} catch (error) {
console.error(error);
return NextResponse.json({ success: false, message: "Gagal menambah komentar, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
}
}

View File

@@ -0,0 +1,241 @@
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 ONE DISCUSSION BY ID
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 cat = searchParams.get("cat");
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 cek = await prisma.divisionDisscussion.count({
where: {
id: id
}
})
if (cek == 0) {
return NextResponse.json(
{
success: false,
message: "Gagal mendapatkan diskusi, data tidak ditemukan",
},
{ status: 404 }
);
}
if (cat == "comment") {
const data = await prisma.divisionDisscussionComment.findMany({
where: {
idDisscussion: id
},
select: {
id: true,
comment: true,
createdAt: true,
User: {
select: {
name: true,
img: true
}
}
}
})
const omitMember = data.map((v: any) => ({
..._.omit(v, ["User", "createdAt"]),
username: v.User.name,
img: v.User.img,
createdAt: moment(v.createdAt).format("ll")
}))
return NextResponse.json({ success: true, message: "Berhasil mendapatkan komentar", data: omitMember }, { status: 200 });
} else {
const data = await prisma.divisionDisscussion.findUnique({
where: {
id: id
},
select: {
isActive: true,
id: true,
title: true,
desc: true,
status: true,
createdAt: true,
createdBy: true,
User: {
select: {
name: true,
img: true
}
}
}
});
const { ...userMember } = _.omit(data, ["User", "createdAt"])
const username = data?.User.name
const user_img = data?.User.img
const createdAt = moment(data?.createdAt).format("ll")
const isCreator = data?.createdBy == userMobile.id
const result = { ...userMember, username, createdAt, user_img, isCreator }
return NextResponse.json({ success: true, message: "Berhasil mendapatkan diskusi", data: result }, { status: 200 });
}
} catch (error) {
console.error(error);
return NextResponse.json({ success: false, message: "Gagal mendapatkan diskusi, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
}
}
// OPEN OR CLOSE DISCUSSION
export async function DELETE(request: Request, context: { params: { id: string } }) {
try {
const { id } = context.params
const { status, 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 });
}
let newStatus;
if (status === 1) {
newStatus = 2;
} else if (status === 2) {
newStatus = 1;
} else {
return NextResponse.json({ success: false, message: "Invalid status" }, { status: 200 });
}
const data = await prisma.divisionDisscussion.count({
where: {
id: id
},
});
if (data == 0) {
return NextResponse.json({ success: false, message: "Gagal mendapatkan diskusi, data tidak ditemukan" }, { status: 404 });
}
const result = await prisma.divisionDisscussion.update({
where: {
id: id
},
data: {
status: newStatus
}
});
// create log user
const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User mengupdate status diskusi', table: 'divisionDisscussion', data: id, user: userMobile.id })
return NextResponse.json({ success: true, message: "Berhasil mengedit diskusi" }, { status: 200 });
} catch (error) {
console.error(error);
return NextResponse.json({ success: false, message: "Gagal mengedit diskusi, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
}
}
// DELETE DISCUSSION
export async function PUT(request: Request, context: { params: { id: string } }) {
try {
const { id } = context.params
const { active, 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 cek = await prisma.divisionDisscussion.count({
where: {
id: id
},
});
if (cek == 0) {
return NextResponse.json({ success: false, message: "Gagal mengarsipkan diskusi, data tidak ditemukan" }, { status: 200 });
}
const data = await prisma.divisionDisscussion.update({
where: {
id: id
},
data: {
isActive: active
}
});
// create log user
if (active) {
const log = await createLogUserMobile({ act: 'DELETE', desc: 'User mengaktifkan data diskusi', table: 'divisionDisscussion', data: id, user: userMobile.id })
return NextResponse.json({ success: true, message: "Berhasil mengaktifkan diskusi", user: userMobile.id }, { status: 200 });
} else {
const log = await createLogUserMobile({ act: 'DELETE', desc: 'User mengarsipkan data diskusi', table: 'divisionDisscussion', data: id, user: userMobile.id })
return NextResponse.json({ success: true, message: "Berhasil mengarsipkan diskusi", user: userMobile.id }, { status: 200 });
}
} catch (error) {
console.error(error);
return NextResponse.json({ success: false, message: "Gagal mengubah diskusi, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
}
}
// EDIT DISCUSSION
export async function POST(request: Request, context: { params: { id: string } }) {
try {
const { id } = context.params
const { title, desc, 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.divisionDisscussion.count({
where: {
id: id
},
});
if (data == 0) {
return NextResponse.json({ success: false, message: "Gagal mengedit diskusi, data tidak ditemukan" }, { status: 200 });
}
const update = await prisma.divisionDisscussion.update({
where: {
id: id
},
data: {
desc: desc
}
});
// create log user
const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User mengupdate data diskusi', table: 'divisionDisscussion', data: id, user: userMobile.id })
return NextResponse.json({ success: true, message: "Berhasil mengedit diskusi" }, { status: 200 });
} catch (error) {
console.error(error);
return NextResponse.json({ success: false, message: "Gagal mengedit diskusi, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
}
}

View File

@@ -0,0 +1,251 @@
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 DISCUSSION DIVISION ACTIVE = TRUE
export async function GET(request: Request) {
try {
const { searchParams } = new URL(request.url);
const idDivision = searchParams.get("division");
const name = searchParams.get('search');
const page = searchParams.get('page');
const status = searchParams.get('active');
const user = searchParams.get('user');
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 });
}
if (idDivision != "null" && idDivision != null && idDivision != undefined) {
const cekDivision = await prisma.division.count({
where: {
id: idDivision,
// isActive: true
}
})
if (cekDivision == 0) {
return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, data tidak ditemukan" }, { status: 404 });
}
const data = await prisma.divisionDisscussion.findMany({
// skip: dataSkip,
// take: 10,
where: {
isActive: status == "false" ? false : true,
idDivision: idDivision,
desc: {
contains: (name == undefined || name == "null") ? "" : name,
mode: "insensitive"
},
},
orderBy: {
createdAt: 'desc'
},
select: {
id: true,
title: true,
desc: true,
status: true,
createdAt: true,
User: {
select: {
name: true,
img: true
}
},
DivisionDisscussionComment: {
select: {
id: true,
}
}
}
});
const fixData = data.map((v: any) => ({
..._.omit(v, ["User", "DivisionDisscussionComment", "createdAt"]),
user_name: v.User.name,
img: v.User.img,
total_komentar: v.DivisionDisscussionComment.length,
createdAt: moment(v.createdAt).format("ll")
}))
return NextResponse.json({ success: true, message: "Berhasil mendapatkan diskusi", data: fixData, }, { status: 200 });
} else {
return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, data tidak ditemukan" }, { status: 200 });
}
} catch (error) {
console.error(error);
return NextResponse.json({ success: false, message: "Gagal mendapatkan diskusi, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
}
}
// CREATE DISCUSSION
export async function POST(request: Request) {
try {
const { idDivision, desc, 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 userRoleLogin = userMobile.idUserRole
const userId = userMobile.id
const cekDivision = await prisma.division.count({
where: {
id: idDivision,
isActive: true
}
})
if (cekDivision == 0) {
return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, data tidak ditemukan" }, { status: 404 });
}
const data = await prisma.divisionDisscussion.create({
data: {
idDivision,
desc,
createdBy: userId
},
select: {
id: true
}
});
const memberDivision = await prisma.divisionMember.findMany({
where: {
idDivision: idDivision
},
select: {
User: {
select: {
id: true,
Subscribe: {
select: {
subscription: true
}
}
}
}
}
})
// mengirim notifikasi
// datanotif untuk realtime notifikasi
// datapush untuk web push notifikasi ketika aplikasi tidak aktif
const dataNotif = memberDivision.map((v: any) => ({
..._.omit(v, ["User", "Subscribe"]),
idUserTo: v.User.id,
idUserFrom: String(userId),
category: 'division/' + idDivision + '/discussion',
idContent: data.id,
title: 'Diskusi Baru',
desc: 'Terdapat diskusi baru. Silahkan periksa detailnya.'
}))
const dataPush = memberDivision.map((v: any) => ({
..._.omit(v, ["User", "Subscribe"]),
idUser: v.User.id,
subscription: v.User.Subscribe?.subscription,
}))
if (userRoleLogin != "supadmin") {
const perbekel = await prisma.user.findFirst({
where: {
isActive: true,
idUserRole: "supadmin",
idVillage: userMobile.idVillage
},
select: {
id: true,
Subscribe: {
select: {
subscription: true
}
}
}
})
dataNotif.push({
idUserTo: perbekel?.id,
idUserFrom: userId,
category: 'division/' + idDivision + '/discussion',
idContent: data.id,
title: 'Diskusi Baru',
desc: 'Terdapat diskusi baru. Silahkan periksa detailnya.'
})
dataPush.push({
idUser: perbekel?.id,
subscription: perbekel?.Subscribe?.subscription
})
}
if (userRoleLogin != "cosupadmin") {
const ketuaGrup = await prisma.user.findFirst({
where: {
isActive: true,
idUserRole: "cosupadmin",
idGroup: userMobile.idGroup
},
select: {
id: true,
Subscribe: {
select: {
subscription: true
}
}
}
})
dataNotif.push({
idUserTo: ketuaGrup?.id,
idUserFrom: userId,
category: 'division/' + idDivision + '/discussion',
idContent: data.id,
title: 'Diskusi Baru',
desc: 'Terdapat diskusi baru. Silahkan periksa detailnya.'
})
dataPush.push({
idUser: ketuaGrup?.id,
subscription: ketuaGrup?.Subscribe?.subscription
})
}
const pushNotif = dataPush.filter((item) => item.subscription != undefined)
// const sendWebPush = await funSendWebPush({ sub: pushNotif, message: { body: 'Terdapat diskusi baru. Silahkan periksa detailnya.', title: 'Diskusi Baru' } })
const insertNotif = await prisma.notifications.createMany({
data: dataNotif
})
// create log user
const log = await createLogUserMobile({ act: 'CREATE', desc: 'User membuat data diskusi', table: 'divisionDisscussion', data: data.id, user: userId })
return NextResponse.json({ success: true, message: "Berhasil menambahkan diskusi", notif: dataNotif }, { status: 200 });
} catch (error) {
console.error(error);
return NextResponse.json({ success: false, message: "Gagal menambahkan diskusi, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
}
};

View File

@@ -231,6 +231,25 @@ export async function GET(request: Request, context: { params: { id: string } })
date: moment(v.createdAt).format("ll"),
user: v.User.name
}))
} else if (kategori == "check-member") {
const member = await prisma.divisionMember.findFirst({
where: {
idDivision: String(id),
idUser: userMobile.id
},
})
allData = member ? true : false
} else if (kategori == "check-admin") {
const member = await prisma.divisionMember.findFirst({
where: {
idDivision: String(id),
idUser: userMobile.id,
isAdmin: true
},
})
allData = member ? true : false
}
return NextResponse.json({ success: true, message: "Berhasil mendapatkan divisi", data: allData }, { status: 200 });