Repository navigation
Expand file tree
/
Copy pathseed.ts
More file actions
254 lines (233 loc) · 9.22 KB
/
Copy pathseed.ts
File metadata and controls
254 lines (233 loc) · 9.22 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/**
* Development seed.
*
* pnpm db:seed
*
* Loads the reference data the application expects (roles, feature flags,
* system settings) plus — ONLY outside production — a small set of obviously
* fake accounts and the `INKLOOMEARLYACCESS` campaign.
*
* The reference data is idempotent and safe anywhere. The sample data is
* refused outright when INKLOOM_ENV is production, so this script can never
* put test users into a real database.
*/
import { sql } from "drizzle-orm";
import { createDb, type Database } from "@inkloom/db/client";
import { newId } from "@inkloom/db";
import { fingerprintCode, maskCode, normalizeCode } from "@inkloom/core/access-codes";
import { FEATURE_FLAGS, SYSTEM_SETTINGS } from "@inkloom/core/settings";
import { ROLE_PERMISSIONS, ROLE_RANK, ROLES } from "@inkloom/core/rbac";
import { describeTarget, optional, required } from "./_env";
const ROLE_DESCRIPTIONS: Record<string, string> = {
user: "Ordinary Inkloom account",
support: "Front-line support: read accounts, answer tickets",
operations: "Operations and incident response: suspend, revoke, pause",
admin: "Administrator: full read plus campaign lifecycle",
super_admin: "Full control including credits, roles and emergency controls",
};
/** Reference data. Idempotent, and safe to run against any environment. */
async function seedReferenceData(db: Database) {
for (const name of ROLES) {
await db.execute(sql`
INSERT INTO roles (id, name, rank, description, permissions)
VALUES (
${newId("rol")}, ${name}, ${ROLE_RANK[name]},
${ROLE_DESCRIPTIONS[name] ?? name},
${JSON.stringify(ROLE_PERMISSIONS[name])}::jsonb
)
ON CONFLICT (name) DO UPDATE
SET rank = EXCLUDED.rank,
description = EXCLUDED.description,
permissions = EXCLUDED.permissions,
updated_at = now()
`);
}
console.log(` roles ${ROLES.length} seeded`);
for (const flag of Object.values(FEATURE_FLAGS)) {
// DO NOTHING, not DO UPDATE: an operator who has turned a flag off must not
// have it silently turned back on by a deploy running the seed.
await db.execute(sql`
INSERT INTO feature_flags (id, key, description, enabled, high_risk)
VALUES (${newId("flag")}, ${flag.key}, ${flag.description}, ${flag.default}, ${flag.highRisk})
ON CONFLICT (key) DO NOTHING
`);
}
console.log(` feature_flags ${Object.keys(FEATURE_FLAGS).length} seeded`);
for (const setting of Object.values(SYSTEM_SETTINGS)) {
await db.execute(sql`
INSERT INTO system_settings (id, key, value, description, high_risk)
VALUES (
${newId("set")}, ${setting.key},
${JSON.stringify(setting.default)}::jsonb,
${setting.description}, ${setting.highRisk}
)
ON CONFLICT (key) DO NOTHING
`);
}
console.log(` system_settings ${Object.keys(SYSTEM_SETTINGS).length} seeded`);
}
/** The launch campaign, created through the same fingerprinting path as the API. */
async function seedLaunchCampaign(db: Database, pepper: string) {
const plaintext = "INKLOOMEARLYACCESS";
const normalized = normalizeCode(plaintext);
const fingerprint = await fingerprintCode(normalized, pepper);
const { masked, last4 } = maskCode(normalized);
const result = await db.execute<{ id: string }>(sql`
INSERT INTO access_code_campaigns
(id, name, description, code_fingerprint, code_masked, code_last4,
credit_amount, max_total_redemptions, max_redemptions_per_user,
expires_at, target_cohort, status)
VALUES (
${newId("cmp")},
'Inkloom Early Access',
'Seeded launch campaign for local development. Not hardcoded in any route.',
${fingerprint}, ${masked}, ${last4},
20, 2000, 1,
${new Date(Date.now() + 365 * 24 * 3600 * 1000)},
'launch-2026',
'enabled'
)
ON CONFLICT (code_fingerprint) DO NOTHING
RETURNING id
`);
if (result.rows.length > 0) {
console.log(` campaign INKLOOMEARLYACCESS -> ${masked} ($20 in credits)`);
} else {
console.log(` campaign INKLOOMEARLYACCESS already present (${masked})`);
}
}
/**
* Obviously-fake development accounts.
*
* `@example.test` is a reserved TLD that can never resolve, so none of these
* can ever receive real mail. They have NO password set, so they cannot be
* signed into — they exist to make the admin screens legible during
* development, not to be used.
*/
async function seedSampleUsers(db: Database) {
const samples = [
{
name: "Ada Lovelace",
email: "ada@example.test",
verified: true,
status: "active",
credits: 20,
},
{
name: "Grace Hopper",
email: "grace@example.test",
verified: true,
status: "active",
credits: 10,
},
{
name: "Alan Turing",
email: "alan@example.test",
verified: false,
status: "active",
credits: 0,
},
{
name: "Spam Bot",
email: "spam@example.test",
verified: true,
status: "suspended",
credits: 0,
},
];
for (const sample of samples) {
const userId = newId("usr");
const inserted = await db.execute<{ id: string }>(sql`
INSERT INTO users (id, name, email, email_verified, status, suspended_at, suspended_reason, early_access_joined_at)
VALUES (
${userId}, ${sample.name}, ${sample.email}, ${sample.verified}, ${sample.status},
${sample.status === "suspended" ? new Date() : null},
${sample.status === "suspended" ? "Seeded example of a suspended account" : null},
now()
)
ON CONFLICT (normalized_email) DO NOTHING
RETURNING id
`);
const id = inserted.rows[0]?.id;
if (!id) continue;
const walletId = newId("wal");
await db.execute(sql`
INSERT INTO credit_wallets (id, user_id, balance) VALUES (${walletId}, ${id}, 0)
ON CONFLICT (user_id) DO NOTHING
`);
await db.execute(sql`
INSERT INTO notification_preferences (id, user_id) VALUES (${newId("npf")}, ${id})
ON CONFLICT (user_id) DO NOTHING
`);
await db.execute(sql`
INSERT INTO profiles (id, user_id, display_name) VALUES (${newId("prf")}, ${id}, ${sample.name})
ON CONFLICT (user_id) DO NOTHING
`);
if (sample.credits > 0) {
// Written through the ledger, not by setting a balance directly, so the
// seed data satisfies the same invariant as real data and reconciliation
// reports zero drift on a freshly seeded database.
const entryId = newId("led");
await db.execute(sql`
INSERT INTO credit_ledger
(id, user_id, wallet_id, amount, type, balance_after, reference_type,
idempotency_key, actor_type, reason, metadata)
VALUES (
${entryId}, ${id}, ${walletId}, ${sample.credits}, 'PROMOTIONAL_GRANT',
${sample.credits}, 'system_bootstrap', ${"seed_" + id}, 'system',
'Development seed data', ${JSON.stringify({ seed: true })}::jsonb
)
`);
await db.execute(sql`
UPDATE credit_wallets
SET balance = ${sample.credits}, version = 1, last_entry_id = ${entryId}
WHERE id = ${walletId}
`);
}
}
console.log(` sample users ${samples.length} (all @example.test, no passwords)`);
}
async function main() {
const url = required("DATABASE_URL");
const env = optional("INKLOOM_ENV", "development");
/*
* Reference data without the fixtures, in an environment that would normally
* get them.
*
* Staging wants this: the console's counters are only worth reading if every
* figure came from real activity, and six seeded accounts sitting in the user
* total make "12 signups" a number nobody can act on. The alternative was to
* claim INKLOOM_ENV=production to reach the same branch, which would be a lie
* told to a script that is specifically trying to protect production.
*/
const referenceOnly = process.argv.includes("--reference-only") || env === "production";
// Only the campaign needs the pepper, so demanding it for a reference-only
// run would block a seed that has no use for it.
const pepper = referenceOnly ? "" : required("ACCESS_CODE_PEPPER");
const { db, pool } = createDb({ connectionString: url, max: 1 });
try {
console.log(`\n Seeding ${describeTarget(url)} (${env})\n`);
await seedReferenceData(db);
if (referenceOnly) {
const why = env === "production" ? "Production" : "Reference-only";
console.log(`\n ${why}: reference data only. No sample users, no seeded campaign.`);
console.log(" Create the launch campaign with `pnpm codes:create` or from /admin.\n");
return;
}
await seedLaunchCampaign(db, pepper);
await seedSampleUsers(db);
console.log("\n Done.\n");
console.log(" Next:");
console.log(" 1. Sign up at http://localhost:5173/auth/signup");
console.log(" 2. Read the verification mail at http://localhost:8025 (Mailpit)");
console.log(" 3. pnpm bootstrap:superadmin --email <your address>");
console.log(" 4. Enrol in 2FA at /app/security, then open /admin");
console.log(" 5. Redeem INKLOOMEARLYACCESS at /app/redeem for $20 in credits\n");
} finally {
await pool.end();
}
}
main().catch((error) => {
console.error("Seed failed:", error);
process.exit(1);
});