Add optional activity experience on leads and people with form mapping and filters.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-11-19 21:04:43 +03:30
co-authored by Cursor
parent e90f9f9538
commit a455ab71ab
19 changed files with 430 additions and 20 deletions
+16
View File
@@ -100,6 +100,12 @@ export class ListQueryDto {
@IsString()
@MaxLength(2000)
groupId?: string;
/** فیلتر سابقه فعالیت؛ چندتایی با کاما */
@IsOptional()
@IsString()
@MaxLength(400)
activityExperience?: string;
}
class CompanyExtraFieldsDto {
@@ -360,6 +366,11 @@ export class CreatePersonDto {
@MaxLength(120)
referrer?: string;
@IsOptional()
@IsString()
@MaxLength(80)
activityExperience?: string | null;
@IsOptional()
@IsArray()
@IsString({ each: true })
@@ -486,6 +497,11 @@ export class UpdatePersonDto {
@MaxLength(120)
referrer?: string;
@IsOptional()
@IsString()
@MaxLength(80)
activityExperience?: string | null;
@IsOptional()
@IsArray()
@IsString({ each: true })
@@ -63,6 +63,7 @@ export class PeopleController {
hasMobile,
disc: query.disc,
groupId: query.groupId,
activityExperience: query.activityExperience,
},
);
}
@@ -81,6 +82,7 @@ export class PeopleController {
hasMobile,
disc: query.disc,
groupId: query.groupId,
activityExperience: query.activityExperience,
});
}
+18
View File
@@ -23,6 +23,7 @@ import {
PERSON_MOODS,
applyPersonGenderTitle,
canViewAllRecords,
normalizeActivityExperience,
splitPersonHonorific,
} from '@mento/shared';
import { Prisma } from '@prisma/client';
@@ -102,6 +103,7 @@ type PersonRow = {
instagram: string | null;
birthDate: Date | null;
referrer: string | null;
activityExperience: string | null;
addresses: unknown;
notes: string | null;
mood: PersonMood;
@@ -174,6 +176,7 @@ export class PeopleService implements OnModuleInit {
hasMobile?: boolean;
disc?: string;
groupId?: string;
activityExperience?: string;
},
): Prisma.PersonWhereInput {
const sources = [...new Set(splitCsv(opts?.importSource).map(normalizePersonImportSource))];
@@ -181,6 +184,7 @@ export class PeopleService implements OnModuleInit {
(PERSON_MOODS as readonly string[]).includes(m),
);
const groupIds = splitCsv(opts?.groupId);
const experiences = splitCsv(opts?.activityExperience);
const search = buildSearchWhere<Prisma.PersonWhereInput>(q, (t) => [
{ firstName: ci(t) },
{ lastName: ci(t) },
@@ -231,6 +235,9 @@ export class PeopleService implements OnModuleInit {
if (sources.length) {
facets.push({ importSource: { in: sources } });
}
if (experiences.length) {
facets.push({ activityExperience: { in: experiences } });
}
if (facets.length === 1) and.push(facets[0]!);
else if (facets.length > 1) and.push({ OR: facets });
return {
@@ -258,6 +265,7 @@ export class PeopleService implements OnModuleInit {
hasMobile?: boolean;
disc?: string;
groupId?: string;
activityExperience?: string;
},
): Promise<PaginatedResponse<PersonDto>> {
const seeDisc = await this.behaviorAccess.canView(actor);
@@ -296,6 +304,7 @@ export class PeopleService implements OnModuleInit {
hasMobile?: boolean;
disc?: string;
groupId?: string;
activityExperience?: string;
},
max = FILTERED_IDS_MAX,
): Promise<FilteredIdsResponse> {
@@ -475,6 +484,9 @@ export class PeopleService implements OnModuleInit {
instagram: emptyToNull(dto.instagram),
birthDate: parseBirthDate(dto.birthDate),
referrer: emptyToNull(dto.referrer),
activityExperience:
normalizeActivityExperience(dto.activityExperience) ??
emptyToNull(dto.activityExperience),
addresses: toJsonAddresses(dto.addresses),
notes: emptyToNull(dto.notes),
mood: dto.mood ?? 'newcomer',
@@ -587,6 +599,11 @@ export class PeopleService implements OnModuleInit {
if ('instagram' in dto) data.instagram = emptyToNull(dto.instagram);
if ('birthDate' in dto) data.birthDate = parseBirthDate(dto.birthDate);
if ('referrer' in dto) data.referrer = emptyToNull(dto.referrer);
if ('activityExperience' in dto) {
data.activityExperience =
normalizeActivityExperience(dto.activityExperience) ??
emptyToNull(dto.activityExperience);
}
if ('addresses' in dto) data.addresses = toJsonAddresses(dto.addresses);
if ('notes' in dto) data.notes = emptyToNull(dto.notes);
if (dto.mood !== undefined) data.mood = dto.mood;
@@ -1022,6 +1039,7 @@ export class PeopleService implements OnModuleInit {
instagram: row.instagram,
birthDate: row.birthDate?.toISOString() ?? null,
referrer: row.referrer,
activityExperience: row.activityExperience,
addresses: (() => {
const fromCol = asAddresses(row.addresses);
if (fromCol.length) return fromCol;
+2
View File
@@ -30,9 +30,11 @@ const MAP_TO = [
'lead.city',
'lead.region',
'lead.trade',
'lead.activityExperience',
'lead.notes',
'lead.source',
'lead.gender',
'person.activityExperience',
'person.gender',
'notes',
'custom',
+31 -2
View File
@@ -13,7 +13,12 @@ import type {
SubmitLeadFormResponse,
UpdateLeadFormRequest,
} from '@mento/shared';
import { isGenderQuestionLabel, parsePersonGenderAnswer } from '@mento/shared';
import {
isActivityExperienceLabel,
isGenderQuestionLabel,
normalizeActivityExperience,
parsePersonGenderAnswer,
} from '@mento/shared';
import { Prisma } from '@prisma/client';
import { PrismaService } from '../prisma/prisma.service';
import { LocalFileStorage } from '../storage/local-file.storage';
@@ -569,6 +574,9 @@ export class LeadFormsService {
region: emptyToNull(mapped.region) ?? existingLead.region,
trade: emptyToNull(mapped.trade) ?? existingLead.trade,
source: source ?? existingLead.source,
activityExperience:
emptyToNull(mapped.activityExperience) ??
existingLead.activityExperience,
notes: emptyToNull(mapped.notes) ?? existingLead.notes,
...(mapped.gender ? { gender: mapped.gender } : {}),
assigneeId: existingLead.assigneeId ?? assigneeId,
@@ -620,6 +628,7 @@ export class LeadFormsService {
region: emptyToNull(mapped.region),
trade: emptyToNull(mapped.trade),
source,
activityExperience: emptyToNull(mapped.activityExperience),
notes: emptyToNull(mapped.notes),
gender: mapped.gender ?? 'none',
stageId,
@@ -684,6 +693,8 @@ export class LeadFormsService {
phone: phone ?? existing.phone,
position: emptyToNull(mapped.position) ?? existing.position,
notes: emptyToNull(mapped.notes) ?? existing.notes,
activityExperience:
emptyToNull(mapped.activityExperience) ?? existing.activityExperience,
...(mapped.gender ? { gender: mapped.gender } : {}),
ownerId: existing.ownerId ?? ownerId,
},
@@ -701,6 +712,7 @@ export class LeadFormsService {
phone,
position: emptyToNull(mapped.position),
notes: emptyToNull(mapped.notes),
activityExperience: emptyToNull(mapped.activityExperience),
gender: mapped.gender ?? 'none',
ownerId,
},
@@ -964,6 +976,7 @@ type MappedValues = {
city?: string;
region?: string;
trade?: string;
activityExperience?: string;
leadSource?: string;
gender?: 'male' | 'female' | 'none';
};
@@ -995,6 +1008,15 @@ function mapValues(
if (gender) mapped.gender = gender;
continue;
}
if (
field.mapTo === 'lead.activityExperience' ||
field.mapTo === 'person.activityExperience' ||
isActivityExperienceLabel(field.label)
) {
mapped.activityExperience =
normalizeActivityExperience(raw) ?? raw;
continue;
}
switch (field.mapTo) {
case 'person.fullName': {
const parts = raw.split(/\s+/).filter(Boolean);
@@ -1048,6 +1070,11 @@ function mapValues(
case 'lead.trade':
mapped.trade = raw;
break;
case 'lead.activityExperience':
case 'person.activityExperience':
mapped.activityExperience =
normalizeActivityExperience(raw) ?? raw;
break;
case 'lead.notes':
mapped.notes = raw;
break;
@@ -1118,7 +1145,9 @@ function collectExtras(
for (const field of fields) {
if (field.fieldType === 'heading' || field.mapTo !== 'custom') continue;
const raw = (values?.[field.key] ?? '').toString().trim();
if (!raw || (isGenderQuestionLabel(field.label) && parsePersonGenderAnswer(raw))) continue;
if (!raw) continue;
if (isGenderQuestionLabel(field.label) && parsePersonGenderAnswer(raw)) continue;
if (isActivityExperienceLabel(field.label)) continue;
lines.push(`${field.label}: ${raw}`);
}
return lines.join('\n');
+2
View File
@@ -37,6 +37,7 @@ export class CreateLeadDto {
@IsOptional() @IsArray() @IsString({ each: true }) addresses?: string[];
@IsOptional() @IsString() trade?: string;
@IsOptional() @IsString() source?: string;
@IsOptional() @IsString() activityExperience?: string | null;
@IsOptional() @IsString() notes?: string;
@IsOptional() @IsNumber() estimatedValue?: number;
@IsOptional() @IsString() stageId?: string;
@@ -60,6 +61,7 @@ export class UpdateLeadDto {
@IsOptional() @IsArray() @IsString({ each: true }) addresses?: string[];
@IsOptional() @IsString() trade?: string | null;
@IsOptional() @IsString() source?: string | null;
@IsOptional() @IsString() activityExperience?: string | null;
@IsOptional() @IsString() notes?: string | null;
@IsOptional() @IsNumber() estimatedValue?: number | null;
@IsOptional() @IsString() stageId?: string;
+4
View File
@@ -249,6 +249,7 @@ export class LeadsController {
@Query('region') region?: string,
@Query('trade') trade?: string,
@Query('source') source?: string,
@Query('activityExperience') activityExperience?: string,
@Query('unassigned') unassigned?: string,
@Query('archived') archived?: string,
@Query('converted') converted?: string,
@@ -265,6 +266,7 @@ export class LeadsController {
region,
trade,
source,
activityExperience,
unassigned: unassigned === '1' || unassigned === 'true',
archived: archived === '1' || archived === 'true',
converted: converted === '1' || converted === 'true',
@@ -287,6 +289,7 @@ export class LeadsController {
@Query('region') region?: string,
@Query('trade') trade?: string,
@Query('source') source?: string,
@Query('activityExperience') activityExperience?: string,
@Query('unassigned') unassigned?: string,
@Query('archived') archived?: string,
@Query('converted') converted?: string,
@@ -306,6 +309,7 @@ export class LeadsController {
region,
trade,
source,
activityExperience,
unassigned: unassigned === '1' || unassigned === 'true',
archived: archived === '1' || archived === 'true',
converted: converted === '1' || converted === 'true',
+44 -6
View File
@@ -20,7 +20,11 @@ import type {
PaginatedResponse,
} from '@mento/shared';
import { FILTERED_IDS_MAX } from '@mento/shared';
import { applyLeadGenderName, applyPersonGenderTitle } from '@mento/shared';
import {
applyLeadGenderName,
applyPersonGenderTitle,
normalizeActivityExperience,
} from '@mento/shared';
import { PrismaService } from '../prisma/prisma.service';
import { SmsGatewayService } from '../sms-billing/sms-gateway.service';
import { renderSmsText } from '../common/sms-text';
@@ -119,6 +123,7 @@ const boardLeadSelect = {
birthDate: true,
trade: true,
source: true,
activityExperience: true,
logoStorageKey: true,
estimatedValue: true,
stageId: true,
@@ -177,6 +182,7 @@ export class LeadsService {
region?: string;
trade?: string;
source?: string;
activityExperience?: string;
unassigned?: boolean;
archived?: boolean;
converted?: boolean;
@@ -196,6 +202,7 @@ export class LeadsService {
{ trade: ci(t) },
{ instagram: ci(t) },
{ source: ci(t) },
{ activityExperience: ci(t) },
]);
const and: Prisma.LeadWhereInput[] = [];
if (search) and.push(search);
@@ -222,6 +229,9 @@ export class LeadsService {
...(opts.source
? { source: { contains: opts.source, mode: 'insensitive' } }
: {}),
...(opts.activityExperience
? { activityExperience: opts.activityExperience }
: {}),
},
...(opts.unassigned ? [{ assigneeId: null }] : []),
...(opts.assigneeId && !opts.unassigned ? [{ assigneeId: opts.assigneeId }] : []),
@@ -288,6 +298,7 @@ export class LeadsService {
region?: string;
trade?: string;
source?: string;
activityExperience?: string;
unassigned?: boolean;
archived?: boolean;
converted?: boolean;
@@ -341,6 +352,7 @@ export class LeadsService {
region?: string;
trade?: string;
source?: string;
activityExperience?: string;
unassigned?: boolean;
archived?: boolean;
converted?: boolean;
@@ -492,6 +504,7 @@ export class LeadsService {
addresses?: string[];
trade?: string | null;
source?: string | null;
activityExperience?: string | null;
notes?: string | null;
estimatedValue?: number | null;
stageId?: string;
@@ -553,6 +566,9 @@ export class LeadsService {
addresses: toJsonAddresses(dto.addresses),
trade: emptyToNull(dto.trade),
source: emptyToNull(dto.source),
activityExperience:
normalizeActivityExperience(dto.activityExperience) ??
emptyToNull(dto.activityExperience),
notes: emptyToNull(dto.notes),
estimatedValue:
dto.estimatedValue != null
@@ -684,6 +700,13 @@ export class LeadsService {
...(dto.source !== undefined
? { source: emptyToNull(dto.source as string) }
: {}),
...(dto.activityExperience !== undefined
? {
activityExperience:
normalizeActivityExperience(dto.activityExperience as string | null) ??
emptyToNull(dto.activityExperience as string),
}
: {}),
...(dto.notes !== undefined
? { notes: emptyToNull(dto.notes as string) }
: {}),
@@ -1194,6 +1217,7 @@ export class LeadsService {
]
.filter(Boolean)
.join('\n'),
activityExperience: lead.activityExperience,
ownerId: lead.assigneeId ?? actor.userId,
createdAt: lead.createdAt,
importSource: 'lead_form',
@@ -1201,11 +1225,23 @@ export class LeadsService {
},
});
createdPerson = true;
} else if (!person.birthDate && lead.birthDate) {
await this.prisma.person.update({
where: { id: person.id },
data: { birthDate: lead.birthDate },
});
} else {
const personPatch: {
birthDate?: Date | null;
activityExperience?: string | null;
} = {};
if (!person.birthDate && lead.birthDate) {
personPatch.birthDate = lead.birthDate;
}
if (!person.activityExperience && lead.activityExperience) {
personPatch.activityExperience = lead.activityExperience;
}
if (Object.keys(personPatch).length) {
await this.prisma.person.update({
where: { id: person.id },
data: personPatch,
});
}
}
const convertedStage = await this.prisma.leadStage.findFirst({
@@ -1626,6 +1662,7 @@ export class LeadsService {
addresses: asAddresses(row.addresses),
trade: row.trade,
source: row.source,
activityExperience: row.activityExperience,
notes: row.notes,
estimatedValue:
row.estimatedValue != null ? Number(row.estimatedValue) : null,
@@ -1668,6 +1705,7 @@ export class LeadsService {
addresses: [],
trade: row.trade,
source: row.source,
activityExperience: row.activityExperience,
notes: null,
estimatedValue:
row.estimatedValue != null ? Number(row.estimatedValue) : null,