Speed up lead board load and polish lead detail/list UX.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -595,7 +595,9 @@ export class LeadFormsService {
|
||||
};
|
||||
}
|
||||
|
||||
const stages = await this.leadStages.list(form.organizationId);
|
||||
const stages = await this.leadStages.list(form.organizationId, {
|
||||
withLeadCounts: false,
|
||||
});
|
||||
const stageId =
|
||||
(form.leadStageId && stages.find((s) => s.id === form.leadStageId)?.id) ||
|
||||
stages.find((s) => !s.isConverted && !s.isLost && !s.isUnqualified)?.id ||
|
||||
|
||||
@@ -43,18 +43,26 @@ export class LeadStagesService {
|
||||
});
|
||||
}
|
||||
|
||||
async list(organizationId: string): Promise<LeadStageDto[]> {
|
||||
async list(
|
||||
organizationId: string,
|
||||
opts: { withLeadCounts?: boolean } = {},
|
||||
): Promise<LeadStageDto[]> {
|
||||
const withLeadCounts = opts.withLeadCounts !== false;
|
||||
await this.ensureDefaults(organizationId);
|
||||
const rows = await this.prisma.leadStage.findMany({
|
||||
where: { organizationId },
|
||||
orderBy: { position: 'asc' },
|
||||
include: {
|
||||
_count: {
|
||||
select: {
|
||||
leads: { where: { archivedAt: null } },
|
||||
},
|
||||
},
|
||||
},
|
||||
...(withLeadCounts
|
||||
? {
|
||||
include: {
|
||||
_count: {
|
||||
select: {
|
||||
leads: { where: { archivedAt: null } },
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
});
|
||||
return rows.map((r) => this.toDto(r));
|
||||
}
|
||||
|
||||
@@ -181,7 +181,9 @@ export class LeadsImportService {
|
||||
const job = await this.prisma.leadImportJob.findUnique({ where: { id } });
|
||||
if (!job) return;
|
||||
|
||||
const stages = await this.stages.list(job.organizationId);
|
||||
const stages = await this.stages.list(job.organizationId, {
|
||||
withLeadCounts: false,
|
||||
});
|
||||
const defaultStageId = stages[0]?.id;
|
||||
if (!defaultStageId) {
|
||||
await this.prisma.leadImportJob.update({
|
||||
|
||||
@@ -70,8 +70,13 @@ export class LeadsController {
|
||||
}
|
||||
|
||||
@Get('stages')
|
||||
listStages(@CurrentUser() user: Actor) {
|
||||
return this.stages.list(user.organizationId);
|
||||
listStages(
|
||||
@CurrentUser() user: Actor,
|
||||
@Query('counts') counts?: string,
|
||||
) {
|
||||
return this.stages.list(user.organizationId, {
|
||||
withLeadCounts: counts !== '0' && counts !== 'false',
|
||||
});
|
||||
}
|
||||
|
||||
@Post('stages')
|
||||
|
||||
@@ -104,7 +104,38 @@ const leadInclude = {
|
||||
assignee: { select: { id: true, fullName: true } },
|
||||
} as const;
|
||||
|
||||
/** فیلدهای لازم برای کارت مسیر لیدها — بدون notes/addresses حجیم و بدون join مرحله */
|
||||
const boardLeadSelect = {
|
||||
id: true,
|
||||
title: true,
|
||||
contactName: true,
|
||||
gender: true,
|
||||
showGenderInName: true,
|
||||
mobile: true,
|
||||
phone: true,
|
||||
city: true,
|
||||
region: true,
|
||||
instagram: true,
|
||||
birthDate: true,
|
||||
trade: true,
|
||||
source: true,
|
||||
logoStorageKey: true,
|
||||
estimatedValue: true,
|
||||
stageId: true,
|
||||
position: true,
|
||||
assigneeId: true,
|
||||
lastContactAt: true,
|
||||
nextFollowUpAt: true,
|
||||
convertedPersonId: true,
|
||||
convertedAt: true,
|
||||
archivedAt: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
assignee: { select: { id: true, fullName: true } },
|
||||
} as const;
|
||||
|
||||
type LeadRow = Prisma.LeadGetPayload<{ include: typeof leadInclude }>;
|
||||
type BoardLeadRow = Prisma.LeadGetPayload<{ select: typeof boardLeadSelect }>;
|
||||
|
||||
@Injectable()
|
||||
export class LeadsService {
|
||||
@@ -338,33 +369,56 @@ export class LeadsService {
|
||||
}
|
||||
|
||||
async board(actor: Actor): Promise<LeadBoardColumnDto[]> {
|
||||
const stages = await this.stages.list(actor.organizationId);
|
||||
// بدون شمارش لید روی هر مرحله — شمارش را یکبار با groupBy میگیریم
|
||||
const stages = await this.stages.list(actor.organizationId, {
|
||||
withLeadCounts: false,
|
||||
});
|
||||
const boardStages = stages.filter((s) => s.showOnBoard);
|
||||
const whereBase = {
|
||||
if (!boardStages.length) return [];
|
||||
|
||||
const access = leadAccessWhere(actor);
|
||||
const stageIds = boardStages.map((s) => s.id);
|
||||
const whereBase: Prisma.LeadWhereInput = {
|
||||
organizationId: actor.organizationId,
|
||||
...leadAccessWhere(actor),
|
||||
...access,
|
||||
archivedAt: null,
|
||||
stageId: { in: stageIds },
|
||||
};
|
||||
const perColumn = 120;
|
||||
return Promise.all(
|
||||
boardStages.map(async (stage) => {
|
||||
const where = { ...whereBase, stageId: stage.id };
|
||||
const [total, rows] = await Promise.all([
|
||||
this.prisma.lead.count({ where }),
|
||||
this.prisma.lead.findMany({
|
||||
where,
|
||||
include: leadInclude,
|
||||
orderBy: [{ position: 'asc' }, { updatedAt: 'desc' }],
|
||||
take: perColumn,
|
||||
}),
|
||||
]);
|
||||
return {
|
||||
stage,
|
||||
total,
|
||||
leads: rows.map((row) => this.toDto(row)),
|
||||
};
|
||||
|
||||
const [counts, ...columnRows] = await Promise.all([
|
||||
this.prisma.lead.groupBy({
|
||||
by: ['stageId'],
|
||||
where: whereBase,
|
||||
_count: { _all: true },
|
||||
}),
|
||||
...boardStages.map((stage) =>
|
||||
this.prisma.lead.findMany({
|
||||
where: {
|
||||
organizationId: actor.organizationId,
|
||||
...access,
|
||||
archivedAt: null,
|
||||
stageId: stage.id,
|
||||
},
|
||||
// کارت بورد به join مرحله نیاز ندارد؛ اسم مرحله را از ستون میگذاریم
|
||||
select: boardLeadSelect,
|
||||
orderBy: [{ position: 'asc' }, { updatedAt: 'desc' }],
|
||||
take: perColumn,
|
||||
}),
|
||||
),
|
||||
]);
|
||||
|
||||
const totalByStage = new Map(
|
||||
counts.map((row) => [row.stageId, row._count._all]),
|
||||
);
|
||||
|
||||
return boardStages.map((stage, index) => ({
|
||||
stage,
|
||||
total: totalByStage.get(stage.id) ?? 0,
|
||||
leads: (columnRows[index] ?? []).map((row) =>
|
||||
this.toBoardDto(row, stage.name),
|
||||
),
|
||||
}));
|
||||
}
|
||||
|
||||
async get(actor: Actor, id: string): Promise<LeadDto & { events: LeadEventDto[] }> {
|
||||
@@ -459,7 +513,9 @@ export class LeadsService {
|
||||
);
|
||||
}
|
||||
|
||||
const stages = await this.stages.list(actor.organizationId);
|
||||
const stages = await this.stages.list(actor.organizationId, {
|
||||
withLeadCounts: false,
|
||||
});
|
||||
const stageId = dto.stageId || stages[0]?.id;
|
||||
if (!stageId) throw new BadRequestException('مرحلهای تعریف نشده');
|
||||
|
||||
@@ -1232,7 +1288,9 @@ export class LeadsService {
|
||||
...leadAccessWhere(actor),
|
||||
archivedAt: null,
|
||||
};
|
||||
const stages = await this.stages.list(actor.organizationId);
|
||||
const stages = await this.stages.list(actor.organizationId, {
|
||||
withLeadCounts: false,
|
||||
});
|
||||
const convertedIds = stages.filter((s) => s.isConverted).map((s) => s.id);
|
||||
const lostIds = stages.filter((s) => s.isLost).map((s) => s.id);
|
||||
// همان مقصد فرم و ورود از اکسل: اولین ستون باز، مستقل از اسم «جدید»
|
||||
@@ -1374,7 +1432,9 @@ export class LeadsService {
|
||||
}
|
||||
|
||||
await this.stages.ensureDefaults(input.organizationId);
|
||||
const stages = await this.stages.list(input.organizationId);
|
||||
const stages = await this.stages.list(input.organizationId, {
|
||||
withLeadCounts: false,
|
||||
});
|
||||
const stageId = stages[0]?.id;
|
||||
if (!stageId) throw new BadRequestException('مرحلهای تعریف نشده');
|
||||
|
||||
@@ -1586,6 +1646,48 @@ export class LeadsService {
|
||||
};
|
||||
}
|
||||
|
||||
private toBoardDto(row: BoardLeadRow, stageName: string): LeadDto {
|
||||
const daysInStage = Math.max(
|
||||
0,
|
||||
Math.floor(
|
||||
(Date.now() - row.updatedAt.getTime()) / (24 * 60 * 60 * 1000),
|
||||
),
|
||||
);
|
||||
return {
|
||||
id: row.id,
|
||||
title: row.title,
|
||||
contactName: row.contactName,
|
||||
gender: row.gender === 'male' || row.gender === 'female' ? row.gender : 'none',
|
||||
showGenderInName: row.showGenderInName,
|
||||
mobile: row.mobile,
|
||||
phone: row.phone,
|
||||
city: row.city,
|
||||
region: row.region,
|
||||
instagram: row.instagram,
|
||||
birthDate: row.birthDate?.toISOString() ?? null,
|
||||
addresses: [],
|
||||
trade: row.trade,
|
||||
source: row.source,
|
||||
notes: null,
|
||||
estimatedValue:
|
||||
row.estimatedValue != null ? Number(row.estimatedValue) : null,
|
||||
stageId: row.stageId,
|
||||
stageName,
|
||||
position: row.position,
|
||||
assigneeId: row.assigneeId,
|
||||
assigneeName: row.assignee?.fullName ?? null,
|
||||
lastContactAt: row.lastContactAt?.toISOString() ?? null,
|
||||
nextFollowUpAt: row.nextFollowUpAt?.toISOString() ?? null,
|
||||
convertedPersonId: row.convertedPersonId,
|
||||
convertedAt: row.convertedAt?.toISOString() ?? null,
|
||||
archivedAt: row.archivedAt?.toISOString() ?? null,
|
||||
daysInStage,
|
||||
hasLogo: Boolean(row.logoStorageKey),
|
||||
createdAt: row.createdAt.toISOString(),
|
||||
updatedAt: row.updatedAt.toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
async saveLogo(
|
||||
actor: Actor,
|
||||
id: string,
|
||||
|
||||
Reference in New Issue
Block a user