diff --git a/apps/api/src/automations/automations.service.ts b/apps/api/src/automations/automations.service.ts index ebbfd36..4d91cba 100644 --- a/apps/api/src/automations/automations.service.ts +++ b/apps/api/src/automations/automations.service.ts @@ -416,7 +416,10 @@ export class AutomationsService { await this.syncLeadAssigneeFromActivity(ctx, type === 'call' ? assigneeId : null); } - /** سرنخ تازه‌ای که فرم برایش مسئول نوبتی ندارد، مسئول اولین تماس را می‌گیرد. */ + /** + * سرنخ تازه‌ای که فرم برایش مسئول نوبتی ندارد، مسئول اولین تماس اتوماسیون را می‌گیرد. + * مالک سازمان به‌عنوان fallback ست نمی‌شود — فقط مسئول صریح فعالیت. + */ private async syncLeadAssigneeFromActivity( ctx: AutomationContext, activityAssigneeId: string | null, @@ -468,6 +471,7 @@ export class AutomationsService { ctx: AutomationContext, ref: StepRef, ): Promise { + /** مسئول فعلی رکورد؛ هرگز مالک سازمان را به‌صورت ضمنی اضافه نکن */ const fallback = ctx.assigneeId || ctx.ownerId || null; const mode = config.assignMode ?? (config.assigneeId ? 'user' : 'owner'); @@ -502,6 +506,7 @@ export class AutomationsService { return pickRoundRobin(valid, cursor.counter) ?? fallback; } + // mode === 'owner' → مسئول فعلی رکورد؛ اگر نباشد فعالیت بدون مسئول می‌ماند return fallback; } diff --git a/apps/api/src/lead-forms/lead-forms.service.ts b/apps/api/src/lead-forms/lead-forms.service.ts index 28f9fd6..ec38073 100644 --- a/apps/api/src/lead-forms/lead-forms.service.ts +++ b/apps/api/src/lead-forms/lead-forms.service.ts @@ -298,14 +298,20 @@ export class LeadFormsService { mapped.notes = [mapped.notes, utmNote].filter(Boolean).join('\n'); } - const ownerId = await this.nextAssignee(form); + /** فقط از لیست صریح «توزیع نوبتی»؛ بدون fallback به مالک سازمان */ + const formAssigneeId = await this.nextAssignee(form); + /** فقط برای createdBy / رویدادها — مسئول لید نمی‌شود */ const actorId = - ownerId ?? + formAssigneeId ?? ( await this.prisma.user.findFirst({ where: { memberships: { - some: { organizationId: form.organizationId, role: 'owner' }, + some: { + organizationId: form.organizationId, + role: { in: ['owner', 'admin'] }, + isActive: true, + }, }, }, select: { id: true }, @@ -324,7 +330,7 @@ export class LeadFormsService { let message: string | null = null; if (form.createLead) { - const result = await this.upsertLead(form, mapped, ownerId ?? actorId, actorId); + const result = await this.upsertLead(form, mapped, formAssigneeId, actorId); leadId = result.leadId; status = result.status; leadCreated = result.created; @@ -344,12 +350,12 @@ export class LeadFormsService { personId = await this.upsertPerson( form.organizationId, mapped, - ownerId ?? actorId, + formAssigneeId ?? actorId, ); } if (form.createDeal) { - await this.createDeal(form, mapped, personId, actorId, ownerId); + await this.createDeal(form, mapped, personId, actorId, formAssigneeId); } else if (personId && mapped.notes && !form.createLead) { await this.prisma.person.update({ where: { id: personId }, @@ -394,7 +400,9 @@ export class LeadFormsService { entityId: personId ?? leadId ?? form.id, title, personId, - ownerId: ownerId ?? actorId, + /** فقط مسئول صریح فرم؛ مالک سازمان به‌عنوان fallback ست نمی‌شود */ + ownerId: formAssigneeId, + assigneeId: formAssigneeId, }; await this.automations.runTrigger('lead_submitted', { ...leadCtx, @@ -402,7 +410,7 @@ export class LeadFormsService { sourceId: form.id, sourceType: 'lead_form', leadId, - syncLeadAssignee: !ownerId && leadCreated && !!leadId, + syncLeadAssignee: !formAssigneeId && leadCreated && !!leadId, mobile: mapped.mobile ?? mapped.phone ?? null, formName: form.name, formValues: values, @@ -437,7 +445,7 @@ export class LeadFormsService { private async upsertLead( form: FormRow, mapped: MappedValues, - assigneeId: string, + assigneeId: string | null, actorId: string, ): Promise<{ leadId: string | null; diff --git a/apps/web/src/views/LeadFormBuilderView.vue b/apps/web/src/views/LeadFormBuilderView.vue index 733aad0..fd2a22d 100644 --- a/apps/web/src/views/LeadFormBuilderView.vue +++ b/apps/web/src/views/LeadFormBuilderView.vue @@ -855,7 +855,7 @@ onMounted(load); {{ u.fullName }} -

مسئولی انتخاب نشده. سرنخ جدید مسئولش همان مسئول اولین تماس اتوماسیون می‌شود؛ اگر تماسی ساخته نشود، مالک سازمان مسئول می‌ماند.

+

مسئولی انتخاب نشده. سرنخ جدید بدون مسئول ساخته می‌شود؛ اگر اتوماسیون تماس بسازد و مسئول مشخص داشته باشد، همان فرد روی لید هم ست می‌شود. مالک سازمان می‌تواند همهٔ لیدها را ببیند و کارشناس تعیین کند.