File size: 1,501 Bytes
8fb4cca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { LeadData, AdminConfig } from '../types';

export const crmService = {
  /**
   * Syncs a lead to the external CRM system defined in AdminConfig.
   * Uses a mock implementation if no URL is provided or for demo purposes.
   */
  syncLead: async (lead: LeadData, config: AdminConfig): Promise<{ success: boolean; crmId?: string }> => {
    // 1. Check if CRM integration is enabled
    if (!config.crmApiUrl) {
      console.log('CRM Sync skipped: No API URL configured');
      return { success: false };
    }

    try {
      console.log(`Syncing lead ${lead.name} to ${config.crmApiUrl}...`);
      
      // 2. Simulate Network Request
      // In production, use: await fetch(config.crmApiUrl, { method: 'POST', headers: { 'Authorization': `Bearer ${config.crmApiKey}` }, body: JSON.stringify(lead) });
      
      await new Promise(resolve => setTimeout(resolve, 1000)); // Mock latency

      // 3. Return Mock Success
      return { 
        success: true, 
        crmId: `CRM-${Date.now()}` 
      };
    } catch (error) {
      console.error('CRM Sync Failed:', error);
      return { success: false };
    }
  },

  /**
   * Generates a CSV/XML string compatible with LiaArk (Generic ERP format)
   */
  exportToLiaArk: (leads: LeadData[]): string => {
    const header = "Name,Phone,Service,Date,Status,Source\n";
    const rows = leads.map(l => 
      `${l.name},${l.phone},${l.service},${l.date || ''},${l.status},AI-Fitting`
    ).join("\n");
    return header + rows;
  }
};