cre-sync/lib/ghl/helpers.ts
BusyBee3333 4e6467ffb0 Add CRESync CRM application with Setup page
- Build complete Next.js CRM for commercial real estate
- Add authentication with JWT sessions and role-based access
- Add GoHighLevel API integration for contacts, conversations, opportunities
- Add AI-powered Control Center with tool calling
- Add Setup page with onboarding checklist (/setup)
- Add sidebar navigation with Setup menu item
- Fix type errors in onboarding API, GHL services, and control center tools
- Add Prisma schema with SQLite for local development
- Add UI components with clay morphism design system

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-14 17:30:55 -05:00

45 lines
1.3 KiB
TypeScript

import { prisma } from '@/lib/db';
import { GHL } from './index';
import { decrypt } from '@/lib/settings/encryption';
import { settingsService } from '@/lib/settings/settings-service';
/**
* Get GHL client for a specific user.
* First checks user's own credentials, then falls back to global system settings.
*/
export async function getGHLClientForUser(userId: string): Promise<GHL | null> {
// First, try to get user-specific GHL credentials
const user = await prisma.user.findUnique({
where: { id: userId },
select: { ghlLocationId: true, ghlAccessToken: true },
});
if (user?.ghlLocationId && user?.ghlAccessToken) {
return new GHL({
accessToken: decrypt(user.ghlAccessToken),
locationId: user.ghlLocationId,
});
}
// Fall back to global system settings (for single-tenant or testing)
return getGHLClientFromSettings();
}
/**
* Get GHL client using global system settings.
* Useful for admin operations or single-tenant deployments.
*/
export async function getGHLClientFromSettings(): Promise<GHL | null> {
const accessToken = await settingsService.get('ghlAccessToken');
const locationId = await settingsService.get('ghlLocationId');
if (!accessToken || !locationId) {
return null;
}
return new GHL({
accessToken,
locationId,
});
}