api ai
Deskripsi: - api get list calendar - api get detail calendar No Issues
This commit is contained in:
99
src/app/api/ai/calendar/[id]/route.ts
Normal file
99
src/app/api/ai/calendar/[id]/route.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import { prisma } from "@/module/_global";
|
||||
import _ from "lodash";
|
||||
import moment from "moment";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
// GET ONE CALENDER BY ID KALENDER REMINDER
|
||||
export async function GET(request: Request, context: { params: { id: string } }) {
|
||||
try {
|
||||
const { id } = context.params
|
||||
|
||||
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: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
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, title, desc, linkMeet, repeatEventTyper, repeatValue }
|
||||
|
||||
|
||||
const member = await prisma.divisionCalendarMember.findMany({
|
||||
where: {
|
||||
idCalendar
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
idUser: true,
|
||||
User: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true,
|
||||
img: true
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
const fixMember = member.map((v: any) => ({
|
||||
..._.omit(v, ["User"]),
|
||||
name: v.User.name,
|
||||
email: v.User.email,
|
||||
img: v.User.img
|
||||
}))
|
||||
|
||||
|
||||
const dataFix = {
|
||||
...result,
|
||||
member: fixMember,
|
||||
}
|
||||
|
||||
|
||||
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 });
|
||||
}
|
||||
}
|
||||
149
src/app/api/ai/calendar/route.ts
Normal file
149
src/app/api/ai/calendar/route.ts
Normal file
@@ -0,0 +1,149 @@
|
||||
import { prisma } from "@/module/_global";
|
||||
import _ from "lodash";
|
||||
import moment from "moment";
|
||||
import "moment/locale/id";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
//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 villageId = searchParams.get("desa")
|
||||
const active = searchParams.get("active")
|
||||
const search = searchParams.get("search")
|
||||
const page = searchParams.get("page")
|
||||
const get = searchParams.get("get")
|
||||
|
||||
let getFix = 0;
|
||||
if (get == null || get == undefined || get == "" || _.isNaN(Number(get))) {
|
||||
getFix = 10;
|
||||
} else {
|
||||
getFix = Number(get);
|
||||
}
|
||||
|
||||
|
||||
|
||||
const dataSkip = page == null || page == undefined ? 0 : Number(page) * getFix - getFix;
|
||||
|
||||
|
||||
let kondisi: any = {}
|
||||
|
||||
if (idDivision != "" && idDivision != null && idDivision != undefined) {
|
||||
if (isDate != null && isDate != undefined && isDate != "") {
|
||||
kondisi = {
|
||||
idDivision: String(idDivision),
|
||||
dateStart: new Date(String(isDate)),
|
||||
DivisionCalendar: {
|
||||
title: {
|
||||
contains: (search == undefined || search == null) ? "" : search,
|
||||
mode: "insensitive"
|
||||
},
|
||||
isActive: (active == "false" || active == undefined) ? false : true,
|
||||
Division: {
|
||||
idVillage: String(villageId)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
kondisi = {
|
||||
idDivision: String(idDivision),
|
||||
DivisionCalendar: {
|
||||
title: {
|
||||
contains: (search == undefined || search == null) ? "" : search,
|
||||
mode: "insensitive"
|
||||
},
|
||||
isActive: (active == "false" || active == undefined) ? false : true,
|
||||
Division: {
|
||||
idVillage: String(villageId)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (isDate != null && isDate != undefined && isDate != "") {
|
||||
kondisi = {
|
||||
dateStart: new Date(String(isDate)),
|
||||
DivisionCalendar: {
|
||||
title: {
|
||||
contains: (search == undefined || search == null) ? "" : search,
|
||||
mode: "insensitive"
|
||||
},
|
||||
isActive: (active == "false" || active == undefined) ? false : true,
|
||||
Division: {
|
||||
idVillage: String(villageId)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
kondisi = {
|
||||
DivisionCalendar: {
|
||||
title: {
|
||||
contains: (search == undefined || search == null) ? "" : search,
|
||||
mode: "insensitive"
|
||||
},
|
||||
isActive: (active == "false" || active == undefined) ? false : true,
|
||||
Division: {
|
||||
idVillage: String(villageId)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const data = await prisma.divisionCalendarReminder.findMany({
|
||||
where: kondisi,
|
||||
skip: dataSkip,
|
||||
take: getFix,
|
||||
select: {
|
||||
id: true,
|
||||
dateStart: true,
|
||||
timeStart: true,
|
||||
timeEnd: true,
|
||||
createdAt: true,
|
||||
DivisionCalendar: {
|
||||
select: {
|
||||
isActive: true,
|
||||
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,
|
||||
createdBy: v.DivisionCalendar.User.name,
|
||||
isActive: v.DivisionCalendar.isActive,
|
||||
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 });
|
||||
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
return NextResponse.json({ success: false, message: "Gagal mendapatkan kalender, data tidak ditemukan (error: 500)" }, { status: 404 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user