migration to postgres

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-26 17:42:59 +00:00
co-authored by Claude Opus 4.6
parent 7f04ecd644
commit 500a70910e
54 changed files with 4016 additions and 264 deletions
+17 -15
View File
@@ -1,9 +1,9 @@
import { join } from 'node:path';
import type { Database } from 'bun:sqlite';
import type { JobHandler } from '../types';
import { registerHandler } from '../handler-registry';
import { DATA_PATH, SERVER_CONFIG_DIR } from '../../data-path';
import { DATA_PATH } from '../../data-path';
import { openEmailDb, upsertFromRawEml, getSyncMeta, setSyncMeta, updateEmailLabels } from '../../api/email/email-db';
import { getServerIntegration, getUserByEmail, getUserIntegration } from 'officerdb';
type GoogleCredentials = {
accessToken: string;
@@ -13,26 +13,28 @@ type GoogleCredentials = {
clientSecret: string;
};
const googleConfigPath = join(SERVER_CONFIG_DIR, 'google-oauth.json');
async function loadCredentials(userId: string): Promise<GoogleCredentials> {
const config = await Bun.file(googleConfigPath).json().catch(() => null);
if (!config?.clientId || !config?.clientSecret) {
async function loadCredentials(email: string): Promise<GoogleCredentials> {
const serverGoogle = await getServerIntegration('google');
const serverConfig = serverGoogle?.config as Record<string, unknown> | undefined;
if (!serverConfig?.clientId || !serverConfig?.clientSecret) {
throw new Error('Google OAuth not configured — ask your admin to set up credentials');
}
const tokenPath = join(DATA_PATH, userId, 'integrations', 'google.json');
const token = await Bun.file(tokenPath).json().catch(() => null);
if (!token?.accessToken) {
const dbUser = await getUserByEmail(email);
if (!dbUser) throw new Error('User not found');
const userGoogle = await getUserIntegration(dbUser.id, 'google');
const userConfig = userGoogle?.config as Record<string, unknown> | undefined;
if (!userConfig?.accessToken) {
throw new Error('Google account not connected — connect in Settings → Integrations');
}
return {
accessToken: token.accessToken,
refreshToken: token.refreshToken ?? '',
expiresAt: token.expiresAt ?? 0,
clientId: config.clientId,
clientSecret: config.clientSecret,
accessToken: userConfig.accessToken as string,
refreshToken: (userConfig.refreshToken as string) ?? '',
expiresAt: (userConfig.expiresAt as number) ?? 0,
clientId: serverConfig.clientId as string,
clientSecret: serverConfig.clientSecret as string,
};
}