migration to postgres
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -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,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user