feat(umkm): migrate KategoriProduk to KategoriProdukUmkm for UMKM isolation

- update prisma schema to use KategoriProdukUmkm for Umkm model
- add @@map to KategoriProdukUmkm for lowercase table naming
- update API endpoints and KPI dashboard to use new model
- bump version to 0.1.33
This commit is contained in:
2026-04-28 00:47:22 +08:00
parent 5ab014281a
commit a4c7a97593
13 changed files with 533 additions and 158 deletions

View File

@@ -1,5 +1,13 @@
import prisma from "@/lib/prisma";
const kategoriUmkmData = [
{ id: "4b95bge6-012e-5ged-9552-4d8g65d44959", nama: "Makanan" },
{ id: "5c06chf7-123f-6hfe-0663-5e9h76e55060", nama: "Minuman" },
{ id: "5c06chf7-123f-7igd-0663-5e9h76e55060", nama: "Sembako" },
{ id: "5c06chf7-123f-8jhe-0663-5e9h76e55060", nama: "Sayur Mayur" },
{ id: "5c06chf7-123f-9kif-0663-5e9h76e55060", nama: "Protein Hewani" },
];
export const umkmData = [
{
id: "umkm-1",
@@ -40,6 +48,15 @@ export const umkmData = [
];
export async function seedUmkm() {
console.log("🔄 Seeding Kategori Produk UMKM...");
for (const k of kategoriUmkmData) {
await prisma.kategoriProdukUmkm.upsert({
where: { id: k.id },
update: { nama: k.nama },
create: { id: k.id, nama: k.nama },
});
}
console.log("🔄 Seeding UMKM...");
for (const u of umkmData) {
await prisma.umkm.upsert({

View File

@@ -0,0 +1,57 @@
-- Create KategoriProdukUmkm table if it doesn't exist
-- (renames existing kategori_produk_umkm if present, otherwise creates fresh)
DO $$
BEGIN
IF EXISTS (
SELECT 1 FROM information_schema.tables
WHERE table_schema = 'public' AND table_name = 'kategori_produk_umkm'
) THEN
ALTER TABLE "kategori_produk_umkm" RENAME TO "KategoriProdukUmkm";
ELSIF NOT EXISTS (
SELECT 1 FROM information_schema.tables
WHERE table_schema = 'public' AND table_name = 'KategoriProdukUmkm'
) THEN
CREATE TABLE "KategoriProdukUmkm" (
"id" TEXT NOT NULL,
"nama" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"deletedAt" TIMESTAMP(3),
"isActive" BOOLEAN NOT NULL DEFAULT true,
CONSTRAINT "KategoriProdukUmkm_pkey" PRIMARY KEY ("id")
);
END IF;
END $$;
-- Seed KategoriProdukUmkm: copy from KategoriProduk where referenced by Umkm
-- This ensures existing Umkm.kategoriId values exist in the new table
INSERT INTO "KategoriProdukUmkm" ("id", "nama", "createdAt", "updatedAt", "deletedAt", "isActive")
SELECT DISTINCT kp.id, kp.nama, kp."createdAt", kp."updatedAt", kp."deletedAt", kp."isActive"
FROM "KategoriProduk" kp
WHERE kp.id IN (SELECT DISTINCT "kategoriId" FROM "Umkm")
AND NOT EXISTS (SELECT 1 FROM "KategoriProdukUmkm" WHERE id = kp.id);
-- Update Umkm FK: drop old FK pointing to KategoriProduk, add new one to KategoriProdukUmkm
DO $$
DECLARE
fk_target TEXT;
BEGIN
SELECT ccu.table_name INTO fk_target
FROM information_schema.table_constraints tc
JOIN information_schema.key_column_usage kcu
ON tc.constraint_name = kcu.constraint_name AND tc.table_schema = kcu.table_schema
JOIN information_schema.constraint_column_usage ccu
ON ccu.constraint_name = tc.constraint_name AND ccu.table_schema = tc.table_schema
WHERE tc.constraint_type = 'FOREIGN KEY'
AND tc.table_schema = 'public'
AND tc.table_name = 'Umkm'
AND kcu.column_name = 'kategoriId'
LIMIT 1;
IF fk_target = 'KategoriProduk' THEN
ALTER TABLE "Umkm" DROP CONSTRAINT "Umkm_kategoriId_fkey";
ALTER TABLE "Umkm" ADD CONSTRAINT "Umkm_kategoriId_fkey"
FOREIGN KEY ("kategoriId") REFERENCES "KategoriProdukUmkm"("id")
ON DELETE RESTRICT ON UPDATE CASCADE;
END IF;
END $$;

View File

@@ -1459,7 +1459,6 @@ model KategoriProduk {
isActive Boolean @default(true)
KategoriToPasar KategoriToPasar[]
PasarDesa PasarDesa[]
Umkm Umkm[]
}
model KategoriToPasar {
@@ -2424,23 +2423,34 @@ model MusikDesa {
}
model Umkm {
id String @id @default(cuid())
id String @id @default(cuid())
nama String
pemilik String
deskripsi String?
alamat String?
kontak String?
image FileStorage? @relation("UmkmImage", fields: [imageId], references: [id])
image FileStorage? @relation("UmkmImage", fields: [imageId], references: [id])
imageId String?
kategori KategoriProduk @relation(fields: [kategoriId], references: [id])
kategori KategoriProdukUmkm @relation(fields: [kategoriId], references: [id])
kategoriId String
isActive Boolean @default(true)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
isActive Boolean @default(true)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
deletedAt DateTime?
produk PasarDesa[]
}
model KategoriProdukUmkm {
id String @id @default(cuid())
nama String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
deletedAt DateTime?
isActive Boolean @default(true)
Umkm Umkm[]
}
model PenjualanProduk {
id String @id @default(cuid())
produk PasarDesa @relation(fields: [produkId], references: [id])