127 lines
3.5 KiB
TypeScript
127 lines
3.5 KiB
TypeScript
import Elysia, { t } from "elysia";
|
|
import { prisma } from "../lib/prisma";
|
|
import { generateNoPengaduan } from "../lib/no-pengaduan";
|
|
import { normalizePhoneNumber } from "../lib/normalizePhone";
|
|
|
|
const TestPengaduanRoute = new Elysia({
|
|
prefix: "online-pengaduan",
|
|
tags: ["test"]
|
|
})
|
|
.get("/category", async () => {
|
|
const data = await prisma.categoryPengaduan.findMany({
|
|
where: {
|
|
isActive: true
|
|
},
|
|
orderBy: {
|
|
name: "asc"
|
|
},
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
}
|
|
})
|
|
|
|
return { data }
|
|
}, {
|
|
detail: {
|
|
summary: "List Kategori Pengaduan",
|
|
description: `tool untuk mendapatkan list kategori pengaduan`,
|
|
tags: ["test"]
|
|
}
|
|
})
|
|
|
|
.post("/create", async ({ body }) => {
|
|
const { judulPengaduan, detailPengaduan, lokasi, kategoriId, noTelepon, image } = body
|
|
const noPengaduan = await generateNoPengaduan()
|
|
let idCategoryFix = kategoriId
|
|
|
|
if (idCategoryFix) {
|
|
const category = await prisma.categoryPengaduan.findUnique({
|
|
where: {
|
|
id: idCategoryFix,
|
|
}
|
|
})
|
|
|
|
if (!category) {
|
|
const cariCategory = await prisma.categoryPengaduan.findFirst({
|
|
where: {
|
|
name: kategoriId,
|
|
}
|
|
})
|
|
|
|
if (!cariCategory) {
|
|
idCategoryFix = "lainnya"
|
|
} else {
|
|
idCategoryFix = cariCategory.id
|
|
}
|
|
|
|
}
|
|
} else {
|
|
idCategoryFix = "lainnya"
|
|
}
|
|
|
|
|
|
|
|
const nomorHP = normalizePhoneNumber({ phone: "089697338821" })
|
|
const cariWarga = await prisma.warga.upsert({
|
|
where: {
|
|
phone: nomorHP,
|
|
},
|
|
create: {
|
|
name: "malik",
|
|
phone: nomorHP,
|
|
},
|
|
update: {
|
|
name: "malik",
|
|
phone: nomorHP,
|
|
},
|
|
})
|
|
|
|
|
|
const pengaduan = await prisma.pengaduan.create({
|
|
data: {
|
|
title: judulPengaduan,
|
|
detail: detailPengaduan,
|
|
idCategory: idCategoryFix,
|
|
idWarga: cariWarga.id,
|
|
location: lokasi,
|
|
image: body.image || "",
|
|
noPengaduan,
|
|
},
|
|
select: {
|
|
id: true,
|
|
}
|
|
})
|
|
|
|
if (!pengaduan.id) {
|
|
return { success: false, message: 'gagal membuat pengaduan' }
|
|
}
|
|
|
|
await prisma.historyPengaduan.create({
|
|
data: {
|
|
idPengaduan: pengaduan.id,
|
|
deskripsi: "Pengaduan dibuat",
|
|
}
|
|
})
|
|
|
|
return { success: true, message: 'pengaduan sudah dibuat dengan nomer ' + noPengaduan + ', nomer ini akan digunakan untuk mengakses pengaduan ini' }
|
|
}, {
|
|
body: t.Object({
|
|
judulPengaduan: t.String(),
|
|
detailPengaduan: t.String(),
|
|
lokasi: t.String(),
|
|
kategoriId: t.String(),
|
|
noTelepon: t.Optional(t.String()),
|
|
image: t.Optional(t.String()),
|
|
}),
|
|
|
|
detail: {
|
|
summary: "Buat Pengaduan Warga",
|
|
description: `
|
|
Endpoint ini digunakan untuk membuat data pengaduan (laporan) baru dari warga.`
|
|
}
|
|
})
|
|
|
|
export default TestPengaduanRoute
|
|
|