FIX UI & API Menu Desa, Submenu Pelayanan Surat Keterangan
This commit is contained in:
@@ -7,6 +7,7 @@ type FormCreate = Prisma.PelayananSuratKeteranganGetPayload<{
|
||||
name: true;
|
||||
deskripsi: true;
|
||||
imageId: true;
|
||||
image2Id: true;
|
||||
};
|
||||
}>;
|
||||
async function createPelayananSuratKeterangan(context: Context) {
|
||||
@@ -17,6 +18,7 @@ async function createPelayananSuratKeterangan(context: Context) {
|
||||
name: body.name,
|
||||
deskripsi: body.deskripsi,
|
||||
imageId: body.imageId,
|
||||
image2Id: body.image2Id,
|
||||
},
|
||||
});
|
||||
return {
|
||||
|
||||
@@ -17,6 +17,7 @@ const pelayananSuratKeteranganDelete = async (context: Context) => {
|
||||
where: { id },
|
||||
include: {
|
||||
image: true,
|
||||
image2: true,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -40,6 +41,18 @@ const pelayananSuratKeteranganDelete = async (context: Context) => {
|
||||
}
|
||||
}
|
||||
|
||||
if (pelayananSuratKeterangan.image2) {
|
||||
try {
|
||||
const filePath = path.join(pelayananSuratKeterangan.image2.path, pelayananSuratKeterangan.image2.name);
|
||||
await fs.unlink(filePath);
|
||||
await prisma.fileStorage.delete({
|
||||
where: { id: pelayananSuratKeterangan.image2.id },
|
||||
});
|
||||
} catch (err) {
|
||||
console.error("Gagal hapus gambar lama:", err);
|
||||
}
|
||||
}
|
||||
|
||||
const deleted = await prisma.pelayananSuratKeterangan.delete({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
@@ -1,24 +1,47 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
export default async function pelayananSuratKeteranganFindMany() {
|
||||
try {
|
||||
const data = await prisma.pelayananSuratKeterangan.findMany({
|
||||
where: { isActive: true },
|
||||
include: {
|
||||
image: true,
|
||||
},
|
||||
});
|
||||
export default async function pelayananSuratKeteranganFindMany(context: Context) {
|
||||
const page = Number(context.query.page) || 1;
|
||||
const limit = Number(context.query.limit) || 10;
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Success fetch pelayanan surat keterangan",
|
||||
data,
|
||||
};
|
||||
} catch (e) {
|
||||
console.error("Find many error:", e);
|
||||
return {
|
||||
success: false,
|
||||
message: "Failed fetch pelayanan surat keterangan",
|
||||
};
|
||||
}
|
||||
try {
|
||||
const [data, total] = await Promise.all([
|
||||
prisma.pelayananSuratKeterangan.findMany({
|
||||
where: { isActive: true },
|
||||
include: {
|
||||
image: true,
|
||||
image2: true,
|
||||
},
|
||||
skip,
|
||||
take: limit,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
}),
|
||||
prisma.pelayananSuratKeterangan.count({
|
||||
where: { isActive: true }
|
||||
})
|
||||
]);
|
||||
|
||||
const totalPages = Math.ceil(total / limit);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Success fetch pelayanan surat keterangan with pagination",
|
||||
data,
|
||||
page,
|
||||
totalPages,
|
||||
total,
|
||||
};
|
||||
} catch (e) {
|
||||
console.error("Find many paginated error:", e);
|
||||
return {
|
||||
success: false,
|
||||
message: "Failed fetch pelayanan surat keterangan with pagination",
|
||||
data: [],
|
||||
page: 1,
|
||||
totalPages: 1,
|
||||
total: 0,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,7 @@ export default async function pelayananSuratKeteranganFindUnique(request: Reques
|
||||
where: { id },
|
||||
include: {
|
||||
image: true,
|
||||
image2: true,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ const PelayananSuratKeterangan = new Elysia({ prefix: "/pelayanansuratketerangan
|
||||
name: t.String(),
|
||||
deskripsi: t.String(),
|
||||
imageId: t.String(),
|
||||
image2Id: t.String(),
|
||||
}),
|
||||
})
|
||||
.delete("/del/:id", pelayananSuratKeteranganDelete)
|
||||
@@ -30,6 +31,7 @@ const PelayananSuratKeterangan = new Elysia({ prefix: "/pelayanansuratketerangan
|
||||
name: t.String(),
|
||||
deskripsi: t.String(),
|
||||
imageId: t.String(),
|
||||
image2Id: t.String(),
|
||||
}),
|
||||
})
|
||||
export default PelayananSuratKeterangan;
|
||||
@@ -9,6 +9,7 @@ type FormUpdate = Prisma.PelayananSuratKeteranganGetPayload<{
|
||||
name: true;
|
||||
deskripsi: true;
|
||||
imageId: true;
|
||||
image2Id: true;
|
||||
};
|
||||
}>;
|
||||
export default async function updatePelayananSuratKeterangan(context: Context) {
|
||||
@@ -16,7 +17,7 @@ export default async function updatePelayananSuratKeterangan(context: Context) {
|
||||
const id = context.params?.id;
|
||||
const body = (await context.body) as Omit<FormUpdate, "id">;
|
||||
|
||||
const { name, deskripsi, imageId } = body;
|
||||
const { name, deskripsi, imageId, image2Id } = body;
|
||||
|
||||
if (!id) {
|
||||
return new Response(JSON.stringify({
|
||||
@@ -63,12 +64,28 @@ export default async function updatePelayananSuratKeterangan(context: Context) {
|
||||
}
|
||||
}
|
||||
|
||||
if (existing.image2Id && existing.image2Id !== image2Id) {
|
||||
const oldImage = existing.image;
|
||||
if (oldImage) {
|
||||
try {
|
||||
const filePath = path.join(oldImage.path, oldImage.name);
|
||||
await fs.unlink(filePath);
|
||||
await prisma.fileStorage.delete({
|
||||
where: { id: oldImage.id },
|
||||
});
|
||||
} catch (err) {
|
||||
console.error("Gagal hapus gambar lama:", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const updated = await prisma.pelayananSuratKeterangan.update({
|
||||
where: { id },
|
||||
data: {
|
||||
name,
|
||||
deskripsi,
|
||||
imageId,
|
||||
image2Id,
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user