diff --git a/src/lib/people/normalization.ts b/src/lib/people/normalization.ts index a96abcb..383f09f 100644 --- a/src/lib/people/normalization.ts +++ b/src/lib/people/normalization.ts @@ -215,6 +215,27 @@ export async function persistProviderOrganization( return saved; } +/** + * Merges an incoming record into a person that already exists. A provider that omits a field + * must not erase what an earlier source found, so empty values keep the stored ones and lists + * are combined. + */ +function mergePersonData>(existing: Record, incoming: T): Partial { + const merged: Record = {}; + for (const [key, value] of Object.entries(incoming)) { + if (Array.isArray(value)) { + const stored = Array.isArray(existing[key]) ? (existing[key] as unknown[]) : []; + merged[key] = Array.from(new Set([...stored, ...value])); + } else if (value !== null && value !== undefined && value !== "") { + merged[key] = value; + } + } + // The provider and its person id identify one record together, so keep the stored pair when no new id arrives. + if (!merged.providerPersonId) delete merged.provider; + merged.searchText = personSearchText({ ...existing, ...merged } as PersistedPersonShape); + return merged as Partial; +} + export async function persistProviderPerson( prisma: PrismaClient, userId: string, @@ -300,10 +321,16 @@ export async function persistProviderPerson( lastResearchedAt: new Date(), }; - const saved = - resolution.canonicalPersonId && resolution.outcome !== EntityResolutionOutcome.UNCERTAIN_MATCH - ? await prisma.discoveredPerson.update({ where: { id: resolution.canonicalPersonId }, data }) - : await prisma.discoveredPerson.create({ data: { ...data, userId } }); + const merging = resolution.canonicalPersonId && resolution.outcome !== EntityResolutionOutcome.UNCERTAIN_MATCH; + const existing = merging + ? byProvider ?? candidates.find((candidate) => candidate.id === resolution.canonicalPersonId) ?? null + : null; + const saved = merging + ? await prisma.discoveredPerson.update({ + where: { id: resolution.canonicalPersonId! }, + data: existing ? mergePersonData(existing, data) : data, + }) + : await prisma.discoveredPerson.create({ data: { ...data, userId } }); await prisma.entityResolutionDecision.create({ data: { diff --git a/tests/person-merge.test.ts b/tests/person-merge.test.ts new file mode 100644 index 0000000..e88adb3 --- /dev/null +++ b/tests/person-merge.test.ts @@ -0,0 +1,81 @@ +import { describe, expect, it, vi } from "vitest"; +import type { PrismaClient } from "@prisma/client"; +import { persistProviderPerson } from "@/lib/people/normalization"; + +const existing = { + id: "person-1", + provider: "first-provider", + providerPersonId: "first-1", + fullName: "Dana Lee", + currentTitle: "Partner", + currentOrganizationName: null, + organizationDomain: null, + linkedinUrl: "https://linkedin.com/in/dana", + emailAddresses: ["dana@fund.com"], + biography: "Partner at Fund", + industries: ["climate"], + skills: [], + normalizedFingerprint: null, +}; + +function fakePrisma() { + const updates: Array<{ where: { id: string }; data: Record }> = []; + const prisma = { + discoveredPerson: { + findUnique: vi.fn().mockResolvedValue(null), + findMany: vi.fn().mockResolvedValue([existing]), + update: vi.fn(async (args: { where: { id: string }; data: Record }) => { + updates.push(args); + return { ...existing, ...args.data }; + }), + create: vi.fn(), + }, + entityResolutionDecision: { create: vi.fn().mockResolvedValue({}) }, + personSource: { findFirst: vi.fn(), create: vi.fn(), update: vi.fn() }, + personClaim: { create: vi.fn() }, + }; + return { prisma: prisma as unknown as PrismaClient, updates }; +} + +describe("persistProviderPerson merge", () => { + it("keeps stored fields when a matching record arrives without them", async () => { + const { prisma, updates } = fakePrisma(); + + await persistProviderPerson(prisma, "user-1", "second-provider", { + fullName: "Dana Lee", + linkedinUrl: "https://linkedin.com/in/dana", + industries: ["fintech"], + sources: [], + claims: [], + }); + + expect(updates).toHaveLength(1); + const { data } = updates[0]!; + expect(data.emailAddresses).toEqual(["dana@fund.com"]); + expect(data).not.toHaveProperty("biography"); + expect(data).not.toHaveProperty("currentTitle"); + expect(data.industries).toEqual(["climate", "fintech"]); + expect(data).not.toHaveProperty("provider"); + expect(data).not.toHaveProperty("providerPersonId"); + expect(data.searchText).toContain("Partner at Fund"); + }); + + it("still applies new values from the incoming record", async () => { + const { prisma, updates } = fakePrisma(); + + await persistProviderPerson(prisma, "user-1", "second-provider", { + providerPersonId: "second-9", + fullName: "Dana Lee", + currentTitle: "Managing Partner", + linkedinUrl: "https://linkedin.com/in/dana", + emailAddresses: ["dana@newfund.com"], + sources: [], + claims: [], + }); + + const { data } = updates[0]!; + expect(data.currentTitle).toBe("Managing Partner"); + expect(data.emailAddresses).toEqual(["dana@fund.com", "dana@newfund.com"]); + expect(data).toMatchObject({ provider: "second-provider", providerPersonId: "second-9" }); + }); +});