fix(contacts): merge the same mobile stored on phone or mobile

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-11-05 11:27:05 +03:30
co-authored by Cursor
parent 57a0b379f6
commit c976c0bcd6
2 changed files with 60 additions and 40 deletions
@@ -2,7 +2,7 @@ import { Injectable, Logger, type OnApplicationBootstrap } from '@nestjs/common'
import { PrismaService } from '../prisma/prisma.service';
import { mergeSameMobilePeople } from './merge-same-mobile';
const FIX_KEY = 'person-mobile-canonical-v1';
const FIX_KEY = 'person-mobile-canonical-v2';
@Injectable()
export class SameMobileMergeService implements OnApplicationBootstrap {
@@ -21,17 +21,20 @@ export class SameMobileMergeService implements OnApplicationBootstrap {
SELECT 1 AS ok
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'Person'
AND column_name = 'gender'
AND lower(table_name) = 'person'
AND lower(column_name) = 'gender'
`;
if (!ready.length) return;
if (!ready.length) {
this.logger.warn('یکسان‌سازی موبایل رد شد: ستون gender هنوز نیست');
return;
}
const table = await this.prisma.$queryRaw<{ ok: number }[]>`
SELECT 1 AS ok
FROM information_schema.tables
WHERE table_schema = 'public' AND table_name = 'DataFix'
await this.prisma.$executeRaw`
CREATE TABLE IF NOT EXISTS "DataFix" (
"key" TEXT PRIMARY KEY,
"doneAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP
)
`;
if (!table.length) return;
const done = await this.prisma.$queryRaw<{ key: string }[]>`
SELECT "key" FROM "DataFix" WHERE "key" = ${FIX_KEY}
@@ -48,11 +51,22 @@ export class SameMobileMergeService implements OnApplicationBootstrap {
const orgs = await this.prisma.organization.findMany({ select: { id: true } });
let merged = 0;
let rewritten = 0;
let failed = 0;
for (const org of orgs) {
const result = await mergeSameMobilePeople(this.prisma, org.id, true);
merged += result.merged;
rewritten += result.rewritten;
try {
const result = await mergeSameMobilePeople(this.prisma, org.id, true);
merged += result.merged;
rewritten += result.rewritten;
failed += result.failed;
} catch (error) {
failed += 1;
this.logger.error(
`یکسان‌سازی موبایل برای محیط ${org.id} انجام نشد`,
error instanceof Error ? error.message : String(error),
);
}
}
if (failed) return;
await this.prisma.$executeRaw`
INSERT INTO "DataFix" ("key") VALUES (${FIX_KEY})
ON CONFLICT ("key") DO NOTHING