Backfill activity experience from form notes so list and filters work.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -23,6 +23,7 @@ import {
|
|||||||
PERSON_MOODS,
|
PERSON_MOODS,
|
||||||
applyPersonGenderTitle,
|
applyPersonGenderTitle,
|
||||||
canViewAllRecords,
|
canViewAllRecords,
|
||||||
|
extractActivityExperienceFromNotes,
|
||||||
normalizeActivityExperience,
|
normalizeActivityExperience,
|
||||||
splitPersonHonorific,
|
splitPersonHonorific,
|
||||||
} from '@mento/shared';
|
} from '@mento/shared';
|
||||||
@@ -184,7 +185,13 @@ export class PeopleService implements OnModuleInit {
|
|||||||
(PERSON_MOODS as readonly string[]).includes(m),
|
(PERSON_MOODS as readonly string[]).includes(m),
|
||||||
);
|
);
|
||||||
const groupIds = splitCsv(opts?.groupId);
|
const groupIds = splitCsv(opts?.groupId);
|
||||||
const experiences = splitCsv(opts?.activityExperience);
|
const experiences = [
|
||||||
|
...new Set(
|
||||||
|
splitCsv(opts?.activityExperience)
|
||||||
|
.map((v) => normalizeActivityExperience(v) ?? v)
|
||||||
|
.filter(Boolean),
|
||||||
|
),
|
||||||
|
];
|
||||||
const search = buildSearchWhere<Prisma.PersonWhereInput>(q, (t) => [
|
const search = buildSearchWhere<Prisma.PersonWhereInput>(q, (t) => [
|
||||||
{ firstName: ci(t) },
|
{ firstName: ci(t) },
|
||||||
{ lastName: ci(t) },
|
{ lastName: ci(t) },
|
||||||
@@ -378,6 +385,38 @@ export class PeopleService implements OnModuleInit {
|
|||||||
|
|
||||||
onModuleInit() {
|
onModuleInit() {
|
||||||
void this.backfillStoredBirthDates();
|
void this.backfillStoredBirthDates();
|
||||||
|
void this.backfillActivityExperienceFromNotes();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** سابقه فعالیت قدیمی که فقط در یادداشت بود، روی ستون مشتری مینشیند. */
|
||||||
|
async backfillActivityExperienceFromNotes() {
|
||||||
|
try {
|
||||||
|
const rows = await this.prisma.person.findMany({
|
||||||
|
where: {
|
||||||
|
activityExperience: null,
|
||||||
|
notes: { contains: 'سابقه' },
|
||||||
|
},
|
||||||
|
select: { id: true, notes: true },
|
||||||
|
take: 20000,
|
||||||
|
});
|
||||||
|
let filled = 0;
|
||||||
|
for (const row of rows) {
|
||||||
|
const value = extractActivityExperienceFromNotes(row.notes);
|
||||||
|
if (!value) continue;
|
||||||
|
const updated = await this.prisma.person.updateMany({
|
||||||
|
where: { id: row.id, activityExperience: null },
|
||||||
|
data: { activityExperience: value },
|
||||||
|
});
|
||||||
|
filled += updated.count;
|
||||||
|
}
|
||||||
|
if (filled) {
|
||||||
|
this.logger.log(`سابقه فعالیت ${filled} مشتری از یادداشت پر شد`);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
this.logger.warn(
|
||||||
|
`پر کردن سابقه فعالیت مشتریان ناموفق بود: ${err instanceof Error ? err.message : err}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** تاریخ تولدی که قبلاً در فیلد اضافی دیدار/اکسل یا پروفایل مونتیفای مانده، روی شخص مینشیند. */
|
/** تاریخ تولدی که قبلاً در فیلد اضافی دیدار/اکسل یا پروفایل مونتیفای مانده، روی شخص مینشیند. */
|
||||||
@@ -1039,7 +1078,9 @@ export class PeopleService implements OnModuleInit {
|
|||||||
instagram: row.instagram,
|
instagram: row.instagram,
|
||||||
birthDate: row.birthDate?.toISOString() ?? null,
|
birthDate: row.birthDate?.toISOString() ?? null,
|
||||||
referrer: row.referrer,
|
referrer: row.referrer,
|
||||||
activityExperience: row.activityExperience,
|
activityExperience:
|
||||||
|
row.activityExperience ||
|
||||||
|
extractActivityExperienceFromNotes(row.notes),
|
||||||
addresses: (() => {
|
addresses: (() => {
|
||||||
const fromCol = asAddresses(row.addresses);
|
const fromCol = asAddresses(row.addresses);
|
||||||
if (fromCol.length) return fromCol;
|
if (fromCol.length) return fromCol;
|
||||||
|
|||||||
@@ -2,8 +2,10 @@ import {
|
|||||||
BadRequestException,
|
BadRequestException,
|
||||||
ConflictException,
|
ConflictException,
|
||||||
Injectable,
|
Injectable,
|
||||||
|
Logger,
|
||||||
NotFoundException,
|
NotFoundException,
|
||||||
ServiceUnavailableException,
|
ServiceUnavailableException,
|
||||||
|
type OnModuleInit,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { Prisma } from '@prisma/client';
|
import { Prisma } from '@prisma/client';
|
||||||
import { parseBirthDate } from '../common/excel';
|
import { parseBirthDate } from '../common/excel';
|
||||||
@@ -23,6 +25,7 @@ import { FILTERED_IDS_MAX } from '@mento/shared';
|
|||||||
import {
|
import {
|
||||||
applyLeadGenderName,
|
applyLeadGenderName,
|
||||||
applyPersonGenderTitle,
|
applyPersonGenderTitle,
|
||||||
|
extractActivityExperienceFromNotes,
|
||||||
normalizeActivityExperience,
|
normalizeActivityExperience,
|
||||||
} from '@mento/shared';
|
} from '@mento/shared';
|
||||||
import { PrismaService } from '../prisma/prisma.service';
|
import { PrismaService } from '../prisma/prisma.service';
|
||||||
@@ -143,7 +146,9 @@ type LeadRow = Prisma.LeadGetPayload<{ include: typeof leadInclude }>;
|
|||||||
type BoardLeadRow = Prisma.LeadGetPayload<{ select: typeof boardLeadSelect }>;
|
type BoardLeadRow = Prisma.LeadGetPayload<{ select: typeof boardLeadSelect }>;
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class LeadsService {
|
export class LeadsService implements OnModuleInit {
|
||||||
|
private readonly logger = new Logger(LeadsService.name);
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly prisma: PrismaService,
|
private readonly prisma: PrismaService,
|
||||||
private readonly stages: LeadStagesService,
|
private readonly stages: LeadStagesService,
|
||||||
@@ -154,6 +159,41 @@ export class LeadsService {
|
|||||||
private readonly outboundSms: OutboundSmsLogService,
|
private readonly outboundSms: OutboundSmsLogService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
onModuleInit() {
|
||||||
|
void this.backfillActivityExperienceFromNotes();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** سابقه فعالیت قدیمی که فقط در یادداشت فرم بود، روی ستون لید مینشیند. */
|
||||||
|
async backfillActivityExperienceFromNotes() {
|
||||||
|
try {
|
||||||
|
const rows = await this.prisma.lead.findMany({
|
||||||
|
where: {
|
||||||
|
activityExperience: null,
|
||||||
|
notes: { contains: 'سابقه' },
|
||||||
|
},
|
||||||
|
select: { id: true, notes: true },
|
||||||
|
take: 20000,
|
||||||
|
});
|
||||||
|
let filled = 0;
|
||||||
|
for (const row of rows) {
|
||||||
|
const value = extractActivityExperienceFromNotes(row.notes);
|
||||||
|
if (!value) continue;
|
||||||
|
const updated = await this.prisma.lead.updateMany({
|
||||||
|
where: { id: row.id, activityExperience: null },
|
||||||
|
data: { activityExperience: value },
|
||||||
|
});
|
||||||
|
filled += updated.count;
|
||||||
|
}
|
||||||
|
if (filled) {
|
||||||
|
this.logger.log(`سابقه فعالیت ${filled} لید از یادداشت فرم پر شد`);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
this.logger.warn(
|
||||||
|
`پر کردن سابقه فعالیت لیدها ناموفق بود: ${err instanceof Error ? err.message : err}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private gameEvent(
|
private gameEvent(
|
||||||
actor: Actor,
|
actor: Actor,
|
||||||
event: Parameters<GamificationService['emit']>[0]['event'],
|
event: Parameters<GamificationService['emit']>[0]['event'],
|
||||||
@@ -230,7 +270,11 @@ export class LeadsService {
|
|||||||
? { source: { contains: opts.source, mode: 'insensitive' } }
|
? { source: { contains: opts.source, mode: 'insensitive' } }
|
||||||
: {}),
|
: {}),
|
||||||
...(opts.activityExperience
|
...(opts.activityExperience
|
||||||
? { activityExperience: opts.activityExperience }
|
? {
|
||||||
|
activityExperience:
|
||||||
|
normalizeActivityExperience(opts.activityExperience) ??
|
||||||
|
opts.activityExperience,
|
||||||
|
}
|
||||||
: {}),
|
: {}),
|
||||||
},
|
},
|
||||||
...(opts.unassigned ? [{ assigneeId: null }] : []),
|
...(opts.unassigned ? [{ assigneeId: null }] : []),
|
||||||
@@ -1662,7 +1706,9 @@ export class LeadsService {
|
|||||||
addresses: asAddresses(row.addresses),
|
addresses: asAddresses(row.addresses),
|
||||||
trade: row.trade,
|
trade: row.trade,
|
||||||
source: row.source,
|
source: row.source,
|
||||||
activityExperience: row.activityExperience,
|
activityExperience:
|
||||||
|
row.activityExperience ||
|
||||||
|
extractActivityExperienceFromNotes(row.notes),
|
||||||
notes: row.notes,
|
notes: row.notes,
|
||||||
estimatedValue:
|
estimatedValue:
|
||||||
row.estimatedValue != null ? Number(row.estimatedValue) : null,
|
row.estimatedValue != null ? Number(row.estimatedValue) : null,
|
||||||
|
|||||||
@@ -1559,6 +1559,30 @@ export function isActivityExperienceLabel(label: string): boolean {
|
|||||||
return /سابقه\s*فعالیت/.test(text);
|
return /سابقه\s*فعالیت/.test(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* استخراج سابقه فعالیت از خطوط یادداشت فرم
|
||||||
|
* (مثلاً «سابقه فعالیت در حوزه نقره: بیش از 5 سال»).
|
||||||
|
*/
|
||||||
|
export function extractActivityExperienceFromNotes(
|
||||||
|
notes: string | null | undefined,
|
||||||
|
): ActivityExperience | null {
|
||||||
|
const text = (notes ?? "").trim();
|
||||||
|
if (!text || !/سابقه\s*فعالیت/.test(text.replace(/ي/g, "ی").replace(/ك/g, "ک"))) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
for (const line of text.split(/\r?\n/)) {
|
||||||
|
const trimmed = line.trim();
|
||||||
|
if (!trimmed) continue;
|
||||||
|
const splitAt = trimmed.indexOf(":");
|
||||||
|
if (splitAt <= 0 || splitAt > 200) continue;
|
||||||
|
const label = trimmed.slice(0, splitAt).trim();
|
||||||
|
const value = trimmed.slice(splitAt + 1).trim();
|
||||||
|
if (!value || !isActivityExperienceLabel(label)) continue;
|
||||||
|
return normalizeActivityExperience(value);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
export interface PersonImportSourceStatDto {
|
export interface PersonImportSourceStatDto {
|
||||||
source: PersonImportSource | string;
|
source: PersonImportSource | string;
|
||||||
label: string;
|
label: string;
|
||||||
|
|||||||
Reference in New Issue
Block a user