@hasna/microservices 0.0.8 → 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,93 @@
1
+ export interface MigrationEntry {
2
+ id: number;
3
+ name: string;
4
+ sql: string;
5
+ }
6
+
7
+ export const MIGRATIONS: MigrationEntry[] = [
8
+ {
9
+ id: 1,
10
+ name: "core_leads",
11
+ sql: `
12
+ CREATE TABLE IF NOT EXISTS leads (
13
+ id TEXT PRIMARY KEY,
14
+ name TEXT,
15
+ email TEXT,
16
+ phone TEXT,
17
+ company TEXT,
18
+ title TEXT,
19
+ website TEXT,
20
+ linkedin_url TEXT,
21
+ source TEXT DEFAULT 'manual',
22
+ status TEXT DEFAULT 'new' CHECK(status IN ('new','contacted','qualified','unqualified','converted','lost')),
23
+ score INTEGER DEFAULT 0,
24
+ score_reason TEXT,
25
+ tags TEXT DEFAULT '[]',
26
+ notes TEXT,
27
+ metadata TEXT DEFAULT '{}',
28
+ enriched INTEGER DEFAULT 0,
29
+ enriched_at TEXT,
30
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
31
+ updated_at TEXT NOT NULL DEFAULT (datetime('now'))
32
+ );
33
+
34
+ CREATE UNIQUE INDEX IF NOT EXISTS idx_leads_email ON leads(email) WHERE email IS NOT NULL;
35
+ CREATE INDEX IF NOT EXISTS idx_leads_status ON leads(status);
36
+ CREATE INDEX IF NOT EXISTS idx_leads_score ON leads(score DESC);
37
+ CREATE INDEX IF NOT EXISTS idx_leads_source ON leads(source);
38
+
39
+ CREATE TABLE IF NOT EXISTS lead_activities (
40
+ id TEXT PRIMARY KEY,
41
+ lead_id TEXT NOT NULL REFERENCES leads(id) ON DELETE CASCADE,
42
+ type TEXT NOT NULL CHECK(type IN ('email_sent','email_opened','call','meeting','note','status_change','score_change','enriched')),
43
+ description TEXT,
44
+ metadata TEXT DEFAULT '{}',
45
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
46
+ );
47
+
48
+ CREATE INDEX IF NOT EXISTS idx_activities_lead ON lead_activities(lead_id);
49
+ `,
50
+ },
51
+ {
52
+ id: 2,
53
+ name: "enrichment_cache",
54
+ sql: `
55
+ CREATE TABLE IF NOT EXISTS enrichment_cache (
56
+ id TEXT PRIMARY KEY,
57
+ email TEXT UNIQUE,
58
+ company_data TEXT DEFAULT '{}',
59
+ person_data TEXT DEFAULT '{}',
60
+ social_profiles TEXT DEFAULT '{}',
61
+ tech_stack TEXT DEFAULT '[]',
62
+ company_size TEXT,
63
+ industry TEXT,
64
+ location TEXT,
65
+ revenue_range TEXT,
66
+ fetched_at TEXT,
67
+ source TEXT
68
+ );
69
+ `,
70
+ },
71
+ {
72
+ id: 3,
73
+ name: "lead_lists",
74
+ sql: `
75
+ CREATE TABLE IF NOT EXISTS lead_lists (
76
+ id TEXT PRIMARY KEY,
77
+ name TEXT NOT NULL,
78
+ description TEXT,
79
+ filter_query TEXT,
80
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
81
+ );
82
+
83
+ CREATE TABLE IF NOT EXISTS lead_list_members (
84
+ lead_list_id TEXT NOT NULL,
85
+ lead_id TEXT NOT NULL,
86
+ added_at TEXT NOT NULL DEFAULT (datetime('now')),
87
+ PRIMARY KEY(lead_list_id, lead_id),
88
+ FOREIGN KEY(lead_list_id) REFERENCES lead_lists(id) ON DELETE CASCADE,
89
+ FOREIGN KEY(lead_id) REFERENCES leads(id) ON DELETE CASCADE
90
+ );
91
+ `,
92
+ },
93
+ ];
@@ -0,0 +1,65 @@
1
+ /**
2
+ * microservice-leads — Lead generation, storage, scoring, and data enrichment microservice
3
+ */
4
+
5
+ export {
6
+ createLead,
7
+ getLead,
8
+ listLeads,
9
+ updateLead,
10
+ deleteLead,
11
+ searchLeads,
12
+ findByEmail,
13
+ bulkImportLeads,
14
+ exportLeads,
15
+ addActivity,
16
+ getActivities,
17
+ getLeadTimeline,
18
+ getLeadStats,
19
+ getPipeline,
20
+ deduplicateLeads,
21
+ mergeLeads,
22
+ type Lead,
23
+ type CreateLeadInput,
24
+ type UpdateLeadInput,
25
+ type ListLeadsOptions,
26
+ type LeadActivity,
27
+ type LeadStats,
28
+ type PipelineStage,
29
+ type BulkImportResult,
30
+ type DuplicatePair,
31
+ } from "./db/leads.js";
32
+
33
+ export {
34
+ createList,
35
+ getList,
36
+ listLists,
37
+ deleteList,
38
+ addToList,
39
+ removeFromList,
40
+ getListMembers,
41
+ getSmartListMembers,
42
+ type LeadList,
43
+ type CreateListInput,
44
+ } from "./db/lists.js";
45
+
46
+ export {
47
+ enrichLead,
48
+ enrichFromEmail,
49
+ enrichFromDomain,
50
+ getCachedEnrichment,
51
+ cacheEnrichment,
52
+ bulkEnrich,
53
+ type EnrichmentData,
54
+ type CachedEnrichment,
55
+ } from "./lib/enrichment.js";
56
+
57
+ export {
58
+ scoreLead,
59
+ autoScoreAll,
60
+ getScoreDistribution,
61
+ type ScoreResult,
62
+ type ScoreDistribution,
63
+ } from "./lib/scoring.js";
64
+
65
+ export { getDatabase, closeDatabase } from "./db/database.js";
@@ -0,0 +1,202 @@
1
+ /**
2
+ * Lead enrichment — AI-powered data enrichment from email and domain
3
+ */
4
+
5
+ import { getDatabase } from "../db/database.js";
6
+ import { getLead, updateLead, addActivity } from "../db/leads.js";
7
+
8
+ export interface EnrichmentData {
9
+ company?: string;
10
+ title?: string;
11
+ industry?: string;
12
+ location?: string;
13
+ company_size?: string;
14
+ revenue_range?: string;
15
+ tech_stack?: string[];
16
+ social_profiles?: Record<string, string>;
17
+ person_data?: Record<string, unknown>;
18
+ company_data?: Record<string, unknown>;
19
+ }
20
+
21
+ export interface CachedEnrichment {
22
+ id: string;
23
+ email: string;
24
+ company_data: Record<string, unknown>;
25
+ person_data: Record<string, unknown>;
26
+ social_profiles: Record<string, string>;
27
+ tech_stack: string[];
28
+ company_size: string | null;
29
+ industry: string | null;
30
+ location: string | null;
31
+ revenue_range: string | null;
32
+ fetched_at: string | null;
33
+ source: string | null;
34
+ }
35
+
36
+ interface CacheRow {
37
+ id: string;
38
+ email: string;
39
+ company_data: string;
40
+ person_data: string;
41
+ social_profiles: string;
42
+ tech_stack: string;
43
+ company_size: string | null;
44
+ industry: string | null;
45
+ location: string | null;
46
+ revenue_range: string | null;
47
+ fetched_at: string | null;
48
+ source: string | null;
49
+ }
50
+
51
+ function rowToCache(row: CacheRow): CachedEnrichment {
52
+ return {
53
+ ...row,
54
+ company_data: JSON.parse(row.company_data || "{}"),
55
+ person_data: JSON.parse(row.person_data || "{}"),
56
+ social_profiles: JSON.parse(row.social_profiles || "{}"),
57
+ tech_stack: JSON.parse(row.tech_stack || "[]"),
58
+ };
59
+ }
60
+
61
+ export function getCachedEnrichment(email: string): CachedEnrichment | null {
62
+ const db = getDatabase();
63
+ const row = db
64
+ .prepare("SELECT * FROM enrichment_cache WHERE email = ?")
65
+ .get(email) as CacheRow | null;
66
+ return row ? rowToCache(row) : null;
67
+ }
68
+
69
+ export function cacheEnrichment(email: string, data: EnrichmentData): CachedEnrichment {
70
+ const db = getDatabase();
71
+ const id = crypto.randomUUID();
72
+
73
+ db.prepare(
74
+ `INSERT OR REPLACE INTO enrichment_cache
75
+ (id, email, company_data, person_data, social_profiles, tech_stack, company_size, industry, location, revenue_range, fetched_at, source)
76
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, datetime('now'), 'ai')`
77
+ ).run(
78
+ id,
79
+ email,
80
+ JSON.stringify(data.company_data || {}),
81
+ JSON.stringify(data.person_data || {}),
82
+ JSON.stringify(data.social_profiles || {}),
83
+ JSON.stringify(data.tech_stack || []),
84
+ data.company_size || null,
85
+ data.industry || null,
86
+ data.location || null,
87
+ data.revenue_range || null
88
+ );
89
+
90
+ return getCachedEnrichment(email)!;
91
+ }
92
+
93
+ /**
94
+ * Enrich a lead from email — uses AI to research the person based on email domain.
95
+ * Returns enrichment data. In a production setup, this would call OpenAI/Anthropic.
96
+ * For now, returns domain-based heuristics.
97
+ */
98
+ export function enrichFromEmail(email: string): EnrichmentData {
99
+ const domain = email.split("@")[1];
100
+ if (!domain) return {};
101
+
102
+ // Check cache first
103
+ const cached = getCachedEnrichment(email);
104
+ if (cached) {
105
+ return {
106
+ company: cached.company_data?.name as string | undefined,
107
+ industry: cached.industry || undefined,
108
+ location: cached.location || undefined,
109
+ company_size: cached.company_size || undefined,
110
+ revenue_range: cached.revenue_range || undefined,
111
+ tech_stack: cached.tech_stack,
112
+ social_profiles: cached.social_profiles,
113
+ person_data: cached.person_data,
114
+ company_data: cached.company_data,
115
+ };
116
+ }
117
+
118
+ // Domain-based heuristic enrichment (production would call AI APIs)
119
+ const data: EnrichmentData = {};
120
+
121
+ // Detect company from email domain
122
+ const freeEmailDomains = ["gmail.com", "yahoo.com", "hotmail.com", "outlook.com", "aol.com", "protonmail.com"];
123
+ if (!freeEmailDomains.includes(domain)) {
124
+ data.company = domain.split(".")[0].charAt(0).toUpperCase() + domain.split(".")[0].slice(1);
125
+ data.company_data = { domain, name: data.company };
126
+ }
127
+
128
+ // Cache the result
129
+ cacheEnrichment(email, data);
130
+
131
+ return data;
132
+ }
133
+
134
+ /**
135
+ * Enrich from domain — AI researches company info.
136
+ * Returns company data. Production would call AI APIs.
137
+ */
138
+ export function enrichFromDomain(domain: string): EnrichmentData {
139
+ const data: EnrichmentData = {};
140
+ const companyName = domain.split(".")[0].charAt(0).toUpperCase() + domain.split(".")[0].slice(1);
141
+ data.company = companyName;
142
+ data.company_data = { domain, name: companyName };
143
+ return data;
144
+ }
145
+
146
+ /**
147
+ * Enrich a single lead by ID
148
+ */
149
+ export function enrichLead(leadId: string): Lead | null {
150
+ const lead = getLead(leadId);
151
+ if (!lead) return null;
152
+
153
+ let enrichmentData: EnrichmentData = {};
154
+
155
+ if (lead.email) {
156
+ enrichmentData = enrichFromEmail(lead.email);
157
+ } else if (lead.website) {
158
+ try {
159
+ const domain = new URL(lead.website.startsWith("http") ? lead.website : `https://${lead.website}`).hostname;
160
+ enrichmentData = enrichFromDomain(domain);
161
+ } catch {
162
+ // Invalid URL, skip
163
+ }
164
+ }
165
+
166
+ // Update lead with enriched data
167
+ const updates: Record<string, unknown> = { enriched: true, enriched_at: new Date().toISOString() };
168
+ if (enrichmentData.company && !lead.company) updates.company = enrichmentData.company;
169
+ if (enrichmentData.title && !lead.title) updates.title = enrichmentData.title;
170
+ if (enrichmentData.industry) {
171
+ const meta = { ...lead.metadata, industry: enrichmentData.industry };
172
+ updates.metadata = meta;
173
+ }
174
+
175
+ updateLead(leadId, updates as any);
176
+ addActivity(leadId, "enriched", `Enriched from ${lead.email || lead.website || "unknown"}`);
177
+
178
+ return getLead(leadId);
179
+ }
180
+
181
+ // Import Lead type for return
182
+ import type { Lead } from "../db/leads.js";
183
+
184
+ /**
185
+ * Bulk enrich multiple leads
186
+ */
187
+ export function bulkEnrich(leadIds: string[]): { enriched: number; failed: number } {
188
+ let enriched = 0;
189
+ let failed = 0;
190
+
191
+ for (const id of leadIds) {
192
+ try {
193
+ const result = enrichLead(id);
194
+ if (result) enriched++;
195
+ else failed++;
196
+ } catch {
197
+ failed++;
198
+ }
199
+ }
200
+
201
+ return { enriched, failed };
202
+ }
@@ -0,0 +1,134 @@
1
+ /**
2
+ * Lead scoring — rule-based scoring with AI fallback
3
+ */
4
+
5
+ import { getLead, updateLead, addActivity, getActivities, listLeads } from "../db/leads.js";
6
+ import type { Lead } from "../db/leads.js";
7
+
8
+ export interface ScoreResult {
9
+ score: number;
10
+ reason: string;
11
+ }
12
+
13
+ const FREE_EMAIL_DOMAINS = [
14
+ "gmail.com", "yahoo.com", "hotmail.com", "outlook.com",
15
+ "aol.com", "protonmail.com", "mail.com", "icloud.com",
16
+ ];
17
+
18
+ /**
19
+ * Score a lead using rule-based analysis.
20
+ * Returns score 0-100 with reason.
21
+ */
22
+ export function scoreLead(leadId: string): ScoreResult | null {
23
+ const lead = getLead(leadId);
24
+ if (!lead) return null;
25
+
26
+ let score = 0;
27
+ const reasons: string[] = [];
28
+
29
+ // +10 for company email (not free provider)
30
+ if (lead.email) {
31
+ const domain = lead.email.split("@")[1];
32
+ if (domain && !FREE_EMAIL_DOMAINS.includes(domain)) {
33
+ score += 10;
34
+ reasons.push("company email (+10)");
35
+ }
36
+ }
37
+
38
+ // +15 for having a title
39
+ if (lead.title) {
40
+ score += 15;
41
+ reasons.push("has title (+15)");
42
+
43
+ // Bonus for decision-maker titles
44
+ const decisionMakerKeywords = ["ceo", "cto", "cfo", "coo", "vp", "director", "head", "chief", "founder", "owner", "president"];
45
+ const titleLower = lead.title.toLowerCase();
46
+ if (decisionMakerKeywords.some((kw) => titleLower.includes(kw))) {
47
+ score += 10;
48
+ reasons.push("decision maker title (+10)");
49
+ }
50
+ }
51
+
52
+ // +10 for having a phone number
53
+ if (lead.phone) {
54
+ score += 10;
55
+ reasons.push("has phone (+10)");
56
+ }
57
+
58
+ // +20 for having LinkedIn
59
+ if (lead.linkedin_url) {
60
+ score += 20;
61
+ reasons.push("has LinkedIn (+20)");
62
+ }
63
+
64
+ // +10 for having a company
65
+ if (lead.company) {
66
+ score += 10;
67
+ reasons.push("has company (+10)");
68
+ }
69
+
70
+ // +5 per activity (up to 25)
71
+ const activities = getActivities(leadId);
72
+ const activityScore = Math.min(activities.length * 5, 25);
73
+ if (activityScore > 0) {
74
+ score += activityScore;
75
+ reasons.push(`${activities.length} activities (+${activityScore})`);
76
+ }
77
+
78
+ // +5 for being enriched
79
+ if (lead.enriched) {
80
+ score += 5;
81
+ reasons.push("enriched (+5)");
82
+ }
83
+
84
+ // Cap at 100
85
+ score = Math.min(score, 100);
86
+
87
+ const reason = reasons.join(", ") || "no scoring signals";
88
+
89
+ // Update the lead
90
+ updateLead(leadId, { score, score_reason: reason });
91
+ addActivity(leadId, "score_change", `Score updated to ${score}: ${reason}`);
92
+
93
+ return { score, reason };
94
+ }
95
+
96
+ /**
97
+ * Auto-score all leads with score=0
98
+ */
99
+ export function autoScoreAll(): { scored: number; total: number } {
100
+ const leads = listLeads();
101
+ const unscored = leads.filter((l) => l.score === 0);
102
+ let scored = 0;
103
+
104
+ for (const lead of unscored) {
105
+ const result = scoreLead(lead.id);
106
+ if (result) scored++;
107
+ }
108
+
109
+ return { scored, total: unscored.length };
110
+ }
111
+
112
+ export interface ScoreDistribution {
113
+ range: string;
114
+ count: number;
115
+ }
116
+
117
+ /**
118
+ * Get score distribution across all leads
119
+ */
120
+ export function getScoreDistribution(): ScoreDistribution[] {
121
+ const leads = listLeads();
122
+ const ranges = [
123
+ { range: "0-20", min: 0, max: 20 },
124
+ { range: "21-40", min: 21, max: 40 },
125
+ { range: "41-60", min: 41, max: 60 },
126
+ { range: "61-80", min: 61, max: 80 },
127
+ { range: "81-100", min: 81, max: 100 },
128
+ ];
129
+
130
+ return ranges.map(({ range, min, max }) => ({
131
+ range,
132
+ count: leads.filter((l) => l.score >= min && l.score <= max).length,
133
+ }));
134
+ }