UI & API Tabs Detail Data Pengangguran
This commit is contained in:
@@ -1,3 +1,64 @@
|
||||
// import prisma from "@/lib/prisma";
|
||||
// import { Prisma } from "@prisma/client";
|
||||
// import { Context } from "elysia";
|
||||
|
||||
// type FormCreate = Prisma.DetailDataPengangguranGetPayload<{
|
||||
// select: {
|
||||
// month: true;
|
||||
// year: true;
|
||||
// totalUnemployment: true;
|
||||
// educatedUnemployment: true;
|
||||
// uneducatedUnemployment: true;
|
||||
// percentageChange: true;
|
||||
// };
|
||||
// }>;
|
||||
|
||||
// export default async function detailDataPengangguranCreate(context: Context) {
|
||||
// const body = context.body as FormCreate;
|
||||
|
||||
// // Cek apakah data untuk bulan & tahun tersebut sudah ada
|
||||
// const existing = await prisma.detailDataPengangguran.findFirst({
|
||||
// where: {
|
||||
// month: body.month,
|
||||
// year: body.year,
|
||||
// },
|
||||
// });
|
||||
|
||||
// if (existing) {
|
||||
// return {
|
||||
// success: false,
|
||||
// message: `Data bulan ${body.month} ${body.year} sudah ada.`,
|
||||
// data: null,
|
||||
// };
|
||||
// }
|
||||
|
||||
// const created = await prisma.detailDataPengangguran.create({
|
||||
// data: {
|
||||
// month: body.month,
|
||||
// year: body.year,
|
||||
// totalUnemployment: body.totalUnemployment,
|
||||
// educatedUnemployment: body.educatedUnemployment,
|
||||
// uneducatedUnemployment: body.uneducatedUnemployment,
|
||||
// percentageChange: body.percentageChange ?? null,
|
||||
// },
|
||||
// select: {
|
||||
// id: true,
|
||||
// month: true,
|
||||
// year: true,
|
||||
// totalUnemployment: true,
|
||||
// educatedUnemployment: true,
|
||||
// uneducatedUnemployment: true,
|
||||
// percentageChange: true,
|
||||
// },
|
||||
// });
|
||||
|
||||
// return {
|
||||
// success: true,
|
||||
// message: "Berhasil menambahkan data pengangguran bulanan",
|
||||
// data: created,
|
||||
// };
|
||||
// }
|
||||
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { Context } from "elysia";
|
||||
@@ -9,14 +70,14 @@ type FormCreate = Prisma.DetailDataPengangguranGetPayload<{
|
||||
totalUnemployment: true;
|
||||
educatedUnemployment: true;
|
||||
uneducatedUnemployment: true;
|
||||
percentageChange: true;
|
||||
};
|
||||
}>;
|
||||
|
||||
const monthOrder = ["Jan", "Feb", "Mar", "Apr", "Mei", "Jun", "Jul", "Agu", "Sep", "Okt", "Nov", "Des"];
|
||||
|
||||
export default async function detailDataPengangguranCreate(context: Context) {
|
||||
const body = context.body as FormCreate;
|
||||
|
||||
// Cek apakah data untuk bulan & tahun tersebut sudah ada
|
||||
const existing = await prisma.detailDataPengangguran.findFirst({
|
||||
where: {
|
||||
month: body.month,
|
||||
@@ -32,6 +93,24 @@ export default async function detailDataPengangguranCreate(context: Context) {
|
||||
};
|
||||
}
|
||||
|
||||
// Cari bulan sebelumnya
|
||||
const currentMonthIndex = monthOrder.indexOf(body.month);
|
||||
const prevMonth = currentMonthIndex > 0 ? monthOrder[currentMonthIndex - 1] : "Des";
|
||||
const prevYear = currentMonthIndex > 0 ? body.year : body.year - 1;
|
||||
|
||||
const prevData = await prisma.detailDataPengangguran.findFirst({
|
||||
where: {
|
||||
month: prevMonth,
|
||||
year: prevYear,
|
||||
},
|
||||
});
|
||||
|
||||
let percentageChange: number | null = null;
|
||||
if (prevData) {
|
||||
const change = ((body.totalUnemployment - prevData.totalUnemployment) / prevData.totalUnemployment) * 100;
|
||||
percentageChange = parseFloat(change.toFixed(1));
|
||||
}
|
||||
|
||||
const created = await prisma.detailDataPengangguran.create({
|
||||
data: {
|
||||
month: body.month,
|
||||
@@ -39,7 +118,7 @@ export default async function detailDataPengangguranCreate(context: Context) {
|
||||
totalUnemployment: body.totalUnemployment,
|
||||
educatedUnemployment: body.educatedUnemployment,
|
||||
uneducatedUnemployment: body.uneducatedUnemployment,
|
||||
percentageChange: body.percentageChange ?? null,
|
||||
percentageChange,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
export default async function findByMonthYear(context: Context) {
|
||||
const { month, year } = context.params as { month: string; year: string };
|
||||
|
||||
if (!month || !year) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Bulan dan tahun wajib diisi",
|
||||
data: null,
|
||||
};
|
||||
}
|
||||
|
||||
const data = await prisma.detailDataPengangguran.findFirst({
|
||||
where: {
|
||||
month,
|
||||
year: Number(year),
|
||||
},
|
||||
});
|
||||
|
||||
if (!data) {
|
||||
return {
|
||||
success: false,
|
||||
message: `Data untuk bulan ${month} tahun ${year} tidak ditemukan`,
|
||||
data: null,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Data ditemukan",
|
||||
data,
|
||||
};
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import detailDataPengangguranCreate from "./create";
|
||||
import detailDataPengangguranUpdate from "./updt";
|
||||
import detailDataPengangguranFindUnique from "./findUnique";
|
||||
import detailDataPengangguranDelete from "./del";
|
||||
import findByMonthYear from "./findByMonthYear";
|
||||
|
||||
const DetailDataPengangguran = new Elysia({
|
||||
prefix: "/detaildatapengangguran",
|
||||
@@ -43,6 +44,12 @@ const DetailDataPengangguran = new Elysia({
|
||||
params: t.Object({
|
||||
id: t.String(),
|
||||
}),
|
||||
})
|
||||
.get("/month/:month/year/:year", findByMonthYear, {
|
||||
params: t.Object({
|
||||
month: t.String(),
|
||||
year: t.String(),
|
||||
}),
|
||||
});
|
||||
|
||||
export default DetailDataPengangguran;
|
||||
|
||||
@@ -1,68 +1,145 @@
|
||||
// import prisma from "@/lib/prisma";
|
||||
// import { Context } from "elysia";
|
||||
|
||||
// export default async function detailDataPengangguranUpdate(context: Context) {
|
||||
// const id = context.params?.id;
|
||||
|
||||
// if (!id) {
|
||||
// return {
|
||||
// success: false,
|
||||
// message: "ID tidak ditemukan",
|
||||
// }
|
||||
// }
|
||||
|
||||
// const { month, year, totalUnemployment, educatedUnemployment, uneducatedUnemployment, percentageChange } = context.body as {
|
||||
// month: string;
|
||||
// year: number;
|
||||
// totalUnemployment: number;
|
||||
// educatedUnemployment: number;
|
||||
// uneducatedUnemployment: number;
|
||||
// percentageChange: number;
|
||||
// }
|
||||
|
||||
// const duplicate = await prisma.detailDataPengangguran.findFirst({
|
||||
// where: {
|
||||
// month,
|
||||
// year,
|
||||
// NOT: { id },
|
||||
// },
|
||||
// });
|
||||
|
||||
// if (duplicate) {
|
||||
// return {
|
||||
// success: false,
|
||||
// message: `Data bulan ${month} ${year} sudah ada.`,
|
||||
// };
|
||||
// }
|
||||
|
||||
// const existing = await prisma.detailDataPengangguran.findUnique({
|
||||
// where: {
|
||||
// id: id,
|
||||
// },
|
||||
// })
|
||||
|
||||
// if (!existing) {
|
||||
// return {
|
||||
// success: false,
|
||||
// message: "Data tidak ditemukan",
|
||||
// }
|
||||
// }
|
||||
|
||||
// const updated = await prisma.detailDataPengangguran.update({
|
||||
// where: { id },
|
||||
// data: {
|
||||
// month,
|
||||
// year,
|
||||
// totalUnemployment,
|
||||
// educatedUnemployment,
|
||||
// uneducatedUnemployment,
|
||||
// percentageChange,
|
||||
// },
|
||||
// })
|
||||
|
||||
// return {
|
||||
// success: true,
|
||||
// message: "Data berhasil diupdate",
|
||||
// data: updated,
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
|
||||
const monthOrder = ["Jan", "Feb", "Mar", "Apr", "Mei", "Jun", "Jul", "Agu", "Sep", "Okt", "Nov", "Des"];
|
||||
|
||||
export default async function detailDataPengangguranUpdate(context: Context) {
|
||||
const id = context.params?.id;
|
||||
|
||||
|
||||
if (!id) {
|
||||
return {
|
||||
success: false,
|
||||
message: "ID tidak ditemukan",
|
||||
}
|
||||
return { success: false, message: "ID tidak ditemukan" };
|
||||
}
|
||||
|
||||
const { month, year, totalUnemployment, educatedUnemployment, uneducatedUnemployment, percentageChange } = context.body as {
|
||||
month: string;
|
||||
year: number;
|
||||
totalUnemployment: number;
|
||||
educatedUnemployment: number;
|
||||
uneducatedUnemployment: number;
|
||||
percentageChange: number;
|
||||
}
|
||||
|
||||
|
||||
const { month, year, totalUnemployment, educatedUnemployment, uneducatedUnemployment } = context.body as {
|
||||
month: string;
|
||||
year: number;
|
||||
totalUnemployment: number;
|
||||
educatedUnemployment: number;
|
||||
uneducatedUnemployment: number;
|
||||
};
|
||||
|
||||
const duplicate = await prisma.detailDataPengangguran.findFirst({
|
||||
where: {
|
||||
month,
|
||||
year,
|
||||
NOT: { id },
|
||||
},
|
||||
where: {
|
||||
month,
|
||||
year,
|
||||
NOT: { id },
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
if (duplicate) {
|
||||
return {
|
||||
success: false,
|
||||
message: `Data bulan ${month} ${year} sudah ada.`,
|
||||
};
|
||||
}
|
||||
|
||||
const existing = await prisma.detailDataPengangguran.findUnique({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
})
|
||||
|
||||
if (!existing) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Data tidak ditemukan",
|
||||
}
|
||||
return { success: false, message: `Data bulan ${month} ${year} sudah ada.` };
|
||||
}
|
||||
|
||||
|
||||
const current = await prisma.detailDataPengangguran.findUnique({ where: { id } });
|
||||
|
||||
if (!current) {
|
||||
return { success: false, message: "Data tidak ditemukan" };
|
||||
}
|
||||
|
||||
const currentMonthIndex = monthOrder.indexOf(month);
|
||||
const prevMonth = currentMonthIndex > 0 ? monthOrder[currentMonthIndex - 1] : "Des";
|
||||
const prevYear = currentMonthIndex > 0 ? year : year - 1;
|
||||
|
||||
const prevData = await prisma.detailDataPengangguran.findFirst({
|
||||
where: {
|
||||
month: prevMonth,
|
||||
year: prevYear,
|
||||
},
|
||||
});
|
||||
|
||||
let percentageChange: number | null = null;
|
||||
if (prevData) {
|
||||
const change = ((totalUnemployment - prevData.totalUnemployment) / prevData.totalUnemployment) * 100;
|
||||
percentageChange = parseFloat(change.toFixed(1));
|
||||
}
|
||||
|
||||
const updated = await prisma.detailDataPengangguran.update({
|
||||
where: { id },
|
||||
data: {
|
||||
month,
|
||||
year,
|
||||
totalUnemployment,
|
||||
educatedUnemployment,
|
||||
uneducatedUnemployment,
|
||||
percentageChange,
|
||||
},
|
||||
})
|
||||
|
||||
where: { id },
|
||||
data: {
|
||||
month,
|
||||
year,
|
||||
totalUnemployment,
|
||||
educatedUnemployment,
|
||||
uneducatedUnemployment,
|
||||
percentageChange,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Data berhasil diupdate",
|
||||
data: updated,
|
||||
}
|
||||
}
|
||||
success: true,
|
||||
message: "Data berhasil diupdate",
|
||||
data: updated,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user