upd: calender

Deskripsi:
- repeat pengulangan
- install new package
- tambah field table
- ubah semua api mengikuti struktur tb baru

No Issues
This commit is contained in:
amel
2024-09-09 16:34:38 +08:00
parent 789fda0b02
commit 61e3431f9c
15 changed files with 330 additions and 155 deletions

View File

@@ -2,8 +2,74 @@ import { prisma } from "@/module/_global";
import { funGetUserByCookies } from "@/module/auth";
import { createLogUser } 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 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 cek = await prisma.divisionCalendar.count({
where: {
id: id
}
})
if (cek == 0) {
return NextResponse.json(
{
success: false,
message: "Gagal mendapatkan calender, data tidak ditemukan",
},
{ status: 404 }
);
}
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 calender", data: result }, { status: 200 });
} catch (error) {
return NextResponse.json(
{
success: false,
message: "Gagal mendapatkan calender, data tidak ditemukan",
},
{ status: 404 }
);
}
}
// TAMBAH MEMBER KALENDER
export async function POST(request: Request, context: { params: { id: string } }) {
try {

View File

@@ -1,11 +1,12 @@
import { prisma } from "@/module/_global";
import { funGetUserByCookies } from "@/module/auth";
import { createLogUser } from "@/module/user";
import _ from "lodash";
import _, { remove } from "lodash";
import moment from "moment";
import { NextResponse } from "next/server";
import { Frequency, RRule } from 'rrule';
// GET ONE CALENDER
// GET ONE CALENDER BY ID KALENDER REMINDER
export async function GET(request: Request, context: { params: { id: string } }) {
try {
const user = await funGetUserByCookies()
@@ -15,7 +16,7 @@ export async function GET(request: Request, context: { params: { id: string } })
const { id } = context.params
const cek = await prisma.divisionCalendar.count({
const cek = await prisma.divisionCalendarReminder.count({
where: {
id: id
}
@@ -25,39 +26,51 @@ export async function GET(request: Request, context: { params: { id: string } })
return NextResponse.json(
{
success: false,
message: "Gagal mendapatkan calender, data tidak ditemukan",
message: "Gagal mendapatkan acara, data tidak ditemukan",
},
{ status: 404 }
);
}
const data = await prisma.divisionCalendar.findUnique({
const data: any = await prisma.divisionCalendarReminder.findUnique({
where: {
id: id
},
select: {
id: true,
title: true,
desc: true,
timeStart: true,
dateStart: true,
timeEnd: true,
createdAt: true,
linkMeet: true,
repeatEventTyper: true,
DivisionCalendar: {
select: {
id: true,
title: true,
desc: true,
linkMeet: true,
repeatEventTyper: true,
repeatValue: true,
}
}
}
});
const { ...dataCalender } = data
const { DivisionCalendar, ...dataCalender } = data
const timeStart = moment.utc(dataCalender?.timeStart).format("HH:mm")
const timeEnd = moment.utc(dataCalender?.timeEnd).format("HH:mm")
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
const result = { ...dataCalender, timeStart, timeEnd }
const result = { ...dataCalender, timeStart, timeEnd, idCalendar, title, desc, linkMeet, repeatEventTyper, repeatValue }
const member = await prisma.divisionCalendarMember.findMany({
where: {
idCalendar: id
idCalendar: data?.DivisionCalendar.id
},
select: {
id: true,
@@ -120,7 +133,7 @@ export async function DELETE(request: Request, context: { params: { id: string }
return NextResponse.json(
{
success: false,
message: "Gagal menghapus calender, data tidak ditemukan",
message: "Gagal menghapus acara kalender, data tidak ditemukan",
},
{ status: 404 }
);
@@ -138,7 +151,7 @@ export async function DELETE(request: Request, context: { params: { id: string }
// create log user
const log = await createLogUser({ act: 'DELETE', desc: 'User menghapus data acara kalender', table: 'divisionCalendar', data: id })
return NextResponse.json({ success: true, message: "Berhasil menghapus calender", data }, { status: 200 });
return NextResponse.json({ success: true, message: "Berhasil menghapus acara kalender", data }, { status: 200 });
} catch (error) {
return NextResponse.json(
@@ -151,7 +164,7 @@ export async function DELETE(request: Request, context: { params: { id: string }
}
}
// EDIT CALENDER BY ID
// EDIT CALENDER BY IDKALENDER
export async function PUT(request: Request, context: { params: { id: string } }) {
try {
@@ -162,7 +175,7 @@ export async function PUT(request: Request, context: { params: { id: string } })
const { id } = context.params
const userId = user.id
const { title, desc, timeStart, dateStart, timeEnd, linkMeet, repeatEventTyper } = await request.json()
const { title, desc, timeStart, dateStart, timeEnd, linkMeet, repeatEventTyper, repeatValue } = await request.json()
const cek = await prisma.divisionCalendar.count({
where: {
@@ -198,10 +211,48 @@ export async function PUT(request: Request, context: { params: { id: string } })
timeEnd: timeEndFix,
linkMeet: linkMeet,
repeatEventTyper: repeatEventTyper,
status: statusCalender
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 createLogUser({ act: 'UPDATE', desc: 'User mengupdate data acara kalender', table: 'divisionCalendar', data: id })

View File

@@ -29,21 +29,29 @@ export async function GET(request: Request) {
return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, data tidak ditemukan" }, { status: 404 });
}
const data = await prisma.divisionCalendar.findMany({
const data = await prisma.divisionCalendarReminder.findMany({
where: {
isActive: true,
idDivision: idDivision,
title: {
contains: (name == undefined || name == "null") ? "" : name,
mode: "insensitive"
DivisionCalendar: {
title: {
contains: (name == undefined || name == "null") ? "" : name,
mode: "insensitive"
},
isActive: true
}
},
select: {
id: true,
title: true,
timeStart: true,
dateStart: true,
timeEnd: true,
DivisionCalendar: {
select: {
title: true,
}
}
},
orderBy: [
{
@@ -59,8 +67,8 @@ export async function GET(request: Request) {
});
const allOmit = data.map((v: any) => ({
..._.omit(v, [""]),
dateStart: v.dateStart,
..._.omit(v, ["DivisionCalendar"]),
title: v.DivisionCalendar.title
}))
// groupBy untuk dateStart

View File

@@ -31,13 +31,16 @@ export async function GET(request: Request) {
return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, data tidak ditemukan" }, { status: 404 });
}
const data = await prisma.divisionCalendar.findMany({
const data = await prisma.divisionCalendarReminder.findMany({
where: {
isActive: true,
idDivision: String(idDivision),
dateStart: {
gte: new Date(awalDate),
lte: new Date(akhirDate),
},
DivisionCalendar: {
isActive: true
}
}
})

View File

@@ -1,4 +1,3 @@
import { User } from './../../../module/discussion/lib/type_discussion';
import { prisma } from "@/module/_global";
import { funGetUserByCookies } from "@/module/auth";
import _ from "lodash";
@@ -6,6 +5,7 @@ import moment from "moment";
import { NextResponse } from "next/server";
import "moment/locale/id";
import { createLogUser } from '@/module/user';
import { Frequency, RRule } from 'rrule';
//GET ALL CALENDER
export async function GET(request: Request) {
@@ -32,25 +32,33 @@ export async function GET(request: Request) {
return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, data tidak ditemukan" }, { status: 404 });
}
const data = await prisma.divisionCalendar.findMany({
const data = await prisma.divisionCalendarReminder.findMany({
where: {
isActive: true,
idDivision: idDivision,
dateStart: new Date(String(isDate))
dateStart: new Date(String(isDate)),
DivisionCalendar:{
isActive: true
}
},
select: {
id: true,
title: true,
desc: true,
status: true,
idCalendar: true,
timeStart: true,
dateStart: true,
timeEnd: true,
dateEnd: true,
createdAt: true,
User: {
status: true,
DivisionCalendar: {
select: {
name: true
title: true,
desc: true,
User: {
select: {
name: true
}
}
}
}
},
@@ -60,8 +68,10 @@ export async function GET(request: Request) {
});
const allOmit = data.map((v: any) => ({
..._.omit(v, ["User"]),
user_name: v.User.name,
..._.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')
}))
@@ -88,7 +98,7 @@ export async function POST(request: Request) {
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
}
const { idDivision, title, desc, timeStart, timeEnd, dateStart, dateEnd, repeatEventTyper, member, linkMeet } = (await request.json());
const { idDivision, title, desc, timeStart, timeEnd, dateStart, dateEnd, repeatEventTyper, member, linkMeet, repeatValue } = (await request.json());
const userId = user.id
@@ -103,8 +113,6 @@ export async function POST(request: Request) {
return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, data tidak ditemukan" }, { status: 404 });
}
const statusCalender = 0
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()
@@ -121,16 +129,44 @@ export async function POST(request: Request) {
linkMeet,
repeatEventTyper,
desc,
status: statusCalender
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"]),
..._.omit(v, ["name", "idUser", "img"]),
idCalendar: data.id,
idUser: v.idUser
}))