This commit is contained in:
2026-04-14 15:14:14 +07:00
parent 16fb38b18d
commit 6a0fa980bd
8 changed files with 1785 additions and 40 deletions

View File

@ -0,0 +1,716 @@
/**
* Usage:
* ts-node src/scripts/seed-auth-data.ts
* npm run seeder
*/
import { ILogObj, Logger } from "tslog";
import { Application, Menu, Status, User, UserRole } from "entity";
import { OrmHelper } from "../helpers/orm";
import fs from "fs";
import path from "path";
import * as fastcsv from "fast-csv";
const log: Logger<ILogObj> = new Logger({
name: "[seed-auth-data]",
type: "pretty",
});
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function waitForDbInitialized(timeoutMs: number = 15000) {
const start = Date.now();
while (!OrmHelper.DB || !OrmHelper.DB.isInitialized) {
if (Date.now() - start > timeoutMs) {
throw new Error("Database initialization timeout");
}
await sleep(100);
}
}
async function ensureAuthSchemaReady() {
// OrmHelper uses `synchronize: true`, but in some environments the connection can become
// initialized before all tables are actually present/visible. Running synchronize again is safe
// and ensures required auth tables exist before seeding.
await OrmHelper.DB.synchronize();
const qr = OrmHelper.DB.createQueryRunner();
try {
const hasApplication = await qr.hasTable("application");
if (!hasApplication) {
throw new Error(
'Table "application" is not available after synchronize()',
);
}
} finally {
await qr.release();
}
}
async function upsertApplication(data: {
id: string;
name: string;
reset_password_url: string;
status?: Status;
deleted_at?: Date | null;
}) {
const repo = OrmHelper.DB.getRepository(Application);
let app = await repo.findOne({
where: { id: data.id },
});
if (!app) {
app = new Application();
app.id = data.id;
}
app.name = data.name;
app.reset_password_url = data.reset_password_url;
app.status = data.status ?? Status.Active;
if (data.deleted_at !== undefined) {
app.deleted_at = data.deleted_at ?? null;
}
return await repo.save(app);
}
async function upsertMenu(data: {
id: string;
application: Application;
module: string;
name: string;
link: string;
order_number: number;
icon?: string;
id_parent?: string;
status?: Status;
deleted_at?: Date | null;
}) {
const repo = OrmHelper.DB.getRepository(Menu);
let menu = await repo.findOne({ where: { id: data.id } as any });
if (!menu) {
menu = new Menu();
menu.id = data.id;
}
menu.application = data.application;
menu.module = data.module;
menu.name = data.name;
menu.link = data.link;
menu.id_parent = data.id_parent ?? null;
menu.order_number = data.order_number;
menu.icon = data.icon ?? null;
menu.status = data.status ?? Status.Active;
if (data.deleted_at !== undefined) {
menu.deleted_at = data.deleted_at ?? null;
}
return await repo.save(menu);
}
async function upsertUserRole(data: {
id: string;
application: Application;
name: string;
roles: string[];
status?: Status;
created_by?: string;
updated_by?: string;
deleted_at?: Date | null;
deleted_by?: string | null;
}) {
const repo = OrmHelper.DB.getRepository(UserRole);
let role = await repo.findOne({
relations: { application: true } as any,
where: { id: data.id } as any,
});
if (!role) {
role = new UserRole();
role.id = data.id;
role.application = data.application;
role.name = data.name;
role.created_by = data.created_by ?? "system-seed";
role.created_at = new Date();
}
role.application = data.application;
role.name = data.name;
role.roles = data.roles;
role.status = data.status ?? Status.Active;
role.updated_by = data.updated_by ?? "system-seed";
role.updated_at = new Date();
if (data.deleted_at !== undefined) {
role.deleted_at = data.deleted_at ?? null;
}
if (data.deleted_by !== undefined) {
role.deleted_by = data.deleted_by ?? null;
}
return await repo.save(role);
}
async function upsertUser(data: {
id?: string;
email: string;
username: string;
name: string;
password: string;
status?: Status;
created_by?: string;
updated_by?: string;
reset_token?: string | null;
deleted_at?: Date | null;
deleted_by?: string | null;
}) {
const repo = OrmHelper.DB.getRepository(User);
const id = data.id && isUuid(data.id) ? data.id : null;
let user = id
? await repo.findOne({
relations: { roles: true } as any,
where: { id } as any,
})
: await repo.findOne({
relations: { roles: true } as any,
where: { username: data.username } as any,
});
if (!user) {
user = new User();
user.email = data.email;
user.username = data.username;
user.name = data.name;
user.status = data.status ?? Status.Active;
user.employee = null;
user.created_by = data.created_by ?? "system-seed";
user.created_at = new Date();
if (id) {
user.id = id;
}
}
user.email = data.email;
user.username = data.username;
user.name = data.name;
user.status = data.status ?? Status.Active;
user.updated_by = data.updated_by ?? "system-seed";
user.updated_at = new Date();
if (data.reset_token !== undefined) {
user.reset_token = data.reset_token ?? null;
}
if (data.deleted_at !== undefined) {
user.deleted_at = data.deleted_at ?? null;
}
if (data.deleted_by !== undefined) {
user.deleted_by = data.deleted_by ?? null;
}
user.password = data.password;
// Preserve bcrypt hashes from seed; only hash plain passwords.
if (!isBcryptHash(user.password)) {
user.hashPassword();
}
return await repo.save(user);
}
function isBcryptHash(value: string) {
return typeof value === "string" && /^\$2[aby]\$\d{2}\$/.test(value);
}
function isUuid(value: any) {
return (
typeof value === "string" &&
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(
value,
)
);
}
function parseStatus(value: any): Status {
if (value === Status.Active || value === Status["Not Active"]) {
return value as Status;
}
if (value === "Y") return Status.Active;
if (value === "N") return Status["Not Active"];
return Status.Active;
}
function parseNullableString(value: any) {
if (value === undefined || value === null) return null;
const s = String(value).trim();
return s === "" ? null : s;
}
function parseNullableDate(value: any): Date | null {
if (value === undefined || value === null) return null;
const s = String(value).trim();
if (s === "" || s.toLowerCase() === "null") return null;
const d = new Date(s);
return Number.isNaN(d.getTime()) ? null : d;
}
function parseIntSafe(value: any, fallback: number = 0) {
const n = Number.parseInt(String(value), 10);
return Number.isFinite(n) ? n : fallback;
}
async function readCsv<T = any>(filePath: string): Promise<T[]> {
return await new Promise((resolve, reject) => {
const rows: T[] = [];
fs.createReadStream(filePath)
.pipe(
fastcsv.parse({
headers: true,
ignoreEmpty: true,
trim: true,
}),
)
.on("error", reject)
.on("data", (row: any) => rows.push(row))
.on("end", () => resolve(rows));
});
}
function pickLatestSeedFile(seedDir: string, prefix: string) {
const files = fs
.readdirSync(seedDir)
.filter((f) => f.startsWith(prefix) && f.endsWith(".csv"));
if (files.length === 0) {
throw new Error(`Seed file not found: ${prefix}*.csv in ${seedDir}`);
}
files.sort();
return path.join(seedDir, files[files.length - 1]);
}
function pickLatestSeedJsonFile(seedDir: string, prefix: string) {
const files = fs
.readdirSync(seedDir)
.filter((f) => f.startsWith(prefix) && f.endsWith(".json"));
if (files.length === 0) {
throw new Error(`Seed file not found: ${prefix}*.json in ${seedDir}`);
}
files.sort();
return path.join(seedDir, files[files.length - 1]);
}
function escapeRegex(value: string) {
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
function listSeedJsonFiles(seedDir: string, prefix: string) {
// Match strictly: "<prefix><timestamp>.json" to avoid overlap
// e.g. "users_" should not match "users_roles_user_roles_..."
const pattern = new RegExp(`^${escapeRegex(prefix)}\\d+\\.json$`);
const files = fs.readdirSync(seedDir).filter((f) => pattern.test(f));
files.sort();
return files.map((f) => path.join(seedDir, f));
}
async function readJsonFile<T = any>(filePath: string): Promise<T> {
const raw = await fs.promises.readFile(filePath, "utf8");
return JSON.parse(raw) as T;
}
async function main() {
OrmHelper.setup();
await waitForDbInitialized();
try {
log.info("Starting seed auth data...");
await ensureAuthSchemaReady();
// Source of truth for auth seed (JSON exports)
const seedDir = path.resolve(process.cwd(), "seed", "auth");
// IMPORTANT:
// Seeder must be 100% synchronized with seed/auth.
// That means we process *all* JSON files in this folder (not only the latest export).
const expectedPrefixes = [
"application_",
"menus_",
"user_roles_",
"users_",
"users_roles_user_roles_",
] as const;
const allJsonFiles = fs
.readdirSync(seedDir)
.filter((f) => f.endsWith(".json"))
.sort();
if (allJsonFiles.length === 0) {
throw new Error(`No JSON seed files found in ${seedDir}`);
}
const filesByPrefix: Record<(typeof expectedPrefixes)[number], string[]> = {
application_: listSeedJsonFiles(seedDir, "application_"),
menus_: listSeedJsonFiles(seedDir, "menus_"),
user_roles_: listSeedJsonFiles(seedDir, "user_roles_"),
users_: listSeedJsonFiles(seedDir, "users_"),
users_roles_user_roles_: listSeedJsonFiles(
seedDir,
"users_roles_user_roles_",
),
};
for (const prefix of expectedPrefixes) {
if (filesByPrefix[prefix].length === 0) {
throw new Error(`Seed file not found: ${prefix}*.json in ${seedDir}`);
}
}
const recognized = new Set<string>();
for (const prefix of expectedPrefixes) {
for (const fp of filesByPrefix[prefix]) recognized.add(path.basename(fp));
}
const unrecognized = allJsonFiles.filter((f) => !recognized.has(f));
if (unrecognized.length > 0) {
// Fail fast so we never silently miss new seed files.
throw new Error(
`Unrecognized seed/auth files: ${unrecognized.join(", ")}`,
);
}
log.info("Using seed/auth files:", {
application: filesByPrefix["application_"].map((f) => path.basename(f)),
menus: filesByPrefix["menus_"].map((f) => path.basename(f)),
user_roles: filesByPrefix["user_roles_"].map((f) => path.basename(f)),
users: filesByPrefix["users_"].map((f) => path.basename(f)),
users_roles_user_roles: filesByPrefix["users_roles_user_roles_"].map(
(f) => path.basename(f),
),
total_files: allJsonFiles.length,
});
// applications
const appById: Record<string, Application> = {};
let defaultApp: Application = null;
let applicationsTotal = 0;
for (const fileApplications of filesByPrefix["application_"]) {
const appsDoc = await readJsonFile<{ application?: any[] }>(
fileApplications,
);
const appsRaw = Array.isArray(appsDoc?.application)
? appsDoc.application
: null;
if (!appsRaw) {
throw new Error(
`Invalid JSON structure for applications: ${path.basename(fileApplications)}`,
);
}
let processed = 0;
for (const row of appsRaw) {
if (row?.id === undefined || row?.id === null) {
throw new Error(
`Invalid application row (missing id) in ${path.basename(fileApplications)}`,
);
}
const app = await upsertApplication({
id: String(row.id),
name: String(row.name),
reset_password_url: String(row.reset_password_url ?? ""),
status: parseStatus(row.status),
deleted_at: parseNullableDate(row.deleted_at),
});
appById[app.id] = app;
if (!defaultApp) {
defaultApp = app;
}
processed++;
}
applicationsTotal += processed;
log.info("Seeded applications file", {
file: path.basename(fileApplications),
rows: processed,
});
}
// menus
let menusTotal = 0;
for (const fileMenus of filesByPrefix["menus_"]) {
const menusDoc = await readJsonFile<{ menus?: any[] }>(fileMenus);
const menusRaw = Array.isArray(menusDoc?.menus) ? menusDoc.menus : null;
if (!menusRaw) {
throw new Error(
`Invalid JSON structure for menus: ${path.basename(fileMenus)}`,
);
}
let processed = 0;
for (const row of menusRaw) {
const menuId = parseNullableString(row.id);
if (!menuId) {
throw new Error(
`Invalid menu row (missing id) in ${path.basename(fileMenus)}`,
);
}
if (!isUuid(menuId)) {
throw new Error(
`Invalid menu id (not uuid): ${menuId} in ${path.basename(fileMenus)}`,
);
}
const appId = parseNullableString(row.application) ?? defaultApp?.id;
if (!appId) {
throw new Error(
`Application not found for menu: ${String(row.application ?? "")}`,
);
}
const app =
appById[appId] ??
(await OrmHelper.DB.getRepository(Application).findOneBy({
id: appId,
}));
if (!app) {
throw new Error(`Application not found for menu: ${appId}`);
}
await upsertMenu({
id: menuId,
application: app,
module: String(row.module),
name: String(row.name),
link: String(row.link),
id_parent: parseNullableString(row.id_parent) ?? undefined,
order_number: parseIntSafe(row.order_number, 1),
icon: parseNullableString(row.icon) ?? undefined,
status: parseStatus(row.status),
deleted_at: parseNullableDate(row.deleted_at),
});
processed++;
}
menusTotal += processed;
log.info("Seeded menus file", {
file: path.basename(fileMenus),
rows: processed,
});
}
// user_roles
const roleById: Record<string, UserRole> = {};
let rolesTotal = 0;
for (const fileUserRoles of filesByPrefix["user_roles_"]) {
const rolesDoc = await readJsonFile<{ user_roles?: any[] }>(
fileUserRoles,
);
const rolesRaw = Array.isArray(rolesDoc?.user_roles)
? rolesDoc.user_roles
: null;
if (!rolesRaw) {
throw new Error(
`Invalid JSON structure for user_roles: ${path.basename(fileUserRoles)}`,
);
}
let processed = 0;
for (const row of rolesRaw) {
const roleId = parseNullableString(row.id);
if (!roleId) {
throw new Error(
`Invalid user_role row (missing id) in ${path.basename(fileUserRoles)}`,
);
}
if (!isUuid(roleId)) {
throw new Error(
`Invalid user_role id (not uuid): ${roleId} in ${path.basename(fileUserRoles)}`,
);
}
const appId = parseNullableString(row.application) ?? defaultApp?.id;
if (!appId) {
throw new Error(
`Application not found for user_role: ${String(row.application ?? "")}`,
);
}
const app =
appById[appId] ??
(await OrmHelper.DB.getRepository(Application).findOneBy({
id: appId,
}));
if (!app) {
throw new Error(`Application not found for user_role: ${appId}`);
}
const rolesList =
typeof row.roles === "string" && row.roles.trim() !== ""
? JSON.parse(row.roles)
: (row.roles ?? []);
const role = await upsertUserRole({
id: roleId,
application: app,
name: String(row.name),
roles: Array.isArray(rolesList) ? rolesList.map(String) : [],
status: parseStatus(row.status),
created_by: parseNullableString(row.created_by) ?? "system-seed",
updated_by: parseNullableString(row.updated_by) ?? "system-seed",
deleted_at: parseNullableDate(row.deleted_at),
deleted_by: parseNullableString(row.deleted_by),
});
roleById[role.id] = role;
processed++;
}
rolesTotal += processed;
log.info("Seeded user_roles file", {
file: path.basename(fileUserRoles),
rows: processed,
});
}
// users
const userById: Record<string, User> = {};
let usersTotal = 0;
for (const fileUsers of filesByPrefix["users_"]) {
const usersDoc = await readJsonFile<{ users?: any[] }>(fileUsers);
const usersRaw = Array.isArray(usersDoc?.users) ? usersDoc.users : null;
if (!usersRaw) {
throw new Error(
`Invalid JSON structure for users: ${path.basename(fileUsers)}`,
);
}
let processed = 0;
for (const row of usersRaw) {
const userId = parseNullableString(row.id);
if (userId && !isUuid(userId)) {
throw new Error(
`Invalid user id (not uuid): ${userId} in ${path.basename(fileUsers)}`,
);
}
const passwordFromSeed = String(row.password ?? "");
const user = await upsertUser({
id: userId ?? undefined,
email: String(row.email),
username: String(row.username),
name: String(row.name),
password: passwordFromSeed,
status: parseStatus(row.status),
created_by: parseNullableString(row.created_by) ?? "system-seed",
updated_by: parseNullableString(row.updated_by) ?? "system-seed",
reset_token: parseNullableString(row.reset_token),
deleted_at: parseNullableDate(row.deleted_at),
deleted_by: parseNullableString(row.deleted_by),
});
if (user && user.id) {
userById[user.id] = user;
}
processed++;
}
usersTotal += processed;
log.info("Seeded users file", {
file: path.basename(fileUsers),
rows: processed,
});
}
// users_roles_user_roles (join table)
const userRepo = OrmHelper.DB.getRepository(User);
const userRoleRepo = OrmHelper.DB.getRepository(UserRole);
const joinsByUserId: Record<string, string[]> = {};
let joinsTotal = 0;
for (const fileUsersRoles of filesByPrefix["users_roles_user_roles_"]) {
const joinsDoc = await readJsonFile<{ users_roles_user_roles?: any[] }>(
fileUsersRoles,
);
const joinsRaw = Array.isArray(joinsDoc?.users_roles_user_roles)
? joinsDoc.users_roles_user_roles
: null;
if (!joinsRaw) {
throw new Error(
`Invalid JSON structure for users_roles_user_roles: ${path.basename(fileUsersRoles)}`,
);
}
let processed = 0;
for (const row of joinsRaw) {
const usersId = parseNullableString(row.usersId);
const userRolesId = parseNullableString(row.userRolesId);
if (!usersId || !userRolesId) {
throw new Error(
`Invalid join row (missing usersId/userRolesId) in ${path.basename(fileUsersRoles)}`,
);
}
if (!isUuid(usersId) || !isUuid(userRolesId)) {
throw new Error(
`Invalid join row ids (must be uuid) usersId=${usersId} userRolesId=${userRolesId} in ${path.basename(fileUsersRoles)}`,
);
}
if (!joinsByUserId[usersId]) joinsByUserId[usersId] = [];
joinsByUserId[usersId].push(userRolesId);
processed++;
}
joinsTotal += processed;
log.info("Loaded join file", {
file: path.basename(fileUsersRoles),
rows: processed,
});
}
for (const userId of Object.keys(joinsByUserId)) {
if (!isUuid(userId)) {
throw new Error(`Invalid user id in join map (not uuid): ${userId}`);
}
const user =
userById[userId] ??
(await userRepo.findOne({
relations: { roles: true } as any,
where: { id: userId } as any,
}));
if (!user) continue;
if (!user.roles) user.roles = [];
for (const roleId of joinsByUserId[userId]) {
const role =
roleById[roleId] ??
(await userRoleRepo.findOne({
relations: { application: true } as any,
where: { id: roleId } as any,
}));
if (!role) continue;
if (!user.roles.find((r) => r.id === role.id)) {
user.roles.push(role);
}
}
await userRepo.save(user);
}
log.info("Seed auth data summary", {
applications_rows: applicationsTotal,
menus_rows: menusTotal,
user_roles_rows: rolesTotal,
users_rows: usersTotal,
join_rows: joinsTotal,
files_processed: allJsonFiles.length,
});
log.info("✅ Seed auth data completed");
process.exit(0);
} catch (error) {
log.error("Fatal error:", error);
process.exit(1);
}
}
main();