import { GHLAgencyClient } from '../agency-client'; import { GHLLocation } from '@/types/ghl'; export interface CreateLocationDTO { name: string; email: string; phone?: string; address?: string; city?: string; state?: string; country?: string; postalCode?: string; timezone?: string; website?: string; } export interface UpdateLocationDTO { name?: string; email?: string; phone?: string; address?: string; city?: string; state?: string; country?: string; postalCode?: string; timezone?: string; website?: string; } export interface LocationSettings { allowDuplicateContact?: boolean; allowDuplicateOpportunity?: boolean; allowFacebookNameMerge?: boolean; disableContactTimezone?: boolean; } export class LocationsService { constructor(private agencyClient: GHLAgencyClient) {} // Create a new location (sub-account) for a user async create(data: CreateLocationDTO): Promise { const response = await this.agencyClient.createLocation({ name: data.name, email: data.email, phone: data.phone, address: data.address, city: data.city, state: data.state, country: data.country || 'US', postalCode: data.postalCode, timezone: data.timezone || 'America/New_York', }); return response as GHLLocation; } // Get a location by ID async getById(locationId: string): Promise { const response = await this.agencyClient.getLocation(locationId); return response as GHLLocation; } // List all locations for the agency async getAll(): Promise { const response = await this.agencyClient.listLocations(); return (response as any).locations || []; } // Update a location async update(locationId: string, data: UpdateLocationDTO): Promise { return this.agencyClient.request(`/locations/${locationId}`, { method: 'PUT', body: JSON.stringify(data), }) as Promise; } // Delete a location (use with caution!) async delete(locationId: string): Promise { await this.agencyClient.request(`/locations/${locationId}`, { method: 'DELETE', }); } // Get location settings async getSettings(locationId: string): Promise { const response = await this.agencyClient.request(`/locations/${locationId}/settings`); return response as LocationSettings; } // Update location settings async updateSettings(locationId: string, settings: LocationSettings): Promise { return this.agencyClient.request(`/locations/${locationId}/settings`, { method: 'PUT', body: JSON.stringify(settings), }) as Promise; } // Search locations by name or email async search(query: string): Promise { const all = await this.getAll(); const lowerQuery = query.toLowerCase(); return all.filter(loc => loc.name?.toLowerCase().includes(lowerQuery) || loc.email?.toLowerCase().includes(lowerQuery) ); } // Check if a location exists by email async existsByEmail(email: string): Promise { const all = await this.getAll(); return all.some(loc => loc.email?.toLowerCase() === email.toLowerCase()); } // Create location for a new user signup async createForUser(user: { email: string; firstName: string; lastName: string; brokerage?: string; }): Promise { const locationName = user.brokerage ? `${user.firstName} ${user.lastName} - ${user.brokerage}` : `${user.firstName} ${user.lastName}`; return this.create({ name: locationName, email: user.email, timezone: 'America/New_York', // Default, can be updated later }); } }