Stop auto-assigning org owner on form leads; leave unassigned unless chosen.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-11-18 18:50:51 +03:30
co-authored by Cursor
parent c185348c2d
commit 749fca6738
3 changed files with 24 additions and 11 deletions
@@ -416,7 +416,10 @@ export class AutomationsService {
await this.syncLeadAssigneeFromActivity(ctx, type === 'call' ? assigneeId : null); await this.syncLeadAssigneeFromActivity(ctx, type === 'call' ? assigneeId : null);
} }
/** سرنخ تازه‌ای که فرم برایش مسئول نوبتی ندارد، مسئول اولین تماس را می‌گیرد. */ /**
* سرنخ تازه‌ای که فرم برایش مسئول نوبتی ندارد، مسئول اولین تماس اتوماسیون را می‌گیرد.
* مالک سازمان به‌عنوان fallback ست نمی‌شود — فقط مسئول صریح فعالیت.
*/
private async syncLeadAssigneeFromActivity( private async syncLeadAssigneeFromActivity(
ctx: AutomationContext, ctx: AutomationContext,
activityAssigneeId: string | null, activityAssigneeId: string | null,
@@ -468,6 +471,7 @@ export class AutomationsService {
ctx: AutomationContext, ctx: AutomationContext,
ref: StepRef, ref: StepRef,
): Promise<string | null> { ): Promise<string | null> {
/** مسئول فعلی رکورد؛ هرگز مالک سازمان را به‌صورت ضمنی اضافه نکن */
const fallback = ctx.assigneeId || ctx.ownerId || null; const fallback = ctx.assigneeId || ctx.ownerId || null;
const mode = config.assignMode ?? (config.assigneeId ? 'user' : 'owner'); const mode = config.assignMode ?? (config.assigneeId ? 'user' : 'owner');
@@ -502,6 +506,7 @@ export class AutomationsService {
return pickRoundRobin(valid, cursor.counter) ?? fallback; return pickRoundRobin(valid, cursor.counter) ?? fallback;
} }
// mode === 'owner' → مسئول فعلی رکورد؛ اگر نباشد فعالیت بدون مسئول می‌ماند
return fallback; return fallback;
} }
+17 -9
View File
@@ -298,14 +298,20 @@ export class LeadFormsService {
mapped.notes = [mapped.notes, utmNote].filter(Boolean).join('\n'); 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 = const actorId =
ownerId ?? formAssigneeId ??
( (
await this.prisma.user.findFirst({ await this.prisma.user.findFirst({
where: { where: {
memberships: { memberships: {
some: { organizationId: form.organizationId, role: 'owner' }, some: {
organizationId: form.organizationId,
role: { in: ['owner', 'admin'] },
isActive: true,
},
}, },
}, },
select: { id: true }, select: { id: true },
@@ -324,7 +330,7 @@ export class LeadFormsService {
let message: string | null = null; let message: string | null = null;
if (form.createLead) { 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; leadId = result.leadId;
status = result.status; status = result.status;
leadCreated = result.created; leadCreated = result.created;
@@ -344,12 +350,12 @@ export class LeadFormsService {
personId = await this.upsertPerson( personId = await this.upsertPerson(
form.organizationId, form.organizationId,
mapped, mapped,
ownerId ?? actorId, formAssigneeId ?? actorId,
); );
} }
if (form.createDeal) { 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) { } else if (personId && mapped.notes && !form.createLead) {
await this.prisma.person.update({ await this.prisma.person.update({
where: { id: personId }, where: { id: personId },
@@ -394,7 +400,9 @@ export class LeadFormsService {
entityId: personId ?? leadId ?? form.id, entityId: personId ?? leadId ?? form.id,
title, title,
personId, personId,
ownerId: ownerId ?? actorId, /** فقط مسئول صریح فرم؛ مالک سازمان به‌عنوان fallback ست نمی‌شود */
ownerId: formAssigneeId,
assigneeId: formAssigneeId,
}; };
await this.automations.runTrigger('lead_submitted', { await this.automations.runTrigger('lead_submitted', {
...leadCtx, ...leadCtx,
@@ -402,7 +410,7 @@ export class LeadFormsService {
sourceId: form.id, sourceId: form.id,
sourceType: 'lead_form', sourceType: 'lead_form',
leadId, leadId,
syncLeadAssignee: !ownerId && leadCreated && !!leadId, syncLeadAssignee: !formAssigneeId && leadCreated && !!leadId,
mobile: mapped.mobile ?? mapped.phone ?? null, mobile: mapped.mobile ?? mapped.phone ?? null,
formName: form.name, formName: form.name,
formValues: values, formValues: values,
@@ -437,7 +445,7 @@ export class LeadFormsService {
private async upsertLead( private async upsertLead(
form: FormRow, form: FormRow,
mapped: MappedValues, mapped: MappedValues,
assigneeId: string, assigneeId: string | null,
actorId: string, actorId: string,
): Promise<{ ): Promise<{
leadId: string | null; leadId: string | null;
+1 -1
View File
@@ -855,7 +855,7 @@ onMounted(load);
{{ u.fullName }} {{ u.fullName }}
</button> </button>
</div> </div>
<p v-if="!assigneeIds.length" class="settings-empty">مسئولی انتخاب نشده. سرنخ جدید مسئولش همان مسئول اولین تماس اتوماسیون می‌شود؛ اگر تماسی ساخته نشود، مالک سازمان مسئول می‌ماند.</p> <p v-if="!assigneeIds.length" class="settings-empty">مسئولی انتخاب نشده. سرنخ جدید بدون مسئول ساخته می‌شود؛ اگر اتوماسیون تماس بسازد و مسئول مشخص داشته باشد، همان فرد روی لید هم ست می‌شود. مالک سازمان می‌تواند همهٔ لیدها را ببیند و کارشناس تعیین کند.</p>
</article> </article>
<article class="settings-card optional"> <article class="settings-card optional">