-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjetshare_seed.sql
More file actions
351 lines (326 loc) · 8.26 KB
/
Copy pathjetshare_seed.sql
File metadata and controls
351 lines (326 loc) · 8.26 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
-- JetShare Seed Script for Supabase SQL Editor
-- Check if settings exist, if not, create default
INSERT INTO jetshare_settings (handling_fee_percentage, allow_crypto_payments, allow_fiat_payments)
SELECT 7.5, true, true
WHERE NOT EXISTS (
SELECT 1 FROM jetshare_settings LIMIT 1
);
-- Helper function to generate UUIDs
CREATE OR REPLACE FUNCTION random_uuid() RETURNS uuid AS $$
BEGIN
RETURN gen_random_uuid();
END;
$$ LANGUAGE plpgsql;
-- Helper function to get random user ID with profile
CREATE OR REPLACE FUNCTION random_user_id() RETURNS uuid AS $$
DECLARE
user_id uuid;
BEGIN
SELECT p.id INTO user_id
FROM profiles p
WHERE p.user_type = 'traveler'
ORDER BY RANDOM()
LIMIT 1;
RETURN user_id;
END;
$$ LANGUAGE plpgsql;
-- Helper function to get different random user ID with profile
CREATE OR REPLACE FUNCTION different_random_user_id(exclude_id uuid) RETURNS uuid AS $$
DECLARE
user_id uuid;
BEGIN
SELECT p.id INTO user_id
FROM profiles p
WHERE p.id != exclude_id AND p.user_type = 'traveler'
ORDER BY RANDOM()
LIMIT 1;
RETURN user_id;
END;
$$ LANGUAGE plpgsql;
-- Helper function to generate random future date
CREATE OR REPLACE FUNCTION random_future_date(days_ahead int) RETURNS timestamptz AS $$
BEGIN
RETURN NOW() + (random() * days_ahead || ' days')::interval;
END;
$$ LANGUAGE plpgsql;
-- Helper function to generate random past date
CREATE OR REPLACE FUNCTION random_past_date(days_ago int) RETURNS timestamptz AS $$
BEGIN
RETURN NOW() - (random() * days_ago || ' days')::interval;
END;
$$ LANGUAGE plpgsql;
-- Helper function to generate random flight cost
CREATE OR REPLACE FUNCTION random_flight_cost() RETURNS numeric AS $$
DECLARE
min_cost int;
max_cost int;
bucket int;
BEGIN
bucket := floor(random() * 4);
IF bucket = 0 THEN
min_cost := 20000;
max_cost := 35000;
ELSIF bucket = 1 THEN
min_cost := 35000;
max_cost := 50000;
ELSIF bucket = 2 THEN
min_cost := 50000;
max_cost := 75000;
ELSE
min_cost := 75000;
max_cost := 100000;
END IF;
RETURN min_cost + floor(random() * (max_cost - min_cost));
END;
$$ LANGUAGE plpgsql;
-- Helper function to calculate share amount
CREATE OR REPLACE FUNCTION calculate_share_amount(total_cost numeric) RETURNS numeric AS $$
BEGIN
RETURN floor(total_cost * (0.4 + random() * 0.2));
END;
$$ LANGUAGE plpgsql;
-- Helper function to generate random location
CREATE OR REPLACE FUNCTION random_location() RETURNS text AS $$
DECLARE
locations text[] := ARRAY[
'New York, NY',
'Los Angeles, CA',
'Chicago, IL',
'Miami, FL',
'Las Vegas, NV',
'San Francisco, CA',
'Seattle, WA',
'Boston, MA',
'Dallas, TX',
'Atlanta, GA',
'Denver, CO',
'Austin, TX',
'Nashville, TN',
'New Orleans, LA',
'Washington, DC'
];
BEGIN
RETURN locations[floor(random() * array_length(locations, 1)) + 1];
END;
$$ LANGUAGE plpgsql;
-- Helper function to generate different random location
CREATE OR REPLACE FUNCTION different_random_location(exclude_location text) RETURNS text AS $$
DECLARE
result text;
BEGIN
LOOP
result := random_location();
EXIT WHEN result != exclude_location;
END LOOP;
RETURN result;
END;
$$ LANGUAGE plpgsql;
-- Create sample users and profiles first
DO $$
DECLARE
new_user_id uuid;
first_names text[] := ARRAY['John', 'Jane', 'Michael', 'Sarah', 'David', 'Emily', 'James', 'Emma', 'William', 'Olivia'];
last_names text[] := ARRAY['Smith', 'Johnson', 'Williams', 'Brown', 'Jones', 'Garcia', 'Miller', 'Davis', 'Rodriguez', 'Martinez'];
i int;
BEGIN
FOR i IN 1..10 LOOP
-- Create user in auth.users
INSERT INTO auth.users (id, email)
VALUES (
gen_random_uuid(),
lower(first_names[i] || '.' || last_names[i] || '@example.com')
)
RETURNING id INTO new_user_id;
-- Create corresponding profile
INSERT INTO profiles (
id,
first_name,
last_name,
user_type,
verification_status,
created_at,
updated_at
) VALUES (
new_user_id,
first_names[i],
last_names[i],
'traveler',
'verified',
NOW(),
NOW()
);
END LOOP;
END;
$$;
-- Create sample open offers
DO $$
DECLARE
departure text;
arrival text;
user_id uuid;
total_cost numeric;
share_amount numeric;
BEGIN
FOR i IN 1..5 LOOP
user_id := random_user_id();
departure := random_location();
arrival := different_random_location(departure);
total_cost := random_flight_cost();
share_amount := calculate_share_amount(total_cost);
INSERT INTO jetshare_offers (
user_id,
flight_date,
departure_location,
arrival_location,
total_flight_cost,
requested_share_amount,
status
) VALUES (
user_id,
random_future_date(60),
departure,
arrival,
total_cost,
share_amount,
'open'
);
END LOOP;
END;
$$;
-- Create sample accepted offers with transactions
DO $$
DECLARE
departure text;
arrival text;
user_id uuid;
matched_user_id uuid;
total_cost numeric;
share_amount numeric;
offer_id uuid;
handling_fee numeric;
BEGIN
FOR i IN 1..3 LOOP
user_id := random_user_id();
matched_user_id := different_random_user_id(user_id);
departure := random_location();
arrival := different_random_location(departure);
total_cost := random_flight_cost();
share_amount := calculate_share_amount(total_cost);
INSERT INTO jetshare_offers (
user_id,
flight_date,
departure_location,
arrival_location,
total_flight_cost,
requested_share_amount,
status,
matched_user_id
) VALUES (
user_id,
random_future_date(30),
departure,
arrival,
total_cost,
share_amount,
'accepted',
matched_user_id
) RETURNING id INTO offer_id;
-- Create transaction for this offer
handling_fee := share_amount * 0.075;
INSERT INTO jetshare_transactions (
offer_id,
payer_user_id,
recipient_user_id,
amount,
handling_fee,
payment_method,
payment_status,
transaction_date,
transaction_reference
) VALUES (
offer_id,
matched_user_id,
user_id,
share_amount,
handling_fee,
CASE WHEN random() > 0.5 THEN 'fiat' ELSE 'crypto' END,
'pending',
NOW(),
'tx_' || substr(md5(random()::text), 1, 10)
);
END LOOP;
END;
$$;
-- Create sample completed offers with transactions
DO $$
DECLARE
departure text;
arrival text;
user_id uuid;
matched_user_id uuid;
total_cost numeric;
share_amount numeric;
offer_id uuid;
handling_fee numeric;
BEGIN
FOR i IN 1..4 LOOP
user_id := random_user_id();
matched_user_id := different_random_user_id(user_id);
departure := random_location();
arrival := different_random_location(departure);
total_cost := random_flight_cost();
share_amount := calculate_share_amount(total_cost);
INSERT INTO jetshare_offers (
user_id,
flight_date,
departure_location,
arrival_location,
total_flight_cost,
requested_share_amount,
status,
matched_user_id
) VALUES (
user_id,
random_past_date(45),
departure,
arrival,
total_cost,
share_amount,
'completed',
matched_user_id
) RETURNING id INTO offer_id;
-- Create transaction for this offer
handling_fee := share_amount * 0.075;
INSERT INTO jetshare_transactions (
offer_id,
payer_user_id,
recipient_user_id,
amount,
handling_fee,
payment_method,
payment_status,
transaction_date,
transaction_reference
) VALUES (
offer_id,
matched_user_id,
user_id,
share_amount,
handling_fee,
CASE WHEN random() > 0.5 THEN 'fiat' ELSE 'crypto' END,
'completed',
random_past_date(30),
'tx_' || substr(md5(random()::text), 1, 10)
);
END LOOP;
END;
$$;
-- Clean up helper functions
DROP FUNCTION IF EXISTS random_user_id;
DROP FUNCTION IF EXISTS different_random_user_id;
DROP FUNCTION IF EXISTS random_future_date;
DROP FUNCTION IF EXISTS random_past_date;
DROP FUNCTION IF EXISTS random_flight_cost;
DROP FUNCTION IF EXISTS calculate_share_amount;
DROP FUNCTION IF EXISTS random_location;
DROP FUNCTION IF EXISTS different_random_location;