mirror of
https://github.com/Portabase/portabase.git
synced 2026-07-14 11:16:13 +02:00
* docs: add AGENTS.md with feature-based architecture conventions Defines the target src/ structure, feature list, naming conventions (kebab-case, file type patterns), import direction rules, and feature boundary guidelines for the planned big-bang refactor. * docs: add features/channel/ to architecture conventions Generic channel UI (card, modal, form) goes to features/channel/. Provider-specific sub-folders: channel/notifications/ and channel/storages/. * refactor: move event emitter to lib/, project service to db/services/, dedup use-mobile hook * fix: remove unused auth import in db/services/project.ts * refactor: migrate wrappers/common to components/common (flat) * fix: remove dead commented-out code from button-with-loading.tsx * refactor: merge features/browser into features/theme, flatten upload/updates, migrate statistics * refactor: migrate wrappers/auth to features/auth Move all auth components and forms from src/components/wrappers/auth/ into src/features/auth/ with flat kebab-case naming. Handle extra reset-password-form.tsx and differing reset-password-schema.ts by copying them as reset-password-page-form.tsx and reset-password-page.schema.ts. * refactor: remove dead auth files and fix zPassword schema Delete reset-password-section.tsx and reset-password-page-form.tsx (zero consumers). Fix reset-password-page.schema.ts to import zPassword from @/lib/zod. * refactor: migrate wrappers/dashboard/common to features/layout, rename Header.tsx to header.tsx * fix: decouple layout from theme/updates cross-feature imports Use slot props (actions, updateNotification) so app/ composes ModeToggle and UpdateNotification into Header and AppSidebar. Delete dead side-bar-logo.tsx (zero consumers). * refactor: merge features/keys into features/agents, flatten sub-dirs, migrate agent wrappers - Merge src/features/keys/keys.action.ts into src/features/agents/ - Flatten src/features/agents/components/ and hooks/ into feature root - Migrate all src/components/wrappers/dashboard/agent/ files into src/features/agents/ - Update all import paths across app/ and src/ accordingly - Fix broken code-snippet import in agent-key-modal.tsx (was pointing to non-existent wrappers path) * fix: move github-releases to lib/, remove dead code and unused imports Move GitHub release fetch logic from features/updates/github.ts to lib/github-releases.ts so both features/updates and features/agents can import it without cross-feature imports. Remove commented-out impersonation block from agent-database-card.tsx. Remove unused imports (CopyButton, Terminal, Key, Info). * refactor: flatten features/projects/components, migrate project wrappers Move project.dialog/form out of sub-dir, migrate project-card, project-database-card, and delete-project button/action from wrappers into features/projects/. Update all import paths across src/ and app/. Leave wrappers/dashboard/projects/database/ untouched for Task 10. * fix: extract DatabaseCard to components/common, remove cross-feature import DatabaseCard was imported by both features/projects and features/agents, violating the no-cross-feature-import rule. Extract to components/common/ database-card.tsx so both features import from the shared layer. Also tighten extendedProps type and remove commented-out maxCount prop. * refactor: migrate all database/backup/restore/health UI to features/database Consolidates ~40 files from 5 source locations (features/dashboard/backup, features/dashboard/restore, wrappers/dashboard/backup, wrappers/dashboard/database, wrappers/dashboard/health, wrappers/dashboard/projects/database) into the new flat features/database directory with kebab-case filenames and updated @/ imports. * fix: remove dead restore-form files and console.log from health-grid Delete restore-form.tsx (186 lines all commented-out, zero consumers) and restore-form.schema.ts (only a commented import). Remove console.log(logs) from getOldestLog helper in health-grid.tsx. Cross-feature imports (database→storages for dispatchStorage/storeBackupFiles) are accepted as known architectural coupling — storage dispatch is called from database server actions at runtime and cannot route through app/ props. * refactor: migrate wrappers/admin/users to features/users, fix table-colums typo * fix: fix relative imports, string interpolation, and console.error in users feature Replace relative imports with @/ aliases in user-actions-cell.tsx and admin-user-edit-modal.tsx. Fix string interpolation bug in dialog title (was regular string, now template literal). Remove console.error from admin-user-change-role-modal.tsx. * refactor: migrate wrappers/profile to features/profile, fix profile-apperance typo * fix: remove console.error calls and ts-ignore in profile feature Remove console.error from profile-account.tsx, 2fa-form.tsx (x2). Replace @ts-ignore on fileInput.onchange with typed cast in avatar-with-upload.tsx. * refactor: rename features/organization to features/organizations, migrate all org wrappers Moves 37 org-related files from features/organization/, features/dashboard/, hooks/, wrappers/admin/organizations/, wrappers/admin/channels/organization/, and wrappers/dashboard/organization/ into a flat features/organizations/ feature directory. Updates all import paths codebase-wide. Fixes @ts-ignore and removes unused @better-fetch/fetch import in admin-organization-form. * fix: remove console.error, @ts-ignore, and unused destructures in features/organizations * refactor: migrate wrappers/organization/migration to features/migration Move all 5 migration files to src/features/migration/, update internal imports to @/features/migration/ absolute paths, remove console.log, and delete src/components/wrappers/dashboard/organization/ entirely. * refactor: migrate wrappers/admin/settings to features/settings Move all 14 settings files (email, notification, storage, s3) from wrappers/dashboard/admin/settings/ into features/settings/, rewriting imports, replacing @ts-ignore with @ts-expect-error, and renaming error params to _error where applicable. * refactor: rename notifications files to kebab convention, migrate notification logs - Rename dispatch.ts/helpers.ts/types.ts to notifications.*.ts - Migrate notification logs components from wrappers/dashboard/admin/notifications/logs/ to features/notifications/ - Fix all import paths across codebase and in providers/ - Remove unused useRouter import, replace @ts-ignore with @ts-expect-error - Delete src/components/wrappers/dashboard/admin/notifications/ * refactor: rename storages dispatch/helpers/types to kebab convention - Rename dispatch.ts/helpers.ts/types.ts to storages.*.ts - Fix all import paths across codebase and in providers/ - Fix relative ../types and ../../types imports in all storage providers to absolute @/ paths - Replace @ts-ignore with @ts-expect-error, remove console.debug/console.error * refactor: create features/channel with generic UI and notifications/storages provider sub-folders Consolidates 4 source locations into src/features/channel/: - components/wrappers/dashboard/admin/channels/ → features/channel/ (11 generic UI files) - features/notifications/providers/ → features/channel/notifications/ (9 backend + 18 form files) - features/storages/providers/ → features/channel/storages/ (backend + forms) The two conflicting google-drive/helpers.ts files (client helpers vs OAuth action) are kept separate: helpers.ts and google-drive-refresh.action.ts. Rewrites all imports codebase-wide to point to new locations. Removes console.log/console.error calls, upgrades @ts-ignore to @ts-expect-error. * fix: remove console.error from local storage provider * fix: multi-select.tsx scroll issue * fix: multi-select.tsx scroll issue
276 lines
10 KiB
TypeScript
276 lines
10 KiB
TypeScript
import {NextResponse} from "next/server";
|
|
import {Body} from "./route";
|
|
import {isUuidv4} from "@/utils/verify-uuid";
|
|
import {Agent} from "@/db/schema/08_agent";
|
|
import {DatabaseWith} from "@/db/schema/07_database";
|
|
import * as drizzleDb from "@/db";
|
|
import {db, db as dbClient} from "@/db";
|
|
import {and, eq, inArray} from "drizzle-orm";
|
|
import {dbmsEnumSchema, EDbmsSchema} from "@/db/schema/types";
|
|
import {withUpdatedAt} from "@/db/utils";
|
|
import type {StorageInput} from "@/features/storages/storages.types";
|
|
import {dispatchStorage} from "@/features/storages/storages.dispatch";
|
|
import {Setting} from "@/db/schema/01_setting";
|
|
import {logger} from "@/lib/logger";
|
|
|
|
const log = logger.child({module: "api/agent/status/helpers"});
|
|
|
|
export async function handleDatabases(body: Body, agent: Agent, lastContact: Date, settings: Setting) {
|
|
const databasesResponse = [];
|
|
|
|
const formatDatabase = (database: DatabaseWith, backupAction: boolean, restoreAction: boolean, UrlBackup: string | null, storages: PingDatabaseStorageChannels[], urlMeta: string | null) => ({
|
|
generatedId: database.agentDatabaseId,
|
|
dbms: database.dbms,
|
|
storages: storages,
|
|
encrypt: settings.encryption,
|
|
data: {
|
|
backup: {
|
|
action: backupAction,
|
|
cron: database.backupPolicy,
|
|
},
|
|
restore: {
|
|
action: restoreAction,
|
|
file: UrlBackup,
|
|
metaFile: urlMeta
|
|
},
|
|
},
|
|
});
|
|
|
|
for (const db of body.databases) {
|
|
|
|
const existingDatabase = await dbClient.query.database.findFirst({
|
|
where: eq(drizzleDb.schemas.database.agentDatabaseId, db.generatedId),
|
|
with: {
|
|
project: true
|
|
}
|
|
});
|
|
|
|
let backupAction: boolean = false
|
|
let restoreAction: boolean = false
|
|
let urlBackup: string | null = null;
|
|
let urlMeta: string | null = null
|
|
|
|
if (!existingDatabase) {
|
|
if (!isUuidv4(db.generatedId)) {
|
|
return NextResponse.json(
|
|
{error: "generatedId is not a valid uuid"},
|
|
{status: 500}
|
|
);
|
|
}
|
|
|
|
if (!dbmsEnumSchema.safeParse(db.dbms).success) {
|
|
log.error({name: "handleDatabases"},`Database type not available: ${db.dbms}`);
|
|
continue;
|
|
}
|
|
|
|
const [databaseCreated] = await dbClient
|
|
.insert(drizzleDb.schemas.database)
|
|
.values({
|
|
agentId: agent.id,
|
|
name: db.name,
|
|
dbms: db.dbms as EDbmsSchema,
|
|
agentDatabaseId: db.generatedId,
|
|
lastContact: db.pingStatus ? lastContact : null,
|
|
healthErrorCount: null
|
|
})
|
|
.returning();
|
|
|
|
|
|
if (databaseCreated) {
|
|
|
|
|
|
await dbClient
|
|
.insert(drizzleDb.schemas.healthcheckLog)
|
|
.values({
|
|
kind: "database",
|
|
status: db.pingStatus ? "success" : "failed",
|
|
objectId: databaseCreated.id,
|
|
date: lastContact
|
|
})
|
|
|
|
const storages = await getDatabaseStorageChannels(databaseCreated.id)
|
|
|
|
databasesResponse.push(formatDatabase(databaseCreated, backupAction, restoreAction, urlBackup, storages, null));
|
|
}
|
|
} else {
|
|
|
|
const [databaseUpdated] = await dbClient
|
|
.update(drizzleDb.schemas.database)
|
|
.set(withUpdatedAt({
|
|
name: db.name,
|
|
agentId: agent.id,
|
|
dbms: db.dbms as EDbmsSchema,
|
|
lastContact: db.pingStatus ? lastContact : existingDatabase.lastContact,
|
|
healthErrorCount: db.pingStatus ? null : existingDatabase.healthErrorCount,
|
|
}))
|
|
.where(eq(drizzleDb.schemas.database.id, existingDatabase.id))
|
|
.returning();
|
|
|
|
|
|
await dbClient
|
|
.insert(drizzleDb.schemas.healthcheckLog)
|
|
.values({
|
|
kind: "database",
|
|
status: db.pingStatus ? "success" : "failed",
|
|
objectId: databaseUpdated.id,
|
|
date: lastContact
|
|
})
|
|
|
|
|
|
const activeBackup = await dbClient.query.backup.findFirst({
|
|
where: and(
|
|
eq(drizzleDb.schemas.backup.databaseId, databaseUpdated.id),
|
|
inArray(drizzleDb.schemas.backup.status, ["waiting", "ongoing"])
|
|
)
|
|
})
|
|
|
|
const restoration = await dbClient.query.restoration.findFirst({
|
|
where: and(eq(drizzleDb.schemas.restoration.databaseId, databaseUpdated.id), eq(drizzleDb.schemas.restoration.status, "waiting")),
|
|
with: {
|
|
backupStorage: true
|
|
}
|
|
})
|
|
|
|
if (activeBackup && activeBackup.status == "waiting") {
|
|
backupAction = true
|
|
|
|
await dbClient
|
|
.update(drizzleDb.schemas.backup)
|
|
.set(withUpdatedAt({status: "ongoing"}))
|
|
.where(eq(drizzleDb.schemas.backup.id, activeBackup.id));
|
|
}
|
|
|
|
if (restoration) {
|
|
restoreAction = true
|
|
|
|
if (!restoration.backupStorage || restoration.backupStorage.status != "success" || !restoration.backupStorage.path) {
|
|
restoreAction = false
|
|
continue;
|
|
}
|
|
|
|
const input: StorageInput = {
|
|
action: "get",
|
|
data: {
|
|
path: restoration.backupStorage.path,
|
|
signedUrl: true,
|
|
},
|
|
metadata: {
|
|
storageId: restoration.backupStorage.storageChannelId,
|
|
fileKind: "backups"
|
|
}
|
|
};
|
|
|
|
const inputMeta: StorageInput = {
|
|
action: "get",
|
|
data: {
|
|
path: `${restoration.backupStorage.path}.meta`,
|
|
signedUrl: true,
|
|
},
|
|
metadata: {
|
|
storageId: restoration.backupStorage.storageChannelId,
|
|
fileKind: "backups"
|
|
}
|
|
};
|
|
|
|
|
|
try {
|
|
const result = await dispatchStorage(input, undefined, restoration.backupStorage.storageChannelId);
|
|
const resultMeta = await dispatchStorage(inputMeta, undefined, restoration.backupStorage.storageChannelId);
|
|
|
|
if (result.success) {
|
|
urlBackup = result.url ?? null;
|
|
urlMeta = resultMeta.url ?? null
|
|
} else {
|
|
await dbClient
|
|
.update(drizzleDb.schemas.restoration)
|
|
.set(withUpdatedAt({status: "failed"}))
|
|
.where(eq(drizzleDb.schemas.restoration.id, restoration.id));
|
|
|
|
const errorMessage = "Failed to get backup URL";
|
|
log.error({error: errorMessage, name: "handleDatabases"}, "Restoration failed");
|
|
continue;
|
|
}
|
|
} catch (err) {
|
|
log.error({error: err, name: "handleDatabases"}, "Restoration crashed unexpectedly");
|
|
await dbClient
|
|
.update(drizzleDb.schemas.restoration)
|
|
.set(withUpdatedAt({status: "failed"}))
|
|
.where(eq(drizzleDb.schemas.restoration.id, restoration.id));
|
|
continue;
|
|
}
|
|
|
|
await dbClient
|
|
.update(drizzleDb.schemas.restoration)
|
|
.set(withUpdatedAt({status: "ongoing"}))
|
|
.where(eq(drizzleDb.schemas.restoration.id, restoration.id));
|
|
}
|
|
const storages = await getDatabaseStorageChannels(databaseUpdated.id)
|
|
databasesResponse.push(formatDatabase(databaseUpdated, backupAction, restoreAction, urlBackup, storages, urlMeta));
|
|
}
|
|
}
|
|
return databasesResponse;
|
|
}
|
|
|
|
|
|
type PingDatabaseStorageChannels = {
|
|
id: string;
|
|
config: any
|
|
provider: string
|
|
}
|
|
|
|
async function getDatabaseStorageChannels(databaseId: string): Promise<PingDatabaseStorageChannels[]> {
|
|
|
|
const database = await db.query.database.findFirst({
|
|
where: eq(drizzleDb.schemas.database.id, databaseId),
|
|
with: {
|
|
project: true,
|
|
retentionPolicy: true,
|
|
alertPolicies: true,
|
|
storagePolicies: true
|
|
}
|
|
});
|
|
|
|
if (!database) {
|
|
return []
|
|
}
|
|
|
|
const settings = await db.query.setting.findFirst({
|
|
where: eq(drizzleDb.schemas.setting.name, "system"),
|
|
with: {storageChannel: true},
|
|
});
|
|
|
|
const defaultStorageChannel: PingDatabaseStorageChannels[] = settings?.storageChannel
|
|
? [{
|
|
id: settings.storageChannel.id,
|
|
provider: settings.storageChannel.provider,
|
|
config: settings.storageChannel.config,
|
|
}]
|
|
: [];
|
|
|
|
|
|
const enabledDatabaseStorageChannels = await Promise.all(
|
|
(database.storagePolicies ?? [])
|
|
.filter(p => p.enabled)
|
|
.map(async policy => {
|
|
const storageChannel = await db.query.storageChannel.findFirst({
|
|
where: eq(drizzleDb.schemas.storageChannel.id, policy.storageChannelId),
|
|
});
|
|
|
|
if (!storageChannel) return null;
|
|
|
|
return {
|
|
id: storageChannel.id,
|
|
config: storageChannel.config,
|
|
provider: storageChannel.provider,
|
|
} as PingDatabaseStorageChannels;
|
|
})
|
|
);
|
|
|
|
const filteredChannels: PingDatabaseStorageChannels[] = enabledDatabaseStorageChannels.filter(
|
|
(c): c is PingDatabaseStorageChannels => c !== null
|
|
);
|
|
|
|
return filteredChannels.length > 0 ? filteredChannels : defaultStorageChannel;
|
|
}
|
|
|