skailan-crm
Version:
Servicio de CRM y gestión de ventas para Skailan
40 lines • 1.59 kB
JavaScript
export class GetSalesForecast {
opportunityRepository;
constructor(opportunityRepository) {
this.opportunityRepository = opportunityRepository;
}
async execute(request) {
const { organizationId } = request;
const opportunities = await this.opportunityRepository.findAll(organizationId);
let totalPotentialRevenue = 0;
let weightedRevenue = 0;
const opportunitiesByStage = {};
opportunities.forEach((opportunity) => {
totalPotentialRevenue += opportunity.amount || 0;
weightedRevenue +=
(opportunity.amount || 0) * ((opportunity.probability || 0) / 100);
if (!opportunitiesByStage[opportunity.stage]) {
opportunitiesByStage[opportunity.stage] = { count: 0, totalAmount: 0 };
}
const stageData = opportunitiesByStage[opportunity.stage];
if (stageData) {
stageData.count++;
stageData.totalAmount += opportunity.amount || 0;
}
});
const formattedOpportunitiesByStage = Object.keys(opportunitiesByStage).map((stage) => {
const stageData = opportunitiesByStage[stage];
return {
stage,
count: stageData?.count || 0,
totalAmount: stageData?.totalAmount || 0,
};
});
return {
totalPotentialRevenue,
weightedRevenue,
opportunitiesByStage: formattedOpportunitiesByStage,
};
}
}
//# sourceMappingURL=GetSalesForecast.js.map