diff --git a/src/app/api/mobile/division/[id]/detail/route.ts b/src/app/api/mobile/division/[id]/detail/route.ts new file mode 100644 index 0000000..65b268d --- /dev/null +++ b/src/app/api/mobile/division/[id]/detail/route.ts @@ -0,0 +1,402 @@ +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 DATA DIVISI :: UNTUK TAMPIL DETAIL DIVISI (FITUR DIVISI) PADA HALAMAN DETAIL +export async function GET(request: Request, context: { params: { id: string } }) { + try { + let allData + const { id } = context.params; + const { searchParams } = new URL(request.url); + const kategori = searchParams.get("cat"); + 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.division.findUnique({ + where: { + id: String(id), + // isActive: true + } + }); + + if (!data) { + return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, data tidak ditemukan", }, { status: 200 }); + } + + if (kategori == "jumlah") { + const tugas = await prisma.divisionProject.count({ + where: { + idDivision: String(id), + status: { + lte: 1 + }, + isActive: true + } + }) + + const dokumen = await prisma.divisionDocumentFolderFile.count({ + where: { + idDivision: String(id), + isActive: true, + category: "FILE" + } + }) + + const dokumenShare = await prisma.divisionDocumentShare.count({ + where: { + idDivision: String(id), + isActive: true, + DivisionDocumentFolderFile: { + isActive: true, + category: "FILE" + } + } + }) + + const diskusi = await prisma.divisionDisscussion.count({ + where: { + idDivision: String(id), + isActive: true, + status: 1 + } + }) + + const kalender = await prisma.divisionCalendarReminder.count({ + where: { + idDivision: String(id), + isActive: true, + dateStart: { + lte: new Date() + }, + DivisionCalendar: { + isActive: true + } + } + }) + + + allData = { + tugas: tugas, + dokumen: dokumen + dokumenShare, + diskusi: diskusi, + kalender: kalender + } + } else if (kategori == "today-task") { + const tugas = await prisma.divisionProjectTask.findMany({ + skip: 0, + take: 5, + where: { + idDivision: String(id), + status: 0, + isActive: true, + dateStart: { + lte: new Date() + }, + dateEnd: { + gte: new Date() + }, + DivisionProject: { + status: { + lt: 3 + } + } + }, + select: { + id: true, + idProject: true, + title: true, + dateStart: true, + dateEnd: true, + DivisionProject: { + select: { + title: true + } + } + }, + orderBy: { + dateEnd: "asc" + } + }) + + allData = tugas.map((v: any) => ({ + ..._.omit(v, ["dateStart", "dateEnd", "DivisionProject"]), + dateStart: moment(v.dateStart).format("ll"), + dateEnd: moment(v.dateEnd).format("ll"), + projectTitle: v.DivisionProject.title + })) + } else if (kategori == "new-file") { + const dataShare = await prisma.divisionDocumentShare.findMany({ + skip: 0, + take: 5, + where: { + isActive: true, + idDivision: String(id), + DivisionDocumentFolderFile: { + isActive: true, + category: "FILE" + } + }, + select: { + DivisionDocumentFolderFile: { + select: { + id: true, + name: true, + extension: true, + path: true, + } + }, + createdAt: true + }, + orderBy: { + createdAt: 'desc' + } + }) + + const fixShare = dataShare.map((v: any) => ({ + ..._.omit(v, ["DivisionDocumentFolderFile"]), + id: v.DivisionDocumentFolderFile.id, + name: v.DivisionDocumentFolderFile.name, + extension: v.DivisionDocumentFolderFile.extension, + path: 'home', + share: true + })) + + const dataDokumen = await prisma.divisionDocumentFolderFile.findMany({ + skip: 0, + take: 5, + where: { + idDivision: String(id), + isActive: true, + category: "FILE" + }, + select: { + id: true, + name: true, + extension: true, + path: true, + createdAt: true + }, + orderBy: { + createdAt: "desc" + } + }) + + const fixData = dataDokumen.map((v: any) => ({ + ..._.omit(v, [""]), + share: false + })) + + if (fixShare.length > 0) { + fixData.push(...fixShare) + } + + allData = _.orderBy(fixData, ['createdAt'], ['desc']); + + } else if (kategori == "new-discussion") { + const diskusi = await prisma.divisionDisscussion.findMany({ + skip: 0, + take: 5, + where: { + idDivision: String(id), + isActive: true, + status: 1 + }, + select: { + id: true, + title: true, + desc: true, + createdAt: true, + User: { + select: { + name: true + } + } + }, + orderBy: { + createdAt: "desc" + } + }) + + allData = diskusi.map((v: any) => ({ + ..._.omit(v, ["createdAt", "User"]), + date: moment(v.createdAt).format("ll"), + user: v.User.name + })) + } + + return NextResponse.json({ success: true, message: "Berhasil mendapatkan divisi", data: allData }, { status: 200 }); + } + + + + catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +} + + +// MENGELUARKAN ANGGOTA DARI DIVISI +export async function DELETE(request: Request, context: { params: { id: string } }) { + try { + const idDivision = context.params.id + 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.division.count({ + where: { + id: idDivision, + isActive: true + }, + }); + + if (data == 0) { + return NextResponse.json( + { + success: false, + message: "Hapus anggota divisi gagal, data tidak ditemukan", + }, + { status: 200 } + ); + } + + const update = await prisma.divisionMember.delete({ + where: { + id: id, + }, + }); + + // create log user + const log = await createLogUserMobile({ act: 'DELETE', desc: 'User mengeluarkan anggota divisi', table: 'division', data: idDivision, user: userMobile.id }) + + return NextResponse.json( + { + success: true, + message: "Anggota divisi berhasil dihapus", + }, + { status: 200 } + ); + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal mengeluarkan anggota divisi, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +} + + +// MENGGANTI STATUS ADMIN DIVISI +export async function PUT(request: Request, context: { params: { id: string } }) { + try { + const idDivision = context.params.id; + const { id, isAdmin, 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.division.count({ + where: { + id: idDivision, + isActive: true + }, + }); + + if (data == 0) { + return NextResponse.json( + { + success: false, + message: "Perubahan status admin gagal, data tidak ditemukan", + }, + { status: 200 } + ); + } + + const update = await prisma.divisionMember.update({ + where: { + id: id, + }, + data: { + isAdmin: !isAdmin + } + }); + + // create log user + const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User mengupdate status anggota divisi', table: 'division', data: idDivision, user: userMobile.id }) + + return NextResponse.json( + { + success: true, + message: "Status admin berhasil diupdate", + }, + { status: 200 } + ); + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal mengubah status admin divisi, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +} + + +// TAMBAH ANGGOTA DIVISI +export async function POST(request: Request, context: { params: { id: string } }) { + try { + const { member, user } = await request.json() + const idDivision = context.params.id + 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.division.count({ + where: { + id: idDivision, + isActive: true + }, + }); + + if (data == 0) { + return NextResponse.json( + { + success: false, + message: "Tambah anggota divisi gagal, data tidak ditemukan", + }, + { status: 200 } + ); + } + + + const dataMember = member.map((v: any) => ({ + ..._.omit(v, ["name", "img"]), + idUser: v.idUser, + idDivision: idDivision, + })) + + const insertMember = await prisma.divisionMember.createMany({ + data: dataMember + }) + + // create log user + const log = await createLogUserMobile({ act: 'CREATE', desc: 'User menambah anggota divisi', table: 'division', data: idDivision, user: userMobile.id }) + + return NextResponse.json({ success: true, message: "Berhasil menambahkan anggota divisi" }, { status: 200 }); + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal menambahkan anggota divisi, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +}; \ No newline at end of file diff --git a/src/app/api/mobile/division/[id]/member/route.ts b/src/app/api/mobile/division/[id]/member/route.ts new file mode 100644 index 0000000..5c6c2da --- /dev/null +++ b/src/app/api/mobile/division/[id]/member/route.ts @@ -0,0 +1,70 @@ +import { prisma } from "@/module/_global"; +import { funGetUserByCookies } from "@/module/auth"; +import _ from "lodash"; +import { NextResponse } from "next/server"; + + +// GET MEMBER 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 = await funGetUserByCookies() + const name = searchParams.get('search') + if (user.id == undefined) { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 }); + } + + const data = await prisma.division.findUnique({ + where: { + id: String(id), + // isActive: true, + } + }); + + if (!data) { + return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, data tidak ditemukan", }, { status: 404 }); + } + + const member = await prisma.divisionMember.findMany({ + where: { + idDivision: String(id), + isActive: true, + User: { + name: { + contains: (name == undefined || name == null) ? "" : name, + mode: "insensitive", + } + } + }, + select: { + id: true, + isAdmin: true, + isLeader: true, + idUser: true, + User: { + select: { + name: true, + img: true + } + } + }, + orderBy: { + isAdmin: 'desc', + } + }) + + const fixMember = member.map((v: any) => ({ + ..._.omit(v, ["User"]), + name: v.User.name, + img: v.User.img + })) + + + + return NextResponse.json({ success: true, data: fixMember }) + + } catch (error) { + return NextResponse.json({ success: false, message: "Gagal mendapatkan anggota, data tidak ditemukan (error: 500)", }, { status: 500 }); + } +} \ No newline at end of file diff --git a/src/app/api/mobile/division/[id]/route.ts b/src/app/api/mobile/division/[id]/route.ts new file mode 100644 index 0000000..8c5e884 --- /dev/null +++ b/src/app/api/mobile/division/[id]/route.ts @@ -0,0 +1,134 @@ +import { prisma } from "@/module/_global"; +import { funGetUserById } from "@/module/auth"; +import { createLogUserMobile } from "@/module/user"; +import _ from "lodash"; +import { NextResponse } from "next/server"; + + +// GET ONE DATA DIVISI :: UNTUK TAMPIL DATA DI HALAMAN EDIT DAN INFO +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.division.findUnique({ + where: { + id: String(id), + // isActive: true + } + }); + + if (!data) { + return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, data tidak ditemukan", }, { status: 200 }); + } + + const member = await prisma.divisionMember.findMany({ + where: { + idDivision: String(id), + isActive: true, + }, + select: { + id: true, + isAdmin: true, + isLeader: true, + idUser: true, + User: { + select: { + name: true, + img: true + } + } + }, + orderBy: [ + { + isAdmin: 'desc', + }, + { + User: { + name: 'asc' + } + } + ] + }) + + const fixMember = member.map((v: any) => ({ + ..._.omit(v, ["User"]), + name: v.User.name, + img: v.User.img + })) + + const dataFix = { + division: data, + member: fixMember + } + + + return NextResponse.json({ success: true, message: "Berhasil mendapatkan divisi", data: dataFix, }, { status: 200 }); + + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +} + + + +// EDIT DATA DIVISI +export async function PUT(request: Request, context: { params: { id: string } }) { + try { + const { id } = context.params; + const { name, 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.division.count({ + where: { + id: id, + }, + }); + + if (data == 0) { + return NextResponse.json( + { + success: false, + message: "Edit divisi gagal, data tidak ditemukan", + }, + { status: 200 } + ); + } + + const update = await prisma.division.update({ + where: { + id: id, + }, + data: { + name: name, + desc: desc + }, + }); + + // create log user + const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User mengupdate data divisi', table: 'division', data: id, user: userMobile.id }) + + return NextResponse.json( + { + success: true, + message: "Divisi berhasil diedit", + }, + { status: 200 } + ); + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal mengedit divisi, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +} \ No newline at end of file diff --git a/src/app/api/mobile/division/[id]/status/route.ts b/src/app/api/mobile/division/[id]/status/route.ts new file mode 100644 index 0000000..57193aa --- /dev/null +++ b/src/app/api/mobile/division/[id]/status/route.ts @@ -0,0 +1,50 @@ +import { prisma } from "@/module/_global"; +import { funGetUserById } from "@/module/auth"; +import { createLogUserMobile } from "@/module/user"; +import { NextResponse } from "next/server"; + +export async function POST(request: Request, context: { params: { id: string } }) { + try { + const { id } = context.params; + const { isActive, 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.division.count({ + where: { + id: id, + }, + }); + + if (data == 0) { + return NextResponse.json( + { + success: false, + message: "Edit status divisi gagal, data tidak ditemukan", + }, + { status: 200 } + ); + } + + const update = await prisma.division.update({ + where: { + id: id, + }, + data: { + isActive: !isActive, + }, + }); + + // create log user + const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User mengedit status data divisi', table: 'division', data: id, user: userMobile.id }) + + return NextResponse.json({ success: true, message: "Status divisi berhasil diupdate", }, { status: 200 }); + + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal mengubah status divisi, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +} \ No newline at end of file diff --git a/src/app/api/mobile/division/more/route.ts b/src/app/api/mobile/division/more/route.ts new file mode 100644 index 0000000..9afb71b --- /dev/null +++ b/src/app/api/mobile/division/more/route.ts @@ -0,0 +1,52 @@ +import { prisma } from "@/module/_global"; +import { funGetUserByCookies } from "@/module/auth"; +import { NextResponse } from "next/server"; + +// GET LIST DIVISI BY ID DIVISI (CONTOH : UNTUK SHARE DOKUMEN) +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 idDivision = searchParams.get("division"); + const name = searchParams.get('search'); + + const dataDivision = await prisma.division.findUnique({ + where: { + id: String(idDivision), + isActive: true + } + }) + + if (!dataDivision) { + return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, data tidak ditemukan", }, { status: 404 }); + } + + const data = await prisma.division.findMany({ + where: { + isActive: true, + idGroup: dataDivision.idGroup, + name: { + contains: (name == undefined || name == "null") ? "" : name, + mode: "insensitive" + } + }, + select: { + id: true, + name: true, + }, + orderBy: { + name: "asc" + } + }); + + return NextResponse.json({ success: true, message: "Berhasil mendapatkan divisi", data }, { status: 200 }); + + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +} \ No newline at end of file diff --git a/src/app/api/mobile/division/report/route.ts b/src/app/api/mobile/division/report/route.ts new file mode 100644 index 0000000..efc4517 --- /dev/null +++ b/src/app/api/mobile/division/report/route.ts @@ -0,0 +1,379 @@ +import { prisma } from "@/module/_global"; +import { funGetUserById } from "@/module/auth"; +import _, { ceil } from "lodash"; +import { NextResponse } from "next/server"; + +export async function GET(request: Request) { + try { + const { searchParams } = new URL(request.url) + const idGroup = searchParams.get("group") + const division = searchParams.get("division") + const date = searchParams.get("date") + const dateAkhir = searchParams.get("date-end") + const kat = searchParams.get("cat") + const user = searchParams.get("user") + let grup + + 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 (idGroup == "null" || idGroup == undefined || idGroup == "" || idGroup == null || idGroup == "undefined") { + grup = userMobile.idGroup + } else { + grup = idGroup + } + + if (kat == "table-progress") { + let kondisiProgress + if (division == "undefined") { + kondisiProgress = { + isActive: true, + Division: { + idGroup: String(grup) + }, + DivisionProjectTask: { + some: { + dateStart: { + gte: new Date(String(date)) + }, + dateEnd: { + lte: new Date(String(dateAkhir)) + } + } + } + } + + } else { + kondisiProgress = { + isActive: true, + idDivision: String(division), + DivisionProjectTask: { + some: { + dateStart: { + gte: new Date(String(date)) + }, + dateEnd: { + lte: new Date(String(dateAkhir)) + } + } + } + } + } + + + const data = await prisma.divisionProject.findMany({ + where: kondisiProgress, + select: { + id: true, + title: true, + status: true, + DivisionProjectTask: { + where: { + isActive: true + }, + select: { + title: true, + status: true + } + } + } + }) + + const dataFix = data.map((v: any) => ({ + ..._.omit(v, ["DivisionProjectTask"]), + progress: ceil((v.DivisionProjectTask.filter((i: any) => i.status == 1).length * 100) / v.DivisionProjectTask.length), + })) + + return NextResponse.json({ success: true, message: "Berhasil mendapatkan data", data: dataFix }, { status: 200 }); + + } else { + // CHART PROGRESS + let kondisiProgress + if (division == "undefined") { + kondisiProgress = { + isActive: true, + Division: { + idGroup: String(grup) + }, + DivisionProjectTask: { + some: { + dateStart: { + gte: new Date(String(date)) + }, + dateEnd: { + lte: new Date(String(dateAkhir)) + } + } + } + } + } else { + kondisiProgress = { + isActive: true, + idDivision: String(division), + DivisionProjectTask: { + some: { + dateStart: { + gte: new Date(String(date)) + }, + dateEnd: { + lte: new Date(String(dateAkhir)) + } + } + } + } + } + + const data = await prisma.divisionProject.groupBy({ + where: kondisiProgress, + by: ["status"], + _count: true + }) + + const dataStatus = [{ name: 'Segera', status: 0 }, { name: 'Dikerjakan', status: 1 }, { name: 'Selesai', status: 2 }, { name: 'Dibatalkan', status: 3 }] + const hasilProgres: any[] = [] + let input + for (let index = 0; index < dataStatus.length; index++) { + const cek = data.some((i: any) => i.status == dataStatus[index].status) + if (cek) { + const find = ((Number(data.find((i: any) => i.status == dataStatus[index].status)?._count) * 100) / data.reduce((n, { _count }) => n + _count, 0)).toFixed(2) + const fix = find != "100.00" ? find.substr(-2, 2) == "00" ? find.substr(0, 2) : find : "100" + input = { + name: dataStatus[index].name, + value: fix + } + } else { + input = { + name: dataStatus[index].name, + value: 0 + } + } + hasilProgres.push(input) + } + + + + + // CHART DOKUMEN + let kondisi + if (division == "undefined") { + kondisi = { + isActive: true, + category: 'FILE', + Division: { + idGroup: String(grup) + }, + createdAt: { + gte: new Date(String(date)), + lte: new Date(String(dateAkhir)) + }, + } + } else { + kondisi = { + isActive: true, + category: 'FILE', + idDivision: String(division), + createdAt: { + gte: new Date(String(date)), + lte: new Date(String(dateAkhir)) + }, + } + } + + const dataDokumen = await prisma.divisionDocumentFolderFile.findMany({ + where: kondisi, + }) + + const groupData = _.map(_.groupBy(dataDokumen, "extension"), (v: any) => ({ + file: v[0].extension, + jumlah: v.length, + })) + + const image = ['jpg', 'jpeg', 'png', 'heic'] + + let hasilImage = { + name: 'Gambar', + value: 0 + } + + let hasilFile = { + name: 'Dokumen', + value: 0 + } + + groupData.map((v: any) => { + if (image.some((i: any) => i == v.file)) { + hasilImage = { + name: 'Gambar', + value: hasilImage.value + v.jumlah + } + } else { + hasilFile = { + name: 'Dokumen', + value: hasilFile.value + v.jumlah + } + } + }) + + const hasilDokumen = [hasilImage, hasilFile] + + + + // CHART EVENT + let kondisiEvent, kondisiSelesai, kondisiComingSoon + if (division == "undefined") { + // kondisiEvent = { + // isActive: true, + // Division: { + // idGroup: String(grup) + // }, + // DivisionCalendarReminder: { + // some: { + // dateStart: { + // gte: new Date(String(date)), + // lte: new Date(String(dateAkhir)) + // } + // } + // } + // } + + kondisiSelesai = { + isActive: true, + Division: { + idGroup: String(grup) + }, + DivisionCalendarReminder: { + some: { + dateStart: { + gte: new Date(String(date)), + lte: new Date() + } + } + } + } + + kondisiComingSoon = { + isActive: true, + Division: { + idGroup: String(grup) + }, + DivisionCalendarReminder: { + some: { + dateStart: { + gt: new Date(), + lte: new Date(String(dateAkhir)) + } + } + } + } + } else { + // kondisiEvent = { + // isActive: true, + // idDivision: String(division), + // DivisionCalendarReminder: { + // some: { + // dateStart: { + // gte: new Date(String(date)), + // lte: new Date(String(dateAkhir)) + // } + // } + // } + // } + + kondisiSelesai = { + isActive: true, + idDivision: String(division), + DivisionCalendarReminder: { + some: { + dateStart: { + gte: new Date(String(date)), + lte: new Date() + } + } + } + } + + kondisiComingSoon = { + isActive: true, + idDivision: String(division), + DivisionCalendarReminder: { + some: { + dateStart: { + gt: new Date(), + lte: new Date(String(dateAkhir)) + } + } + } + } + } + + const eventSelesai = await prisma.divisionCalendar.count({ + where: kondisiSelesai + }) + + const eventComingSoon = await prisma.divisionCalendar.count({ + where: kondisiComingSoon + }) + + const hasilEvent = [ + { + name: 'Selesai', + value: eventSelesai + }, + { + name: 'Akan Datang', + value: eventComingSoon + } + ] + + // const dataEvent = await prisma.divisionCalendar.findMany({ + // where: kondisiEvent, + // select: { + // id: true, + // idDivision: true, + // title: true, + // desc: true, + // status: true, + // timeStart: true, + // dateStart: true, + // timeEnd: true, + // dateEnd: true, + // createdAt: true, + // User: { + // select: { + // name: true + // } + // } + // }, + // orderBy: { + // createdAt: 'desc' + // } + // }) + + // const hasilEvent = dataEvent.map((v: any) => ({ + // ..._.omit(v, ["User"]), + // user_name: v.User.name, + // timeStart: moment.utc(v.timeStart).format('HH:mm'), + // timeEnd: moment.utc(v.timeEnd).format('HH:mm') + // })) + + + const allData = { + progress: hasilProgres, + dokumen: hasilDokumen, + event: hasilEvent + } + + return NextResponse.json({ success: true, message: "Berhasil mendapatkan data", data: allData }, { status: 200 }); + } + + + } + catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal mendapatkan data, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +} \ No newline at end of file diff --git a/src/app/api/mobile/division/route.ts b/src/app/api/mobile/division/route.ts new file mode 100644 index 0000000..71441e6 --- /dev/null +++ b/src/app/api/mobile/division/route.ts @@ -0,0 +1,302 @@ +import { funSendWebPush, prisma } from "@/module/_global"; +import { funGetUserByCookies, funGetUserById } from "@/module/auth"; +import { createLogUser, createLogUserMobile } from "@/module/user"; +import _ from "lodash"; +import { NextResponse } from "next/server"; + + +// GET ALL DATA DIVISI == LIST DATA DIVISI +export async function GET(request: Request) { + try { + let grup + + const { searchParams } = new URL(request.url); + const idGroup = searchParams.get("group"); + const kategori = searchParams.get("cat"); + const name = searchParams.get('search'); + const page = searchParams.get('page'); + const active = 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 }); + } + + const villaId = userMobile.idVillage + const roleUser = userMobile.idUserRole + + + if (idGroup == "null" || idGroup == undefined || idGroup == "" || idGroup == "undefined" || idGroup == null) { + grup = userMobile.idGroup + } else { + grup = idGroup + } + + + // JIKA (ROLE BUKAN USER DAN COADMIN) ATAU SEMUA ROLE DG KATEGORI == SEMUA + let kondisi: any = { + isActive: active == 'false' ? false : true, + idVillage: String(villaId), + idGroup: grup, + name: { + contains: (name == undefined || name == "null") ? "" : name, + mode: "insensitive" + } + } + + + // JIKA ROLE = USER ATAU COADMIN DAN KATEGORI != SEMUA (DIVISI SAYA) + if (roleUser != "supadmin" && roleUser != "developer" && roleUser != "cosupadmin" && roleUser != "admin") { + if (kategori != "semua") { + kondisi = { + isActive: active == 'false' ? false : true, + idVillage: String(villaId), + idGroup: grup, + name: { + contains: (name == undefined || name == "null") ? "" : name, + mode: "insensitive" + }, + DivisionMember: { + some: { + isActive: true, + idUser: String(userMobile.id) + } + } + } + } + } + + const totalData = await prisma.division.count({ + where: kondisi + }) + + const data = await prisma.division.findMany({ + // skip: dataSkip, + // take: 10, + where: kondisi, + select: { + id: true, + name: true, + desc: true, + DivisionMember: { + where: { + isActive: true + }, + select: { + idUser: true + } + } + }, + orderBy: { + createdAt: 'desc' + } + }); + + const allData = data.map((v: any) => ({ + ..._.omit(v, ["DivisionMember"]), + jumlah_member: v.DivisionMember.length + })) + + + const filter = await prisma.group.findUnique({ + where: { + id: grup + }, + select: { + id: true, + name: true + } + }) + + + return NextResponse.json({ success: true, message: "Berhasil mendapatkan divisi", data: allData, total: totalData, filter }, { status: 200 }); + + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +} + + + +// CREATE DATA DIVISI +export async function POST(request: Request) { + try { + + const sent = (await request.json()) + const user = await funGetUserById({ id: String(sent.user) }) + if (user.id == undefined) { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 }); + } + + const userId = user.id + const userRoleLogin = user.idUserRole + + let fixGroup + if (sent.data.idGroup == "null" || sent.data.idGroup == undefined || sent.data.idGroup == "") { + fixGroup = user.idGroup + } else { + fixGroup = sent.data.idGroup + } + + + + const data = await prisma.division.create({ + data: { + name: sent.data.name, + idVillage: String(user.idVillage), + idGroup: fixGroup, + desc: sent.data.desc, + createdBy: String(user.id) + }, + select: { + id: true + } + }) + + + const dataMember = sent.member.map((v: any) => ({ + ..._.omit(v, ["idUser", "name", "img"]), + idUser: v.idUser, + idDivision: data.id, + isAdmin: sent.admin.some((i: any) => i == v.idUser) + })) + + const insertMember = await prisma.divisionMember.createMany({ + data: dataMember + }) + + + // mengirim notifikasi + // datanotif untuk realtime notifikasi + // datapush untuk web push notifikasi ketika aplikasi tidak aktif + const dataNotif = sent.member.map((v: any) => ({ + ..._.omit(v, ["idUser", "name", "img"]), + idUserTo: v.idUser, + idUserFrom: userId, + category: 'division', + idContent: data.id, + title: 'Divisi Baru', + desc: 'Terdapat divisi baru. Silahkan periksa detailnya.' + })) + + const selectUser = await prisma.divisionMember.findMany({ + where: { + isActive: true, + idDivision: data.id + }, + select: { + idUser: true, + User: { + select: { + Subscribe: { + select: { + subscription: true + } + } + } + } + } + }) + + const dataPush = selectUser.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: 'division', + idContent: data.id, + title: 'Divisi Baru', + desc: 'Terdapat divisi baru. Silahkan periksa detailnya.' + }) + + dataPush.push({ + idUser: perbekel?.id, + subscription: perbekel?.Subscribe?.subscription + }) + + } else { + const atasanGroup = await prisma.user.findMany({ + where: { + isActive: true, + idGroup: sent.data.idGroup, + AND: { + OR: [ + { idUserRole: 'cosupadmin' }, + { idUserRole: 'admin' }, + ] + } + }, + select: { + id: true, + Subscribe: { + select: { + subscription: true + } + } + } + }) + + const omitData = atasanGroup.map((v: any) => ({ + ..._.omit(v, ["id", "Subscribe"]), + idUserTo: v.id, + idUserFrom: userId, + category: 'division', + idContent: data.id, + title: 'Divisi Baru', + desc: 'Terdapat divisi baru. Silahkan periksa detailnya.' + })) + + const omitPush = atasanGroup.map((v: any) => ({ + ..._.omit(v, ["id", "Subscribe"]), + idUser: v.id, + subscription: v.Subscribe?.subscription, + })) + + dataNotif.push(...omitData) + dataPush.push(...omitPush) + + } + + + const pushNotif = dataPush.filter((item) => item.subscription != undefined) + + // const sendWebPush = await funSendWebPush({ sub: pushNotif, message: { title: 'Divisi Baru', body: 'Terdapat divisi 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 divisi', table: 'division', data: data.id, user: userId }) + + return NextResponse.json({ success: true, message: "Berhasil menambahkan divisi", notif: dataNotif, }, { status: 200 }); + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal menambahkan divisi, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +}; \ No newline at end of file diff --git a/src/app/api/mobile/project/file/[id]/route.ts b/src/app/api/mobile/project/file/[id]/route.ts index 106d916..40d7648 100644 --- a/src/app/api/mobile/project/file/[id]/route.ts +++ b/src/app/api/mobile/project/file/[id]/route.ts @@ -159,34 +159,34 @@ export async function POST(request: Request, context: { params: { id: string } } } if (cekFile) { - console.log('masuk file') - // for (var pair of body.entries()) { - // if (String(pair[0]).substring(0, 4) == "file") { - // const file = body.get(pair[0]) as File - // const fExt = file.name.split(".").pop() - // const fName = file.name.replace("." + fExt, "") + body.delete("data") + for (var pair of body.entries()) { + if (String(pair[0]).substring(0, 4) == "file") { + const file = body.get(pair[0]) as File + const fExt = file.name.split(".").pop() + const fName = file.name.replace("." + fExt, "") - // const upload = await funUploadFile({ file: file, dirId: DIR.project }) - // if (upload.success) { - // const insertToTable = await prisma.projectFile.create({ - // data: { - // idStorage: upload.data.id, - // idProject: id, - // name: fName, - // extension: String(fExt), + const upload = await funUploadFile({ file: file, dirId: DIR.project }) + if (upload.success) { + const insertToTable = await prisma.projectFile.create({ + data: { + idStorage: upload.data.id, + idProject: id, + name: fName, + extension: String(fExt), - // }, - // select: { - // id: true - // } - // }) - // } - // } - // } + }, + select: { + id: true + } + }) + } + } + } } - // // create log user - // const log = await createLogUserMobile({ act: 'CREATE', desc: 'User menambah file kegiatan', table: 'project', data: String(id), user: user.id }) + // create log user + const log = await createLogUserMobile({ act: 'CREATE', desc: 'User menambah file kegiatan', table: 'project', data: String(id), user: user.id }) return NextResponse.json({ success: true, message: "Berhasil mengupload file kegiatan" }, { status: 200 }); } catch (error) { diff --git a/src/app/api/mobile/project/route.ts b/src/app/api/mobile/project/route.ts index 1eddfb8..afdee06 100644 --- a/src/app/api/mobile/project/route.ts +++ b/src/app/api/mobile/project/route.ts @@ -1,5 +1,4 @@ -import { title } from 'process'; -import { DIR, funSendWebPush, funUploadFile, prisma } from "@/module/_global"; +import { DIR, funUploadFile, prisma } from "@/module/_global"; import { funGetUserById } from "@/module/auth"; import { createLogUserMobile } from "@/module/user"; import _, { ceil } from "lodash"; @@ -150,7 +149,6 @@ export async function GET(request: Request) { // CREATE PROJECT export async function POST(request: Request) { try { - const body = await request.formData() const dataBody = body.get("data") const cekFile = body.has("file0") @@ -206,6 +204,7 @@ export async function POST(request: Request) { } if (cekFile) { + body.delete("data") for (var pair of body.entries()) { if (String(pair[0]).substring(0, 4) == "file") { const file = body.get(pair[0]) as File @@ -347,7 +346,7 @@ export async function POST(request: Request) { // create log user const log = await createLogUserMobile({ act: 'CREATE', desc: 'User membuat data kegiatan', table: 'project', data: data.id, user: userMobile.id }) - return NextResponse.json({ success: true, message: "Berhasil membuat kegiatan"}, { status: 200 }); + return NextResponse.json({ success: true, message: "Berhasil membuat kegiatan" }, { status: 200 }); } catch (error) { console.error(error);