| import fs from "node:fs"; |
| import path from "node:path"; |
| import { createClient } from "@supabase/supabase-js"; |
|
|
| const repoRoot = process.cwd(); |
|
|
| function loadEnv(filePath) { |
| if (!fs.existsSync(filePath)) { |
| return {}; |
| } |
|
|
| const env = {}; |
| const lines = fs.readFileSync(filePath, "utf8").split(/\r?\n/); |
| for (const raw of lines) { |
| const line = raw.trim(); |
| if (!line || line.startsWith("#")) { |
| continue; |
| } |
| const idx = line.indexOf("="); |
| if (idx < 0) { |
| continue; |
| } |
| const key = line.slice(0, idx).trim(); |
| const value = line.slice(idx + 1); |
| env[key] = value; |
| } |
| return env; |
| } |
|
|
| function required(name, env) { |
| const value = process.env[name] || env[name]; |
| if (!value) { |
| throw new Error(`${name} is required for dev bootstrap.`); |
| } |
| return value; |
| } |
|
|
| async function main() { |
| const env = loadEnv(path.join(repoRoot, ".env.local")); |
| const url = required("NEXT_PUBLIC_SUPABASE_URL", env); |
| const key = required("SUPABASE_SERVICE_ROLE_KEY", env); |
| const organizationId = required("DEV_ORGANIZATION_ID", env); |
| const userId = required("DEV_USER_ID", env); |
|
|
| const supabase = createClient(url, key, { |
| auth: { persistSession: false, autoRefreshToken: false } |
| }); |
|
|
| const slug = `dev-org-${organizationId.slice(0, 8)}`; |
| const { error: orgError } = await supabase.from("organizations").upsert( |
| { |
| id: organizationId, |
| name: "TenderHub Dev Org", |
| slug |
| }, |
| { onConflict: "id" } |
| ); |
| if (orgError) { |
| throw new Error(`Failed to upsert organization: ${orgError.message}`); |
| } |
|
|
| const { error: memberError } = await supabase.from("members").upsert( |
| { |
| organization_id: organizationId, |
| user_id: userId, |
| role: "owner" |
| }, |
| { onConflict: "organization_id,user_id" } |
| ); |
| if (memberError) { |
| throw new Error(`Failed to upsert member: ${memberError.message}`); |
| } |
|
|
| const { error: profileError } = await supabase.from("company_profiles").upsert( |
| { |
| organization_id: organizationId, |
| legal_name: "TenderHub Dev Company", |
| kra_pin: "P000000000X", |
| counties: ["Nairobi"], |
| profile_data: {} |
| }, |
| { onConflict: "organization_id" } |
| ); |
| if (profileError) { |
| throw new Error(`Failed to upsert company profile: ${profileError.message}`); |
| } |
|
|
| console.log("Dev bootstrap complete."); |
| console.log(`organization_id=${organizationId}`); |
| console.log(`user_id=${userId}`); |
| } |
|
|
| main().catch((error) => { |
| console.error(error instanceof Error ? error.message : String(error)); |
| process.exit(1); |
| }); |
|
|