File size: 2,558 Bytes
7f88bdf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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);
});