import deepline from 'deepline'; function emailFromResult(value: unknown): string | null { if (!value || typeof value !== 'object' || !('email' in value)) { throw new Error('Work-email Play returned no email field.'); } if (value.email !== null && typeof value.email !== 'string') { throw new Error('Work-email Play returned an invalid email field.'); } return value.email; } type Lead = { lead_id: string; first_name: string; last_name: string; linkedin_url: string; company_name: string; company_domain: string; }; export const config = { description: 'Find verified work emails', compatibility: { authoringContract: 'explicit-freshness' }, }; export default async function (input: { csv: string }) { const leads = await deepline.csv(input.csv); const withEmail = await deepline .dataset('lead_email', leads) .withColumn('email_result', (lead) => deepline.runPlay( 'person_email', 'person-linkedin-to-email', { linkedin_url: lead.linkedin_url }, { description: 'Find a verified work email for this lead.' }, ), ) .withColumn('email', (lead) => emailFromResult(lead.email_result)) .run({ key: 'lead_id', description: 'Find one verified work email per lead.', }); const rows = await deepline .dataset('email_validation', withEmail) .withColumn('email_status', async (lead) => { if (!lead.email) return null; const result = await deepline.tools.execute({ id: 'verify_email', tool: 'hunter_email_verifier', input: { email: lead.email }, description: 'Verify the email before exporting the row.', staleAfterSeconds: 0, }); return result.extractedValues.email_status?.get()?.status ?? null; }) .run({ key: 'lead_id', description: 'Validate the emails that were found.', }); return { rows }; }