-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
154 lines (141 loc) · 4.93 KB
/
schema.sql
File metadata and controls
154 lines (141 loc) · 4.93 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
CREATE TABLE IF NOT EXISTS companies (
id UUID PRIMARY KEY,
name TEXT NOT NULL,
slug TEXT UNIQUE NOT NULL,
plan TEXT NOT NULL,
owner_id UUID,
created_at TIMESTAMP NOT NULL DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY,
company_id UUID NOT NULL REFERENCES companies(id) ON DELETE CASCADE,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
password_hash TEXT NOT NULL,
role TEXT NOT NULL CHECK (role IN ('Admin', 'Estimator', 'Viewer')),
profile_settings JSONB NOT NULL DEFAULT '{}'::jsonb,
is_active BOOLEAN NOT NULL DEFAULT TRUE,
created_at TIMESTAMP NOT NULL DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS projects (
id UUID PRIMARY KEY,
company_id UUID NOT NULL REFERENCES companies(id) ON DELETE CASCADE,
name TEXT NOT NULL,
location TEXT NOT NULL,
description TEXT NOT NULL,
status TEXT NOT NULL,
area_sqm NUMERIC(10, 2) NOT NULL,
blueprint_summary JSONB NOT NULL DEFAULT '{}'::jsonb,
created_at TIMESTAMP NOT NULL DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS documents (
id UUID PRIMARY KEY,
company_id UUID NOT NULL REFERENCES companies(id) ON DELETE CASCADE,
project_id UUID NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
filename TEXT NOT NULL,
stored_path TEXT NOT NULL,
notes TEXT NOT NULL DEFAULT '',
area_hint NUMERIC(10, 2),
extraction_summary TEXT NOT NULL,
extracted JSONB NOT NULL DEFAULT '{}'::jsonb,
boq JSONB NOT NULL DEFAULT '[]'::jsonb,
review_status TEXT NOT NULL DEFAULT 'Pending',
created_at TIMESTAMP NOT NULL DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS estimate_templates (
id UUID PRIMARY KEY,
company_id UUID NOT NULL REFERENCES companies(id) ON DELETE CASCADE,
name TEXT NOT NULL,
overhead_percent NUMERIC(5, 2) NOT NULL,
profit_percent NUMERIC(5, 2) NOT NULL,
contingency_percent NUMERIC(5, 2) NOT NULL
);
CREATE TABLE IF NOT EXISTS prompt_templates (
id UUID PRIMARY KEY,
company_id UUID NOT NULL REFERENCES companies(id) ON DELETE CASCADE,
label TEXT NOT NULL,
type TEXT NOT NULL DEFAULT 'General',
is_default BOOLEAN NOT NULL DEFAULT FALSE,
prompt TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS materials (
id UUID PRIMARY KEY,
company_id UUID NOT NULL REFERENCES companies(id) ON DELETE CASCADE,
name TEXT NOT NULL,
unit TEXT NOT NULL,
average_price NUMERIC(12, 2) NOT NULL,
last_month_price NUMERIC(12, 2) NOT NULL,
trend TEXT NOT NULL,
suppliers JSONB NOT NULL DEFAULT '[]'::jsonb
);
CREATE TABLE IF NOT EXISTS estimates (
id UUID PRIMARY KEY,
company_id UUID NOT NULL REFERENCES companies(id) ON DELETE CASCADE,
project_id UUID REFERENCES projects(id) ON DELETE SET NULL,
prompt TEXT NOT NULL,
status TEXT NOT NULL DEFAULT 'Draft',
location TEXT,
area_sqm NUMERIC(10, 2),
direct_cost NUMERIC(12, 2) NOT NULL,
final_contract_price NUMERIC(12, 2) NOT NULL,
labor_cost NUMERIC(12, 2) NOT NULL,
equipment_cost NUMERIC(12, 2) NOT NULL,
waste_factor_percent NUMERIC(5, 2) NOT NULL,
overhead_percent NUMERIC(5, 2) NOT NULL,
profit_percent NUMERIC(5, 2) NOT NULL,
contingency_percent NUMERIC(5, 2) NOT NULL,
items JSONB NOT NULL DEFAULT '[]'::jsonb,
approved_by_user_id UUID REFERENCES users(id) ON DELETE SET NULL,
reviewed_at TIMESTAMP,
approved_at TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
created_at TIMESTAMP NOT NULL DEFAULT NOW()
);
ALTER TABLE estimates ADD COLUMN IF NOT EXISTS status TEXT NOT NULL DEFAULT 'Draft';
ALTER TABLE estimates ADD COLUMN IF NOT EXISTS approved_by_user_id UUID REFERENCES users(id) ON DELETE SET NULL;
ALTER TABLE estimates ADD COLUMN IF NOT EXISTS reviewed_at TIMESTAMP;
ALTER TABLE estimates ADD COLUMN IF NOT EXISTS approved_at TIMESTAMP;
ALTER TABLE estimates ADD COLUMN IF NOT EXISTS updated_at TIMESTAMP NOT NULL DEFAULT NOW();
CREATE TABLE IF NOT EXISTS price_research (
id TEXT PRIMARY KEY,
material TEXT NOT NULL,
supplier TEXT NOT NULL,
source TEXT NOT NULL DEFAULT 'seed',
location TEXT NOT NULL,
price NUMERIC(12, 2) NOT NULL,
unit TEXT NOT NULL,
delivery TEXT NOT NULL,
distance_km NUMERIC(8, 2) NOT NULL,
confidence TEXT NOT NULL DEFAULT 'medium',
checked_at TIMESTAMP NOT NULL DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS alerts (
id TEXT PRIMARY KEY,
type TEXT NOT NULL,
title TEXT NOT NULL,
severity TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS audit_logs (
id UUID PRIMARY KEY,
company_id UUID,
actor_user_id UUID,
action TEXT NOT NULL,
entity_type TEXT NOT NULL,
entity_id TEXT,
details JSONB NOT NULL DEFAULT '{}'::jsonb,
created_at TIMESTAMP NOT NULL DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS subscriptions (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
price_monthly NUMERIC(10, 2) NOT NULL,
features JSONB NOT NULL DEFAULT '[]'::jsonb
);
CREATE TABLE IF NOT EXISTS resets (
id UUID PRIMARY KEY,
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
email TEXT NOT NULL,
token TEXT NOT NULL UNIQUE,
created_at TIMESTAMP NOT NULL DEFAULT NOW()
);