/**
* Utility to fetch parent record names for notes
* Handles standard object types: people, companies
*/
import type { AttioClient } from '../attio-client.js';
interface PersonRecord {
data: {
id: { record_id: string };
values: {
name?: Array<{
full_name?: string;
first_name?: string;
last_name?: string;
}>;
};
};
}
interface CompanyRecord {
data: {
id: { record_id: string };
values: {
name?: Array<{ value: string }>;
};
};
}
/**
* Fetch a single parent record name based on object type
*/
async function fetchSingleRecordName(
client: AttioClient,
parentObject: string,
parentRecordId: string
): Promise<string | null> {
try {
// Handle standard objects
if (parentObject === 'people') {
const response = await client.get<PersonRecord>(
`/objects/people/records/${parentRecordId}`
);
const nameObj = response.data.values.name?.[0];
return (
nameObj?.full_name ||
[nameObj?.first_name, nameObj?.last_name].filter(Boolean).join(' ') ||
null
);
}
if (parentObject === 'companies') {
const response = await client.get<CompanyRecord>(
`/objects/companies/records/${parentRecordId}`
);
return response.data.values.name?.[0]?.value || null;
}
// Other object types - return null (name will show as null in response)
return null;
} catch {
// Silently skip records that can't be fetched
return null;
}
}
/**
* Fetch parent record names for multiple notes in parallel
*
* @param client - AttioClient instance
* @param notes - Array of notes with parent_object and parent_record_id
* @returns Map of "parent_object:parent_record_id" -> name
*/
export async function fetchParentRecordNames(
client: AttioClient,
notes: Array<{ parent_object: string; parent_record_id: string }>
): Promise<Map<string, string>> {
const result = new Map<string, string>();
// Create unique keys to avoid duplicate fetches
const uniqueKeys = new Set(
notes.map((n) => `${n.parent_object}:${n.parent_record_id}`)
);
// Fetch all names in parallel
const fetchPromises = Array.from(uniqueKeys).map(async (key) => {
const parts = key.split(':');
const parentObject = parts[0];
const parentRecordId = parts[1];
if (!parentObject || !parentRecordId) return;
const name = await fetchSingleRecordName(
client,
parentObject,
parentRecordId
);
if (name) {
result.set(key, name);
}
});
await Promise.all(fetchPromises);
return result;
}