🚑 hotfix: seeding system

This commit is contained in:
Rafi Arrafif
2026-01-29 02:40:25 +07:00
parent e305d955f1
commit 5bfb376e88
5 changed files with 77 additions and 39 deletions

View File

@ -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");
}

View File

@ -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,
},
});
});
};

View File

@ -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;
};