Files
desa-darmasaba/src/app/api/[[...slugs]]/_lib/kesehatan/kontak-darurat/updt.ts
nico b9d43eb723 fix(storage): migrate domain-specific delete and update handlers to MinIO
- Replaces local filesystem operations (fs.unlink, path.join) with MinIO removeObject in all domain lib handlers
- Updated handlers for: Berita, GalleryFoto, Layanan, Musik, Penghargaan, Potensi, Profile, Inovasi, Keamanan, Kesehatan, LandingPage, and PPID
- bump: version 0.1.19 -> 0.1.20
2026-04-23 17:05:44 +08:00

103 lines
2.9 KiB
TypeScript

import prisma from "@/lib/prisma";
import { Prisma } from "@prisma/client";
import { Context } from "elysia";
import minio, { MINIO_BUCKET } from "@/lib/minio";
type FormUpdate = Prisma.KontakDaruratGetPayload<{
select: {
id: true;
name: true;
deskripsi: true;
imageId: true;
whatsapp: true;
}
}>
export default async function kontakDaruratUpdate(context: Context) {
try {
const id = context.params?.id as string;
const body = (await context.body) as Omit<FormUpdate, "id">;
const {
name,
deskripsi,
imageId,
whatsapp,
} = body;
if(!id) {
return new Response(JSON.stringify({
success: false,
message: "ID tidak boleh kosong",
}), {
status: 400,
headers: {
'Content-Type': 'application/json'
}
})
}
const existing = await prisma.kontakDarurat.findUnique({
where: { id },
include: {
image: true,
}
})
if (!existing) {
return new Response(JSON.stringify({
success: false,
message: "Kontak Darurat tidak ditemukan",
}), {
status: 404,
headers: {
'Content-Type': 'application/json'
}
})
}
if (existing.imageId && existing.imageId !== imageId) {
const oldImage = existing.image;
if (oldImage) {
try {
await minio.removeObject(MINIO_BUCKET, `${oldImage.path}/${oldImage.name}`);
await prisma.fileStorage.delete({
where: { id: oldImage.id },
});
} catch (err) {
console.error("Gagal hapus gambar lama:", err);
}
}
}
const updated = await prisma.kontakDarurat.update({
where: { id },
data: {
name,
deskripsi,
imageId,
whatsapp,
}
})
return new Response(JSON.stringify({
success: true,
message: "Kontak Darurat berhasil diupdate",
data: updated,
}), {
status: 200,
headers: {
'Content-Type': 'application/json'
}
})
} catch (error) {
console.error("Error updating kontak darurat:", error);
return new Response(
JSON.stringify({
success: false,
message: "Terjadi kesalahan saat mengupdate kontak darurat",
}),
{ status: 500, headers: { 'Content-Type': 'application/json' } }
);
}
}