Repository navigation
Expand file tree
/
Copy pathseed.ts
More file actions
170 lines (156 loc) · 4.76 KB
/
Copy pathseed.ts
File metadata and controls
170 lines (156 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* SchemeAI Database Seeder
* Populates the official source registry and the verified scheme
* reference database. Idempotent — safe to run multiple times.
*
* bun scripts/seed.ts
*/
import { PrismaClient } from "@prisma/client";
import { ALL_SEED_SCHEMES, ALL_SEED_SOURCES } from "../src/lib/schemes";
const db = new PrismaClient();
function slugKey(slug: string) {
return slug;
}
async function seedSources() {
for (const s of ALL_SEED_SOURCES) {
await db.governmentSource.upsert({
where: { domain: s.domain },
create: {
name: s.name,
domain: s.domain,
jurisdiction: s.jurisdiction,
department: s.department,
accessMethod: s.accessMethod,
automationSupported: s.automationSupported,
reliability: s.reliability,
notes: s.notes,
accessStatus: "UNKNOWN",
},
update: {
name: s.name,
jurisdiction: s.jurisdiction,
department: s.department,
accessMethod: s.accessMethod,
automationSupported: s.automationSupported,
reliability: s.reliability,
notes: s.notes,
// accessStatus / lastCheckedAt are only updated by REAL checks
},
});
}
console.log(`[seed] sources: ${ALL_SEED_SOURCES.length} upserted`);
}
async function seedSchemes() {
let created = 0;
let updated = 0;
for (const s of ALL_SEED_SCHEMES) {
const source = await db.governmentSource.findFirst({
where: { name: s.sourceName },
});
const existing = await db.scheme.findUnique({ where: { slug: s.slug } });
const baseData = {
slug: s.slug,
name: s.name,
nameHi: s.nameHi,
shortDescription: s.shortDescription,
shortDescriptionHi: s.shortDescriptionHi,
description: s.description,
category: s.category,
ministry: s.ministry,
department: s.department,
jurisdiction: s.jurisdiction,
state: s.state ?? null,
beneficiaryType: s.beneficiaryType,
benefits: s.benefits,
benefitAmount: s.benefitAmount,
applicationUrl: s.applicationUrl,
infoUrl: s.infoUrl,
applicationWindow: s.applicationWindow,
applicationDeadline: s.applicationDeadline,
documentsJson: JSON.stringify(s.documents),
processJson: JSON.stringify(s.process),
keywords: s.keywords,
confidence: s.confidence,
extractionMethod: "SEED_REFERENCE",
sourceId: source?.id,
// NOTE: verificationStatus stays as-is if a real live check has already
// upgraded it — we never overwrite real verification with seed state.
};
let schemeId: string;
if (existing) {
const upd = await db.scheme
.update({ where: { slug: s.slug }, data: baseData })
.catch(() => db.scheme.create({
data: {
...baseData,
id: slugKey(s.slug),
verificationStatus: "REFERENCE",
dataCompiledAt: new Date(),
},
}));
schemeId = upd.id;
updated++;
} else {
const cr = await db.scheme.create({
data: {
...baseData,
id: slugKey(s.slug),
verificationStatus: "REFERENCE",
dataCompiledAt: new Date(),
},
});
schemeId = cr.id;
created++;
}
// --- eligibility rules ---
await db.schemeEligibilityRule.deleteMany({ where: { schemeId: schemeId } });
for (const r of s.rules) {
await db.schemeEligibilityRule.create({
data: {
schemeId: schemeId,
attribute: r.attribute,
operator: r.operator,
value: JSON.stringify(r.value ?? null),
required: r.required ?? true,
label: r.label,
labelHi: r.labelHi,
ruleGroup: r.ruleGroup ?? null,
},
});
}
// --- evidence (refresh excerpts, keep real LIVE_FETCH evidence) ---
await db.sourceEvidence.deleteMany({
where: { schemeId: schemeId, evidenceType: "GUIDELINE_EXCERPT" },
});
for (const e of s.evidence) {
await db.sourceEvidence.create({
data: {
schemeId: schemeId,
sourceUrl: e.sourceUrl,
excerpt: e.excerpt,
evidenceType: e.evidenceType ?? "GUIDELINE_EXCERPT",
note: e.note,
},
});
}
}
console.log(`[seed] schemes: ${created} created, ${updated} updated`);
}
async function main() {
console.log("[seed] starting…");
await seedSources();
await seedSchemes();
const counts = {
sources: await db.governmentSource.count(),
schemes: await db.scheme.count(),
rules: await db.schemeEligibilityRule.count(),
evidence: await db.sourceEvidence.count(),
};
console.log("[seed] done:", counts);
}
main()
.catch((e) => {
console.error("[seed] FAILED:", e);
process.exit(1);
})
.finally(() => db.$disconnect());