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";
|
2025-08-24 20:11:34 +02:00
|
|
|
import {retentionJob} from "@/lib/tasks";
|
2025-10-26 11:54:25 +01:00
|
|
|
import {generateRSAKeys} from "@/utils/rsa-keys";
|
2026-01-13 22:22:01 +01:00
|
|
|
import {Provider} from "react";
|
|
|
|
|
import type {ProviderKind} from "@/features/notifications/types";
|
|
|
|
|
import {StorageProviderKind} from "@/features/storages/types";
|
2025-07-16 12:36:11 +02:00
|
|
|
|
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-10-26 11:54:25 +01:00
|
|
|
await generateRSAKeys();
|
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====");
|
2025-08-24 20:11:34 +02:00
|
|
|
await setupCronJobs()
|
2024-11-16 22:00:22 +01:00
|
|
|
}
|
|
|
|
|
|
2025-08-24 20:11:34 +02:00
|
|
|
async function setupCronJobs() {
|
|
|
|
|
console.log("==== Setting up Cron Jobs ====");
|
|
|
|
|
retentionJob.start();
|
|
|
|
|
console.log(`==== Cron job started ====`);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-16 22:00:22 +01:00
|
|
|
async function createSettingsIfNotExist() {
|
2024-11-17 16:31:34 +01:00
|
|
|
const configSettings = {
|
|
|
|
|
name: "system",
|
2026-01-14 23:01:06 +01: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,
|
2026-01-14 23:01:06 +01:00
|
|
|
// 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
|
|
|
}
|
2026-01-13 22:22:01 +01:00
|
|
|
|
|
|
|
|
const [existingLocalChannelStorage] = await db.select().from(drizzleDb.schemas.storageChannel).where(eq(drizzleDb.schemas.storageChannel.provider, "local")).limit(1);
|
|
|
|
|
|
|
|
|
|
const localChannelValues = {
|
|
|
|
|
provider: "local" as StorageProviderKind,
|
|
|
|
|
enabled: true,
|
|
|
|
|
name: "System",
|
|
|
|
|
config: {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!existingLocalChannelStorage) {
|
|
|
|
|
console.log("====Local Storage : Create ====");
|
2026-01-14 23:01:06 +01:00
|
|
|
const [localChannelCreated] = await db.insert(drizzleDb.schemas.storageChannel).values(localChannelValues).returning();
|
|
|
|
|
|
|
|
|
|
if (localChannelCreated){
|
|
|
|
|
await db.update(drizzleDb.schemas.setting).set({
|
|
|
|
|
defaultStorageChannelId: localChannelCreated.id,
|
|
|
|
|
}).where(eq(drizzleDb.schemas.setting.name, "system"));
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-13 22:22:01 +01:00
|
|
|
} else {
|
|
|
|
|
console.log("====Local Storage : Update ====");
|
|
|
|
|
await db.update(drizzleDb.schemas.storageChannel).set(localChannelValues).where(eq(drizzleDb.schemas.storageChannel.provider, "local"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2026-01-14 23:01:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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(
|
2025-09-23 19:42:38 +02:00
|
|
|
" \n" +
|
|
|
|
|
" ____ __ __ \n" +
|
|
|
|
|
" / __ \\____ _____/ /_____ _/ /_ ____ _________ \n" +
|
|
|
|
|
" / /_/ / __ \\/ ___/ __/ __ / __ \\/ __ / ___/ _ \\ \n" +
|
|
|
|
|
" / ____/ /_/ / / / /_/ /_/ / /_/ / /_/ (__ ) __/ \n" +
|
|
|
|
|
" /_/ \\____/_/ \\__/\\__,_/_.___/\\__,_/____/\\___/ \n" +
|
|
|
|
|
" \n" +
|
|
|
|
|
` Community Edition v${env.NEXT_PUBLIC_PROJECT_VERSION} \n ` +
|
|
|
|
|
" \n"
|
|
|
|
|
)
|
2025-05-16 15:54:17 +02:00
|
|
|
}
|