Require a stage-move reason and record it on the lead timeline.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-11-18 18:18:52 +03:30
co-authored by Cursor
parent cd51439353
commit c185348c2d
7 changed files with 278 additions and 23 deletions
+27 -4
View File
@@ -673,17 +673,21 @@ export class LeadsService {
});
if (typeof dto.stageId === 'string' && dto.stageId !== existing.stageId) {
const reason = this.requireStageReason(
typeof dto.reason === 'string' ? dto.reason : null,
);
await this.addEvent(
actor,
id,
'stage_changed',
`وضعیت به «${row.stage.name}» تغییر کرد`,
null,
reason,
{
fromStageId: existing.stageId,
fromStageName: existing.stage?.name ?? null,
toStageId: row.stageId,
toStageName: row.stage.name,
reason,
},
);
this.gameEvent(actor, 'LEAD_STAGE_CHANGED', row, {
@@ -699,6 +703,7 @@ export class LeadsService {
id: string,
stageId: string,
position?: number,
reasonRaw?: string | null,
): Promise<LeadDto> {
const before = await this.findAccessible(actor, id);
const stage = await this.prisma.leadStage.findFirst({
@@ -706,6 +711,9 @@ export class LeadsService {
});
if (!stage) throw new NotFoundException('مرحله پیدا نشد');
const stageChanged = before.stageId !== stageId;
const reason = stageChanged ? this.requireStageReason(reasonRaw) : null;
let placedIds: string[] | null = null;
let nextPosition = position ?? 0;
if (position != null) {
@@ -736,18 +744,19 @@ export class LeadsService {
this.prisma.lead.update({ where: { id: leadId }, data: { position: leadPosition } }),
);
}
if (before.stageId !== stageId) {
if (stageChanged && reason) {
await this.addEvent(
actor,
id,
'stage_changed',
`به مرحله «${stage.name}» منتقل شد`,
null,
reason,
{
fromStageId: before.stageId,
fromStageName: before.stage?.name ?? null,
toStageId: stageId,
toStageName: stage.name,
reason,
},
);
this.gameEvent(actor, 'LEAD_STAGE_CHANGED', row, {
@@ -799,7 +808,9 @@ export class LeadsService {
actor: Actor,
leadIds: string[],
stageId: string,
reasonRaw?: string | null,
): Promise<number> {
const reason = this.requireStageReason(reasonRaw);
if (!elevated(actor.role)) {
throw new BadRequestException('فقط مدیر می‌تواند وضعیت گروهی تغییر دهد');
}
@@ -834,12 +845,13 @@ export class LeadsService {
lead.id,
'stage_changed',
`وضعیت گروهی به «${stage.name}»`,
null,
reason,
{
fromStageId: lead.stageId,
fromStageName: lead.stage.name,
toStageId: stageId,
toStageName: stage.name,
reason,
},
);
}
@@ -1500,6 +1512,17 @@ export class LeadsService {
return row;
}
private requireStageReason(raw: string | null | undefined): string {
const reason = String(raw ?? '').trim();
if (!reason) {
throw new BadRequestException('دلیل تغییر ستون الزامی است.');
}
if (reason.length > 1000) {
throw new BadRequestException('دلیل تغییر ستون نباید بیش از ۱۰۰۰ کاراکتر باشد.');
}
return reason;
}
private async addEvent(
actor: Actor,
leadId: string,