skailan-crm
Version:
Servicio de CRM y gestión de ventas para Skailan
311 lines • 11.1 kB
JavaScript
import { CreateLead } from "../../app/use-cases/CreateLead";
import { GetLeadById } from "../../app/use-cases/GetLeadById";
import { ListLeads } from "../../app/use-cases/ListLeads";
import { UpdateLead } from "../../app/use-cases/UpdateLead";
import { DeleteLead } from "../../app/use-cases/DeleteLead";
import { SendLeadFollowUpEmail } from "../../app/use-cases/SendLeadFollowUpEmail";
import { StartConversationWithLead } from "../../app/use-cases/StartConversationWithLead";
import { GetLeadSummaryByStatus } from "../../app/use-cases/GetLeadSummaryByStatus";
import { LeadPrismaRepository } from "../../infra/database/prisma/LeadPrismaRepository";
import { ContactIntegrationServiceImpl } from "../../domain/services/ContactIntegrationService";
import { SyncLeadWithContact } from "../../app/use-cases/SyncLeadWithContact";
export const createLead = async (req, res) => {
try {
const { name, email, phone, source, notes, company, title, industry } = req.body;
const organizationId = req.organization?.id;
if (!organizationId) {
return res
.status(400)
.json({ error: "Organization ID not found in request." });
}
const prisma = req.tenantPrisma;
if (!prisma) {
return res
.status(500)
.json({ error: "Prisma client not initialized for tenant." });
}
const leadRepository = new LeadPrismaRepository(prisma);
const contactIntegrationService = new ContactIntegrationServiceImpl();
const createLeadUseCase = new CreateLead(leadRepository, contactIntegrationService);
const newLead = await createLeadUseCase.execute({
organizationId,
name,
email,
phone,
source,
notes,
company,
title,
industry,
});
res.status(201).json(newLead);
}
catch (error) {
res.status(500).json({ error: error.message || "Error creating lead." });
}
};
export const getLeadById = async (req, res) => {
try {
const { id } = req.params;
const organizationId = req.organization?.id;
if (!organizationId) {
return res
.status(400)
.json({ error: "Organization ID not found in request." });
}
if (!id) {
return res.status(400).json({ error: "Lead ID is required." });
}
const prisma = req.tenantPrisma;
if (!prisma) {
return res
.status(500)
.json({ error: "Prisma client not initialized for tenant." });
}
const leadRepository = new LeadPrismaRepository(prisma);
const getLeadByIdUseCase = new GetLeadById(leadRepository);
const lead = await getLeadByIdUseCase.execute({
id,
organizationId,
});
if (!lead) {
return res.status(404).json({ error: "Lead not found." });
}
res.status(200).json(lead);
}
catch (error) {
res.status(500).json({ error: error.message || "Error retrieving lead." });
}
};
export const listLeads = async (req, res) => {
try {
const organizationId = req.organization?.id;
if (!organizationId) {
return res
.status(400)
.json({ error: "Organization ID not found in request." });
}
const prisma = req.tenantPrisma;
if (!prisma) {
return res
.status(500)
.json({ error: "Prisma client not initialized for tenant." });
}
const leadRepository = new LeadPrismaRepository(prisma);
const listLeadsUseCase = new ListLeads(leadRepository);
const leads = await listLeadsUseCase.execute({
organizationId,
});
res.status(200).json(leads);
}
catch (error) {
res.status(500).json({ error: error.message || "Error listing leads." });
}
};
export const updateLead = async (req, res) => {
try {
const { id } = req.params;
const { name, email, phone, status, source, notes, company, title, industry, score, } = req.body;
const organizationId = req.organization?.id;
if (!organizationId) {
return res
.status(400)
.json({ error: "Organization ID not found in request." });
}
if (!id) {
return res.status(400).json({ error: "Lead ID is required." });
}
const prisma = req.tenantPrisma;
if (!prisma) {
return res
.status(500)
.json({ error: "Prisma client not initialized for tenant." });
}
const leadRepository = new LeadPrismaRepository(prisma);
const updateLeadUseCase = new UpdateLead(leadRepository);
const updatedLead = await updateLeadUseCase.execute({
id,
organizationId,
name,
email,
phone,
status,
source,
notes,
company,
title,
industry,
score,
});
res.status(200).json(updatedLead);
}
catch (error) {
res.status(500).json({ error: error.message || "Error updating lead." });
}
};
export const deleteLead = async (req, res) => {
try {
const { id } = req.params;
const organizationId = req.organization?.id;
if (!organizationId) {
return res
.status(400)
.json({ error: "Organization ID not found in request." });
}
if (!id) {
return res.status(400).json({ error: "Lead ID is required." });
}
const prisma = req.tenantPrisma;
if (!prisma) {
return res
.status(500)
.json({ error: "Prisma client not initialized for tenant." });
}
const leadRepository = new LeadPrismaRepository(prisma);
const deleteLeadUseCase = new DeleteLead(leadRepository);
await deleteLeadUseCase.execute({
id,
organizationId,
});
res.status(204).send();
}
catch (error) {
res.status(500).json({ error: error.message || "Error deleting lead." });
}
};
export const sendLeadFollowUpEmail = async (req, res) => {
try {
const { id } = req.params;
const { subject, message } = req.body;
const organizationId = req.organization?.id;
if (!organizationId) {
return res
.status(400)
.json({ error: "Organization ID not found in request." });
}
if (!id) {
return res.status(400).json({ error: "Lead ID is required." });
}
const prisma = req.tenantPrisma;
if (!prisma) {
return res
.status(500)
.json({ error: "Prisma client not initialized for tenant." });
}
const leadRepository = new LeadPrismaRepository(prisma);
const sendLeadFollowUpEmailUseCase = new SendLeadFollowUpEmail(leadRepository);
await sendLeadFollowUpEmailUseCase.execute({
organizationId,
leadId: id,
subject,
body: message,
});
res.status(200).json({ message: "Follow-up email sent successfully." });
}
catch (error) {
res
.status(500)
.json({ error: error.message || "Error sending follow-up email." });
}
};
export const startConversationWithLead = async (req, res) => {
try {
const { id } = req.params;
const { channelId } = req.body;
const organizationId = req.organization?.id;
if (!organizationId) {
return res
.status(400)
.json({ error: "Organization ID not found in request." });
}
if (!id) {
return res.status(400).json({ error: "Lead ID is required." });
}
const prisma = req.tenantPrisma;
if (!prisma) {
return res
.status(500)
.json({ error: "Prisma client not initialized for tenant." });
}
const leadRepository = new LeadPrismaRepository(prisma);
const startConversationWithLeadUseCase = new StartConversationWithLead(leadRepository);
const conversation = await startConversationWithLeadUseCase.execute({
organizationId,
leadId: id,
channelId,
});
res.status(201).json(conversation);
}
catch (error) {
res
.status(500)
.json({
error: error.message || "Error starting conversation with lead.",
});
}
};
export const getLeadSummaryByStatus = async (req, res) => {
try {
const organizationId = req.organization?.id;
if (!organizationId) {
return res
.status(400)
.json({ error: "Organization ID not found in request." });
}
const prisma = req.tenantPrisma;
if (!prisma) {
return res
.status(500)
.json({ error: "Prisma client not initialized for tenant." });
}
const leadRepository = new LeadPrismaRepository(prisma);
const getLeadSummaryByStatusUseCase = new GetLeadSummaryByStatus(leadRepository);
const summary = await getLeadSummaryByStatusUseCase.execute({
organizationId,
});
res.status(200).json(summary);
}
catch (error) {
res
.status(500)
.json({
error: error.message || "Error getting lead summary by status.",
});
}
};
export const syncLeadWithContact = async (req, res) => {
try {
const { id } = req.params;
const organizationId = req.organization?.id;
if (!organizationId) {
return res
.status(400)
.json({ error: "Organization ID not found in request." });
}
if (!id) {
return res.status(400).json({ error: "Lead ID is required." });
}
const prisma = req.tenantPrisma;
if (!prisma) {
return res
.status(500)
.json({ error: "Prisma client not initialized for tenant." });
}
const leadRepository = new LeadPrismaRepository(prisma);
const contactIntegrationService = new ContactIntegrationServiceImpl();
const syncLeadWithContactUseCase = new SyncLeadWithContact(leadRepository, contactIntegrationService);
const result = await syncLeadWithContactUseCase.execute({
leadId: id,
organizationId,
});
res.status(200).json(result);
}
catch (error) {
res
.status(500)
.json({
error: error.message || "Error syncing lead with contact.",
});
}
};
//# sourceMappingURL=leadController.js.map