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);
}
/** سرنخ تازه‌ای که فرم برایش مسئول نوبتی ندارد، مسئول اولین تماس را می‌گیرد. */
/**
* سرنخ تازه‌ای که فرم برایش مسئول نوبتی ندارد، مسئول اولین تماس اتوماسیون را می‌گیرد.
* مالک سازمان به‌عنوان fallback ست نمی‌شود — فقط مسئول صریح فعالیت.
*/
private async syncLeadAssigneeFromActivity(
ctx: AutomationContext,
activityAssigneeId: string | null,
@@ -468,6 +471,7 @@ export class AutomationsService {
ctx: AutomationContext,
ref: StepRef,
): Promise<string | null> {
/** مسئول فعلی رکورد؛ هرگز مالک سازمان را به‌صورت ضمنی اضافه نکن */
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;
}
+17 -9
View File
@@ -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;