import { Injectable } from '@nestjs/common'; import { HttpService } from '@nestjs/axios'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { Lead } from './lead.entity';
@Injectable() export class CrmService { constructor(@InjectRepository(Lead) private leads: Repository<Lead>, private http: HttpService) {} async create(data: Pick<Lead, 'name'|'phone'|'email'>) { const lead = await this.leads.save({ ...data, source: 'website' }); const url = process.env.WHATSAPP_API_URL; if (url && process.env.WHATSAPP_TOKEN) this.http.post(url, { to: lead.phone, type: 'text', text: { body: `Halo ${lead.name}, terima kasih sudah menghubungi RodaKita.` } }, { headers: { Authorization: `Bearer ${process.env.WHATSAPP_TOKEN}` } }).subscribe(); return lead; } list() { return this.leads.find({ order: { createdAt: 'DESC' } }); } }

