-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path001_core_entities.sql
More file actions
79 lines (68 loc) · 2.75 KB
/
Copy path001_core_entities.sql
File metadata and controls
79 lines (68 loc) · 2.75 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
-- 001_core_entities.sql
-- Users, customers, services, bookings, teams, staff.
-- Mirrors the resources and roles defined in freclean-api/src/core/roles.ts
-- and freclean-api/src/modules/schemas.ts — keep these in sync.
CREATE EXTENSION IF NOT EXISTS "pgcrypto"; -- for gen_random_uuid()
CREATE TYPE user_role AS ENUM (
'founder', 'management', 'finance', 'operations',
'product_management', 'cleaning_staff', 'support',
'entrepreneur', 'customer'
);
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
email TEXT NOT NULL UNIQUE,
full_name TEXT NOT NULL,
role user_role NOT NULL,
password_hash TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE TABLE customers (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID REFERENCES users(id) ON DELETE SET NULL,
full_name TEXT NOT NULL,
phone TEXT NOT NULL,
email TEXT,
address TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE TYPE service_category AS ENUM ('residential', 'airbnb', 'office', 'hotel', 'kitchen', 'specialized');
CREATE TYPE service_status AS ENUM ('active', 'in_development', 'planned');
CREATE TABLE services (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT NOT NULL,
category service_category NOT NULL,
status service_status NOT NULL DEFAULT 'planned',
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE TABLE teams (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE TABLE staff (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
position TEXT NOT NULL,
team_id UUID REFERENCES teams(id) ON DELETE SET NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE TYPE booking_status AS ENUM ('requested', 'assigned', 'in_progress', 'completed', 'cancelled');
CREATE TABLE bookings (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
customer_id UUID NOT NULL REFERENCES customers(id) ON DELETE RESTRICT,
service_id UUID NOT NULL REFERENCES services(id) ON DELETE RESTRICT,
team_id UUID REFERENCES teams(id) ON DELETE SET NULL,
scheduled_for TIMESTAMPTZ NOT NULL,
address TEXT NOT NULL,
status booking_status NOT NULL DEFAULT 'requested',
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX idx_bookings_customer ON bookings(customer_id);
CREATE INDEX idx_bookings_status ON bookings(status);
CREATE INDEX idx_bookings_scheduled_for ON bookings(scheduled_for);