Merge pull request #451 from bipproduction/amalia/23-mei-25
upd: api mobile
This commit is contained in:
174
src/app/api/mobile/calendar/[id]/member/route.ts
Normal file
174
src/app/api/mobile/calendar/[id]/member/route.ts
Normal file
@@ -0,0 +1,174 @@
|
||||
import { prisma } from "@/module/_global";
|
||||
import { funGetUserById } from "@/module/auth";
|
||||
import { createLogUserMobile } from "@/module/user";
|
||||
import _ from "lodash";
|
||||
import moment from "moment";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
|
||||
// GET ONE DATA KALENDER BY ID KALENDER
|
||||
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 cek = await prisma.divisionCalendar.count({
|
||||
where: {
|
||||
id: id
|
||||
}
|
||||
})
|
||||
|
||||
if (cek == 0) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: "Gagal mendapatkan kalender, data tidak ditemukan",
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
}
|
||||
|
||||
const data = await prisma.divisionCalendar.findUnique({
|
||||
where: {
|
||||
id: id
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
desc: true,
|
||||
timeStart: true,
|
||||
dateStart: true,
|
||||
timeEnd: true,
|
||||
createdAt: true,
|
||||
linkMeet: true,
|
||||
repeatValue: true,
|
||||
repeatEventTyper: true,
|
||||
}
|
||||
});
|
||||
|
||||
const { ...dataCalender } = data
|
||||
const timeStart = moment.utc(dataCalender?.timeStart).format("HH:mm")
|
||||
const timeEnd = moment.utc(dataCalender?.timeEnd).format("HH:mm")
|
||||
|
||||
const result = { ...dataCalender, timeStart, timeEnd }
|
||||
|
||||
return NextResponse.json({ success: true, message: "Berhasil mendapatkan kalender", data: result }, { status: 200 });
|
||||
|
||||
} catch (error) {
|
||||
return NextResponse.json({ success: false, message: "Gagal mendapatkan kalender, data tidak ditemukan (error: 500)", }, { status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// TAMBAH MEMBER KALENDER
|
||||
export async function POST(request: Request, context: { params: { id: string } }) {
|
||||
try {
|
||||
const { id } = context.params
|
||||
const { member, 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.divisionCalendar.count({
|
||||
where: {
|
||||
id: id
|
||||
}
|
||||
})
|
||||
|
||||
if (cek == 0) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: "Gagal menambahkan anggota, data tidak ditemukan",
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
if (member.length > 0) {
|
||||
const dataMember = member.map((v: any) => ({
|
||||
..._.omit(v, ["idUser", "name", "img"]),
|
||||
idCalendar: id,
|
||||
idUser: v.idUser,
|
||||
}))
|
||||
|
||||
const insertMember = await prisma.divisionCalendarMember.createMany({
|
||||
data: dataMember
|
||||
})
|
||||
}
|
||||
|
||||
// create log user
|
||||
const log = await createLogUserMobile({ act: 'CREATE', desc: 'User menambah anggota kalender', table: 'divisionCalendar', data: String(id), user: userMobile.id })
|
||||
|
||||
return NextResponse.json({ success: true, message: "Berhasil menambahkan anggota", }, { status: 200 });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal menambah anggota, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
// MENGELUARKAN ANGGOTA
|
||||
export async function DELETE(request: Request, context: { params: { id: string } }) {
|
||||
try {
|
||||
const { id } = context.params;
|
||||
const { idUser, 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.divisionCalendar.count({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
if (data == 0) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: "Gagal mengeluarkan anggota, data tidak ditemukan",
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
}
|
||||
|
||||
const del = await prisma.divisionCalendarMember.deleteMany({
|
||||
where: {
|
||||
idUser: idUser,
|
||||
idCalendar: id
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
// create log user
|
||||
const log = await createLogUserMobile({ act: 'DELETE', desc: 'User mengeluarkan anggota acara kalender', table: 'divisionCalendar', data: String(id), user: userMobile.id })
|
||||
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
message: "Berhasil mengeluarkan anggota",
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal mengeluarkan anggota, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
265
src/app/api/mobile/calendar/[id]/route.ts
Normal file
265
src/app/api/mobile/calendar/[id]/route.ts
Normal file
@@ -0,0 +1,265 @@
|
||||
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";
|
||||
import { Frequency, RRule } from 'rrule';
|
||||
|
||||
// GET ONE CALENDER BY ID KALENDER REMINDER
|
||||
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 kategori = 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.divisionCalendarReminder.count({
|
||||
where: {
|
||||
id: id
|
||||
}
|
||||
})
|
||||
|
||||
if (cek == 0) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: "Gagal mendapatkan acara, data tidak ditemukan",
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
}
|
||||
let dataFix
|
||||
|
||||
const data: any = await prisma.divisionCalendarReminder.findUnique({
|
||||
where: {
|
||||
id: id
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
timeStart: true,
|
||||
dateStart: true,
|
||||
timeEnd: true,
|
||||
createdAt: true,
|
||||
DivisionCalendar: {
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
desc: true,
|
||||
linkMeet: true,
|
||||
repeatEventTyper: true,
|
||||
repeatValue: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
if (kategori == "data") {
|
||||
const { DivisionCalendar, ...dataCalender } = data
|
||||
const timeStart = moment.utc(dataCalender?.timeStart).format("HH:mm")
|
||||
const timeEnd = moment.utc(dataCalender?.timeEnd).format("HH:mm")
|
||||
const dateStart = moment.utc(dataCalender?.dateStart).format("LL")
|
||||
const idCalendar = data?.DivisionCalendar.id
|
||||
const title = data?.DivisionCalendar?.title
|
||||
const desc = data?.DivisionCalendar?.desc
|
||||
const linkMeet = data?.DivisionCalendar?.linkMeet
|
||||
const repeatEventTyper = data?.DivisionCalendar?.repeatEventTyper
|
||||
const repeatValue = data?.DivisionCalendar?.repeatValue
|
||||
|
||||
|
||||
dataFix = { ...dataCalender, timeStart, timeEnd, dateStart, idCalendar, title, desc, linkMeet, repeatEventTyper, repeatValue }
|
||||
} else if (kategori == "member") {
|
||||
const member = await prisma.divisionCalendarMember.findMany({
|
||||
where: {
|
||||
idCalendar: data?.DivisionCalendar.id
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
idUser: true,
|
||||
User: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true,
|
||||
img: true
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
dataFix = member.map((v: any) => ({
|
||||
..._.omit(v, ["User"]),
|
||||
name: v.User.name,
|
||||
email: v.User.email,
|
||||
img: v.User.img
|
||||
}))
|
||||
|
||||
}
|
||||
|
||||
|
||||
return NextResponse.json({ success: true, message: "Berhasil mendapatkan kalender", data: dataFix }, { status: 200 });
|
||||
|
||||
} catch (error) {
|
||||
return NextResponse.json({ success: false, message: "Gagal mendapatkan kalender, data tidak ditemukan (error: 500)", }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE CALENDER BY ID
|
||||
export async function DELETE(request: Request, context: { params: { id: string } }) {
|
||||
try {
|
||||
const { id } = context.params
|
||||
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 cek = await prisma.divisionCalendar.count({
|
||||
where: {
|
||||
id: id
|
||||
}
|
||||
})
|
||||
|
||||
if (cek == 0) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: "Gagal menghapus acara kalender, data tidak ditemukan",
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
}
|
||||
|
||||
const data = await prisma.divisionCalendar.update({
|
||||
where: {
|
||||
id: id
|
||||
},
|
||||
data: {
|
||||
isActive: false
|
||||
},
|
||||
select: {
|
||||
dateStart: true
|
||||
}
|
||||
});
|
||||
|
||||
// create log user
|
||||
const log = await createLogUserMobile({ act: 'DELETE', desc: 'User menghapus data acara kalender', table: 'divisionCalendar', data: id, user: userMobile.id })
|
||||
|
||||
return NextResponse.json({ success: true, message: "Berhasil menghapus acara kalender", data, user: user.id }, { status: 200 });
|
||||
|
||||
} catch (error) {
|
||||
return NextResponse.json({ success: false, message: "Gagal menghapus kalender, coba lagi nanti (error: 500)", }, { status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// EDIT CALENDER BY IDKALENDER
|
||||
export async function PUT(request: Request, context: { params: { id: string } }) {
|
||||
try {
|
||||
|
||||
const { id } = context.params
|
||||
const { title, desc, timeStart, dateStart, timeEnd, linkMeet, repeatEventTyper, repeatValue, 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 userId = userMobile.id
|
||||
|
||||
const cek = await prisma.divisionCalendar.count({
|
||||
where: {
|
||||
id: id
|
||||
}
|
||||
})
|
||||
|
||||
if (cek == 0) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: "Gagal mengedit acara, data tidak ditemukan",
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
}
|
||||
|
||||
const y = new Date('1970-01-01 ' + timeStart)
|
||||
const x = new Date('1970-01-01 ' + timeEnd)
|
||||
const timeStartFix = new Date(y.getTime() - (y.getTimezoneOffset() * 60000)).toISOString()
|
||||
const timeEndFix = new Date(x.getTime() - (x.getTimezoneOffset() * 60000)).toISOString()
|
||||
const statusCalender = 0
|
||||
const data = await prisma.divisionCalendar.update({
|
||||
where: {
|
||||
id: id
|
||||
},
|
||||
data: {
|
||||
title: title,
|
||||
desc: desc,
|
||||
createdBy: String(userId),
|
||||
timeStart: timeStartFix,
|
||||
dateStart: new Date(dateStart),
|
||||
timeEnd: timeEndFix,
|
||||
linkMeet: linkMeet,
|
||||
repeatEventTyper: repeatEventTyper,
|
||||
status: statusCalender,
|
||||
repeatValue: Number(repeatValue)
|
||||
},
|
||||
select: {
|
||||
idDivision: true
|
||||
}
|
||||
});
|
||||
|
||||
const freq: Frequency = repeatEventTyper === "yearly" ? RRule.YEARLY :
|
||||
repeatEventTyper === "monthly" ? RRule.MONTHLY :
|
||||
repeatEventTyper === "weekly" ? RRule.WEEKLY :
|
||||
repeatEventTyper === "daily" ? RRule.DAILY :
|
||||
repeatEventTyper === "hourly" ? RRule.HOURLY :
|
||||
repeatEventTyper === "minutely" ? RRule.MINUTELY :
|
||||
RRule.SECONDLY;
|
||||
|
||||
const rule = new RRule({
|
||||
freq,
|
||||
interval: 1,
|
||||
dtstart: new Date(dateStart),
|
||||
count: repeatValue
|
||||
});
|
||||
|
||||
const hasil = rule.all().map(recurrenceDate => ({
|
||||
idDivision: data.idDivision,
|
||||
idCalendar: id,
|
||||
dateStart: recurrenceDate,
|
||||
timeStart: timeStartFix,
|
||||
timeEnd: timeEndFix,
|
||||
dateEnd: recurrenceDate
|
||||
}));
|
||||
|
||||
const deleteReminder = await prisma.divisionCalendarReminder.deleteMany({
|
||||
where: {
|
||||
idCalendar: id
|
||||
}
|
||||
})
|
||||
|
||||
const insertReminder = await prisma.divisionCalendarReminder.createMany({
|
||||
data: hasil
|
||||
})
|
||||
|
||||
|
||||
// create log user
|
||||
const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User mengupdate data acara kalender', table: 'divisionCalendar', data: id, user: userMobile.id })
|
||||
|
||||
return NextResponse.json({ success: true, message: "Berhasil mengedit acara" }, { status: 200 });
|
||||
|
||||
} catch (error) {
|
||||
return NextResponse.json({ success: false, message: "Gagal mengedit acara, coba lagi nanti (error: 500)", }, { status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
115
src/app/api/mobile/calendar/history/route.ts
Normal file
115
src/app/api/mobile/calendar/history/route.ts
Normal file
@@ -0,0 +1,115 @@
|
||||
import { prisma } from "@/module/_global";
|
||||
import { funGetUserById } from "@/module/auth";
|
||||
import _ from "lodash";
|
||||
import moment from "moment";
|
||||
import "moment/locale/id";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
// GET HISTORY
|
||||
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 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 && idDivision != "" && 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: 200 });
|
||||
}
|
||||
|
||||
const data = await prisma.divisionCalendarReminder.findMany({
|
||||
// skip: dataSkip,
|
||||
// take: 10,
|
||||
where: {
|
||||
isActive: true,
|
||||
idDivision: idDivision,
|
||||
dateEnd: {
|
||||
lte: new Date()
|
||||
},
|
||||
DivisionCalendar: {
|
||||
title: {
|
||||
contains: (name == undefined || name == "null") ? "" : name,
|
||||
mode: "insensitive"
|
||||
},
|
||||
isActive: true
|
||||
}
|
||||
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
timeStart: true,
|
||||
dateStart: true,
|
||||
timeEnd: true,
|
||||
DivisionCalendar: {
|
||||
select: {
|
||||
title: true,
|
||||
}
|
||||
}
|
||||
},
|
||||
orderBy: [
|
||||
{
|
||||
dateStart: 'asc'
|
||||
},
|
||||
{
|
||||
timeStart: 'asc'
|
||||
},
|
||||
{
|
||||
timeEnd: 'asc'
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
const allOmit = data.map((v: any) => ({
|
||||
..._.omit(v, ["DivisionCalendar", "dateStart"]),
|
||||
dateStart: moment.utc(v.dateStart).format("DD MMM"),
|
||||
year: moment.utc(v.dateStart).format("YYYY"),
|
||||
title: v.DivisionCalendar.title,
|
||||
}))
|
||||
|
||||
|
||||
// groupBy untuk dateStart
|
||||
const groupByDateStart = _.groupBy(allOmit, 'dateStart');
|
||||
|
||||
const result = Object.keys(groupByDateStart).map(key => {
|
||||
const obj = groupByDateStart[key];
|
||||
const year = obj[0].year
|
||||
const data = obj.map((v: any) => ({
|
||||
id: v.id,
|
||||
title: v.title,
|
||||
timeEnd: moment.utc(v.timeEnd).format('HH:mm'),
|
||||
timeStart: moment.utc(v.timeStart).format('HH:mm')
|
||||
}))
|
||||
return {
|
||||
dateStart: key,
|
||||
year: year,
|
||||
data: data
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json({ success: true, message: "Berhasil mendapatkan riwayat acara kalender", data: result }, { status: 200 });
|
||||
|
||||
} else {
|
||||
return NextResponse.json({ success: false, message: "Gagal mendapatkan riwayat acara kalender, coba lagi nanti" }, { status: 200 });
|
||||
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
return NextResponse.json({ success: false, message: "Gagal mendapatkan riwayat acara kalender, coba lagi nanti (error: 500)" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
68
src/app/api/mobile/calendar/indicator/route.ts
Normal file
68
src/app/api/mobile/calendar/indicator/route.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import { prisma } from "@/module/_global";
|
||||
import { funGetUserById } from "@/module/auth";
|
||||
import _ from "lodash";
|
||||
import moment from "moment";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const idDivision = searchParams.get("division");
|
||||
const date = searchParams.get("date");
|
||||
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 awalDate = moment(date).format('YYYY-MM') + '-01'
|
||||
const akhirDate = moment(awalDate).add(1, 'M').format('YYYY-MM-DD')
|
||||
|
||||
|
||||
const cekDivision = await prisma.division.count({
|
||||
where: {
|
||||
id: String(idDivision),
|
||||
// isActive: true
|
||||
}
|
||||
})
|
||||
|
||||
if (cekDivision == 0) {
|
||||
return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, data tidak ditemukan" }, { status: 200 });
|
||||
}
|
||||
|
||||
const data = await prisma.divisionCalendarReminder.findMany({
|
||||
where: {
|
||||
isActive: true,
|
||||
idDivision: String(idDivision),
|
||||
dateStart: {
|
||||
gte: new Date(awalDate),
|
||||
lte: new Date(akhirDate),
|
||||
},
|
||||
DivisionCalendar: {
|
||||
isActive: true
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const dataGroup = _.map(_.groupBy(data, "dateStart"), (v: any) => ({
|
||||
dateContent: v[0].dateStart
|
||||
}))
|
||||
|
||||
const result = dataGroup.map(a => moment(a.dateContent).format('YYYY-MM-DD'));
|
||||
|
||||
|
||||
return NextResponse.json({ success: true, message: "Berhasil mendapatkan list acara", data: result }, { status: 200 });
|
||||
} catch (error) {
|
||||
return NextResponse.json({ success: false, message: "Gagal mendapatkan list acara (error: 500)" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
305
src/app/api/mobile/calendar/route.ts
Normal file
305
src/app/api/mobile/calendar/route.ts
Normal file
@@ -0,0 +1,305 @@
|
||||
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";
|
||||
import { Frequency, RRule } from 'rrule';
|
||||
|
||||
//GET ALL CALENDER
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const idDivision = searchParams.get("division");
|
||||
const isDate = searchParams.get("date")
|
||||
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 });
|
||||
}
|
||||
|
||||
|
||||
if (idDivision != "null" && idDivision != null && idDivision != undefined && idDivision != "") {
|
||||
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: 200 });
|
||||
}
|
||||
|
||||
const data = await prisma.divisionCalendarReminder.findMany({
|
||||
where: {
|
||||
isActive: true,
|
||||
idDivision: idDivision,
|
||||
dateStart: new Date(String(isDate)),
|
||||
DivisionCalendar: {
|
||||
isActive: true
|
||||
}
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
idCalendar: true,
|
||||
timeStart: true,
|
||||
dateStart: true,
|
||||
timeEnd: true,
|
||||
dateEnd: true,
|
||||
createdAt: true,
|
||||
status: true,
|
||||
DivisionCalendar: {
|
||||
select: {
|
||||
title: true,
|
||||
desc: true,
|
||||
User: {
|
||||
select: {
|
||||
name: true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
orderBy: [
|
||||
{
|
||||
dateStart: 'asc'
|
||||
},
|
||||
{
|
||||
timeStart: 'asc'
|
||||
},
|
||||
{
|
||||
timeEnd: 'asc'
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
const allOmit = data.map((v: any) => ({
|
||||
..._.omit(v, ["DivisionCalendar", "User"]),
|
||||
title: v.DivisionCalendar.title,
|
||||
desc: v.DivisionCalendar.desc,
|
||||
user_name: v.DivisionCalendar.User.name,
|
||||
timeStart: moment.utc(v.timeStart).format('HH:mm'),
|
||||
timeEnd: moment.utc(v.timeEnd).format('HH:mm')
|
||||
}))
|
||||
|
||||
|
||||
return NextResponse.json({ success: true, message: "Berhasil mendapatkan kalender", data: allOmit }, { status: 200 });
|
||||
|
||||
} else {
|
||||
return NextResponse.json({ success: false, message: "Gagal mendapatkan kalender, data tidak ditemukan" }, { status: 200 });
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
return NextResponse.json({ success: false, message: "Gagal mendapatkan kalender, data tidak ditemukan (error: 500)" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//CREATE CALENDER
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const { idDivision, title, desc, timeStart, timeEnd, dateStart, dateEnd, repeatEventTyper, member, linkMeet, repeatValue, 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 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: 200 });
|
||||
}
|
||||
|
||||
const y = new Date('1970-01-01 ' + timeStart)
|
||||
const x = new Date('1970-01-01 ' + timeEnd)
|
||||
const timeStartFix = new Date(y.getTime() - (y.getTimezoneOffset() * 60000)).toISOString()
|
||||
const timeEndFix = new Date(x.getTime() - (x.getTimezoneOffset() * 60000)).toISOString()
|
||||
|
||||
const data = await prisma.divisionCalendar.create({
|
||||
data: {
|
||||
idDivision,
|
||||
createdBy: String(userId),
|
||||
title,
|
||||
dateStart: new Date(dateStart),
|
||||
timeStart: timeStartFix,
|
||||
timeEnd: timeEndFix,
|
||||
linkMeet,
|
||||
repeatEventTyper,
|
||||
desc,
|
||||
repeatValue: Number(repeatValue)
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
const freq: Frequency = repeatEventTyper === "yearly" ? RRule.YEARLY :
|
||||
repeatEventTyper === "monthly" ? RRule.MONTHLY :
|
||||
repeatEventTyper === "weekly" ? RRule.WEEKLY :
|
||||
repeatEventTyper === "daily" ? RRule.DAILY :
|
||||
repeatEventTyper === "hourly" ? RRule.HOURLY :
|
||||
repeatEventTyper === "minutely" ? RRule.MINUTELY :
|
||||
RRule.SECONDLY;
|
||||
|
||||
const rule = new RRule({
|
||||
freq,
|
||||
interval: 1,
|
||||
dtstart: new Date(dateStart),
|
||||
count: repeatValue
|
||||
});
|
||||
|
||||
const hasil = rule.all().map(recurrenceDate => ({
|
||||
idDivision: idDivision,
|
||||
idCalendar: data.id,
|
||||
dateStart: recurrenceDate,
|
||||
timeStart: timeStartFix,
|
||||
timeEnd: timeEndFix,
|
||||
dateEnd: recurrenceDate
|
||||
}));
|
||||
|
||||
const insertReminder = await prisma.divisionCalendarReminder.createMany({
|
||||
data: hasil
|
||||
})
|
||||
|
||||
const omitMember = member.map((v: any) => ({
|
||||
..._.omit(v, ["name", "idUser", "img"]),
|
||||
idCalendar: data.id,
|
||||
idUser: v.idUser
|
||||
}))
|
||||
|
||||
const insertMember = await prisma.divisionCalendarMember.createMany({
|
||||
data: omitMember
|
||||
});
|
||||
|
||||
|
||||
// create log user
|
||||
const log = await createLogUserMobile({ act: 'CREATE', desc: 'User membuat data acara kalender', table: 'divisionCalendar', data: data.id, user: String(userId) })
|
||||
|
||||
return NextResponse.json({ success: true, message: "Berhasil membuat acara kalender" }, { status: 200 });
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal membuat acara kalender, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// CEK TGL AVAILABLE
|
||||
export async function PUT(request: Request) {
|
||||
try {
|
||||
const { idDivision, title, desc, timeStart, timeEnd, dateStart, dateEnd, repeatEventTyper, member, linkMeet, repeatValue, 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 userId = userMobile.id
|
||||
const division = await prisma.division.findUnique({
|
||||
where: {
|
||||
id: idDivision
|
||||
}
|
||||
})
|
||||
|
||||
const y = new Date('1970-01-01 ' + timeStart)
|
||||
const x = new Date('1970-01-01 ' + timeEnd)
|
||||
const timeStartFix = new Date(y.getTime() - (y.getTimezoneOffset() * 60000)).toISOString()
|
||||
const timeEndFix = new Date(x.getTime() - (x.getTimezoneOffset() * 60000)).toISOString()
|
||||
|
||||
const cek = await prisma.divisionCalendarReminder.findMany({
|
||||
where: {
|
||||
isActive: true,
|
||||
Division: {
|
||||
idGroup: division?.idGroup
|
||||
},
|
||||
OR: [
|
||||
{
|
||||
dateStart: new Date(dateStart),
|
||||
DivisionCalendar: {
|
||||
title: {
|
||||
equals: title,
|
||||
mode: "insensitive"
|
||||
},
|
||||
}
|
||||
},
|
||||
{
|
||||
dateStart: new Date(dateStart),
|
||||
OR: [
|
||||
{
|
||||
timeStart: { //13:00
|
||||
lte: timeStartFix, //13:30
|
||||
},
|
||||
timeEnd: { //14:00
|
||||
gt: timeStartFix, //13:30
|
||||
}
|
||||
},
|
||||
{
|
||||
timeStart: { //07:00
|
||||
lt: timeEndFix, //07:30
|
||||
},
|
||||
timeEnd: { //08:00
|
||||
gt: timeEndFix, //07:30
|
||||
}
|
||||
},
|
||||
{
|
||||
timeStart: { //07:00
|
||||
gt: timeStartFix, //06:30
|
||||
},
|
||||
timeEnd: { //08:00
|
||||
lte: timeEndFix, //09:30
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
}
|
||||
]
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
dateStart: true,
|
||||
timeStart: true,
|
||||
timeEnd: true,
|
||||
DivisionCalendar: {
|
||||
select: {
|
||||
title: true,
|
||||
}
|
||||
},
|
||||
Division: {
|
||||
select: {
|
||||
name: true
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const dataSama = cek.map((v: any) => ({
|
||||
..._.omit(v, ["DivisionCalendar", "Division"]),
|
||||
title: v.DivisionCalendar.title,
|
||||
divisi: v.Division.name
|
||||
}))
|
||||
|
||||
if (cek.length > 0) {
|
||||
return NextResponse.json({ success: false, message: "Tidak dapat membuat acara kalender, acara kalender sudah ada pada tanggal tersebut", data: dataSama }, { status: 400 });
|
||||
} else {
|
||||
return NextResponse.json({ success: true, message: "Berhasil membuat acara kalender" }, { status: 200 });
|
||||
}
|
||||
|
||||
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
return NextResponse.json({ success: false, message: "Gagal membuat acara kalender, coba lagi nanti (error: 500)" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { prisma } from "@/module/_global";
|
||||
import { funGetUserByCookies } from "@/module/auth";
|
||||
import { funGetUserById } from "@/module/auth";
|
||||
import _ from "lodash";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
@@ -9,26 +9,28 @@ 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 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,
|
||||
}
|
||||
where: {
|
||||
id: String(id),
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
if (!data) {
|
||||
return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, data tidak ditemukan", }, { status: 404 });
|
||||
return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, data tidak ditemukan" }, { status: 200 });
|
||||
}
|
||||
|
||||
const member = await prisma.divisionMember.findMany({
|
||||
where: {
|
||||
idDivision: String(id),
|
||||
idDivision: String(id),
|
||||
isActive: true,
|
||||
User: {
|
||||
name: {
|
||||
@@ -43,15 +45,15 @@ export async function GET(request: Request, context: { params: { id: string } })
|
||||
isLeader: true,
|
||||
idUser: true,
|
||||
User: {
|
||||
select: {
|
||||
name: true,
|
||||
img: true
|
||||
}
|
||||
select: {
|
||||
name: true,
|
||||
img: true
|
||||
}
|
||||
}
|
||||
},
|
||||
orderBy: {
|
||||
},
|
||||
orderBy: {
|
||||
isAdmin: 'desc',
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const fixMember = member.map((v: any) => ({
|
||||
@@ -59,8 +61,6 @@ export async function GET(request: Request, context: { params: { id: string } })
|
||||
name: v.User.name,
|
||||
img: v.User.img
|
||||
}))
|
||||
|
||||
|
||||
|
||||
return NextResponse.json({ success: true, data: fixMember })
|
||||
|
||||
|
||||
Reference in New Issue
Block a user