|
| 1 | +#!/usr/bin/env node |
| 2 | +// ============================================================================ |
| 3 | +// import-crm-starter-to-twenty.mjs |
| 4 | +// |
| 5 | +// Phase 3 of context-v/plans/CRM-Starter-Export-Orgs-Then-People.md: batch |
| 6 | +// insertion of the CRM starter CSVs into reach-edu's self-hosted Twenty. |
| 7 | +// |
| 8 | +// Discipline (house import rules): idempotent + additive + dry-run-first. |
| 9 | +// - External ids ride custom fields (companies.augmentItSlug, |
| 10 | +// people.augmentItPersonUuid) so re-runs UPDATE-nothing/CREATE-missing |
| 11 | +// instead of duplicating. v1 is create-missing only — existing records |
| 12 | +// are reported, never patched (additive enrichment; no clobber). |
| 13 | +// - --dry-run (the default) writes NOTHING. With a valid key it also |
| 14 | +// diffs against the live instance; without one it prints the offline |
| 15 | +// mapping so the operator can review before the key exists. |
| 16 | +// - Opportunities (pipeline Stage/commitments as Twenty opportunity |
| 17 | +// records) are PRINTED as a proposal in dry-run; creation is gated |
| 18 | +// behind --with-opportunities pending the operator's ruling. |
| 19 | +// |
| 20 | +// Usage: |
| 21 | +// node scripts/import-crm-starter-to-twenty.mjs \ |
| 22 | +// [--dir clients/reach-edu/outputs/<date>_crm-starter] \ |
| 23 | +// [--env-file <path to twenty/.env with TWENTY_MCP_API_KEY>] \ |
| 24 | +// [--base-url https://twenty-server-production-7c98.up.railway.app] \ |
| 25 | +// [--live] (default: dry-run) |
| 26 | +// [--with-opportunities] (only with --live, after the ruling) |
| 27 | +// ============================================================================ |
| 28 | + |
| 29 | +import { readFileSync, existsSync } from 'node:fs'; |
| 30 | +import { resolve } from 'node:path'; |
| 31 | + |
| 32 | +// ---- args ------------------------------------------------------------------- |
| 33 | +const args = { |
| 34 | + dir: 'clients/reach-edu/outputs/2026-07-28_crm-starter', |
| 35 | + envFile: '/Users/mpstaton/code/lossless-monorepo/self-host-stack/client-stacks/reach-edu/twenty/.env', |
| 36 | + baseUrl: 'https://twenty-server-production-7c98.up.railway.app', |
| 37 | + live: false, |
| 38 | + withOpportunities: false, |
| 39 | +}; |
| 40 | +for (let i = 2; i < process.argv.length; i += 1) { |
| 41 | + const k = process.argv[i]; |
| 42 | + if (k === '--dir') args.dir = process.argv[++i]; |
| 43 | + else if (k === '--env-file') args.envFile = process.argv[++i]; |
| 44 | + else if (k === '--base-url') args.baseUrl = process.argv[++i]; |
| 45 | + else if (k === '--live') args.live = true; |
| 46 | + else if (k === '--with-opportunities') args.withOpportunities = true; |
| 47 | + else if (k === '--dry-run') args.live = false; |
| 48 | +} |
| 49 | + |
| 50 | +// ---- key -------------------------------------------------------------------- |
| 51 | +function readKey() { |
| 52 | + if (process.env.TWENTY_MCP_API_KEY) return process.env.TWENTY_MCP_API_KEY; |
| 53 | + if (existsSync(args.envFile)) { |
| 54 | + const m = /^TWENTY_MCP_API_KEY=(.+)$/m.exec(readFileSync(args.envFile, 'utf8')); |
| 55 | + if (m) return m[1].trim().replace(/^["']|["']$/g, ''); |
| 56 | + } |
| 57 | + return null; |
| 58 | +} |
| 59 | +const KEY = readKey(); |
| 60 | + |
| 61 | +async function api(method, path, body) { |
| 62 | + const res = await fetch(`${args.baseUrl}${path}`, { |
| 63 | + method, |
| 64 | + headers: { |
| 65 | + Authorization: `Bearer ${KEY}`, |
| 66 | + 'Content-Type': 'application/json', |
| 67 | + }, |
| 68 | + body: body ? JSON.stringify(body) : undefined, |
| 69 | + }); |
| 70 | + const text = await res.text(); |
| 71 | + let json = null; |
| 72 | + try { json = JSON.parse(text); } catch { /* leave null */ } |
| 73 | + return { status: res.status, json, text }; |
| 74 | +} |
| 75 | + |
| 76 | +// ---- csv -------------------------------------------------------------------- |
| 77 | +function parseCsv(path) { |
| 78 | + const text = readFileSync(path, 'utf8'); |
| 79 | + const rows = []; |
| 80 | + let row = [], cell = '', inQ = false; |
| 81 | + for (let i = 0; i < text.length; i += 1) { |
| 82 | + const c = text[i]; |
| 83 | + if (inQ) { |
| 84 | + if (c === '"' && text[i + 1] === '"') { cell += '"'; i += 1; } |
| 85 | + else if (c === '"') inQ = false; |
| 86 | + else cell += c; |
| 87 | + } else if (c === '"') inQ = true; |
| 88 | + else if (c === ',') { row.push(cell); cell = ''; } |
| 89 | + else if (c === '\n') { row.push(cell); rows.push(row); row = []; cell = ''; } |
| 90 | + else if (c !== '\r') cell += c; |
| 91 | + } |
| 92 | + const [h, ...data] = rows; |
| 93 | + return data |
| 94 | + .filter((r) => r.length > 1) |
| 95 | + .map((r) => Object.fromEntries(h.map((c, i) => [c, r[i] ?? '']))); |
| 96 | +} |
| 97 | + |
| 98 | +const DIR = resolve(args.dir); |
| 99 | +const orgRows = parseCsv(`${DIR}/orgs.csv`); |
| 100 | +const peopleRows = parseCsv(`${DIR}/people.csv`); |
| 101 | + |
| 102 | +// ---- shape companies (dedupe by external_id — multi-deal rows share one org) |
| 103 | +const companiesBySlug = new Map(); |
| 104 | +for (const r of orgRows) { |
| 105 | + if (!r.external_id || companiesBySlug.has(r.external_id)) continue; |
| 106 | + // Guard: the canonical layer contains at least one literal "unknown" |
| 107 | + // website value — bad URLs surface as blank domains, never crashes. |
| 108 | + let domain = (r.domains || '').split(' | ')[0]; |
| 109 | + if (!domain && r.website) { |
| 110 | + try { domain = new URL(r.website).hostname.replace(/^www\./, ''); } catch { /* blank */ } |
| 111 | + } |
| 112 | + companiesBySlug.set(r.external_id, { |
| 113 | + name: r.name || r.external_id, |
| 114 | + domainName: domain ? { primaryLinkUrl: `https://${domain}` } : undefined, |
| 115 | + linkedinLink: r.linkedin ? { primaryLinkUrl: r.linkedin } : undefined, |
| 116 | + xLink: r.x ? { primaryLinkUrl: r.x } : undefined, |
| 117 | + augmentItSlug: r.external_id, |
| 118 | + }); |
| 119 | +} |
| 120 | + |
| 121 | +// ---- shape people ------------------------------------------------------------ |
| 122 | +const people = peopleRows.map((r) => { |
| 123 | + const nameParts = (r.name || '').split(' '); |
| 124 | + return { |
| 125 | + name: { |
| 126 | + firstName: r.first_name || nameParts[0] || '', |
| 127 | + lastName: r.surname || nameParts.slice(1).join(' ') || '', |
| 128 | + }, |
| 129 | + emails: r.email ? { primaryEmail: r.email } : undefined, |
| 130 | + linkedinLink: r.linkedin ? { primaryLinkUrl: r.linkedin } : undefined, |
| 131 | + jobTitle: r.role || undefined, |
| 132 | + augmentItPersonUuid: r.external_id, |
| 133 | + _org_slug: r.org_external_id || null, // resolved to companyId at import |
| 134 | + _org_name: r.org_name || null, |
| 135 | + }; |
| 136 | +}); |
| 137 | + |
| 138 | +// ---- opportunity proposal (per pipeline row with deal columns) --------------- |
| 139 | +const opportunities = orgRows |
| 140 | + .filter((r) => r.Stage || r['Total Commitment ($)']) |
| 141 | + .map((r) => ({ |
| 142 | + name: r.pipeline_org_name || r.name, |
| 143 | + stage: r.Stage, |
| 144 | + amount: r['Total Commitment ($)'], |
| 145 | + company_slug: r.external_id || null, |
| 146 | + person_uuid: r.person_external_id || null, |
| 147 | + owner: r.Owner, |
| 148 | + })); |
| 149 | + |
| 150 | +// ---- stage census — Twenty's opportunity.stage is a SELECT: every distinct |
| 151 | +// tracker stage must exist as an option BEFORE an opportunity can carry it |
| 152 | +// (operator requirement 2026-07-28). Options are ensured in the live |
| 153 | +// opportunities pass, ahead of record creation. |
| 154 | +const stageCensus = new Map(); |
| 155 | +for (const o of opportunities) { |
| 156 | + if (o.stage) stageCensus.set(o.stage, (stageCensus.get(o.stage) ?? 0) + 1); |
| 157 | +} |
| 158 | + |
| 159 | +// ---- report ------------------------------------------------------------------ |
| 160 | +console.log(`\n=== CRM starter import ${args.live ? 'LIVE' : 'DRY-RUN'} ===`); |
| 161 | +console.log(`source: ${DIR}`); |
| 162 | +console.log(`companies to consider: ${companiesBySlug.size} (deduped from ${orgRows.length} org rows)`); |
| 163 | +console.log(`people to consider: ${people.length}`); |
| 164 | +console.log(`opportunity rows (proposal${args.withOpportunities ? ', ENABLED' : ' only — gated behind --with-opportunities'}): ${opportunities.length}`); |
| 165 | +console.log('\nfield mapping (companies): name, domainName←domains/website, linkedinLink, xLink, augmentItSlug←external_id (custom TEXT)'); |
| 166 | +console.log('field mapping (people): name←first/surname, emails←email, linkedinLink, jobTitle←role, augmentItPersonUuid←external_id (custom TEXT), company←org_external_id'); |
| 167 | +console.log('NOT imported (stays in augment-it): streams, tags, relations, corpus, other_links — candidates for attached notes in a later pass.'); |
| 168 | +console.log(`\ntracker stages (${stageCensus.size} distinct — must exist as opportunity.stage SELECT options before opportunities import):`); |
| 169 | +for (const [s, n] of [...stageCensus.entries()].sort((a, b) => b[1] - a[1])) { |
| 170 | + console.log(` ${String(n).padStart(3)} · ${s}`); |
| 171 | +} |
| 172 | + |
| 173 | +const sample = [...companiesBySlug.values()][0]; |
| 174 | +console.log('\nsample company:', JSON.stringify(sample)); |
| 175 | +console.log('sample person: ', JSON.stringify({ ...people[0], _org_slug: people[0]?._org_slug })); |
| 176 | +console.log('sample opportunity proposal:', JSON.stringify(opportunities[0])); |
| 177 | + |
| 178 | +// ---- online half ------------------------------------------------------------- |
| 179 | +if (!KEY) { |
| 180 | + console.log('\n⚠ no TWENTY_MCP_API_KEY found — offline dry-run only.'); |
| 181 | + process.exit(0); |
| 182 | +} |
| 183 | +const probe = await api('GET', '/rest/companies?limit=1'); |
| 184 | +if (probe.status === 401) { |
| 185 | + console.log(`\n⚠ API key rejected (401${probe.json?.messages ? ': ' + probe.json.messages.join('; ') : ''}) — offline dry-run only.`); |
| 186 | + console.log(' Mint a durable key: reach-edu Twenty → Settings → APIs (NOT the playground), then update TWENTY_MCP_API_KEY in'); |
| 187 | + console.log(` ${args.envFile}`); |
| 188 | + process.exit(args.live ? 1 : 0); |
| 189 | +} |
| 190 | +console.log('\n✓ API key valid — inspecting instance…'); |
| 191 | + |
| 192 | +// 1. Custom fields. |
| 193 | +const meta = await api('GET', '/rest/metadata/objects'); |
| 194 | +const objects = meta.json?.data?.objects ?? meta.json?.data ?? []; |
| 195 | +const findObj = (n) => objects.find((o) => (o.nameSingular ?? o.name) === n); |
| 196 | +const companyObj = findObj('company'); |
| 197 | +const personObj = findObj('person'); |
| 198 | +if (!companyObj || !personObj) { |
| 199 | + console.log('⚠ could not read object metadata; raw status', meta.status, '— aborting before any write.'); |
| 200 | + process.exit(1); |
| 201 | +} |
| 202 | +const hasField = (obj, name) => (obj.fields ?? []).some((f) => f.name === name); |
| 203 | +const wantFields = [ |
| 204 | + [companyObj, 'augmentItSlug', 'Augment-It Slug'], |
| 205 | + [personObj, 'augmentItPersonUuid', 'Augment-It Person UUID'], |
| 206 | +]; |
| 207 | +for (const [obj, name, label] of wantFields) { |
| 208 | + if (hasField(obj, name)) { |
| 209 | + console.log(` custom field ${name}: exists`); |
| 210 | + } else if (args.live) { |
| 211 | + const r = await api('POST', '/rest/metadata/fields', { |
| 212 | + objectMetadataId: obj.id, name, label, type: 'TEXT', |
| 213 | + }); |
| 214 | + console.log(` custom field ${name}: ${r.status < 300 ? 'CREATED' : 'FAILED ' + r.status + ' ' + r.text.slice(0, 120)}`); |
| 215 | + if (r.status >= 300) process.exit(1); |
| 216 | + } else { |
| 217 | + console.log(` custom field ${name}: MISSING — would create (TEXT) on ${obj.nameSingular ?? obj.name}`); |
| 218 | + } |
| 219 | +} |
| 220 | + |
| 221 | +// 1b. Opportunity stage options — diff the census against the live SELECT. |
| 222 | +const oppObj = findObj('opportunity'); |
| 223 | +if (oppObj) { |
| 224 | + const stageField = (oppObj.fields ?? []).find((f) => f.name === 'stage'); |
| 225 | + const liveOptions = (stageField?.options ?? []).map((o) => o.label ?? o.value); |
| 226 | + const missing = [...stageCensus.keys()].filter( |
| 227 | + (s) => !liveOptions.some((o) => String(o).toLowerCase() === s.toLowerCase()), |
| 228 | + ); |
| 229 | + console.log(` opportunity.stage options live: [${liveOptions.join(', ')}]`); |
| 230 | + if (missing.length) { |
| 231 | + console.log(` MISSING stage options (created ahead of opportunities when --live --with-opportunities): ${missing.join(' · ')}`); |
| 232 | + } else { |
| 233 | + console.log(' all tracker stages present as options'); |
| 234 | + } |
| 235 | +} |
| 236 | + |
| 237 | +// 2. Existing records, indexed by external id (fallback: name). |
| 238 | +async function fetchAll(object) { |
| 239 | + const out = []; |
| 240 | + let after = ''; |
| 241 | + for (let page = 0; page < 50; page += 1) { |
| 242 | + const r = await api('GET', `/rest/${object}?limit=60${after}`); |
| 243 | + const arr = r.json?.data?.[object] ?? []; |
| 244 | + out.push(...arr); |
| 245 | + const cursor = r.json?.pageInfo?.endCursor; |
| 246 | + if (!cursor || arr.length < 60) break; |
| 247 | + after = `&starting_after=${encodeURIComponent(cursor)}`; |
| 248 | + } |
| 249 | + return out; |
| 250 | +} |
| 251 | +const liveCompanies = await fetchAll('companies'); |
| 252 | +const livePeople = await fetchAll('people'); |
| 253 | +console.log(` live instance: ${liveCompanies.length} companies, ${livePeople.length} people`); |
| 254 | +const liveCompanyBySlug = new Map(liveCompanies.filter((c) => c.augmentItSlug).map((c) => [c.augmentItSlug, c])); |
| 255 | +const liveCompanyByName = new Map(liveCompanies.map((c) => [String(c.name ?? '').toLowerCase(), c])); |
| 256 | +const livePersonByUuid = new Map(livePeople.filter((p) => p.augmentItPersonUuid).map((p) => [p.augmentItPersonUuid, p])); |
| 257 | + |
| 258 | +const newCompanies = [...companiesBySlug.values()].filter( |
| 259 | + (c) => !liveCompanyBySlug.has(c.augmentItSlug) && !liveCompanyByName.has(c.name.toLowerCase()), |
| 260 | +); |
| 261 | +const newPeople = people.filter((p) => !livePersonByUuid.has(p.augmentItPersonUuid)); |
| 262 | +console.log(` to create: ${newCompanies.length} companies, ${newPeople.length} people (rest already present — skipped, never patched)`); |
| 263 | + |
| 264 | +if (!args.live) { |
| 265 | + console.log('\nDRY-RUN complete — nothing written. Re-run with --live to import.'); |
| 266 | + process.exit(0); |
| 267 | +} |
| 268 | + |
| 269 | +// 3. Create companies. |
| 270 | +const slugToId = new Map([...liveCompanyBySlug.entries()].map(([s, c]) => [s, c.id])); |
| 271 | +let created = 0, failed = 0; |
| 272 | +for (const c of newCompanies) { |
| 273 | + const body = Object.fromEntries(Object.entries(c).filter(([, v]) => v !== undefined)); |
| 274 | + const r = await api('POST', '/rest/companies', body); |
| 275 | + const rec = r.json?.data?.createCompany ?? r.json?.data ?? null; |
| 276 | + if (r.status < 300 && rec?.id) { |
| 277 | + slugToId.set(c.augmentItSlug, rec.id); |
| 278 | + created += 1; |
| 279 | + } else { |
| 280 | + failed += 1; |
| 281 | + console.log(` ✗ company ${c.augmentItSlug}: ${r.status} ${r.text.slice(0, 140)}`); |
| 282 | + } |
| 283 | +} |
| 284 | +console.log(`companies: created ${created}, failed ${failed}, pre-existing ${companiesBySlug.size - newCompanies.length}`); |
| 285 | + |
| 286 | +// 4. Create people (company attach by slug, fallback name). |
| 287 | +let pCreated = 0, pFailed = 0, pUnattached = 0; |
| 288 | +for (const p of newPeople) { |
| 289 | + const companyId = p._org_slug |
| 290 | + ? (slugToId.get(p._org_slug) ?? liveCompanyByName.get((p._org_name ?? '').toLowerCase())?.id ?? null) |
| 291 | + : null; |
| 292 | + if (p._org_slug && !companyId) pUnattached += 1; |
| 293 | + const body = Object.fromEntries( |
| 294 | + Object.entries({ ...p, companyId: companyId ?? undefined, _org_slug: undefined, _org_name: undefined }) |
| 295 | + .filter(([, v]) => v !== undefined), |
| 296 | + ); |
| 297 | + const r = await api('POST', '/rest/people', body); |
| 298 | + if (r.status < 300) pCreated += 1; |
| 299 | + else { |
| 300 | + pFailed += 1; |
| 301 | + console.log(` ✗ person ${p.name.firstName} ${p.name.lastName}: ${r.status} ${r.text.slice(0, 140)}`); |
| 302 | + } |
| 303 | +} |
| 304 | +console.log(`people: created ${pCreated}, failed ${pFailed}, pre-existing ${people.length - newPeople.length}, attach-misses ${pUnattached}`); |
| 305 | +console.log('\nVERIFY: spot-check five companies for link fidelity, three multi-affiliation people, then re-run this script — it should report 0 to create (the external-id round-trip proof).'); |
| 306 | +if (args.withOpportunities) { |
| 307 | + console.log('⚠ --with-opportunities: NOT implemented until the operator rules opportunities-vs-company-fields at dry-run review.'); |
| 308 | +} |
0 commit comments