MakPr016 commited on
Commit
8deea46
·
1 Parent(s): 45ddad8

Updated static analyze

Browse files
Files changed (2) hide show
  1. data/generate.py +110 -0
  2. data/reseed_200_vendors.sql +0 -0
data/generate.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import uuid
3
+
4
+ # Configuration
5
+ INPUT_FILE = 'master_index.json'
6
+ OUTPUT_FILE = 'reseed_200_vendors.sql'
7
+
8
+ BIG_COUNTRIES = ['Germany', 'France', 'United Kingdom', 'Italy', 'Spain', 'Poland']
9
+ TARGET_COUNTRIES = [
10
+ 'Austria', 'Belgium', 'Bulgaria', 'Croatia', 'Cyprus', 'Czechia', 'Denmark',
11
+ 'Estonia', 'Finland', 'France', 'Germany', 'Greece', 'Hungary', 'Ireland',
12
+ 'Italy', 'Latvia', 'Lithuania', 'Luxembourg', 'Malta', 'Netherlands', 'Norway',
13
+ 'Poland', 'Portugal', 'Romania', 'Slovakia', 'Slovenia', 'Spain', 'Sweden',
14
+ 'Switzerland', 'United Kingdom'
15
+ ]
16
+
17
+ def clean_sql(text):
18
+ if not text: return ""
19
+ return str(text).replace("'", "''")
20
+
21
+ def generate_sql():
22
+ try:
23
+ with open(INPUT_FILE, 'r', encoding='utf-8') as f:
24
+ data = json.load(f)
25
+ except FileNotFoundError:
26
+ print(f"Error: {INPUT_FILE} not found.")
27
+ return
28
+
29
+ vendors_by_country = {country: [] for country in TARGET_COUNTRIES}
30
+
31
+ # Sort vendors into country buckets
32
+ for v in data.get('vendors', []):
33
+ country = v.get('countries_served', [None])[0]
34
+ if country in vendors_by_country:
35
+ vendors_by_country[country].append(v)
36
+
37
+ sql_statements = []
38
+
39
+ # Boilerplate Setup
40
+ sql_statements.append("-- ============================================")
41
+ sql_statements.append("-- AUTO-GENERATED COMPLETE VENDOR RESEED")
42
+ sql_statements.append("-- ============================================")
43
+ sql_statements.append("ALTER TABLE public.profiles DISABLE ROW LEVEL SECURITY;")
44
+ sql_statements.append("ALTER TABLE public.vendors DISABLE ROW LEVEL SECURITY;")
45
+
46
+ # Re-declare the helper if not exists (to ensure the script is self-contained)
47
+ sql_statements.append("""
48
+ CREATE OR REPLACE FUNCTION public.create_user(user_id uuid, email text, password text, user_role text) RETURNS void AS $$
49
+ BEGIN
50
+ INSERT INTO auth.users (instance_id, id, aud, role, email, encrypted_password, email_confirmed_at, raw_app_meta_data, raw_user_meta_data, created_at, updated_at)
51
+ VALUES ('00000000-0000-0000-0000-000000000000', user_id, 'authenticated', 'authenticated', email, crypt(password, gen_salt('bf')), NOW(),
52
+ jsonb_build_object('provider', 'email', 'providers', ARRAY['email']), jsonb_build_object('role', user_role), NOW(), NOW());
53
+ INSERT INTO auth.identities (id, provider_id, user_id, identity_data, provider, last_sign_in_at, created_at, updated_at)
54
+ VALUES (gen_random_uuid(), gen_random_uuid(), user_id, format('{"sub":"%s","email":"%s"}', user_id::text, email)::jsonb, 'email', NOW(), NOW(), NOW());
55
+ END;
56
+ $$ LANGUAGE plpgsql SECURITY DEFINER;
57
+ """)
58
+
59
+ total_count = 0
60
+ for country, vendor_list in vendors_by_country.items():
61
+ limit = 10 if country in BIG_COUNTRIES else 5
62
+ selected_vendors = vendor_list[:limit]
63
+
64
+ if not selected_vendors: continue
65
+
66
+ sql_statements.append(f"\n-- VENDORS FOR {country.upper()}")
67
+
68
+ for v in selected_vendors:
69
+ u_id = str(uuid.uuid4())
70
+ v_id = str(uuid.uuid4())
71
+
72
+ name = clean_sql(v['legal_name'])
73
+ email = v['primary_procurement_contact']['email']
74
+ contact_name = clean_sql(v['primary_procurement_contact']['full_name'])
75
+ phone = v['primary_procurement_contact'].get('direct_phone', '+00 000 000')
76
+ reg_num = v.get('registration_number', f'REG-{uuid.uuid4().hex[:6]}')
77
+ addr = clean_sql(v['general_inquiry']['physical_address'])
78
+ city = addr.split(',')[1].strip() if ',' in addr else "City"
79
+
80
+ # 1. Create Auth User
81
+ sql_statements.append(f"SELECT public.create_user('{u_id}', '{email}', 'Vendor@123', 'vendor');")
82
+
83
+ # 2. Insert Profile
84
+ sql_statements.append(
85
+ f"INSERT INTO public.profiles (id, email, full_name, role, organization_name, phone, address, city, country, is_active) "
86
+ f"VALUES ('{u_id}', '{email}', '{contact_name}', 'vendor', '{name}', '{phone}', '{addr}', '{city}', '{country}', true);"
87
+ )
88
+
89
+ # 3. Insert Vendor Details
90
+ categories = "{" + ",".join([f'"{c}"' for c in v.get('primary_categories', [])]) + "}"
91
+ markets = "{" + ",".join([f'"{m}"' for m in v.get('countries_served', [])]) + "}"
92
+ rating = round(v.get('confidence_score', 80) / 20.0, 1)
93
+
94
+ sql_statements.append(
95
+ f"INSERT INTO public.vendors (id, user_id, vendor_name, company_registration, vendor_type, rating, contact_person, contact_email, contact_phone, address, city, country, is_verified, vat_id, target_markets, interested_categories, duns_number, economic_role) "
96
+ f"VALUES ('{v_id}', '{u_id}', '{name}', '{reg_num}', '{v['primary_categories'][0]}', {rating}, '{contact_name}', '{email}', '{phone}', '{addr}', '{city}', '{country}', true, '{v.get('vat_number', '')}', '{markets}', '{categories}', '{v.get('vendor_id', '')}', 'distributor');"
97
+ )
98
+ total_count += 1
99
+
100
+ sql_statements.append("\nALTER TABLE public.profiles ENABLE ROW LEVEL SECURITY;")
101
+ sql_statements.append("ALTER TABLE public.vendors ENABLE ROW LEVEL SECURITY;")
102
+ sql_statements.append(f"\n-- FINISHED: Created {total_count} Vendors")
103
+
104
+ with open(OUTPUT_FILE, 'w', encoding='utf-8') as f:
105
+ f.write("\n".join(sql_statements))
106
+
107
+ print(f"✅ Generated {OUTPUT_FILE} with {total_count} vendors.")
108
+
109
+ if __name__ == "__main__":
110
+ generate_sql()
data/reseed_200_vendors.sql ADDED
The diff for this file is too large to render. See raw diff