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,
|
||||
applyPersonGenderTitle,
|
||||
canViewAllRecords,
|
||||
extractActivityExperienceFromNotes,
|
||||
normalizeActivityExperience,
|
||||
splitPersonHonorific,
|
||||
} from '@mento/shared';
|
||||
@@ -184,7 +185,13 @@ export class PeopleService implements OnModuleInit {
|
||||
(PERSON_MOODS as readonly string[]).includes(m),
|
||||
);
|
||||
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) => [
|
||||
{ firstName: ci(t) },
|
||||
{ lastName: ci(t) },
|
||||
@@ -378,6 +385,38 @@ export class PeopleService implements OnModuleInit {
|
||||
|
||||
onModuleInit() {
|
||||
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,
|
||||
birthDate: row.birthDate?.toISOString() ?? null,
|
||||
referrer: row.referrer,
|
||||
activityExperience: row.activityExperience,
|
||||
activityExperience:
|
||||
row.activityExperience ||
|
||||
extractActivityExperienceFromNotes(row.notes),
|
||||
addresses: (() => {
|
||||
const fromCol = asAddresses(row.addresses);
|
||||
if (fromCol.length) return fromCol;
|
||||
|
||||
@@ -2,8 +2,10 @@ import {
|
||||
BadRequestException,
|
||||
ConflictException,
|
||||
Injectable,
|
||||
Logger,
|
||||
NotFoundException,
|
||||
ServiceUnavailableException,
|
||||
type OnModuleInit,
|
||||
} from '@nestjs/common';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { parseBirthDate } from '../common/excel';
|
||||
@@ -23,6 +25,7 @@ import { FILTERED_IDS_MAX } from '@mento/shared';
|
||||
import {
|
||||
applyLeadGenderName,
|
||||
applyPersonGenderTitle,
|
||||
extractActivityExperienceFromNotes,
|
||||
normalizeActivityExperience,
|
||||
} from '@mento/shared';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
@@ -143,7 +146,9 @@ type LeadRow = Prisma.LeadGetPayload<{ include: typeof leadInclude }>;
|
||||
type BoardLeadRow = Prisma.LeadGetPayload<{ select: typeof boardLeadSelect }>;
|
||||
|
||||
@Injectable()
|
||||
export class LeadsService {
|
||||
export class LeadsService implements OnModuleInit {
|
||||
private readonly logger = new Logger(LeadsService.name);
|
||||
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly stages: LeadStagesService,
|
||||
@@ -154,6 +159,41 @@ export class LeadsService {
|
||||
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(
|
||||
actor: Actor,
|
||||
event: Parameters<GamificationService['emit']>[0]['event'],
|
||||
@@ -230,7 +270,11 @@ export class LeadsService {
|
||||
? { source: { contains: opts.source, mode: 'insensitive' } }
|
||||
: {}),
|
||||
...(opts.activityExperience
|
||||
? { activityExperience: opts.activityExperience }
|
||||
? {
|
||||
activityExperience:
|
||||
normalizeActivityExperience(opts.activityExperience) ??
|
||||
opts.activityExperience,
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
...(opts.unassigned ? [{ assigneeId: null }] : []),
|
||||
@@ -1662,7 +1706,9 @@ export class LeadsService {
|
||||
addresses: asAddresses(row.addresses),
|
||||
trade: row.trade,
|
||||
source: row.source,
|
||||
activityExperience: row.activityExperience,
|
||||
activityExperience:
|
||||
row.activityExperience ||
|
||||
extractActivityExperienceFromNotes(row.notes),
|
||||
notes: row.notes,
|
||||
estimatedValue:
|
||||
row.estimatedValue != null ? Number(row.estimatedValue) : null,
|
||||
|
||||
Reference in New Issue
Block a user