ifixkart-admin/services/api/crmService.ts

160 lines
4.3 KiB
TypeScript

import { apiFetch } from './client';
export interface CrmCustomer {
customer_id: string;
tenant_id: string;
first_name?: string;
last_name?: string;
name?: string;
email?: string;
phone: string;
company_name?: string;
gst_number?: string;
total_spent?: number;
total_orders_count?: number;
total_repairs_count?: number;
sales_tier: string;
service_tier: string;
is_dual_customer?: boolean;
created_at: string;
}
export interface CrmLead {
lead_id: string;
tenant_id: string;
title?: string;
first_name?: string;
last_name?: string;
email?: string;
phone?: string;
source: string;
status: string;
score?: number;
notes?: string;
quote_amount?: number;
quote_title?: string;
quote_terms?: string;
quote_sent_at?: string;
quote_sent_by?: string;
created_at: string;
}
export interface CrmDeal {
deal_id: string;
tenant_id: string;
customer_id?: string;
title: string;
value?: number;
estimated_value?: number;
stage: string;
expected_close_date?: string;
notes?: string;
created_at: string;
}
export interface ConvertLeadPayload {
category: 'ECOMMERCE' | 'SERVICE' | 'BOTH';
deal_title?: string;
estimated_value?: number;
customer_name?: string;
customer_email?: string;
customer_phone?: string;
notes?: string;
}
// --- PHONE NORMALIZATION & DISPLAY HELPERS ---
export function normalizeIndianPhone(phone: string): string {
if (!phone) return '';
const digits = phone.replace(/\D/g, '');
if (digits.length === 10) {
return `91${digits}`;
}
if (digits.length === 12 && digits.startsWith('91')) {
return digits;
}
// Fallback return digits if non-standard
return digits;
}
export function formatPhoneDisplay(phone?: string): string {
if (!phone) return '—';
const digits = phone.replace(/\D/g, '');
const mobile = digits.length === 12 && digits.startsWith('91') ? digits.slice(2) : digits;
return `+91 ${mobile}`;
}
export interface QuoteMessageParams {
name?: string;
customerName?: string;
phone?: string;
customerPhone?: string;
title?: string;
quoteTitle?: string;
amount: number | string;
terms?: string;
}
export function buildQuotationWhatsAppUrl({ name, customerName, phone, customerPhone, title, quoteTitle, amount, terms }: QuoteMessageParams): string {
const targetPhone = customerPhone || phone || '';
const waPhone = normalizeIndianPhone(targetPhone);
const targetName = customerName || name || 'Customer';
const targetTitle = quoteTitle || title || 'Quotation Estimate';
const formattedAmount = Number(amount).toLocaleString('en-IN');
const message = `Hello ${targetName},\n\nHere is your official iFixKart quotation.\n\nItem: ${targetTitle}\nAmount: ₹${formattedAmount}\nTerms: ${terms || 'Standard Warranty'}\n\nThank you!`;
return `/whatsapp-web?phone=${waPhone}&name=${encodeURIComponent(targetName)}&message=${encodeURIComponent(message)}`;
}
export const crmService = {
normalizeIndianPhone,
formatPhoneDisplay,
buildQuotationWhatsAppUrl,
getDeals: async (stage?: string) => {
const query = stage ? `?stage=${encodeURIComponent(stage)}` : '';
return apiFetch<CrmDeal[]>(`/api/v1/crm/deals${query}`);
},
createDeal: async (data: Partial<CrmDeal>) => {
return apiFetch<CrmDeal>('/api/v1/crm/deals', {
method: 'POST',
body: JSON.stringify(data),
});
},
getLeads: async (status?: string) => {
const query = status ? `?status=${encodeURIComponent(status)}` : '';
return apiFetch<CrmLead[]>(`/api/v1/crm/leads${query}`);
},
createLead: async (data: Partial<CrmLead>) => {
return apiFetch<CrmLead>('/api/v1/crm/leads', {
method: 'POST',
body: JSON.stringify(data),
});
},
updateLeadStatus: async (leadId: string, status: string, notes?: string, extraFields?: Partial<CrmLead>) => {
return apiFetch<CrmLead>(`/api/v1/crm/leads/${leadId}`, {
method: 'PATCH',
body: JSON.stringify({ status, notes, ...extraFields }),
});
},
convertLead: async (leadId: string, payload: ConvertLeadPayload) => {
return apiFetch<{ lead: CrmLead; customer: CrmCustomer; deal: CrmDeal; is_dual_customer: boolean }>(
`/api/v1/crm/leads/${leadId}/convert`,
{
method: 'POST',
body: JSON.stringify(payload),
}
);
},
getCustomers: async () => {
return apiFetch<CrmCustomer[]>('/api/v1/crm/customers');
}
};