Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions src/lib/people/normalization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends Record<string, unknown>>(existing: Record<string, unknown>, incoming: T): Partial<T> {
const merged: Record<string, unknown> = {};
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<T>;
}

export async function persistProviderPerson(
prisma: PrismaClient,
userId: string,
Expand Down Expand Up @@ -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: {
Expand Down
81 changes: 81 additions & 0 deletions tests/person-merge.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown> }> = [];
const prisma = {
discoveredPerson: {
findUnique: vi.fn().mockResolvedValue(null),
findMany: vi.fn().mockResolvedValue([existing]),
update: vi.fn(async (args: { where: { id: string }; data: Record<string, unknown> }) => {
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" });
});
});
Loading