From 5bfb376e88eac6bee85924e52dcf8d6d81475bd5 Mon Sep 17 00:00:00 2001 From: Rafi Arrafif Date: Thu, 29 Jan 2026 02:40:25 +0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=91=20hotfix:=20seeding=20system?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- prisma/seed/index.ts | 4 +-- prisma/seed/userRole.seed.ts | 46 +++++++++++++++++------- prisma/seed/userSystem.seed.ts | 13 ++++++- src/helpers/files/createFile/index.ts | 28 +++++++++++++++ src/utils/databases/prisma/connection.ts | 25 +------------ 5 files changed, 77 insertions(+), 39 deletions(-) create mode 100644 src/helpers/files/createFile/index.ts diff --git a/prisma/seed/index.ts b/prisma/seed/index.ts index 648b6c3..c526df4 100644 --- a/prisma/seed/index.ts +++ b/prisma/seed/index.ts @@ -6,8 +6,8 @@ async function main() { console.log("🌱 Running all seeds..."); console.log("🔌 Connecting to database..."); - const systemUserId = await userSystemSeed(); - await userRoleSeed(systemUserId.id); + await userSystemSeed(); + await userRoleSeed(); console.log("🌳 All seeds completed"); } diff --git a/prisma/seed/userRole.seed.ts b/prisma/seed/userRole.seed.ts index 44eb100..fbc20b6 100644 --- a/prisma/seed/userRole.seed.ts +++ b/prisma/seed/userRole.seed.ts @@ -1,7 +1,9 @@ +import { SystemAccountId } from "../../src/config/account/system"; import { generateUUIDv7 } from "../../src/helpers/databases/uuidv7"; import { prisma } from "../../src/utils/databases/prisma/connection"; -export const userRoleSeed = async (systemId: string) => { +export const userRoleSeed = async () => { + console.log("🔃 Seeding user roles...", SystemAccountId); const roles = [ { id: generateUUIDv7(), @@ -18,7 +20,7 @@ export const userRoleSeed = async (systemId: string) => { canManageUser: true, canEditSystem: true, canManageSystem: true, - createdBy: systemId, + createdBy: SystemAccountId, }, { id: generateUUIDv7(), @@ -35,17 +37,37 @@ export const userRoleSeed = async (systemId: string) => { canManageUser: false, canEditSystem: false, canManageSystem: false, - createdBy: systemId, + createdBy: SystemAccountId, }, ]; - await prisma.$transaction( - roles.map((role) => - prisma.userRole.upsert({ - where: { name: role.name }, - update: role, - create: role, - }), - ), - ); + await prisma.$transaction(async (tx) => { + const createdRoles = await Promise.all( + roles.map( + async (role) => + await tx.userRole.upsert({ + where: { name: role.name }, + update: role, + create: role, + }), + ), + ); + + await tx.userRoleAssignment.upsert({ + where: { + userId_roleId: { + userId: SystemAccountId, + roleId: createdRoles.find((r) => r.name === "ADMIN")!.id, + }, + }, + create: { + userId: SystemAccountId, + roleId: createdRoles.find((r) => r.name === "ADMIN")!.id, + }, + update: { + userId: SystemAccountId, + roleId: createdRoles.find((r) => r.name === "ADMIN")!.id, + }, + }); + }); }; diff --git a/prisma/seed/userSystem.seed.ts b/prisma/seed/userSystem.seed.ts index 8bcc43f..bcab28e 100644 --- a/prisma/seed/userSystem.seed.ts +++ b/prisma/seed/userSystem.seed.ts @@ -1,4 +1,5 @@ import { generateUUIDv7 } from "../../src/helpers/databases/uuidv7"; +import { createFile } from "../../src/helpers/files/createFile"; import { prisma } from "../../src/utils/databases/prisma/connection"; export const userSystemSeed = async () => { @@ -12,10 +13,20 @@ export const userSystemSeed = async () => { "$2a$12$ynOrzVCvRdejGp/7KJW4lOAwRzFYhSHDE.Dp3Fqh3sXAq1BIwfwc6", }; - return await prisma.user.upsert({ + const insertedUserSystem = await prisma.user.upsert({ where: { username: payload.username }, update: {}, create: payload, select: { id: true }, }); + await createFile( + `export const SystemAccountId = "${insertedUserSystem.id}";`, + { + fileName: "system.ts", + targetDir: "src/config/account", + overwriteIfExists: true, + }, + ); + + return insertedUserSystem; }; diff --git a/src/helpers/files/createFile/index.ts b/src/helpers/files/createFile/index.ts new file mode 100644 index 0000000..11f0416 --- /dev/null +++ b/src/helpers/files/createFile/index.ts @@ -0,0 +1,28 @@ +import fs from "fs"; +import path from "path"; + +interface CreateFileConfig { + targetDir: string; + fileName: string; + overwriteIfExists?: boolean; +} + +export const createFile = async (content: string, config: CreateFileConfig) => { + const targetDir = path.join(process.cwd(), config.targetDir); + const targetFile = path.join(targetDir, config.fileName); + + // If file exists and overwrite is not allowed, throw an error + if (fs.existsSync(targetFile) && !config.overwriteIfExists) { + throw new Error( + `File ${config.fileName} already exists in ${config.targetDir}`, + ); + } + + // Ensure target directory exists + if (!fs.existsSync(targetDir)) { + await fs.promises.mkdir(targetDir, { recursive: true }); + } + + // Write content to the file + await fs.promises.writeFile(targetFile, content, "utf8"); +}; diff --git a/src/utils/databases/prisma/connection.ts b/src/utils/databases/prisma/connection.ts index 3a5e1b0..7e2704a 100644 --- a/src/utils/databases/prisma/connection.ts +++ b/src/utils/databases/prisma/connection.ts @@ -2,7 +2,6 @@ import { PrismaClient } from "@prisma/client"; import { PrismaPg } from "@prisma/adapter-pg"; import pg from "pg"; import dotenv from "dotenv"; -import { generateUUIDv7 } from "../../../helpers/databases/uuidv7"; dotenv.config(); const pool = new pg.Pool({ @@ -10,26 +9,4 @@ const pool = new pg.Pool({ }); const adapter = new PrismaPg(pool); -export const prisma = new PrismaClient({ adapter }).$extends({ - query: { - $allModels: { - async create({ args, query }) { - if ( - args.data && - typeof args.data === "object" && - !("id" in args.data) - ) { - // do nothing - } else if (args.data && !args.data.id) { - (args.data as { id?: string }).id = generateUUIDv7(); - } - return query(args); - }, - async upsert({ args, query }) { - const create = args.create as any; - if (create && !create.id) create.id = generateUUIDv7(); - return query(args); - }, - }, - }, -}); +export const prisma = new PrismaClient({ adapter });