Files
CRM_Montzar/apps/api/src/reports/reports.service.ts
T

607 lines
18 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@nestjs/common';
import type { HomeDashboardDto, ReportsDashboardDto } from '@mento/shared';
import { Prisma } from '@prisma/client';
import { PrismaService } from '../prisma/prisma.service';
import {
type Actor,
assigneeWhere,
elevated,
ownerWhere,
ticketAccessWhere,
} from '../common/record-scope';
import { decimalToBase, loadOrgRates } from '../common/currency';
import {
addJalaliMonths,
dateToJalali,
endOfJalaliMonth,
jalaliMonthName,
startOfJalaliMonth,
} from '../common/jalali';
@Injectable()
export class ReportsService {
constructor(private readonly prisma: PrismaService) {}
async dashboard(
actor: Actor,
opts: { from?: string; to?: string },
): Promise<ReportsDashboardDto> {
const { start, end } = resolveRange(opts.from, opts.to);
const orgId = actor.organizationId;
const dealScope = ownerWhere(actor);
const activityScope = assigneeWhere(actor);
const ticketScope = ticketAccessWhere(actor);
const rates = await loadOrgRates(this.prisma, orgId);
const [deals, stages, activities, tickets, peopleCreated, users] =
await Promise.all([
this.prisma.deal.findMany({
where: {
organizationId: orgId,
...dealScope,
OR: [
{ createdAt: { gte: start, lte: end } },
{
status: { in: ['won', 'lost'] },
updatedAt: { gte: start, lte: end },
},
{ status: 'open' },
],
},
select: {
id: true,
status: true,
amount: true,
currency: true,
stageId: true,
ownerId: true,
updatedAt: true,
createdAt: true,
},
}),
this.prisma.pipelineStage.findMany({
where: { pipeline: { organizationId: orgId } },
include: { pipeline: { select: { name: true } } },
orderBy: [{ pipelineId: 'asc' }, { position: 'asc' }],
}),
this.prisma.activity.findMany({
where: {
organizationId: orgId,
...activityScope,
createdAt: { gte: start, lte: end },
},
select: {
id: true,
type: true,
status: true,
dueAt: true,
assigneeId: true,
},
}),
this.prisma.ticket.findMany({
where: {
organizationId: orgId,
...ticketScope,
createdAt: { gte: start, lte: end },
},
select: {
status: true,
slaDueAt: true,
firstRespondedAt: true,
csatScore: true,
},
}),
this.prisma.person.count({
where: {
organizationId: orgId,
createdAt: { gte: start, lte: end },
...(elevated(actor.role) ? {} : { ownerId: actor.userId }),
},
}),
this.prisma.user.findMany({
where: { organizationId: orgId },
select: { id: true, fullName: true },
}),
]);
const openDeals = deals.filter((d) => d.status === 'open');
const wonInPeriod = deals.filter(
(d) =>
d.status === 'won' &&
d.updatedAt >= start &&
d.updatedAt <= end,
);
const lostInPeriod = deals.filter(
(d) =>
d.status === 'lost' &&
d.updatedAt >= start &&
d.updatedAt <= end,
);
const sumBase = (
rows: Array<{ amount: Prisma.Decimal | null; currency: string }>,
) =>
rows.reduce(
(acc, row) => acc + decimalToBase(row.amount, row.currency, rates),
0,
);
const closedCount = wonInPeriod.length + lostInPeriod.length;
const winRate = closedCount ? wonInPeriod.length / closedCount : 0;
const openByStage = new Map<string, { count: number; amount: number }>();
for (const d of openDeals) {
const cur = openByStage.get(d.stageId) ?? { count: 0, amount: 0 };
cur.count += 1;
cur.amount += decimalToBase(d.amount, d.currency, rates);
openByStage.set(d.stageId, cur);
}
const funnel = stages.map((s) => {
const agg = openByStage.get(s.id) ?? { count: 0, amount: 0 };
return {
stageId: s.id,
stageName: s.name,
pipelineName: s.pipeline.name,
count: agg.count,
amount: Math.round(agg.amount),
};
});
const now = new Date();
const activityOpen = activities.filter((a) => a.status === 'open').length;
const activityDone = activities.filter((a) => a.status === 'done').length;
const activityOverdue = activities.filter(
(a) =>
a.status === 'open' &&
a.dueAt != null &&
a.dueAt.getTime() < now.getTime(),
).length;
const byTypeMap = new Map<string, number>();
for (const a of activities) {
byTypeMap.set(a.type, (byTypeMap.get(a.type) ?? 0) + 1);
}
const teamMap = new Map<
string,
{ wonAmount: number; wonCount: number; activitiesDone: number }
>();
for (const u of users) {
teamMap.set(u.id, { wonAmount: 0, wonCount: 0, activitiesDone: 0 });
}
for (const d of wonInPeriod) {
if (!d.ownerId) continue;
const t = teamMap.get(d.ownerId)!;
t.wonCount += 1;
t.wonAmount += decimalToBase(d.amount, d.currency, rates);
}
for (const a of activities) {
if (a.status !== 'done' || !a.assigneeId) continue;
const t = teamMap.get(a.assigneeId);
if (t) t.activitiesDone += 1;
}
const ticketOpen = tickets.filter((t) =>
['open', 'in_progress', 'waiting'].includes(t.status),
).length;
const ticketResolved = tickets.filter(
(t) => t.status === 'resolved',
).length;
const ticketClosed = tickets.filter((t) => t.status === 'closed').length;
const slaBreached = tickets.filter(
(t) =>
t.slaDueAt &&
!t.firstRespondedAt &&
['open', 'in_progress', 'waiting'].includes(t.status) &&
t.slaDueAt.getTime() < now.getTime(),
).length;
const csatScores = tickets
.map((t) => t.csatScore)
.filter((s): s is number => s != null);
const avgCsat = csatScores.length
? csatScores.reduce((a, b) => a + b, 0) / csatScores.length
: null;
return {
from: start.toISOString(),
to: end.toISOString(),
currency: rates.baseCurrency,
sales: {
openCount: openDeals.length,
wonCount: wonInPeriod.length,
lostCount: lostInPeriod.length,
openAmount: Math.round(sumBase(openDeals)),
wonAmount: Math.round(sumBase(wonInPeriod)),
lostAmount: Math.round(sumBase(lostInPeriod)),
winRate: Math.round(winRate * 1000) / 10,
},
funnel,
activities: {
open: activityOpen,
done: activityDone,
overdue: activityOverdue,
byType: [...byTypeMap.entries()].map(([type, count]) => ({
type,
count,
})),
},
team: users
.map((u) => {
const t = teamMap.get(u.id)!;
return {
userId: u.id,
fullName: u.fullName,
wonAmount: Math.round(t.wonAmount),
wonCount: t.wonCount,
activitiesDone: t.activitiesDone,
};
})
.filter(
(u) =>
elevated(actor.role) || u.wonCount > 0 || u.activitiesDone > 0,
)
.sort((a, b) => b.wonAmount - a.wonAmount),
tickets: {
open: ticketOpen,
resolved: ticketResolved,
closed: ticketClosed,
slaBreached,
avgCsat: avgCsat == null ? null : Math.round(avgCsat * 10) / 10,
csatCount: csatScores.length,
},
peopleCreated,
};
}
async home(actor: Actor): Promise<HomeDashboardDto> {
const orgId = actor.organizationId;
const dealScope = ownerWhere(actor);
const activityScope = assigneeWhere(actor);
const personScope = elevated(actor.role) ? {} : { ownerId: actor.userId };
const rates = await loadOrgRates(this.prisma, orgId);
const now = new Date();
const { jy, jm } = dateToJalali(now);
const curMonthStart = startOfJalaliMonth(jy, jm);
const curMonthEnd = endOfJalaliMonth(jy, jm);
const prev = addJalaliMonths(jy, jm, -1);
const prevMonthStart = startOfJalaliMonth(prev.jy, prev.jm);
const prevMonthEnd = endOfJalaliMonth(prev.jy, prev.jm);
const todayStart = startOfDay(now);
const todayEnd = endOfDay(now);
const yesterday = new Date(now);
yesterday.setDate(yesterday.getDate() - 1);
const yesterdayStart = startOfDay(yesterday);
const yesterdayEnd = endOfDay(yesterday);
const weekAgo = new Date(now);
weekAgo.setDate(weekAgo.getDate() - 7);
const [
wonDeals,
openDeals,
staleDeals,
peopleCur,
peoplePrev,
openActivitiesToday,
openActivitiesYesterday,
openDealsWeekAgo,
goals,
overdueCount,
] = await Promise.all([
this.prisma.deal.findMany({
where: {
organizationId: orgId,
status: 'won',
...dealScope,
updatedAt: { gte: addMonthsStart(now, -5) },
},
select: {
amount: true,
currency: true,
updatedAt: true,
source: true,
companyId: true,
personId: true,
company: { select: { id: true, name: true } },
person: { select: { id: true, firstName: true, lastName: true } },
},
}),
this.prisma.deal.count({
where: { organizationId: orgId, status: 'open', ...dealScope },
}),
this.prisma.deal.findMany({
where: {
organizationId: orgId,
status: 'open',
...dealScope,
OR: [
{ isStale: true },
{ isRotten: true },
{ updatedAt: { lt: daysAgo(5) } },
],
},
select: {
id: true,
title: true,
company: { select: { name: true } },
person: { select: { firstName: true, lastName: true } },
},
orderBy: { updatedAt: 'asc' },
take: 5,
}),
this.prisma.person.count({
where: {
organizationId: orgId,
...personScope,
createdAt: { gte: curMonthStart, lte: curMonthEnd },
},
}),
this.prisma.person.count({
where: {
organizationId: orgId,
...personScope,
createdAt: { gte: prevMonthStart, lte: prevMonthEnd },
},
}),
this.prisma.activity.count({
where: {
organizationId: orgId,
...activityScope,
status: 'open',
dueAt: { gte: todayStart, lte: todayEnd },
},
}),
this.prisma.activity.count({
where: {
organizationId: orgId,
...activityScope,
dueAt: { gte: yesterdayStart, lte: yesterdayEnd },
},
}),
this.prisma.deal.count({
where: {
organizationId: orgId,
status: 'open',
...dealScope,
updatedAt: { lte: weekAgo },
},
}),
this.prisma.salesGoal.findMany({
where: {
organizationId: orgId,
periodStart: { lte: curMonthEnd },
periodEnd: { gte: curMonthStart },
},
select: { targetAmount: true },
}),
this.prisma.activity.count({
where: {
organizationId: orgId,
...activityScope,
status: 'open',
dueAt: { lt: now },
},
}),
]);
const sumWonInRange = (start: Date, end: Date) =>
wonDeals
.filter((d) => d.updatedAt >= start && d.updatedAt <= end)
.reduce(
(acc, d) => acc + decimalToBase(d.amount, d.currency, rates),
0,
);
const salesCur = sumWonInRange(curMonthStart, curMonthEnd);
const salesPrev = sumWonInRange(prevMonthStart, prevMonthEnd);
const targetAmount = goals.reduce(
(acc, g) => acc + Number(g.targetAmount),
0,
);
const trendMonths: Array<{ key: string; label: string; start: Date; end: Date }> =
[];
for (let i = 4; i >= 0; i -= 1) {
const m = addJalaliMonths(jy, jm, -i);
trendMonths.push({
key: `${m.jy}-${String(m.jm).padStart(2, '0')}`,
label: jalaliMonthName(m.jm),
start: startOfJalaliMonth(m.jy, m.jm),
end: endOfJalaliMonth(m.jy, m.jm),
});
}
const salesTrend = trendMonths.map((m) => ({
monthKey: m.key,
monthLabel: m.label,
amount: Math.round(sumWonInRange(m.start, m.end)),
}));
const sourceMap = new Map<string, number>();
for (const d of wonDeals) {
const label = normalizeSource(d.source);
sourceMap.set(label, (sourceMap.get(label) ?? 0) + 1);
}
const sourceTotal = [...sourceMap.values()].reduce((a, b) => a + b, 0) || 1;
const sources = [...sourceMap.entries()]
.map(([label, count]) => ({
label,
count,
percent: Math.round((count / sourceTotal) * 1000) / 10,
}))
.sort((a, b) => b.count - a.count)
.slice(0, 6);
const customerMap = new Map<
string,
{ name: string; dealCount: number }
>();
for (const d of wonDeals) {
if (d.company) {
const cur = customerMap.get(`c:${d.company.id}`) ?? {
name: d.company.name,
dealCount: 0,
};
cur.dealCount += 1;
customerMap.set(`c:${d.company.id}`, cur);
} else if (d.person) {
const name = `${d.person.firstName} ${d.person.lastName}`.trim();
const cur = customerMap.get(`p:${d.person.id}`) ?? {
name,
dealCount: 0,
};
cur.dealCount += 1;
customerMap.set(`p:${d.person.id}`, cur);
}
}
const topCustomers = [...customerMap.entries()]
.map(([id, row]) => ({
id: id.slice(2),
name: row.name,
dealCount: row.dealCount,
rank: 0,
}))
.sort((a, b) => b.dealCount - a.dealCount)
.slice(0, 5)
.map((row, index) => ({ ...row, rank: index + 1 }));
const alerts: HomeDashboardDto['alerts'] = [];
if (staleDeals.length) {
const first = staleDeals[0]!;
const who =
first.company?.name ??
(first.person
? `${first.person.firstName} ${first.person.lastName}`.trim()
: first.title);
alerts.push({
key: 'stale-deal',
tone: 'urgent',
text: `${who} در مرحله تصمیم‌گیری است — امروز تماس بگیرید`,
link: '/pipeline',
});
}
if (staleDeals.length > 1) {
alerts.push({
key: 'stale-count',
tone: 'warning',
text: `${staleDeals.length} معامله بیش از ۵ روز پیگیری نشده`,
link: '/pipeline',
});
} else if (overdueCount > 0) {
alerts.push({
key: 'overdue',
tone: 'warning',
text: `${overdueCount} پیگیری عقب‌افتاده دارید`,
link: '/activities',
});
}
if (targetAmount > 0 && salesCur >= targetAmount) {
alerts.push({
key: 'target-ahead',
tone: 'success',
text: 'این ماه از تارگت جلوتر هستید',
link: '/sales/forecast',
});
} else if (targetAmount > 0 && salesCur >= targetAmount * 0.8) {
alerts.push({
key: 'target-close',
tone: 'info',
text: 'به تارگت ماه نزدیک شده‌اید',
link: '/sales/forecast',
});
}
return {
currency: rates.baseCurrency,
kpis: [
{
key: 'sales',
label: 'فروش این ماه',
value: Math.round(salesCur),
changePercent: pctChange(salesCur, salesPrev),
changeLabel: 'نسبت به ماه قبل',
},
{
key: 'customers',
label: 'مشتریان جدید',
value: peopleCur,
changePercent: pctChange(peopleCur, peoplePrev),
changeLabel: 'نسبت به ماه قبل',
},
{
key: 'open-deals',
label: 'معاملات باز',
value: openDeals,
changePercent: pctChange(openDeals, openDealsWeekAgo || openDeals),
changeLabel: 'نسبت به هفته قبل',
},
{
key: 'followups',
label: 'پیگیری امروز',
value: openActivitiesToday,
changePercent: pctChange(openActivitiesToday, openActivitiesYesterday),
changeLabel: 'نسبت به دیروز',
},
],
alerts,
salesTrend,
sources,
topCustomers,
};
}
}
function resolveRange(from?: string, to?: string): { start: Date; end: Date } {
const end = to ? endOfDay(parseDay(to)) : endOfDay(new Date());
const start = from
? startOfDay(parseDay(from))
: startOfDay(new Date(end.getFullYear(), end.getMonth(), 1));
return { start, end };
}
function parseDay(value: string): Date {
const d = new Date(value);
if (Number.isNaN(d.getTime())) return new Date();
return d;
}
function startOfDay(d: Date): Date {
return new Date(d.getFullYear(), d.getMonth(), d.getDate(), 0, 0, 0, 0);
}
function endOfDay(d: Date): Date {
return new Date(d.getFullYear(), d.getMonth(), d.getDate(), 23, 59, 59, 999);
}
function daysAgo(n: number): Date {
const d = new Date();
d.setDate(d.getDate() - n);
return d;
}
function addMonthsStart(from: Date, deltaMonths: number): Date {
const { jy, jm } = dateToJalali(from);
const m = addJalaliMonths(jy, jm, deltaMonths);
return startOfJalaliMonth(m.jy, m.jm);
}
function pctChange(current: number, previous: number): number | null {
if (previous <= 0) return current > 0 ? 100 : null;
return Math.round(((current - previous) / previous) * 1000) / 10;
}
function normalizeSource(raw: string | null | undefined): string {
const value = raw?.trim();
if (!value) return 'سایر';
const map: Record<string, string> = {
referral: 'معرفی دوستان',
website: 'وب‌سایت',
social: 'شبکه‌های اجتماعی',
ads: 'تبلیغات',
instagram: 'شبکه‌های اجتماعی',
google: 'تبلیغات',
};
return map[value.toLowerCase()] ?? value;
}