This commit is contained in:
phaichayon
2026-07-01 00:09:05 +07:00
parent 8feaee61cb
commit 53dfd20913
44 changed files with 2829 additions and 25931 deletions

View File

@@ -1576,7 +1576,7 @@ async function upsertDocumentSequencesForOrganizationV2(
async function upsertApprovalWorkflowsForOrganization(
sql: SqlClient,
organizationId: string,
organization: { id: string; createdBy: string },
) {
for (const workflow of APPROVAL_WORKFLOWS) {
const [workflowRow] = await sql`
@@ -1587,14 +1587,18 @@ async function upsertApprovalWorkflowsForOrganization(
name,
entity_type,
is_active,
created_by,
updated_by,
deleted_at
) values (
${crypto.randomUUID()},
${organizationId},
${organization.id},
${workflow.code},
${workflow.name},
${workflow.entityType},
${true},
${organization.createdBy},
${organization.createdBy},
${null}
)
on conflict (organization_id, code) do update
@@ -1602,6 +1606,7 @@ async function upsertApprovalWorkflowsForOrganization(
name = excluded.name,
entity_type = excluded.entity_type,
is_active = excluded.is_active,
updated_by = excluded.updated_by,
deleted_at = excluded.deleted_at,
updated_at = now()
returning id
@@ -1620,7 +1625,7 @@ async function upsertApprovalWorkflowsForOrganization(
deleted_at
) values (
${crypto.randomUUID()},
${organizationId},
${organization.id},
${workflowRow.id},
${step.stepNumber},
${step.roleCode},
@@ -2338,7 +2343,7 @@ async function main() {
organization,
branchRows,
);
await upsertApprovalWorkflowsForOrganization(sql, organization.id);
await upsertApprovalWorkflowsForOrganization(sql, organization);
await upsertApprovalMatricesForOrganization(sql, organization);
await upsertDocumentTemplatesForOrganization(sql, organization);
await upsertDocumentLibraryForOrganization(sql, organization);

View File

@@ -1,5 +1,9 @@
import { getDefaultPermissions } from '../../lib/auth/rbac.ts';
import { createSqlClient, deterministicId, type SqlClient } from '../../../scripts/lib/seed-utils.ts';
import { getDefaultPermissions } from "../../lib/auth/rbac.ts";
import {
createSqlClient,
deterministicId,
type SqlClient,
} from "../../../scripts/lib/seed-utils.ts";
type UserRow = {
id: string;
@@ -7,17 +11,19 @@ type UserRow = {
};
const DEFAULT_ORGANIZATION = {
id: deterministicId('organization:alla-demo'),
name: 'ALLA Demo Organization',
slug: 'alla-demo',
membershipId: deterministicId('membership:alla-demo:super-admin')
id: deterministicId("organization:alla-demo"),
name: "ALLA Demo",
slug: "alla-demo",
membershipId: deterministicId("membership:alla-demo:super-admin"),
};
async function resolveSuperAdmin(sql: SqlClient): Promise<UserRow> {
const email = process.env.SUPER_ADMIN_EMAIL?.trim().toLowerCase();
if (!email) {
throw new Error('SUPER_ADMIN_EMAIL is required before running system organization seed.');
throw new Error(
"SUPER_ADMIN_EMAIL is required before running system organization seed.",
);
}
const [user] = await sql<UserRow[]>`
@@ -28,7 +34,7 @@ async function resolveSuperAdmin(sql: SqlClient): Promise<UserRow> {
`;
if (!user) {
throw new Error('Super admin user not found. Run seed-super-admin first.');
throw new Error("Super admin user not found. Run seed-super-admin first.");
}
return user;
@@ -46,7 +52,7 @@ async function ensureOrganization(sql: SqlClient, superAdmin: UserRow) {
${DEFAULT_ORGANIZATION.id},
${DEFAULT_ORGANIZATION.name},
${DEFAULT_ORGANIZATION.slug},
${'enterprise'},
${"enterprise"},
${superAdmin.id}
)
on conflict (slug) do update
@@ -56,7 +62,7 @@ async function ensureOrganization(sql: SqlClient, superAdmin: UserRow) {
updated_at = now()
`;
const membershipPermissions = getDefaultPermissions('admin', 'crm_admin');
const membershipPermissions = getDefaultPermissions("admin", "crm_admin");
const [existingMembership] = await sql<{ id: string }[]>`
select id
from memberships
@@ -69,8 +75,8 @@ async function ensureOrganization(sql: SqlClient, superAdmin: UserRow) {
await sql`
update memberships
set
role = ${'admin'},
business_role = ${'crm_admin'},
role = ${"admin"},
business_role = ${"crm_admin"},
permissions = ${sql.array(membershipPermissions)},
updated_at = now()
where id = ${existingMembership.id}
@@ -90,8 +96,8 @@ async function ensureOrganization(sql: SqlClient, superAdmin: UserRow) {
${DEFAULT_ORGANIZATION.membershipId},
${superAdmin.id},
${DEFAULT_ORGANIZATION.id},
${'admin'},
${'crm_admin'},
${"admin"},
${"crm_admin"},
${sql.array(membershipPermissions)},
${sql.array([])},
${sql.array([])}
@@ -122,8 +128,8 @@ async function main() {
main().catch((error) => {
console.error(
'[seed:system-org] Failed:',
error instanceof Error ? error.message : String(error)
"[seed:system-org] Failed:",
error instanceof Error ? error.message : String(error),
);
process.exitCode = 1;
});