From c185348c2dc80d0f4f7c6f43d01b033b82aa1b53 Mon Sep 17 00:00:00 2001 From: Mahdi Date: Wed, 18 Nov 2026 18:18:52 +0330 Subject: [PATCH] Require a stage-move reason and record it on the lead timeline. Co-authored-by: Cursor --- apps/api/src/leads/dto.ts | 4 + apps/api/src/leads/leads.controller.ts | 4 +- apps/api/src/leads/leads.service.ts | 31 +++++++- apps/web/src/views/LeadBoardView.vue | 102 +++++++++++++++++++++++-- apps/web/src/views/LeadDetailView.vue | 88 +++++++++++++++++++-- apps/web/src/views/LeadsView.vue | 66 +++++++++++++++- packages/shared/src/index.ts | 6 ++ 7 files changed, 278 insertions(+), 23 deletions(-) diff --git a/apps/api/src/leads/dto.ts b/apps/api/src/leads/dto.ts index ed8fd9a..0255333 100644 --- a/apps/api/src/leads/dto.ts +++ b/apps/api/src/leads/dto.ts @@ -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 { diff --git a/apps/api/src/leads/leads.controller.ts b/apps/api/src/leads/leads.controller.ts index ffa4310..9ff01f2 100644 --- a/apps/api/src/leads/leads.controller.ts +++ b/apps/api/src/leads/leads.controller.ts @@ -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') diff --git a/apps/api/src/leads/leads.service.ts b/apps/api/src/leads/leads.service.ts index d40104d..15e0b89 100644 --- a/apps/api/src/leads/leads.service.ts +++ b/apps/api/src/leads/leads.service.ts @@ -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 { 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 { + 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, diff --git a/apps/web/src/views/LeadBoardView.vue b/apps/web/src/views/LeadBoardView.vue index 39ff8e6..4ff1e5a 100644 --- a/apps/web/src/views/LeadBoardView.vue +++ b/apps/web/src/views/LeadBoardView.vue @@ -17,6 +17,7 @@ import { Space as ASpace, Spin as ASpin, Tag as ATag, + Textarea as ATextarea, Typography, theme, } from 'ant-design-vue'; @@ -61,6 +62,17 @@ const addLost = ref(false); const addUnqualified = ref(false); const deleteTarget = ref(null); const moveConfirm = ref<{ stageIds: string[]; message: string } | null>(null); +const pendingLeadMove = ref<{ + leadId: string; + stageId: string; + position: number; + fromStageName: string; + toStageName: string; + prev: LeadBoardColumnDto[]; + nextColumns: LeadBoardColumnDto[]; +} | null>(null); +const stageMoveReason = ref(''); +const stageMoveError = ref(''); const draggingId = ref(null); const insertAt = ref(null); const bodyEls = new Map(); @@ -520,7 +532,7 @@ async function onDrop(stageId: string, event: DragEvent, index?: number) { draggingId.value = null; dropStageId.value = null; clearDragUi(); - if (!leadId || moving.value) return; + if (!leadId || moving.value || pendingLeadMove.value) return; const fromCol = columns.value.find((c) => c.leads.some((l) => l.id === leadId), @@ -546,7 +558,7 @@ async function onDrop(stageId: string, event: DragEvent, index?: number) { leads: [...c.leads], })); - columns.value = columns.value.map((c) => { + const nextColumns = columns.value.map((c) => { if (same && c.stage.id === stageId) return { ...c, leads: nextLeads }; if (c.stage.id === fromCol.stage.id) { return { @@ -561,13 +573,66 @@ async function onDrop(stageId: string, event: DragEvent, index?: number) { return c; }); + // جابه‌جایی داخل همان ستون: بدون دلیل + if (same) { + columns.value = nextColumns; + moving.value = true; + error.value = ''; + try { + await api.patch(`/leads/${leadId}/move`, { stageId, position: insertIndex }); + } catch { + columns.value = prev; + error.value = 'جابه‌جایی لید ناموفق بود.'; + } finally { + moving.value = false; + } + return; + } + + // تغییر ستون: اول دلیل، بعد اعمال + stageMoveReason.value = ''; + stageMoveError.value = ''; + pendingLeadMove.value = { + leadId, + stageId, + position: insertIndex, + fromStageName: fromCol.stage.name, + toStageName: target.stage.name, + prev, + nextColumns, + }; +} + +function cancelPendingLeadMove() { + pendingLeadMove.value = null; + stageMoveReason.value = ''; + stageMoveError.value = ''; +} + +async function confirmPendingLeadMove() { + const pending = pendingLeadMove.value; + if (!pending || moving.value) return Promise.reject(); + const reason = stageMoveReason.value.trim(); + if (!reason) { + stageMoveError.value = 'دلیل تغییر ستون را بنویسید.'; + return Promise.reject(); + } + columns.value = pending.nextColumns; moving.value = true; error.value = ''; + stageMoveError.value = ''; try { - await api.patch(`/leads/${leadId}/move`, { stageId, position: insertIndex }); - } catch { - columns.value = prev; - error.value = 'جابه‌جایی لید ناموفق بود.'; + await api.patch(`/leads/${pending.leadId}/move`, { + stageId: pending.stageId, + position: pending.position, + reason, + }); + cancelPendingLeadMove(); + } catch (e) { + columns.value = pending.prev; + stageMoveError.value = apiMessage(e, 'جابه‌جایی لید ناموفق بود.'); + error.value = stageMoveError.value; + return Promise.reject(e); } finally { moving.value = false; } @@ -969,6 +1034,31 @@ onMounted(load); > + + + + + انتقال از «{{ pendingLeadMove.fromStageName }}» به «{{ pendingLeadMove.toStageName }}» + + + + + diff --git a/apps/web/src/views/LeadDetailView.vue b/apps/web/src/views/LeadDetailView.vue index f3a130f..df756be 100644 --- a/apps/web/src/views/LeadDetailView.vue +++ b/apps/web/src/views/LeadDetailView.vue @@ -97,6 +97,9 @@ const users = ref([]); const activities = ref([]); const stageId = ref(''); +const stageMoveOpen = ref(false); +const stageMoveReason = ref(''); +const stageMoveError = ref(''); const assigneeId = ref(''); const callOutcome = ref(''); const callBody = ref(''); @@ -270,18 +273,48 @@ async function saveEdit() { } } -async function changeStage() { +function openStageMove() { if (!stageId.value || stageId.value === lead.value?.stageId) return; + stageMoveReason.value = ''; + stageMoveError.value = ''; + stageMoveOpen.value = true; +} + +function cancelStageMove() { + stageMoveOpen.value = false; + stageMoveReason.value = ''; + stageMoveError.value = ''; + if (lead.value) stageId.value = lead.value.stageId; +} + +async function changeStage() { + if (!stageId.value || stageId.value === lead.value?.stageId) { + return Promise.reject(); + } + const reason = stageMoveReason.value.trim(); + if (!reason) { + stageMoveError.value = 'دلیل تغییر ستون را بنویسید.'; + return Promise.reject(); + } busy.value = true; error.value = ''; + stageMoveError.value = ''; try { - await api.patch(`/leads/${id.value}/move`, { stageId: stageId.value }); + await api.patch(`/leads/${id.value}/move`, { + stageId: stageId.value, + reason, + }); okMsg.value = 'وضعیت به‌روز شد.'; message.success(okMsg.value); moreOpen.value = false; + stageMoveOpen.value = false; + stageMoveReason.value = ''; await load(); } catch (e) { - error.value = apiError(e, 'تغییر وضعیت ناموفق بود.'); + const msg = apiError(e, 'تغییر وضعیت ناموفق بود.'); + stageMoveError.value = msg; + error.value = msg; + return Promise.reject(e); } finally { busy.value = false; } @@ -438,10 +471,15 @@ function openTel() { const feed = computed(() => { const rows: Array<{ key: string; title: string; by: string; at: string }> = []; for (const ev of lead.value?.events ?? []) { + const body = ev.body?.trim(); rows.push({ key: `ev-${ev.id}`, title: ev.title, - by: ev.body?.trim() || 'توسط سیستم', + by: body + ? ev.type === 'stage_changed' + ? `دلیل: ${body}` + : body + : 'توسط سیستم', at: ev.createdAt, }); } @@ -866,7 +904,7 @@ onBeforeUnmount(() => narrowMq?.removeEventListener('change', syncNarrow)); - {{ ev.body }} + {{ ev.body }} @@ -936,7 +974,12 @@ onBeforeUnmount(() => narrowMq?.removeEventListener('change', syncNarrow)); :options="stages.map((s) => ({ value: s.id, label: s.name }))" /> - + اعمال وضعیت @@ -1082,7 +1125,13 @@ onBeforeUnmount(() => narrowMq?.removeEventListener('change', syncNarrow)); :options="stages.map((s) => ({ value: s.id, label: s.name }))" /> - اعمال وضعیت + + اعمال وضعیت + بایگانی @@ -2450,5 +2477,36 @@ onBeforeUnmount(() => { + + + + + انتقال {{ toFaDigits(selectedIds.length) }} لید به + «{{ stages.find((s) => s.id === bulkStageId)?.name || '—' }}» + + + + + diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts index 8577989..29c5290 100644 --- a/packages/shared/src/index.ts +++ b/packages/shared/src/index.ts @@ -6356,6 +6356,8 @@ export interface UpdateLeadRequest { notes?: string | null; estimatedValue?: number | null; stageId?: string; + /** الزامی هنگام تغییر ستون؛ در جدول زمانی لید ثبت می‌شود */ + reason?: string | null; assigneeId?: string | null; nextFollowUpAt?: string | null; lastContactAt?: string | null; @@ -6364,6 +6366,8 @@ export interface UpdateLeadRequest { export interface MoveLeadRequest { stageId: string; position?: number; + /** الزامی هنگام تغییر ستون؛ در جدول زمانی لید ثبت می‌شود */ + reason?: string | null; } export interface AssignLeadsRequest { @@ -6374,6 +6378,8 @@ export interface AssignLeadsRequest { export interface BulkLeadStageRequest { leadIds: string[]; stageId: string; + /** الزامی؛ در جدول زمانی هر لید ثبت می‌شود */ + reason?: string | null; } export interface AutoAssignLeadsRequest {