From 65682f5f51427252e08e424c95549912b138a8c3 Mon Sep 17 00:00:00 2001 From: amel Date: Mon, 13 Jan 2025 17:38:06 +0800 Subject: [PATCH] rev: calender diviis Deskripsi: - validasi judul & tgl || tgl & jam nb: blm tampil data yg konflik No Issues --- src/app/api/calender/route.ts | 75 +++++++++++++++++++++++++++++++++-- 1 file changed, 71 insertions(+), 4 deletions(-) diff --git a/src/app/api/calender/route.ts b/src/app/api/calender/route.ts index 320d328..1ae3295 100644 --- a/src/app/api/calender/route.ts +++ b/src/app/api/calender/route.ts @@ -212,18 +212,85 @@ export async function PUT(request: Request) { } }) - const cek = await prisma.divisionCalendarReminder.count({ + 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, - dateStart: new Date(dateStart), 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 + } } } }) - if (cek > 0) { - return NextResponse.json({ success: false, message: "Tidak dapat membuat acara kalender, acara kalender sudah ada pada tanggal tersebut" }, { status: 400 }); + 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 }); }