-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDBScript.sql
More file actions
309 lines (266 loc) · 11.1 KB
/
Copy pathDBScript.sql
File metadata and controls
309 lines (266 loc) · 11.1 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
-- ==========================================
-- GRAB & GO: CLEAN & COMPLETE DATABASE SCRIPT
-- ==========================================
USE master;
GO
-- 1. Create the Database fresh
IF DB_ID('GrabAndGoDB') IS NOT NULL
BEGIN
ALTER DATABASE GrabAndGoDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE GrabAndGoDB;
END
GO
CREATE DATABASE GrabAndGoDB;
GO
-- CRITICAL: Tell SSMS to use the new DB, not 'master' or 'dbo'
USE GrabAndGoDB;
GO
-- ==========================================
-- Phase 0: Lookup Tables (Dictionary Data)
-- ==========================================
CREATE TABLE SessionStatuses (
SessionStatusId INT PRIMARY KEY,
StatusName NVARCHAR(50) UNIQUE NOT NULL
);
CREATE TABLE PaymentStatuses (
PaymentStatusId INT PRIMARY KEY,
StatusName NVARCHAR(50) UNIQUE NOT NULL
);
CREATE TABLE LedgerEntryTypes (
LedgerEntryTypeId INT PRIMARY KEY,
TypeName NVARCHAR(50) UNIQUE NOT NULL
);
-- Seed Lookup Tables
INSERT INTO SessionStatuses (SessionStatusId, StatusName) VALUES (1, 'Active'), (2, 'Ended');
INSERT INTO PaymentStatuses (PaymentStatusId, StatusName) VALUES (1, 'Pending'), (2, 'Completed'), (3, 'Failed');
INSERT INTO LedgerEntryTypes (LedgerEntryTypeId, TypeName) VALUES (1, 'TopUp'), (2, 'Debit');
-- ==========================================
-- Phase 1: Master Data
-- ==========================================
CREATE TABLE Stores (
StoreId INT IDENTITY(1,1) PRIMARY KEY,
StoreCode NVARCHAR(50) UNIQUE NOT NULL,
Name NVARCHAR(200) NOT NULL,
Timezone NVARCHAR(50) NOT NULL,
IsActive BIT NOT NULL DEFAULT 1
);
CREATE TABLE Zones (
ZoneId INT IDENTITY(1,1) PRIMARY KEY,
StoreId INT NOT NULL FOREIGN KEY REFERENCES Stores(StoreId),
ZoneCode NVARCHAR(50) NOT NULL,
DisplayName NVARCHAR(100) NOT NULL,
ZoneType NVARCHAR(50) NOT NULL,
Range_X1 DECIMAL(10,3) NOT NULL,
Range_X2 DECIMAL(10,3) NOT NULL,
Range_Y1 DECIMAL(10,3) NOT NULL,
Range_Y2 DECIMAL(10,3) NOT NULL,
CreatedAt DATETIME2 NOT NULL DEFAULT GETUTCDATE()
);
CREATE TABLE Cameras (
CameraId INT IDENTITY(1,1) PRIMARY KEY,
StoreId INT NOT NULL FOREIGN KEY REFERENCES Stores(StoreId),
CameraCode NVARCHAR(50) NOT NULL,
IpOrStreamUrl NVARCHAR(500) NOT NULL,
IsActive BIT NOT NULL DEFAULT 1
);
CREATE TABLE Products (
ProductId INT IDENTITY(1,1) PRIMARY KEY,
Name NVARCHAR(200) NOT NULL,
SKU NVARCHAR(100) UNIQUE NOT NULL,
PriceGross DECIMAL(10,2) NOT NULL,
VAT_Rate DECIMAL(5,4) NOT NULL DEFAULT 0.1600,
ImageUrl NVARCHAR(500) NULL,
IsActive BIT NOT NULL DEFAULT 1
);
CREATE TABLE ProductAiLabels (
ProductAiLabelId INT IDENTITY(1,1) PRIMARY KEY,
ProductId INT NOT NULL FOREIGN KEY REFERENCES Products(ProductId),
AiLabel NVARCHAR(120) NOT NULL,
ModelVersion NVARCHAR(50) NOT NULL,
IsPrimary BIT NOT NULL DEFAULT 1
);
CREATE TABLE ProductZoneMapping (
ProductZoneMappingId INT IDENTITY(1,1) PRIMARY KEY,
ProductId INT NOT NULL FOREIGN KEY REFERENCES Products(ProductId),
ZoneId INT NOT NULL FOREIGN KEY REFERENCES Zones(ZoneId),
Priority INT NOT NULL DEFAULT 1
);
CREATE TABLE Users (
UserId INT IDENTITY(1,1) PRIMARY KEY,
FirstName NVARCHAR(50) NOT NULL,
LastName NVARCHAR(50) NOT NULL,
Email NVARCHAR(320) UNIQUE NOT NULL,
PasswordHash NVARCHAR(256) NOT NULL,
CreatedAt DATETIME2 NOT NULL DEFAULT GETUTCDATE(),
IsActive BIT NOT NULL DEFAULT 1
);
CREATE TABLE Wallets (
WalletId INT IDENTITY(1,1) PRIMARY KEY,
UserId INT NOT NULL UNIQUE FOREIGN KEY REFERENCES Users(UserId),
CurrentBalance DECIMAL(12,2) NOT NULL DEFAULT 0.00,
Currency CHAR(3) NOT NULL DEFAULT 'JOD',
LastUpdatedAt DATETIME2 NOT NULL DEFAULT GETUTCDATE()
);
-- ==========================================
-- Phase 2: Session Management
-- ==========================================
CREATE TABLE EntryQrTokens (
EntryQrTokenId INT IDENTITY(1,1) PRIMARY KEY,
UserId INT NOT NULL FOREIGN KEY REFERENCES Users(UserId),
StoreId INT NOT NULL FOREIGN KEY REFERENCES Stores(StoreId),
TokenHash VARBINARY(32) NOT NULL,
IssuedAt DATETIME2 NOT NULL DEFAULT GETUTCDATE(),
ExpiresAt DATETIME2 NOT NULL,
ConsumedAt DATETIME2 NULL
);
CREATE TABLE Sessions (
SessionId INT IDENTITY(1,1) PRIMARY KEY,
StoreId INT NOT NULL FOREIGN KEY REFERENCES Stores(StoreId),
UserId INT NOT NULL FOREIGN KEY REFERENCES Users(UserId),
EntryQrTokenId INT NOT NULL FOREIGN KEY REFERENCES EntryQrTokens(EntryQrTokenId),
StartedAt DATETIME2 NOT NULL DEFAULT GETUTCDATE(),
EndedAt DATETIME2 NULL,
SessionStatusId INT NOT NULL FOREIGN KEY REFERENCES SessionStatuses(SessionStatusId) DEFAULT 1,
ExitDetectedAt DATETIME2 NULL
);
CREATE TABLE SessionTrackBindings (
BindingId INT IDENTITY(1,1) PRIMARY KEY,
SessionId INT NOT NULL FOREIGN KEY REFERENCES Sessions(SessionId),
TrackId NVARCHAR(50) NOT NULL,
Source NVARCHAR(50) NOT NULL,
BoundAt DATETIME2 NOT NULL DEFAULT GETUTCDATE(),
UnboundAt DATETIME2 NULL,
IsCurrent BIT NOT NULL DEFAULT 1
);
CREATE TABLE Carts (
CartId INT IDENTITY(1,1) PRIMARY KEY,
SessionId INT NOT NULL UNIQUE FOREIGN KEY REFERENCES Sessions(SessionId),
UserId INT NOT NULL FOREIGN KEY REFERENCES Users(UserId),
CartVersion INT NOT NULL DEFAULT 0,
CreatedAt DATETIME2 NOT NULL DEFAULT GETUTCDATE(),
LastUpdatedAt DATETIME2 NOT NULL DEFAULT GETUTCDATE()
);
-- ==========================================
-- Phase 3: Vision Events
-- ==========================================
CREATE TABLE VisionEventsRaw (
VisionEventId INT IDENTITY(1,1) PRIMARY KEY,
StoreId INT NOT NULL FOREIGN KEY REFERENCES Stores(StoreId),
CameraId INT NULL FOREIGN KEY REFERENCES Cameras(CameraId),
ZoneId INT NULL FOREIGN KEY REFERENCES Zones(ZoneId),
MatchedSessionId INT NULL FOREIGN KEY REFERENCES Sessions(SessionId),
TrackId NVARCHAR(50) NOT NULL,
AiLabel NVARCHAR(120) NOT NULL,
Action NVARCHAR(10) NOT NULL,
EventTime DATETIME2 NOT NULL,
Confidence DECIMAL(5,4) NOT NULL,
PayloadJson NVARCHAR(MAX) NOT NULL,
IngestedAt DATETIME2 NOT NULL DEFAULT GETUTCDATE(),
ProcessingStatus NVARCHAR(20) NOT NULL DEFAULT 'Pending'
);
CREATE TABLE CartItems (
CartItemId INT IDENTITY(1,1) PRIMARY KEY,
CartId INT NOT NULL FOREIGN KEY REFERENCES Carts(CartId),
ProductId INT NOT NULL FOREIGN KEY REFERENCES Products(ProductId),
LastEventId INT NULL FOREIGN KEY REFERENCES VisionEventsRaw(VisionEventId),
Quantity INT NOT NULL,
LastAction NVARCHAR(10) NOT NULL,
UpdatedAt DATETIME2 NOT NULL DEFAULT GETUTCDATE()
);
CREATE TABLE CartItemEvent (
CartItemEventId INT IDENTITY(1,1) PRIMARY KEY,
CartId INT NOT NULL FOREIGN KEY REFERENCES Carts(CartId),
ProductId INT NOT NULL FOREIGN KEY REFERENCES Products(ProductId),
VisionEventId INT NOT NULL FOREIGN KEY REFERENCES VisionEventsRaw(VisionEventId),
Action NVARCHAR(10) NOT NULL,
DeltaQty INT NOT NULL,
CartVersionAfter INT NOT NULL,
AppliedAt DATETIME2 NOT NULL DEFAULT GETUTCDATE()
);
-- ==========================================
-- Phase 4: Financials
-- ==========================================
CREATE TABLE Transactions (
TransactionId INT IDENTITY(1,1) PRIMARY KEY,
SessionId INT NOT NULL UNIQUE FOREIGN KEY REFERENCES Sessions(SessionId),
UserId INT NOT NULL FOREIGN KEY REFERENCES Users(UserId),
CartId INT NOT NULL FOREIGN KEY REFERENCES Carts(CartId),
Subtotal DECIMAL(12,2) NOT NULL,
Tax DECIMAL(12,2) NOT NULL,
Total DECIMAL(12,2) NOT NULL,
PaymentStatusId INT NOT NULL FOREIGN KEY REFERENCES PaymentStatuses(PaymentStatusId) DEFAULT 1,
CreatedAt DATETIME2 NOT NULL DEFAULT GETUTCDATE(),
CompletedAt DATETIME2 NULL,
FailureReason NVARCHAR(250) NULL
);
CREATE TABLE TransactionItems (
TransactionItemId INT IDENTITY(1,1) PRIMARY KEY,
TransactionId INT NOT NULL FOREIGN KEY REFERENCES Transactions(TransactionId),
ProductId INT NOT NULL FOREIGN KEY REFERENCES Products(ProductId),
UnitPrice DECIMAL(12,2) NOT NULL,
Quantity INT NOT NULL,
LineTotal DECIMAL(12,2) NOT NULL
);
CREATE TABLE WalletLedgerEntries (
LedgerEntryId INT IDENTITY(1,1) PRIMARY KEY,
WalletId INT NOT NULL FOREIGN KEY REFERENCES Wallets(WalletId),
RelatedTransactionId INT NULL FOREIGN KEY REFERENCES Transactions(TransactionId),
LedgerEntryTypeId INT NOT NULL FOREIGN KEY REFERENCES LedgerEntryTypes(LedgerEntryTypeId),
Amount DECIMAL(12,2) NOT NULL,
BalanceAfter DECIMAL(12,2) NOT NULL,
CreatedAt DATETIME2 NOT NULL DEFAULT GETUTCDATE(),
Reference NVARCHAR(100) UNIQUE NOT NULL
);
CREATE TABLE Invoices (
InvoiceId INT IDENTITY(1,1) PRIMARY KEY,
TransactionId INT NOT NULL UNIQUE FOREIGN KEY REFERENCES Transactions(TransactionId),
PdfUrlOrPath NVARCHAR(500) NOT NULL,
GeneratedAt DATETIME2 NOT NULL DEFAULT GETUTCDATE()
);
-- ==========================================
-- MOCK SEED DATA (PHASE 0.6)
-- ==========================================
-- 1. Setup Store
INSERT INTO Stores (StoreCode, Name, Timezone) VALUES ('SWFI', 'Grab&Go - Sweifieh Branch', 'Asia/Amman');
DECLARE @StoreId INT = SCOPE_IDENTITY();
-- 2. Setup Zones
INSERT INTO Zones (StoreId, ZoneCode, DisplayName, ZoneType, Range_X1, Range_X2, Range_Y1, Range_Y2)
VALUES
(@StoreId, 'ENTRANCE_ZONE', 'Entrance', 'Entrance', 0.000, 1.600, 0.000, 2.200),
(@StoreId, 'SHELF_A', 'Shelf A', 'Shelf', 2.000, 3.400, 1.300, 2.000),
(@StoreId, 'EXIT_ZONE', 'Exit', 'Exit', 4.700, 6.200, 0.000, 2.200);
-- 3. Setup Cameras
INSERT INTO Cameras (StoreId, CameraCode, IpOrStreamUrl)
VALUES
(@StoreId, 'CAM_01_ENTRANCE', 'rtsp://10.10.1.10/stream1'),
(@StoreId, 'CAM_02_SHELF_A', 'rtsp://10.10.1.11/stream1'),
(@StoreId, 'CAM_03_EXIT', 'rtsp://10.10.1.12/stream1');
-- 4. Setup Products
INSERT INTO Products (Name, SKU, PriceGross, VAT_Rate, ImageUrl)
VALUES
('Kewpie Mayonnaise 500g', 'KEWPIE-500G', 6.50, 0.1600, 'https://cdn.grabngo.app/img/products/kewpie_500g.png'),
('Coca-Cola Can 330ml', 'COKE-330', 1.20, 0.1600, 'https://cdn.grabngo.app/img/products/coke_330.png');
-- 5. Setup AI Labels
INSERT INTO ProductAiLabels (ProductId, AiLabel, ModelVersion)
VALUES
((SELECT ProductId FROM Products WHERE SKU = 'KEWPIE-500G'), 'Kewpie_Mayonnaise', 'yolo_v10'),
((SELECT ProductId FROM Products WHERE SKU = 'COKE-330'), 'coca_cola_can', 'yolo_v10');
-- 6. Map Products to Zones
INSERT INTO ProductZoneMapping (ProductId, ZoneId)
VALUES
((SELECT ProductId FROM Products WHERE SKU = 'KEWPIE-500G'), (SELECT ZoneId FROM Zones WHERE ZoneCode = 'SHELF_A')),
((SELECT ProductId FROM Products WHERE SKU = 'COKE-330'), (SELECT ZoneId FROM Zones WHERE ZoneCode = 'SHELF_A'));
-- 7. Setup Test User (Ahmad)
INSERT INTO Users (FirstName, LastName, Email, PasswordHash)
VALUES ('Ahmad', 'Edais', 'ahmad.edais@grabngo.com', 'HASH_STRING');
DECLARE @TestUserId INT = SCOPE_IDENTITY();
-- 8. Setup Wallet & Top-up
INSERT INTO Wallets (UserId, CurrentBalance, Currency)
VALUES (@TestUserId, 30.00, 'JOD');
DECLARE @TestWalletId INT = SCOPE_IDENTITY();
INSERT INTO WalletLedgerEntries (WalletId, LedgerEntryTypeId, Amount, BalanceAfter, Reference)
VALUES (@TestWalletId, 1, 30.00, 30.00, 'TOPUP_INITIAL_001');
PRINT 'GrabAndGoDB created and seeded successfully.';
GO
ALTER AUTHORIZATION ON DATABASE::GrabAndGoDB TO [sa];
GO