diff --git a/prisma/data/fetchWithRetry.ts b/prisma/data/fetchWithRetry.ts new file mode 100644 index 00000000..e561ed00 --- /dev/null +++ b/prisma/data/fetchWithRetry.ts @@ -0,0 +1,30 @@ +export default async function fetchWithRetry( + url: string, + retries = 3, + timeoutMs = 20000 +) { + for (let attempt = 1; attempt <= retries; attempt++) { + const controller = new AbortController(); + const timeout = setTimeout(() => controller.abort(), timeoutMs); + + try { + const res = await fetch(url, { signal: controller.signal }); + + if (!res.ok) { + throw new Error(`HTTP ${res.status} ${res.statusText}`); + } + + return res; + } catch (err) { + console.warn(`⚠️ Download attempt ${attempt} failed`); + + if (attempt === retries) { + throw err; + } + } finally { + clearTimeout(timeout); + } + } + + throw new Error("Unreachable"); +} diff --git a/prisma/data/file-storage.json b/prisma/data/file-storage.json index 53c27a0b..3e978084 100644 --- a/prisma/data/file-storage.json +++ b/prisma/data/file-storage.json @@ -1,137 +1,120 @@ [ { - "id": "cmff0rr4z0002vn0twp333m2", - "name": "S6RIjFaPvdQm3oq4rM4X9-desktop.webp", - "realName": "bares.png", - "path": "uploads/images", - "mimeType": "image/webp", - "link": "/api/fileStorage/findUnique/S6RIjFaPvdQm3oq4rM4X9-desktop.webp", - "category": "image" - }, - { - "id": "cmff0tnf00003vn0t3kgzi0u0", - "name": "_pVNEmThU5ICGa8gv3gh_-desktop.webp", - "realName": "bicara-darma.png", - "path": "uploads/images", - "mimeType": "image/webp", - "link": "/api/fileStorage/findUnique/_pVNEmThU5ICGa8gv3gh_-desktop.webp", - "category": "image" - }, - { - "id": "cmff0uykf0004vn0trmmxpgfh", - "name": "bv6rdKvjxkkjUSGLQ0lvB-desktop.webp", - "realName": "daves.png", - "path": "uploads/images", - "mimeType": "image/webp", - "link": "/api/fileStorage/findUnique/bv6rdKvjxkkjUSGLQ0lvB-desktop.webp", - "category": "image" - }, - { - "id": "cmff0z34f0005vn0tjtvq519p", - "name": "Z4hWaV04CvoE20MjccQsV-desktop.webp", - "realName": "mangan.png", - "path": "uploads/images", - "mimeType": "image/webp", - "link": "/api/fileStorage/findUnique/Z4hWaV04CvoE20MjccQsV-desktop.webp", - "category": "image" - }, - { - "id": "cmff38cyq000bvn0t9f01cz3f", - "name": "LvLAtOqWojx4sn6NjJWB9-desktop.webp", - "realName": "gelah-melah.png", - "path": "uploads/images", - "mimeType": "image/webp", - "link": "/api/fileStorage/findUnique/LvLAtOqWojx4sn6NjJWB9-desktop.webp", - "category": "image" - }, - { - "id": "cmff0zqvd0007vn0tv6o5hjcq", - "name": "gR2mcvAQVgJ2-rM5coYJj-desktop.webp", - "realName": "inovasi-desa-darmasaba.png", - "path": "uploads/images", - "mimeType": "image/webp", - "link": "/api/fileStorage/findUnique/gR2mcvAQVgJ2-rM5coYJj-desktop.webp", - "category": "image" - }, - { - "id": "cmff1013m0008vn0th7t0d64d", - "name": "JpL-9F8-IGztMn8E2ce02-desktop.webp", - "realName": "pdkt.png", - "path": "uploads/images", - "mimeType": "image/webp", - "link": "/api/fileStorage/findUnique/JpL-9F8-IGztMn8E2ce02-desktop.webp", - "category": "image" - }, - { - "id": "cmff10cwq0009vn0tse8dzu3j", - "name": "bxAk4AsGbJTC705_IVdes-desktop.webp", - "realName": "sajjiana-dharma-raksaka.png", - "path": "uploads/images", - "mimeType": "image/webp", - "link": "/api/fileStorage/findUnique/bxAk4AsGbJTC705_IVdes-desktop.webp", - "category": "image" - }, - { - "id": "cmff2w5ly000avn0telhct71k", - "name": "Vbj_osnMJUkGEQGDTLwV--desktop.webp", + "id": "cmk27746i0000vnso2aspwf9g", + "name": "Eqlrr1W-pK8ShMGqgPGL3-desktop.webp", "realName": "perbekel.png", "path": "uploads/images", "mimeType": "image/webp", - "link": "/api/fileStorage/findUnique/Vbj_osnMJUkGEQGDTLwV--desktop.webp", + "link": "/api/fileStorage/findUnique/Eqlrr1W-pK8ShMGqgPGL3-desktop.webp", + "category": "image" + } + , + { + "id": "cmk20mg320000vnevxy0k73fr", + "name": "thpgPSJkBxUIRajZt3AVo-desktop.webp", + "realName": "bares.png", + "path": "uploads/images", + "mimeType": "image/webp", + "link": "/api/fileStorage/findUnique/thpgPSJkBxUIRajZt3AVo-desktop.webp", "category": "image" }, { - "id": "cmff3joae0000vn6h8sgs0ilg", - "name": "7hox9spUxj56hY_EBYLnj-desktop.webp", + "id": "cmk20nqmu0001vnevfte29rk0", + "name": "ubna9N6r7RgVWN5plO5mq-desktop.webp", + "realName": "bicara-darma.png", + "path": "uploads/images", + "mimeType": "image/webp", + "link": "/api/fileStorage/findUnique/ubna9N6r7RgVWN5plO5mq-desktop.webp", + "category": "image" + }, + { + "id": "cmk228urs0007vnevi5b66bqn", + "name": "Z4i2RRnnlHq2iWj94ldyo-desktop.webp", + "realName": "daves.png", + "path": "uploads/images", + "mimeType": "image/webp", + "link": "/api/fileStorage/findUnique/Z4i2RRnnlHq2iWj94ldyo-desktop.webp", + "category": "image" + }, + { + "id": "cmk20nyen0002vnevd0hfr3u8", + "name": "y4yaE4XdUP1TSUGhWPW9h-desktop.webp", + "realName": "mangan.png", + "path": "uploads/images", + "mimeType": "image/webp", + "link": "/api/fileStorage/findUnique/y4yaE4XdUP1TSUGhWPW9h-desktop.webp", + "category": "image" + }, + { + "id": "cmk20o7mf0003vnevohrksm1d", + "name": "Vr7CoaYDpk2dIkHx9PxRj-desktop.webp", + "realName": "gelah-melah.png", + "path": "uploads/images", + "mimeType": "image/webp", + "link": "/api/fileStorage/findUnique/Vr7CoaYDpk2dIkHx9PxRj-desktop.webp", + "category": "image" + }, + { + "id": "cmk20of8m0004vnev9ujy5o0l", + "name": "ceoB_sg-HOzljN8j_2nZA-desktop.webp", + "realName": "inovasi-desa-darmasaba.png", + "path": "uploads/images", + "mimeType": "image/webp", + "link": "/api/fileStorage/findUnique/ceoB_sg-HOzljN8j_2nZA-desktop.webp", + "category": "image" + }, + { + "id": "cmk20omzq0005vnevgi6f4edu", + "name": "vOy5YVUXfHXfiFOHylIN7-desktop.webp", + "realName": "pdkt.png", + "path": "uploads/images", + "mimeType": "image/webp", + "link": "/api/fileStorage/findUnique/vOy5YVUXfHXfiFOHylIN7-desktop.webp", + "category": "image" + }, + { + "id": "cmk20pf3d0006vnev3mkoqpyy", + "name": "gE_qcqIbY0mqI6FV9V4CL-desktop.webp", + "realName": "sajjiana-dharma-raksaka.png", + "path": "uploads/images", + "mimeType": "image/webp", + "link": "/api/fileStorage/findUnique/gE_qcqIbY0mqI6FV9V4CL-desktop.webp", + "category": "image" + }, + { + "id": "cmk2cgqgm0003vn96jun52pik", + "name": "q1G995W7cLkC_qquLTlKN-desktop.webp", "realName": "youtube.png", "path": "uploads/images", "mimeType": "image/webp", - "link": "/api/fileStorage/findUnique/7hox9spUxj56hY_EBYLnj-desktop.webp", + "link": "/api/fileStorage/findUnique/q1G995W7cLkC_qquLTlKN-desktop.webp", "category": "image" }, { - "id": "cmff3ll130001vn6hkhls3f5y", - "name": "ChihV7_1eS-AGtSg9UwMv-desktop.webp", - "realName": "gmail.png", - "path": "uploads/images", - "mimeType": "image/webp", - "link": "/api/fileStorage/findUnique/ChihV7_1eS-AGtSg9UwMv-desktop.webp", - "category": "image" - }, - { - "id": "cmff3mtat0002vn6hs8vyyhdd", - "name": "z8v9ZREwOJHKGIRYauROt-desktop.webp", + "id": "cmk2cmr000006vn96qepq6gvl", + "name": "I6mlQ4nRmPX26gm79C_rM-desktop.webp", "realName": "facebook.png", "path": "uploads/images", "mimeType": "image/webp", - "link": "/api/fileStorage/findUnique/z8v9ZREwOJHKGIRYauROt-desktop.webp", + "link": "/api/fileStorage/findUnique/I6mlQ4nRmPX26gm79C_rM-desktop.webp", "category": "image" }, { - "id": "cmff3nv180003vn6h5jvedidq", - "name": "BLjMxTKoCNE31uOURR3IU-desktop.webp", - "realName": "telephone-call.png", - "path": "uploads/images", - "mimeType": "image/webp", - "link": "/api/fileStorage/findUnique/BLjMxTKoCNE31uOURR3IU-desktop.webp", - "category": "image" - }, - { - "id": "cmff3oouh0004vn6hd94brzv9", - "name": "hkJYAeTNWK_vYaYS20w3I-desktop.webp", + "id": "cmk2cpeba0009vn966jcrpf3u", + "name": "WArLC_yvU33MjoqEnQeQ1-desktop.webp", "realName": "instagram.png", "path": "uploads/images", "mimeType": "image/webp", - "link": "/api/fileStorage/findUnique/hkJYAeTNWK_vYaYS20w3I-desktop.webp", + "link": "/api/fileStorage/findUnique/WArLC_yvU33MjoqEnQeQ1-desktop.webp", "category": "image" }, { - "id": "cmff3q12g0005vn6h5ojov2qa", - "name": "6XEoZ9SFu59COpil03Gya-desktop.webp", + "id": "cmk2crcl1000cvn96j8pmgmo5", + "name": "D3RPbNiaNSCjacLjeR_qO-desktop.webp", "realName": "tiktok.png", "path": "uploads/images", "mimeType": "image/webp", - "link": "/api/fileStorage/findUnique/6XEoZ9SFu59COpil03Gya-desktop.webp", + "link": "/api/fileStorage/findUnique/D3RPbNiaNSCjacLjeR_qO-desktop.webp", "category": "image" } ] diff --git a/prisma/data/landing-page/profile/mediaSosial.json b/prisma/data/landing-page/profile/mediaSosial.json index 9af092a0..3f0ba929 100644 --- a/prisma/data/landing-page/profile/mediaSosial.json +++ b/prisma/data/landing-page/profile/mediaSosial.json @@ -3,24 +3,24 @@ "id": "cmds9023u0008vnbe3oxmhwyf", "name": "Desa Darmasaba", "iconUrl": "https://www.youtube.com/channel/UCtPw9WOQO7d2HIKzKgel4Xg", - "imageId": "cmff3joae0000vn6h8sgs0ilg" + "imageId": "cmk2cgqgm0003vn96jun52pik" }, { "id": "cmds90oul000bvnbe2bqkptoi", "name": "Pemerintah Desa Darmasaba", "iconUrl": "https://www.facebook.com/DarmasabaDesaku", - "imageId": "cmff3mtat0002vn6hs8vyyhdd" + "imageId": "cmk2cmr000006vn96qepq6gvl" }, { "id": "cmds91i4e000evnbe8gtf1gub", "name": "ddarmasaba", "iconUrl": "https://www.instagram.com/ddarmasaba/", - "imageId": "cmff3oouh0004vn6hd94brzv9" + "imageId": "cmk2cpeba0009vn966jcrpf3u" }, { "id": "cmds92de5000hvnbemlu6sq5x", "name": "desa.darmasaba", "iconUrl": "https://www.tiktok.com/@desa.darmasaba?is_from_webapp=1&sender_device=pc", - "imageId": "cmff3q12g0005vn6h5ojov2qa" + "imageId": "cmk2crcl1000cvn96j8pmgmo5" } ] diff --git a/prisma/data/landing-page/profile/profile.json b/prisma/data/landing-page/profile/profile.json index a83a8f62..1073354f 100644 --- a/prisma/data/landing-page/profile/profile.json +++ b/prisma/data/landing-page/profile/profile.json @@ -3,6 +3,6 @@ "id": "edit", "name": "I.B Surya Prabhawa Manuaba, S.H., M.H.", "position": "Perbekel Darmasaba periode 2021-2027", - "imageId": "cmff2w5ly000avn0telhct71k" + "imageId": "cmk2a2dl6001nvngck1n0k8qc" } ] diff --git a/prisma/data/landing-page/profile/programInovasi.json b/prisma/data/landing-page/profile/programInovasi.json index f6603f6d..5f0d894b 100644 --- a/prisma/data/landing-page/profile/programInovasi.json +++ b/prisma/data/landing-page/profile/programInovasi.json @@ -4,48 +4,55 @@ "name": "Dmangan", "description": "Darmasaba Aman Pangan", "link": "https://darmasaba.desa.id/berita/61452-kader-d-mangan-berhasil-meraih-prestasi-dalam-ajang-lomba-banjar-bali-quis-bbq-tahun-2024", - "imageId" : "cmff0z34f0005vn0tjtvq519p" + "imageId" : "cmk20nyen0002vnevd0hfr3u8" }, { "id": "cmdr76nqk0008vn5rdddvcxnr", "name": "Bicara Darmasaba", "description": "Bicara Darmasaba", "link": "https://darmasaba.desa.id/berita/42506-bicara-darmasaba", - "imageId" : "cmff0tnf00003vn0t3kgzi0u0" + "imageId" : "cmk20nqmu0001vnevfte29rk0" }, { "id": "cmdr77vbw000bvn5rvpmoq31s", "name": "Bares", "description": "Darmasaba Recycling Stock/Exchange", "link": "http://darmasaba.desa.id/berita/56722-bares", - "imageId" : "cmff0rr4z0002vn0twp333m2" + "imageId" : "cmk20mg320000vnevxy0k73fr" }, { "id": "cmdr7bxtp000evn5rmy85wihx", "name": "Sajjana Dharma Raksaka", "description": "Sajjana Dharma Raksaka", "link": "https://ppid.badungkab.go.id/storage/dokumen/5RS9dldGkrgzMQq6bKdZsqsVRHI8gffWv4PGfb3r.pdf", - "imageId" : "cmff10cwq0009vn0tse8dzu3j" + "imageId" : "cmk20pf3d0006vnev3mkoqpyy" }, { "id": "cmdr7dlnk000hvn5r9lur3z35", "name": "PDKT", "description": "Perangkat Desa Kuat Teknologi", "link": "https://darmasaba.desa.id/berita/53752-p-d-k-t", - "imageId" : "cmff1013m0008vn0th7t0d64d" + "imageId" : "cmk20omzq0005vnevgi6f4edu" }, { "id": "cmdr7ftob000mvn5rfhgdtg8v", "name": "GM", "description": "Galah Melah", "link": "https://darmasaba.desa.id/berita/52880-galah-melah", - "imageId" : "cmff38cyq000bvn0t9f01cz3f" + "imageId" : "cmk20o7mf0003vnevohrksm1d" }, { "id": "cmdr7glue000pvn5r6onzslju", "name": "Inovasi Desa Darmasaba", "description": "Inovasi Desa Darmasaba", "link": "https://darmasaba.desa.id/produk-lokal-desa", - "imageId" : "cmff0zqvd0007vn0tv6o5hjcq" + "imageId" : "cmk20of8m0004vnev9ujy5o0l" + }, + { + "id": "cmk228ust0009vnev5p8i377o", + "name": "Davest", + "description": "

DAVEST (Darmasaba Investment) merupakan program inovasi Desa Darmasaba yang bertujuan mempromosikan potensi investasi desa secara terintegrasi melalui media digital dan pendampingan langsung. Program ini menjadi sarana penghubung antara pemerintah desa, pelaku usaha, dan investor dalam rangka mendorong pertumbuhan ekonomi desa yang berkelanjutan.

DAVEST menyajikan informasi potensi unggulan desa seperti sektor UMKM, pariwisata, ekonomi kreatif, serta peluang investasi berbasis sumber daya lokal dengan prinsip transparansi dan kemudahan akses informasi.

Di tahun 2024 ini Davest (Darmasaba Village Festival) akan diadakan lagi, dengan berbagai kegiatan pemerdayaan, edukasi dan hiburan yang tentunya lebih waahhhh dari dua tahun lalu. Untuk memantapkan hal tersebut, Pemdes Darmasaba melakukan rapat koordinasi (rakor) Davest 2024 yang dipimpin langsung oleh Perbekel Darmasaba I. B. Surya Prabhawa Manuaba, S.H.,M.H. pada hari Senin (22/1/2024) bertempat di Ruang Shanti Gosana Kantor Perbekel Darmasaba.


Tujuan Program


Sasaran Program


Bentuk Inovasi


Ruang Lingkup Kegiatan

", + "link": "https://darmasaba.desa.id/berita/55862-rakor-davest-2024", + "imageId" : "cmk228urs0007vnevi5b66bqn" } ] diff --git a/prisma/data/resolveImageId.ts b/prisma/data/resolveImageId.ts new file mode 100644 index 00000000..58227739 --- /dev/null +++ b/prisma/data/resolveImageId.ts @@ -0,0 +1,11 @@ +import safeImageId from "./safeImageId"; + +export default async function resolveImageIdForSeed( + existingImageId: string | null | undefined, + seedImageId: string | null | undefined +) { + if (existingImageId) return existingImageId; + + // ✅ Skip validasi saat seed + return await safeImageId(seedImageId, true); +} \ No newline at end of file diff --git a/prisma/data/safeImageId.ts b/prisma/data/safeImageId.ts new file mode 100644 index 00000000..ab01a8e6 --- /dev/null +++ b/prisma/data/safeImageId.ts @@ -0,0 +1,24 @@ +import prisma from "@/lib/prisma"; + +export default async function safeImageId( + imageId?: string | null, + skipValidation = false // ✅ tambah param +) { + if (!imageId) return null; + + if (skipValidation) { + console.log(`⚠️ Skipping validation for ${imageId} (seed mode)`); + return imageId; // langsung return tanpa cek DB + } + + const exists = await prisma.fileStorage.findUnique({ + where: { id: imageId }, + }); + + if (!exists) { + console.warn(`⚠️ imageId ${imageId} not found in FileStorage`); + return null; + } + + return imageId; +} \ No newline at end of file diff --git a/prisma/migrations/20260106072549_nico_6_jan2025/migration.sql b/prisma/migrations/20260106072549_nico_6_jan2025/migration.sql new file mode 100644 index 00000000..6952c280 --- /dev/null +++ b/prisma/migrations/20260106072549_nico_6_jan2025/migration.sql @@ -0,0 +1,142 @@ +/* + Warnings: + + - You are about to drop the column `dokterdanTenagaMedisId` on the `FasilitasKesehatan` table. All the data in the column will be lost. + - You are about to drop the column `tarifDanLayananId` on the `FasilitasKesehatan` table. All the data in the column will be lost. + - You are about to drop the `User` table. If the table is not empty, all the data it contains will be lost. + - You are about to drop the `UserSession` table. If the table is not empty, all the data it contains will be lost. + - You are about to drop the `permissions` table. If the table is not empty, all the data it contains will be lost. + +*/ +-- DropForeignKey +ALTER TABLE "FasilitasKesehatan" DROP CONSTRAINT "FasilitasKesehatan_dokterdanTenagaMedisId_fkey"; + +-- DropForeignKey +ALTER TABLE "FasilitasKesehatan" DROP CONSTRAINT "FasilitasKesehatan_tarifDanLayananId_fkey"; + +-- DropForeignKey +ALTER TABLE "User" DROP CONSTRAINT "User_roleId_fkey"; + +-- DropForeignKey +ALTER TABLE "UserSession" DROP CONSTRAINT "UserSession_userId_fkey"; + +-- AlterTable +ALTER TABLE "DokterdanTenagaMedis" ADD COLUMN "jadwalLibur" TEXT, +ADD COLUMN "jamBukaLibur" TEXT, +ADD COLUMN "jamBukaOperasional" TEXT, +ADD COLUMN "jamTutupLibur" TEXT, +ADD COLUMN "jamTutupOperasional" TEXT; + +-- AlterTable +ALTER TABLE "FasilitasKesehatan" DROP COLUMN "dokterdanTenagaMedisId", +DROP COLUMN "tarifDanLayananId"; + +-- AlterTable +ALTER TABLE "MediaSosial" ADD COLUMN "icon" TEXT; + +-- AlterTable +ALTER TABLE "roles" ALTER COLUMN "permissions" DROP NOT NULL; + +-- DropTable +DROP TABLE "User"; + +-- DropTable +DROP TABLE "UserSession"; + +-- DropTable +DROP TABLE "permissions"; + +-- CreateTable +CREATE TABLE "users" ( + "id" TEXT NOT NULL, + "username" TEXT NOT NULL, + "nomor" TEXT NOT NULL, + "roleId" TEXT NOT NULL DEFAULT '2', + "isActive" BOOLEAN NOT NULL DEFAULT false, + "sessionInvalid" BOOLEAN NOT NULL DEFAULT false, + "lastLogin" TIMESTAMP(3), + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "permissions" JSONB, + + CONSTRAINT "users_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "user_sessions" ( + "id" TEXT NOT NULL, + "token" TEXT NOT NULL, + "expiresAt" TIMESTAMP(3) NOT NULL, + "active" BOOLEAN NOT NULL DEFAULT true, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "userId" TEXT NOT NULL, + + CONSTRAINT "user_sessions_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "UserMenuAccess" ( + "id" TEXT NOT NULL, + "userId" TEXT NOT NULL, + "menuId" TEXT NOT NULL, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "UserMenuAccess_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "_Tarif" ( + "A" TEXT NOT NULL, + "B" TEXT NOT NULL, + + CONSTRAINT "_Tarif_AB_pkey" PRIMARY KEY ("A","B") +); + +-- CreateTable +CREATE TABLE "_Dokter" ( + "A" TEXT NOT NULL, + "B" TEXT NOT NULL, + + CONSTRAINT "_Dokter_AB_pkey" PRIMARY KEY ("A","B") +); + +-- CreateIndex +CREATE UNIQUE INDEX "users_nomor_key" ON "users"("nomor"); + +-- CreateIndex +CREATE INDEX "user_sessions_userId_idx" ON "user_sessions"("userId"); + +-- CreateIndex +CREATE INDEX "user_sessions_token_idx" ON "user_sessions"("token"); + +-- CreateIndex +CREATE UNIQUE INDEX "UserMenuAccess_userId_menuId_key" ON "UserMenuAccess"("userId", "menuId"); + +-- CreateIndex +CREATE INDEX "_Tarif_B_index" ON "_Tarif"("B"); + +-- CreateIndex +CREATE INDEX "_Dokter_B_index" ON "_Dokter"("B"); + +-- AddForeignKey +ALTER TABLE "users" ADD CONSTRAINT "users_roleId_fkey" FOREIGN KEY ("roleId") REFERENCES "roles"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "user_sessions" ADD CONSTRAINT "user_sessions_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "UserMenuAccess" ADD CONSTRAINT "UserMenuAccess_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "_Tarif" ADD CONSTRAINT "_Tarif_A_fkey" FOREIGN KEY ("A") REFERENCES "FasilitasKesehatan"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "_Tarif" ADD CONSTRAINT "_Tarif_B_fkey" FOREIGN KEY ("B") REFERENCES "TarifDanLayanan"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "_Dokter" ADD CONSTRAINT "_Dokter_A_fkey" FOREIGN KEY ("A") REFERENCES "DokterdanTenagaMedis"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "_Dokter" ADD CONSTRAINT "_Dokter_B_fkey" FOREIGN KEY ("B") REFERENCES "FasilitasKesehatan"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/prisma/safeseedUnique.ts b/prisma/safeseedUnique.ts index 92d16071..75f942dd 100644 --- a/prisma/safeseedUnique.ts +++ b/prisma/safeseedUnique.ts @@ -1,30 +1,63 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ -// helpers/safeSeedUnique.ts +import prisma from "@/lib/prisma"; import { PrismaClient } from "@prisma/client"; -const prisma = new PrismaClient(); +type SafeSeedOptions = { + skipUpdate?: boolean; +}; -/** - * Helper generic buat seed dengan upsert aman - */ +// prisma/safeseedUnique.ts export async function safeSeedUnique( model: T, where: Record, - data: Record + data: Record, + options: SafeSeedOptions = {} ) { - const m = prisma[model]; - - if (!m) throw new Error(`Model ${String(model)} tidak ditemukan di PrismaClient`); + const m = prisma[model] as any; + if (!m) throw new Error(`Model ${String(model)} tidak ditemukan`); try { - // @ts-expect-error upsert dynamic - await m.upsert({ + // Pastikan `where` berisi field yang benar-benar unique (misal: `id`) + const result = await m.upsert({ where, - update: data, - create: { ...where, ...data }, + update: options.skipUpdate ? {} : data, + create: data, // ✅ Jangan duplikasi `where` ke `create` }); - console.log(`✅ Seeded ${String(model)} -> ${JSON.stringify(where)}`); + console.log(`✅ Seed ${String(model)}:`, where); + return result; } catch (err) { - console.error(`❌ Gagal seed ${String(model)} -> ${JSON.stringify(where)}`, err); + console.error(`❌ Gagal seed ${String(model)}:`, where, err); + throw err; // ✅ Rethrow agar seeding berhenti jika kritis } } + +// /* eslint-disable @typescript-eslint/no-explicit-any */ +// import { PrismaClient } from "@prisma/client"; + +// const prisma = new PrismaClient(); + +// type SafeSeedOptions = { +// skipUpdate?: boolean; +// }; + +// export async function safeSeedUnique( +// model: T, +// where: Record, +// data: Record, +// options: SafeSeedOptions = {} +// ) { +// const m = prisma[model] as any; +// if (!m) throw new Error(`Model ${String(model)} tidak ditemukan`); + +// try { +// await m.upsert({ +// where, +// update: options.skipUpdate ? {} : data, +// create: { ...where, ...data }, +// }); + +// console.log(`✅ Seed ${String(model)}:`, where); +// } catch (err) { +// console.error(`❌ Gagal seed ${String(model)}:`, where, err); +// } +// } diff --git a/prisma/seed.ts b/prisma/seed.ts index bb52224d..e5a2f912 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -60,8 +60,37 @@ import jenjangPendidikan from "./data/pendidikan/info-sekolah/jenjang-pendidikan import seedAssets from "./seed_assets"; import users from "./data/user/users.json"; import { safeSeedUnique } from "./safeseedUnique"; +import safeImageId from "./data/safeImageId"; +import resolveImageIdForSeed from "./data/resolveImageId"; (async () => { + // seed assets + await prisma.fileStorage.deleteMany(); + console.log("🗑️ Cleared existing fileStorage records"); + await seedAssets(); + + // // =========== FILE STORAGE =========== + console.log("🔄 Seeding file storage..."); + for (const f of fileStorage) { + await safeSeedUnique( + "fileStorage", + { name: f.name }, + { + id: f.id, + name: f.name, + realName: f.realName, + path: f.path, + mimeType: f.mimeType, + link: f.link, + category: f.category, + deletedAt: null, + isActive: true, + } + ); + } + + console.log("✅ File storage seeded"); + console.log("🔄 Seeding roles..."); for (const r of roles) { @@ -131,112 +160,119 @@ import { safeSeedUnique } from "./safeseedUnique"; } } console.log("✅ Users seeding completed"); - - // =========== FILE STORAGE =========== - console.log("🔄 Seeding file storage..."); - for (const f of fileStorage) { - try { - await prisma.fileStorage.upsert({ - where: { id: f.id }, - update: { - name: f.name, - realName: f.realName, - path: f.path, - mimeType: f.mimeType, - link: f.link, - category: f.category, - }, - create: { - id: f.id, - name: f.name, - realName: f.realName, - path: f.path, - mimeType: f.mimeType, - link: f.link, - category: f.category, - }, - }); - } catch (error: any) { - console.error(`❌ Failed to seed file storage ${f.name}:`, error.message); - } - } - console.log("✅ File storage seeded"); // =========== LANDING PAGE =========== // =========== SUBMENU PROFILE =========== // =========== PROFILE PEJABAT DESA =========== + // In your seed.ts file, update the PejabatDesa seeding section to: + console.log("🔄 Seeding Pejabat Desa..."); for (const p of profilePejabatDesa) { - await prisma.pejabatDesa.upsert({ - where: { id: p.id }, - update: { - name: p.name, - position: p.position, - imageId: p.imageId, - }, - create: { - id: p.id, - name: p.name, - position: p.position, - imageId: p.imageId, - }, - }); + try { + // First, verify the image exists + if (p.imageId) { + const imageExists = await prisma.fileStorage.findUnique({ + where: { id: p.imageId }, + }); + + if (!imageExists) { + console.warn( + `⚠️ Image not found for PejabatDesa ${p.name}, skipping...` + ); + continue; + } + } + + await safeSeedUnique( + "pejabatDesa", + { id: p.id }, + { + id: p.id, + name: p.name, + position: p.position, + imageId: p.imageId, + } + ); + console.log(`✅ Seeded Pejabat Desa -> ${p.name}`); + } catch (error: any) { + console.error(`❌ Failed to seed Pejabat Desa ${p.name}:`, error.message); + } } - console.log( - "✅ profilePejabatDesa seeded without imageId (editable later via UI)" - ); + console.log("✅ Pejabat Desa seeding completed"); // =========== PROGRAM INOVASI =========== - for (const p of programInovasi) { - let imageId: string | null = null; + // Add this section after the other seed operations in seed.ts + console.log("🔄 Seeding Program Inovasi..."); - if (p.imageId) { - const imageExists = await prisma.fileStorage.findUnique({ + for (const p of programInovasi) { + const existing = await prisma.programInovasi.findUnique({ + where: { id: p.id }, + select: { imageId: true }, + }); + + let imageId = existing?.imageId; // Pertahankan existing + + // Kalau belum ada imageId, cari berdasarkan name/realName + if (!imageId && p.imageId) { + // ✅ Cari langsung berdasarkan ID yang ada di p.imageId + const fileRecord = await prisma.fileStorage.findUnique({ where: { id: p.imageId }, + select: { id: true, name: true }, }); - if (imageExists) { - imageId = p.imageId; - } else { - console.warn( - `⚠️ imageId ${p.imageId} tidak ditemukan untuk ProgramInovasi ${p.name}` + if (fileRecord) { + imageId = fileRecord.id; + console.log( + `✅ Found file by ID: ${fileRecord.name} (${fileRecord.id})` ); + } else { + console.warn(`⚠️ File with ID ${p.imageId} not found for ${p.name}`); + imageId = null; } } + await prisma.programInovasi.upsert({ where: { id: p.id }, update: { name: p.name, description: p.description, link: p.link, - imageId: p.imageId, + imageId, }, create: { id: p.id, name: p.name, description: p.description, link: p.link, - imageId: p.imageId, + imageId, }, }); } - console.log("program inovasi success ..."); // =========== MEDIA SOSIAL =========== - for (const p of mediaSosial) { + for (const m of mediaSosial) { + const existing = await prisma.mediaSosial.findUnique({ + where: { id: m.id }, + select: { imageId: true }, + }); + + const imageId = await resolveImageIdForSeed(existing?.imageId, m.imageId); + await prisma.mediaSosial.upsert({ - where: { id: p.id }, + where: { id: m.id }, update: { - name: p.name, - iconUrl: p.iconUrl, - imageId: p.imageId, + name: m.name, + iconUrl: m.iconUrl, + // ⛔ JANGAN overwrite imageId sembarangan + imageId, }, create: { - id: p.id, - name: p.name, - iconUrl: p.iconUrl, - imageId: p.imageId, + id: m.id, + name: m.name, + iconUrl: m.iconUrl, + imageId, }, }); } + console.log("media sosial success ..."); // =========== SUBMENU DESA ANTI KORUPSI =========== @@ -1245,9 +1281,6 @@ import { safeSeedUnique } from "./safeseedUnique"; } console.log("✅ Jenjang Pendidikan seeded successfully"); - - // seed assets - await seedAssets(); })() .then(() => prisma.$disconnect()) .catch((e) => { diff --git a/prisma/seed_assets.ts b/prisma/seed_assets.ts index f92c0d36..64317887 100644 --- a/prisma/seed_assets.ts +++ b/prisma/seed_assets.ts @@ -1,10 +1,11 @@ +/* eslint-disable @typescript-eslint/no-unused-vars */ // prisma/seedAssets.ts +import prisma from "@/lib/prisma"; +import AdmZip from "adm-zip"; import fs from "fs/promises"; import path from "path"; import sharp from "sharp"; -import fetch from "node-fetch"; -import AdmZip from "adm-zip"; -import prisma from "@/lib/prisma"; +import fetchWithRetry from "./data/fetchWithRetry"; const UPLOADS_DIR = process.env.WIBU_UPLOAD_DIR || path.join(process.cwd(), "uploads"); @@ -18,7 +19,10 @@ function detectCategory(filename: string): "image" | "document" | "other" { } // --- Helper: recursive walk dir --- -async function walkDir(dir: string, fileList: string[] = []): Promise { +async function walkDir( + dir: string, + fileList: string[] = [] +): Promise { const entries = await fs.readdir(dir, { withFileTypes: true }); for (const entry of entries) { @@ -41,18 +45,45 @@ export default async function seedAssets() { // 1. Download zip const url = - "https://cld-dkr-makuro-seafile.wibudev.com/f/ffd5a548a04f47939474/?dl=1"; - const res = await fetch(url); - if (!res.ok) throw new Error(`Gagal download assets: ${res.statusText}`); + "https://cld-dkr-makuro-seafile.wibudev.com/f/90dd12c9713e42379fcd/?dl=1"; + const res = await fetchWithRetry(url, 3, 20000); + + // Validasi content-type + const contentType = res.headers.get("content-type"); + if (!contentType?.includes("zip")) { + throw new Error(`Invalid content-type (${contentType}). Expected ZIP file`); + } + const buffer = Buffer.from(await res.arrayBuffer()); + // Validasi ukuran file + if (buffer.length < 100) { + throw new Error("Downloaded ZIP is empty or corrupted"); + } + + // Validasi signature ZIP ("PK") + if (buffer.toString("utf8", 0, 2) !== "PK") { + throw new Error("Invalid ZIP signature (PK not found)"); + } + // 2. Extract zip ke folder tmp const extractDir = path.join(process.cwd(), "tmp_assets"); await fs.rm(extractDir, { recursive: true, force: true }); await fs.mkdir(extractDir, { recursive: true }); - const zip = new AdmZip(buffer); - zip.extractAllTo(extractDir, true); + let zip: AdmZip; + + try { + zip = new AdmZip(buffer); + } catch (err) { + throw new Error("Failed to parse ZIP file (corrupted or invalid)"); + } + + try { + zip.extractAllTo(extractDir, true); + } catch (err) { + throw new Error("Failed to extract ZIP contents"); + } // 3. Cari semua file valid (recursive) const files = await walkDir(extractDir); @@ -84,18 +115,41 @@ export default async function seedAssets() { await fs.copyFile(filePath, targetPath); } - // 5. Simpan ke DB - await prisma.fileStorage.create({ - data: { - name: finalName, - realName: entryName, - path: targetPath, - mimeType, - link: `/uploads/${category}/${finalName}`, - category, - }, + const existing = await prisma.fileStorage.findUnique({ + where: { name: finalName }, }); + if (existing) { + // Restore kalau soft deleted + await prisma.fileStorage.update({ + where: { name: finalName }, + data: { + path: targetPath, + realName: entryName, + mimeType, + link: `/uploads/${category}/${finalName}`, + category, + deletedAt: null, + isActive: true, + }, + }); + + console.log(`♻️ restored: ${category}/${finalName}`); + } else { + await prisma.fileStorage.create({ + data: { + name: finalName, + realName: entryName, + path: targetPath, + mimeType, + link: `/uploads/${category}/${finalName}`, + category, + }, + }); + + console.log(`📂 created: ${category}/${finalName}`); + } + console.log(`📂 saved: ${category}/${finalName}`); } @@ -103,6 +157,8 @@ export default async function seedAssets() { await fs.rm(extractDir, { recursive: true, force: true }); console.log("✅ Selesai seed assets!"); + console.log("DB URL (asset):", process.env.DATABASE_URL); + } // --- Auto run kalau dipanggil langsung ---