Require a stage-move reason and record it on the lead timeline.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -63,6 +63,8 @@ export class UpdateLeadDto {
|
||||
@IsOptional() @IsString() notes?: string | null;
|
||||
@IsOptional() @IsNumber() estimatedValue?: number | null;
|
||||
@IsOptional() @IsString() stageId?: string;
|
||||
/** الزامی هنگام تغییر ستون؛ در جدول زمانی لید ثبت میشود */
|
||||
@IsOptional() @IsString() @MaxLength(1000) reason?: string | null;
|
||||
@IsOptional() @IsString() assigneeId?: string | null;
|
||||
@IsOptional() @IsString() nextFollowUpAt?: string | null;
|
||||
@IsOptional() @IsString() lastContactAt?: string | null;
|
||||
@@ -71,6 +73,7 @@ export class UpdateLeadDto {
|
||||
export class MoveLeadDto {
|
||||
@IsString() stageId!: string;
|
||||
@IsOptional() @IsNumber() position?: number;
|
||||
@IsOptional() @IsString() @MaxLength(1000) reason?: string | null;
|
||||
}
|
||||
|
||||
export class AssignLeadsDto {
|
||||
@@ -81,6 +84,7 @@ export class AssignLeadsDto {
|
||||
export class BulkLeadStageDto {
|
||||
@IsArray() @IsString({ each: true }) leadIds!: string[];
|
||||
@IsString() stageId!: string;
|
||||
@IsOptional() @IsString() @MaxLength(1000) reason?: string | null;
|
||||
}
|
||||
|
||||
export class AutoAssignLeadsDto {
|
||||
|
||||
@@ -329,7 +329,7 @@ export class LeadsController {
|
||||
@Roles('owner', 'admin')
|
||||
bulkStage(@CurrentUser() user: Actor, @Body() dto: BulkLeadStageDto) {
|
||||
return this.leads
|
||||
.bulkStage(user, dto.leadIds, dto.stageId)
|
||||
.bulkStage(user, dto.leadIds, dto.stageId, dto.reason)
|
||||
.then((count) => ({ count }));
|
||||
}
|
||||
|
||||
@@ -393,7 +393,7 @@ export class LeadsController {
|
||||
@Param('id') id: string,
|
||||
@Body() dto: MoveLeadDto,
|
||||
) {
|
||||
return this.leads.move(user, id, dto.stageId, dto.position);
|
||||
return this.leads.move(user, id, dto.stageId, dto.position, dto.reason);
|
||||
}
|
||||
|
||||
@Get(':id/sms-history')
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user