2025-08-07 15:46:41 +02:00
|
|
|
import {env} from "@/env.mjs";
|
2025-07-16 20:34:28 +02:00
|
|
|
import {db, makeMigration} from "@/db";
|
2025-08-07 15:46:41 +02:00
|
|
|
import {eq} from "drizzle-orm";
|
2025-07-16 12:36:11 +02:00
|
|
|
import * as drizzleDb from "@/db";
|
|
|
|
|
|
2024-11-16 22:00:22 +01:00
|
|
|
|
2025-07-16 20:34:28 +02:00
|
|
|
export async function init() {
|
2025-05-16 15:54:17 +02:00
|
|
|
consoleAscii();
|
|
|
|
|
console.log("====Init Functions====");
|
2025-07-16 20:34:28 +02:00
|
|
|
await makeMigration();
|
|
|
|
|
await createDefaultOrganization();
|
|
|
|
|
await createSettingsIfNotExist()
|
2025-08-07 15:46:41 +02:00
|
|
|
console.log("====Initialization completed====");
|
2024-11-16 22:00:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function createSettingsIfNotExist() {
|
2024-11-17 16:31:34 +01:00
|
|
|
const configSettings = {
|
|
|
|
|
name: "system",
|
2025-08-07 15:46:41 +02:00
|
|
|
storage: env.STORAGE_TYPE!,
|
2024-11-17 16:31:34 +01:00
|
|
|
smtpPassword: env.SMTP_PASSWORD ?? null,
|
|
|
|
|
smtpFrom: env.SMTP_FROM ?? null,
|
|
|
|
|
smtpHost: env.SMTP_HOST ?? null,
|
|
|
|
|
smtpPort: env.SMTP_PORT ?? null,
|
|
|
|
|
smtpUser: env.SMTP_USER ?? null,
|
|
|
|
|
s3EndPointUrl: env.S3_ENDPOINT ?? null,
|
|
|
|
|
s3AccessKeyId: env.S3_ACCESS_KEY ?? null,
|
|
|
|
|
s3SecretAccessKey: env.S3_SECRET_KEY ?? null,
|
|
|
|
|
S3BucketName: env.S3_BUCKET_NAME ?? null,
|
2025-05-16 15:54:17 +02:00
|
|
|
};
|
2024-11-17 16:31:34 +01:00
|
|
|
|
2025-07-16 12:36:11 +02:00
|
|
|
const [existing] = await db.select().from(drizzleDb.schemas.setting).where(eq(drizzleDb.schemas.setting.name, "system")).limit(1);
|
2024-11-17 16:31:34 +01:00
|
|
|
|
2025-05-16 15:54:17 +02:00
|
|
|
if (!existing) {
|
|
|
|
|
console.log("====Init Setting : Create ====");
|
2025-07-16 12:36:11 +02:00
|
|
|
await db.insert(drizzleDb.schemas.setting).values(configSettings);
|
2024-11-29 19:25:44 +01:00
|
|
|
} else {
|
2025-05-16 15:54:17 +02:00
|
|
|
console.log("====Init Setting : Update ====");
|
2025-07-16 12:36:11 +02:00
|
|
|
await db.update(drizzleDb.schemas.setting).set(configSettings).where(eq(drizzleDb.schemas.setting.name, "system"));
|
2024-11-16 22:00:22 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-29 19:25:44 +01:00
|
|
|
async function createDefaultOrganization() {
|
|
|
|
|
const defaultOrganizationConf = {
|
|
|
|
|
slug: "default",
|
|
|
|
|
name: "Default Organization",
|
2025-05-16 15:54:17 +02:00
|
|
|
createdAt: new Date(),
|
|
|
|
|
};
|
2024-11-29 19:25:44 +01:00
|
|
|
|
2025-07-16 12:36:11 +02:00
|
|
|
const [existing] = await db.select().from(drizzleDb.schemas.organization).where(eq(drizzleDb.schemas.organization.slug, "default")).limit(1);
|
2025-05-16 15:54:17 +02:00
|
|
|
|
|
|
|
|
if (!existing) {
|
|
|
|
|
console.log("==== Creating default Organization... ====\n");
|
2025-07-16 12:36:11 +02:00
|
|
|
await db.insert(drizzleDb.schemas.organization).values(defaultOrganizationConf);
|
2024-11-29 19:25:44 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function consoleAscii() {
|
2025-05-16 15:54:17 +02:00
|
|
|
console.log(
|
|
|
|
|
"\n" +
|
2025-08-07 15:46:41 +02:00
|
|
|
" ____ __ __ _____ \n" +
|
|
|
|
|
" / __ \\ ____ _____ / /_ ____ _ / /_ ____ _ _____ ___ / ___/ ___ _____ _ __ ___ _____\n" +
|
|
|
|
|
" / /_/ // __ \\ / ___// __// __ // __ \\ / __ // ___// _ \\ \\__ \\ / _ \\ / ___/| | / // _ \\ / ___/\n" +
|
|
|
|
|
" / ____// /_/ // / / /_ / /_/ // /_/ // /_/ /(__ )/ __/ ___/ // __// / | |/ // __// / \n" +
|
|
|
|
|
"/_/ \\____//_/ \\__/ \\__,_//_.___/ \\__,_//____/ \\___/ /____/ \\___//_/ |___/ \\___//_/ \n" +
|
|
|
|
|
" \n"
|
2025-05-16 15:54:17 +02:00
|
|
|
);
|
|
|
|
|
}
|