-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSUPABASE_SCHEMA.sql
More file actions
355 lines (318 loc) · 11.9 KB
/
Copy pathSUPABASE_SCHEMA.sql
File metadata and controls
355 lines (318 loc) · 11.9 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
352
353
354
355
-- Create companies table with all required fields for company creation
CREATE TABLE companies (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
name VARCHAR(255) NOT NULL,
registration_number VARCHAR(255),
address TEXT,
phone VARCHAR(20),
email VARCHAR(255),
website VARCHAR(255),
industry VARCHAR(100),
description TEXT,
user_id UUID REFERENCES auth.users(id),
-- Additional company fields
pan_number VARCHAR(10),
city VARCHAR(100),
state VARCHAR(100),
pincode VARCHAR(6),
incorporation_type VARCHAR(50),
instrument VARCHAR(50),
llp_agreement_copy TEXT,
moa TEXT,
aoi TEXT,
spv_memo TEXT,
risk_disclosure TEXT
);
-- Create company_board_members table for board member information
CREATE TABLE company_board_members (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
company_id UUID REFERENCES companies(id) ON DELETE CASCADE,
name VARCHAR(255) NOT NULL,
phone_number VARCHAR(20),
email VARCHAR(255),
dsc_din VARCHAR(100),
note TEXT,
has_dsc_din VARCHAR(10),
relevant_document TEXT,
provides_customer_support VARCHAR(10),
whatsapp_number VARCHAR(20)
);
-- Create company_legal_advisors table for legal advisor information
CREATE TABLE company_legal_advisors (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
company_id UUID REFERENCES companies(id) ON DELETE CASCADE,
name VARCHAR(255),
full_name VARCHAR(255),
firm VARCHAR(255),
firm_name VARCHAR(255),
email VARCHAR(255),
phone VARCHAR(20),
designation VARCHAR(100),
linkedin VARCHAR(255),
website VARCHAR(255),
address TEXT,
city VARCHAR(100),
state VARCHAR(100),
pincode VARCHAR(6),
country VARCHAR(100)
);
-- Create company_bank_accounts table for bank account information
CREATE TABLE company_bank_accounts (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
company_id UUID REFERENCES companies(id) ON DELETE CASCADE,
account_holder_name VARCHAR(255),
bank_name VARCHAR(255),
account_number VARCHAR(50),
ifsc_code VARCHAR(20),
category VARCHAR(50),
additional_information TEXT
);
-- Create assets table with all required fields for asset creation
CREATE TABLE assets (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
name VARCHAR(255) NOT NULL,
description TEXT,
type VARCHAR(100),
value DECIMAL(12, 2),
location TEXT,
status VARCHAR(50) DEFAULT 'active',
company_id UUID REFERENCES companies(id),
-- Asset-specific fields
category VARCHAR(100),
sub_category VARCHAR(100),
stage VARCHAR(50),
style VARCHAR(50),
currency VARCHAR(10),
instrument_type VARCHAR(50),
class VARCHAR(50),
about TEXT,
-- Category-specific fields
server_capacity INTEGER,
power_capacity DECIMAL(10, 2),
tier_level VARCHAR(20),
connectivity_providers TEXT,
edge_nodes INTEGER,
latency_ms INTEGER,
coverage_area VARCHAR(100),
storage_capacity DECIMAL(12, 2),
temperature_range VARCHAR(50),
certifications TEXT,
validation_level VARCHAR(20),
warehouse_area DECIMAL(12, 2),
dock_doors INTEGER,
clear_height DECIMAL(5, 2),
automated_systems BOOLEAN,
proximity_to_transport TEXT,
total_desks INTEGER,
private_offices INTEGER,
meeting_rooms INTEGER,
has_24_hour_access BOOLEAN,
amenities_included TEXT,
solar_capacity DECIMAL(10, 2),
manufacturing_area DECIMAL(12, 2),
industrial_zoning VARCHAR(100),
battery_capacity DECIMAL(12, 2),
battery_technology VARCHAR(100),
grid_connection_capacity DECIMAL(10, 2),
renewable_capacity DECIMAL(10, 2),
sustainability_certifications TEXT,
retail_space DECIMAL(12, 2),
frontage DECIMAL(8, 2),
foot_traffic INTEGER,
corner_location BOOLEAN,
nearby_anchors TEXT,
number_of_rooms INTEGER,
star_rating VARCHAR(20),
average_occupancy DECIMAL(5, 2),
brand_affiliation VARCHAR(100),
proximity_to_attractions TEXT,
total_floors INTEGER,
retail_area DECIMAL(12, 2),
office_area DECIMAL(12, 2),
residential_units INTEGER,
transit_connected BOOLEAN,
mixed_use_components TEXT,
-- Token information fields
total_number_of_sfts INTEGER,
price_per_sft DECIMAL(12, 2),
expected_annual_return DECIMAL(5, 2),
investment_horizon INTEGER
);
-- Create asset_locations table for nearby locations
CREATE TABLE asset_locations (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
asset_id UUID REFERENCES assets(id) ON DELETE CASCADE,
name VARCHAR(255),
distance DECIMAL(8, 2),
type VARCHAR(50)
);
-- Create asset_documents table for media and documents
CREATE TABLE asset_documents (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
asset_id UUID REFERENCES assets(id) ON DELETE CASCADE,
type VARCHAR(50), -- 'image', 'video', 'document', 'pitch_deck'
url TEXT,
name VARCHAR(255),
description TEXT
);
-- Create indexes for better performance
CREATE INDEX idx_companies_user_id ON companies(user_id);
CREATE INDEX idx_users_company_id ON users(company_id);
CREATE INDEX idx_assets_company_id ON assets(company_id);
CREATE INDEX idx_company_board_members_company_id ON company_board_members(company_id);
CREATE INDEX idx_company_legal_advisors_company_id ON company_legal_advisors(company_id);
CREATE INDEX idx_company_bank_accounts_company_id ON company_bank_accounts(company_id);
CREATE INDEX idx_asset_locations_asset_id ON asset_locations(asset_id);
CREATE INDEX idx_asset_documents_asset_id ON asset_documents(asset_id);
-- Set up Row Level Security (RLS)
ALTER TABLE companies ENABLE ROW LEVEL SECURITY;
ALTER TABLE users ENABLE ROW LEVEL SECURITY;
ALTER TABLE assets ENABLE ROW LEVEL SECURITY;
ALTER TABLE company_board_members ENABLE ROW LEVEL SECURITY;
ALTER TABLE company_legal_advisors ENABLE ROW LEVEL SECURITY;
ALTER TABLE company_bank_accounts ENABLE ROW LEVEL SECURITY;
ALTER TABLE asset_locations ENABLE ROW LEVEL SECURITY;
ALTER TABLE asset_documents ENABLE ROW LEVEL SECURITY;
-- Create policies for companies
CREATE POLICY "Users can view their own companies" ON companies
FOR SELECT USING (auth.uid() = user_id);
CREATE POLICY "Users can insert their own companies" ON companies
FOR INSERT WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update their own companies" ON companies
FOR UPDATE USING (auth.uid() = user_id);
CREATE POLICY "Users can delete their own companies" ON companies
FOR DELETE USING (auth.uid() = user_id);
-- Create policies for company_board_members
CREATE POLICY "Users can view board members for their companies" ON company_board_members
FOR SELECT USING (company_id IN (
SELECT id FROM companies WHERE user_id = auth.uid()
));
CREATE POLICY "Users can insert board members for their companies" ON company_board_members
FOR INSERT WITH CHECK (company_id IN (
SELECT id FROM companies WHERE user_id = auth.uid()
));
CREATE POLICY "Users can update board members for their companies" ON company_board_members
FOR UPDATE USING (company_id IN (
SELECT id FROM companies WHERE user_id = auth.uid()
));
CREATE POLICY "Users can delete board members for their companies" ON company_board_members
FOR DELETE USING (company_id IN (
SELECT id FROM companies WHERE user_id = auth.uid()
));
-- Create policies for company_legal_advisors
CREATE POLICY "Users can view legal advisors for their companies" ON company_legal_advisors
FOR SELECT USING (company_id IN (
SELECT id FROM companies WHERE user_id = auth.uid()
));
CREATE POLICY "Users can insert legal advisors for their companies" ON company_legal_advisors
FOR INSERT WITH CHECK (company_id IN (
SELECT id FROM companies WHERE user_id = auth.uid()
));
CREATE POLICY "Users can update legal advisors for their companies" ON company_legal_advisors
FOR UPDATE USING (company_id IN (
SELECT id FROM companies WHERE user_id = auth.uid()
));
CREATE POLICY "Users can delete legal advisors for their companies" ON company_legal_advisors
FOR DELETE USING (company_id IN (
SELECT id FROM companies WHERE user_id = auth.uid()
));
-- Create policies for company_bank_accounts
CREATE POLICY "Users can view bank accounts for their companies" ON company_bank_accounts
FOR SELECT USING (company_id IN (
SELECT id FROM companies WHERE user_id = auth.uid()
));
CREATE POLICY "Users can insert bank accounts for their companies" ON company_bank_accounts
FOR INSERT WITH CHECK (company_id IN (
SELECT id FROM companies WHERE user_id = auth.uid()
));
CREATE POLICY "Users can update bank accounts for their companies" ON company_bank_accounts
FOR UPDATE USING (company_id IN (
SELECT id FROM companies WHERE user_id = auth.uid()
));
CREATE POLICY "Users can delete bank accounts for their companies" ON company_bank_accounts
FOR DELETE USING (company_id IN (
SELECT id FROM companies WHERE user_id = auth.uid()
));
-- Create policies for assets
CREATE POLICY "Users can view assets for their companies" ON assets
FOR SELECT USING (company_id IN (
SELECT id FROM companies WHERE user_id = auth.uid()
));
CREATE POLICY "Users can insert assets for their companies" ON assets
FOR INSERT WITH CHECK (company_id IN (
SELECT id FROM companies WHERE user_id = auth.uid()
));
CREATE POLICY "Users can update assets for their companies" ON assets
FOR UPDATE USING (company_id IN (
SELECT id FROM companies WHERE user_id = auth.uid()
));
CREATE POLICY "Users can delete assets for their companies" ON assets
FOR DELETE USING (company_id IN (
SELECT id FROM companies WHERE user_id = auth.uid()
));
-- Create policies for asset_locations
CREATE POLICY "Users can view locations for their assets" ON asset_locations
FOR SELECT USING (asset_id IN (
SELECT id FROM assets WHERE company_id IN (
SELECT id FROM companies WHERE user_id = auth.uid()
)
));
CREATE POLICY "Users can insert locations for their assets" ON asset_locations
FOR INSERT WITH CHECK (asset_id IN (
SELECT id FROM assets WHERE company_id IN (
SELECT id FROM companies WHERE user_id = auth.uid()
)
));
CREATE POLICY "Users can update locations for their assets" ON asset_locations
FOR UPDATE USING (asset_id IN (
SELECT id FROM assets WHERE company_id IN (
SELECT id FROM companies WHERE user_id = auth.uid()
)
));
CREATE POLICY "Users can delete locations for their assets" ON asset_locations
FOR DELETE USING (asset_id IN (
SELECT id FROM assets WHERE company_id IN (
SELECT id FROM companies WHERE user_id = auth.uid()
)
));
-- Create policies for asset_documents
CREATE POLICY "Users can view documents for their assets" ON asset_documents
FOR SELECT USING (asset_id IN (
SELECT id FROM assets WHERE company_id IN (
SELECT id FROM companies WHERE user_id = auth.uid()
)
));
CREATE POLICY "Users can insert documents for their assets" ON asset_documents
FOR INSERT WITH CHECK (asset_id IN (
SELECT id FROM assets WHERE company_id IN (
SELECT id FROM companies WHERE user_id = auth.uid()
)
));
CREATE POLICY "Users can update documents for their assets" ON asset_documents
FOR UPDATE USING (asset_id IN (
SELECT id FROM assets WHERE company_id IN (
SELECT id FROM companies WHERE user_id = auth.uid()
)
));
CREATE POLICY "Users can delete documents for their assets" ON asset_documents
FOR DELETE USING (asset_id IN (
SELECT id FROM assets WHERE company_id IN (
SELECT id FROM companies WHERE user_id = auth.uid()
)
));
-- Grant permissions to authenticated users
GRANT ALL ON TABLE companies TO authenticated;
GRANT ALL ON TABLE users TO authenticated;
GRANT ALL ON TABLE assets TO authenticated;
GRANT ALL ON TABLE company_board_members TO authenticated;
GRANT ALL ON TABLE company_legal_advisors TO authenticated;
GRANT ALL ON TABLE company_bank_accounts TO authenticated;
GRANT ALL ON TABLE asset_locations TO authenticated;
GRANT ALL ON TABLE asset_documents TO authenticated;