-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStoredProcedures.sql
More file actions
1469 lines (1283 loc) · 47.7 KB
/
Copy pathStoredProcedures.sql
File metadata and controls
1469 lines (1283 loc) · 47.7 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
USE [GrabAndGoDB]
GO
CREATE OR ALTER PROCEDURE [dbo].[SP_InsertUser]
@P_JSON_REQUEST NVARCHAR(MAX),
@P_JSON_RESPONSE NVARCHAR(MAX) OUTPUT
AS
BEGIN
SET NOCOUNT ON;
-- Variables to hold the shredded JSON data
DECLARE @FirstName NVARCHAR(50),
@LastName NVARCHAR(50),
@Email NVARCHAR(320),
@PasswordHash NVARCHAR(256);
-- 1. Shred the incoming JSON payload into SQL variables
SELECT
@FirstName = FirstName,
@LastName = LastName,
@Email = Email,
@PasswordHash = PasswordHash
FROM OPENJSON(@P_JSON_REQUEST)
WITH (
FirstName NVARCHAR(50) '$.FirstName',
LastName NVARCHAR(50) '$.LastName',
Email NVARCHAR(320) '$.Email',
PasswordHash NVARCHAR(256) '$.PasswordHash'
);
-- 2. Check if email exists (Business Rule)
IF EXISTS (SELECT 1 FROM Users WHERE Email = @Email)
BEGIN
-- Return -1 in the JSON response to signal a duplicate without throwing an exception
SET @P_JSON_RESPONSE = '{"NewUserId": -1}';
RETURN;
END
DECLARE @NewUserId INT;
-- 3. Begin Transaction Block
BEGIN TRY
BEGIN TRANSACTION;
-- A. Insert the User
INSERT INTO [dbo].[Users] ([FirstName], [LastName], [Email], [PasswordHash])
VALUES (@FirstName, @LastName, @Email, @PasswordHash);
-- B. Capture the new UserId immediately
SET @NewUserId = SCOPE_IDENTITY();
-- C. Create a Wallet for the new User
INSERT INTO [dbo].[Wallets] ([UserId])
VALUES (@NewUserId);
-- Commit both inserts
COMMIT TRANSACTION;
-- 4. Format the success response to match a C# Response class/DTO
-- We construct the JSON directly from the scalar variable
SET @P_JSON_RESPONSE = (
SELECT @NewUserId AS NewUserId
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
);
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
BEGIN
ROLLBACK TRANSACTION;
END;
-- Throw the system exception so the C# executor can catch and log it
;THROW;
END CATCH
END
GO
--------------------------------------------------------------------------------------------------
USE [GrabAndGoDB]
GO
CREATE OR ALTER PROCEDURE [dbo].[SP_GetUserByEmail_JSON]
@Email NVARCHAR(320)
AS
BEGIN
SET NOCOUNT ON;
-- Look up the active user by email and return their profile + the hash
SELECT
UserId,
FirstName,
LastName,
Email,
PasswordHash
FROM [dbo].[Users]
WHERE
Email = @Email
AND IsActive = 1
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER;
-- Note: If no user is found, SQL Server returns NULL,
-- which SqlExecutor will neatly deserialize into a null C# object.
END
--------------------------------------------------------------------------------------------------
GO
CREATE OR ALTER PROCEDURE [dbo].[sp_GetUserById_JSON]
@UserId INT
AS
BEGIN
SET NOCOUNT ON;
-- We select the exact property names to match AuthResponseDto
-- Token is explicitly selected as NULL so it exists in the JSON payload
SELECT
UserId,
FirstName,
LastName,
Email,
NULL AS Token
FROM [dbo].[Users]
WHERE
UserId = @UserId
AND IsActive = 1 -- Best practice: ensure we only return active users
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER, INCLUDE_NULL_VALUES;
END
--------------------------------------------------------------------------------------------------
GO
CREATE OR ALTER PROCEDURE [dbo].[SP_GenerateEntryQrToken]
@P_JSON_REQUEST NVARCHAR(MAX),
@P_JSON_RESPONSE NVARCHAR(MAX) OUTPUT
AS
BEGIN
SET NOCOUNT ON;
-- 1. Declare variables (Note the string for the incoming hash)
DECLARE @UserId INT,
@StoreId INT,
@TokenHashString VARCHAR(64);
-- 2. Extract from JSON
SELECT
@UserId = UserId,
@StoreId = StoreId,
@TokenHashString = TokenHash
FROM OPENJSON(@P_JSON_REQUEST)
WITH (
UserId INT '$.UserId',
StoreId INT '$.StoreId',
TokenHash VARCHAR(64) '$.TokenHash'
);
-- 3. Convert Hex String back to VARBINARY(32)
-- The '2' tells SQL Server to expect a Hex string without the "0x" prefix
DECLARE @TokenHash VARBINARY(32) = CONVERT(VARBINARY(32), @TokenHashString, 2);
DECLARE @EntryQrTokenId INT;
-- 4. Calculate exact UTC timestamps
DECLARE @IssuedAt DATETIME2 = GETDATE();
DECLARE @ExpiresAt DATETIME2 = DATEADD(MINUTE, 30, @IssuedAt); -- FIX: Corrected to ExpiresAt
BEGIN TRY
-- 5. Insert
INSERT INTO EntryQrTokens (UserId, StoreId, TokenHash, IssuedAt, ExpiresAt)
VALUES (@UserId, @StoreId, @TokenHash, @IssuedAt, @ExpiresAt);
SET @EntryQrTokenId = SCOPE_IDENTITY();
-- 6. Return Data
SET @P_JSON_RESPONSE = (
SELECT
@EntryQrTokenId AS TokenId,
@ExpiresAt AS ExpiresAt -- FIX: Corrected to ExpiresAt
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
);
END TRY
BEGIN CATCH
;THROW;
END CATCH
END
--------------------------------------------------------------------------------------------------
GO
CREATE OR ALTER PROCEDURE [dbo].[SP_GetTokenForVerification]
@TokenId INT
AS
BEGIN
SET NOCOUNT ON;
SELECT
EntryQrTokenId AS TokenId,
UserId,
StoreId,
-- Convert VARBINARY(32) back to a 64-character Hex string for JSON transport
CONVERT(VARCHAR(64), TokenHash, 2) AS TokenHash,
IssuedAt,
ExpiresAt,
ConsumedAt
FROM EntryQrTokens
WHERE EntryQrTokenId = @TokenId
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER, INCLUDE_NULL_VALUES;
END
--------------------------------------------------------------------------------------------------
GO
CREATE OR ALTER PROCEDURE [dbo].[SP_ProcessStoreEntry]
@P_JSON_REQUEST NVARCHAR(MAX),
@P_JSON_RESPONSE NVARCHAR(MAX) OUTPUT
AS
BEGIN
SET NOCOUNT ON;
-- 1. Declare variables
DECLARE @TokenId INT,
@UserId INT,
@StoreId INT;
-- 2. Extract from JSON
SELECT
@TokenId = TokenId,
@UserId = UserId,
@StoreId = StoreId
FROM OPENJSON(@P_JSON_REQUEST)
WITH (
TokenId INT '$.TokenId',
UserId INT '$.UserId',
StoreId INT '$.StoreId'
);
BEGIN TRY
BEGIN TRANSACTION;
-- 3. Double-Entry Prevention
-- We check if the user is ALREADY in a session at ANY store
IF EXISTS (SELECT 1 FROM Sessions WHERE UserId = @UserId AND SessionStatusId = 1)
BEGIN
;THROW 50001, 'User already has an active shopping session.', 1;
END
-- 4. Burn the Token
UPDATE EntryQrTokens
SET ConsumedAt = GETDATE()
WHERE EntryQrTokenId = @TokenId;
-- 5. Create the Session
DECLARE @NewSessionId INT;
INSERT INTO Sessions (UserId, StoreId, EntryQrTokenId, SessionStatusId, StartedAt)
VALUES (@UserId, @StoreId, @TokenId, 1, GETDATE());
SET @NewSessionId = SCOPE_IDENTITY();
-- 6. Create the empty Cart
DECLARE @NewCartId INT;
INSERT INTO Carts (SessionId,UserId, CreatedAt, CartVersion)
VALUES (@NewSessionId,@UserId ,GETDATE(), 0);
SET @NewCartId = SCOPE_IDENTITY();
-- 7. Return the IDs for the system handshake
SET @P_JSON_RESPONSE = (
SELECT
@NewSessionId AS SessionId,
@NewCartId AS CartId,
'Access Granted' AS [Message]
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
);
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION;
;THROW;
END CATCH
END
--------------------------------------------------------------------------------------------------
GO
CREATE OR ALTER PROCEDURE [dbo].[SP_TopUpWallet]
@P_JSON_REQUEST NVARCHAR(MAX),
@P_JSON_RESPONSE NVARCHAR(MAX) OUTPUT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @UserId INT, @Amount DECIMAL(18,2);
-- Extract from JSON
SELECT
@UserId = UserId,
@Amount = Amount
FROM OPENJSON(@P_JSON_REQUEST)
WITH (
UserId INT '$.UserId',
Amount DECIMAL(18,2) '$.Amount'
);
BEGIN TRY
BEGIN TRANSACTION;
DECLARE @WalletId INT, @CurrentBalance DECIMAL(18,2), @NewBalance DECIMAL(18,2);
-- 1. Lock the wallet row to prevent race conditions during read/write
SELECT @WalletId = WalletId, @CurrentBalance = CurrentBalance
FROM Wallets WITH (UPDLOCK)
WHERE UserId = @UserId;
IF @WalletId IS NULL
BEGIN
;THROW 50002, 'Wallet not found for this user.', 1;
END
-- 2. Calculate new balance
SET @NewBalance = @CurrentBalance + @Amount;
-- 3. Update the Wallet
UPDATE Wallets
SET CurrentBalance = @NewBalance,
LastUpdatedAt = GETDATE()
WHERE WalletId = @WalletId;
-- 4. Create the Audit Trail (Ledger)
INSERT INTO WalletLedgerEntries (WalletId, LedgerEntryTypeId, Amount, BalanceAfter, CreatedAt)
VALUES (@WalletId, 1, @Amount, @NewBalance, GETDATE());
-- 5. Return the new state
SET @P_JSON_RESPONSE = (
SELECT
@WalletId AS WalletId,
@NewBalance AS NewBalance,
'Wallet topped up successfully.' AS [Message]
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
);
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION;
;THROW;
END CATCH
END
--------------------------------------------------------------------------------------------------
GO
CREATE OR ALTER PROCEDURE [dbo].[SP_GetWalletBalance]
@UserId INT
AS
BEGIN
SET NOCOUNT ON;
SELECT
WalletId,
CurrentBalance,
LastUpdatedAt
FROM Wallets
WHERE UserId = @UserId
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER, INCLUDE_NULL_VALUES;
END
--------------------------------------------------------------------------------------------------
GO
IF OBJECT_ID('dbo.SP_BindSessionTrack', 'P') IS NOT NULL
DROP PROCEDURE [dbo].[SP_BindSessionTrack];
GO
CREATE PROCEDURE [dbo].[SP_BindSessionTrack]
@P_JSON_REQUEST NVARCHAR(MAX),
@P_JSON_RESPONSE NVARCHAR(MAX) OUTPUT -- Added to match your SqlExecutor
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
-- 1. Extract properties natively from the incoming JSON DTO
DECLARE @SessionId NVARCHAR(50);
DECLARE @TrackId NVARCHAR(50);
DECLARE @Source NVARCHAR(100);
SELECT
@SessionId = [SessionId],
@TrackId = [TrackId],
@Source = [Source]
FROM OPENJSON(@P_JSON_REQUEST)
WITH (
[SessionId] NVARCHAR(50),
[TrackId] NVARCHAR(50),
[Source] NVARCHAR(100)
);
-- 2. Begin Transaction to ensure atomic binding
BEGIN TRAN;
-- Retire previous tracks for this session
UPDATE SessionTrackBindings
SET IsCurrent = 0
WHERE SessionId = @SessionId AND IsCurrent = 1;
-- 3. Insert the new physical-to-digital link
DECLARE @NewBindingId INT;
DECLARE @BoundAt DATETIME2 = SYSUTCDATETIME();
INSERT INTO SessionTrackBindings
(SessionId, TrackId, Source, BoundAt, IsCurrent)
VALUES
(@SessionId, @TrackId, @Source, @BoundAt, 1);
SET @NewBindingId = SCOPE_IDENTITY();
COMMIT TRAN;
-- 4. Assign the JSON directly to the OUTPUT parameter your executor is waiting for
SET @P_JSON_RESPONSE = (
SELECT
BindingId = @NewBindingId,
SessionId = @SessionId,
TrackId = @TrackId,
BoundAt = @BoundAt,
IsCurrent = CAST(1 AS BIT)
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER, INCLUDE_NULL_VALUES
);
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0 ROLLBACK TRAN;
THROW;
END CATCH
END
GO
--------------------------------------------------------------------------------------------------
GO
CREATE OR ALTER PROCEDURE SP_ProcessVisionEvent
@P_JSON_REQUEST NVARCHAR(MAX),
@P_JSON_RESPONSE NVARCHAR(MAX) OUTPUT
AS
BEGIN
SET NOCOUNT ON;
-- =========================================================================
-- PHASE A: INGESTION & RESOLUTION (No Locks Yet)
-- =========================================================================
-- 1. Extract values directly from the DTO payload
DECLARE @TrackId NVARCHAR(50),
@AiLabel NVARCHAR(120),
@Action NVARCHAR(10),
@EventTime DATETIME2,
@Confidence DECIMAL(5,4),
@CameraCode NVARCHAR(50);
SELECT
@TrackId = [TrackId],
@AiLabel = [AiLabel],
@Action = [Action],
@EventTime = [EventTime],
@Confidence = [Confidence],
@CameraCode = [CameraCode]
FROM OPENJSON(@P_JSON_REQUEST)
WITH (
TrackId NVARCHAR(50),
AiLabel NVARCHAR(120),
Action NVARCHAR(10),
EventTime DATETIME2,
Confidence DECIMAL(5,4),
CameraCode NVARCHAR(50)
);
-- 2. Lookups (Camera, Store, Zone, Product, Session, Cart)
DECLARE @CameraId INT, @StoreId INT, @ZoneId INT;
SELECT @CameraId = CameraId, @StoreId = StoreId, @ZoneId = ZoneId
FROM Cameras WHERE CameraCode = @CameraCode;
DECLARE @ProductId INT, @UnitPrice DECIMAL(18,2);
SELECT @ProductId = Products.ProductId, @UnitPrice = PriceGross + PriceGross * VAT_Rate
FROM Products INNER JOIN ProductAiLabels
ON Products.ProductId = ProductAiLabels.ProductId
WHERE AiLabel = @AiLabel;
DECLARE @SessionId INT, @CartId INT;
-- Find the active session for this person
SELECT @SessionId = SessionId
FROM SessionTrackBindings
WHERE TrackId = @TrackId;
IF @SessionId IS NOT NULL
BEGIN
-- Find the active cart (StatusId = 1) for this session
SELECT @CartId = CartId
FROM Carts INNER JOIN Sessions
ON Carts.SessionId = Sessions.SessionId
WHERE Sessions.SessionId = @SessionId AND Sessions.SessionStatusId = 1;
END
-- 3. THE QoS 1 IDEMPOTENCY GUARD
-- If this exact event was already processed (network duplicate), exit immediately.
IF EXISTS (
SELECT 1 FROM VisionEventsRaw
WHERE TrackId = @TrackId
AND EventTime = @EventTime
AND AiLabel = @AiLabel
)
BEGIN
-- Skip DB writes, jump straight to returning the current cart state
GOTO OutputSignalRJson;
END
-- =========================================================================
-- PHASE B: THE RAW LOG
-- =========================================================================
DECLARE @VisionEventId INT;
-- Insert with ProcessingStatusId = 1 (Pending)
INSERT INTO VisionEventsRaw (
StoreId, CameraId, ZoneId, MatchedSessionId,
TrackId, AiLabel, Action, EventTime,
Confidence, PayloadJson,IngestedAt ,ProcessingStatusId
)
VALUES (
@StoreId, @CameraId, @ZoneId, @SessionId,
@TrackId, @AiLabel, @Action, @EventTime,
@Confidence, @P_JSON_REQUEST,GETDATE(), 1
);
SET @VisionEventId = SCOPE_IDENTITY();
-- =========================================================================
-- PHASE C: THE ATOMIC CART UPDATE
-- =========================================================================
-- Only proceed with financial cart updates if we successfully mapped the user and product
IF @CartId IS NOT NULL AND @ProductId IS NOT NULL
BEGIN
BEGIN TRY
BEGIN TRANSACTION;
DECLARE @CartStateChanged BIT = 0;
-- Handle Pick (Add 1 or Insert)
IF @Action = 'Pick'
BEGIN
IF EXISTS (SELECT 1 FROM CartItems WHERE CartId = @CartId AND ProductId = @ProductId)
BEGIN
UPDATE CartItems
SET Quantity = Quantity + 1
WHERE CartId = @CartId AND ProductId = @ProductId;
END
ELSE
BEGIN
INSERT INTO CartItems (CartId, ProductId,LastEventId, Quantity,LastAction)
VALUES (@CartId, @ProductId, @VisionEventId ,1, @Action);
END
SET @CartStateChanged = 1;
END
-- Handle Return (Subtract 1, Delete if 0)
ELSE IF @Action = 'Return'
BEGIN
IF EXISTS (SELECT 1 FROM CartItems WHERE CartId = @CartId AND ProductId = @ProductId)
BEGIN
UPDATE CartItems
SET Quantity = Quantity - 1 ,UpdatedAt = GETDATE()
WHERE CartId = @CartId AND ProductId = @ProductId;
-- Crucial: Remove row entirely if quantity hits zero
DELETE FROM CartItems
WHERE CartId = @CartId AND ProductId = @ProductId AND Quantity <= 0;
SET @CartStateChanged = 1;
END
END
IF @CartStateChanged = 1
BEGIN
DECLARE @NewCartVersion INT;
SELECT @NewCartVersion = CartVersion + 1 FROM Carts WHERE CartId = @CartId;
-- Audit Trail
INSERT INTO CartItemEvents(CartId, ProductId, VisionEventId,Action, DeltaQty,CartVersionAfter, AppliedAt)
VALUES (@CartId, @ProductId,@VisionEventId, @Action, CASE WHEN @Action = 'Pick' THEN 1 ELSE -1 END,@NewCartVersion ,@EventTime);
-- Version Bump
UPDATE Carts
SET CartVersion = @NewCartVersion,
LastUpdatedAt = GETDATE()
WHERE CartId = @CartId;
END
-- Mark Raw Log as Applied (ProcessingStatusId = 2)
UPDATE VisionEventsRaw
SET ProcessingStatusId = 2
WHERE VisionEventId = @VisionEventId;
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION;
THROW;
END CATCH
END
-- =========================================================================
-- PHASE D: THE SIGNALR OUTPUT
-- =========================================================================
OutputSignalRJson:
-- If no CartId exists (e.g., event was mapped to a ghost track), return empty JSON
IF @CartId IS NULL
BEGIN
SET @P_JSON_RESPONSE = '{}';
RETURN;
END
-- 1. Pre-calculate the Cart Total and Wallet Balance for performance
DECLARE @CurrentCartTotal DECIMAL(18,2) = 0.00;
SELECT @CurrentCartTotal = ISNULL(SUM(ci.Quantity * (p.PriceGross + (p.PriceGross * p.VAT_Rate))), 0.00)
FROM CartItems ci
INNER JOIN Products p ON ci.ProductId = p.ProductId
WHERE ci.CartId = @CartId;
DECLARE @CurrentBalance DECIMAL(18,2) = 0.00;
-- We already have @SessionId, so we join to find the exact User's Wallet
SELECT @CurrentBalance = w.CurrentBalance
FROM Wallets w
INNER JOIN Sessions s ON w.UserId = s.UserId
WHERE s.SessionId = @SessionId;
-- 2. Determine Shortfall Logic
DECLARE @IsShortfall BIT = CASE WHEN @CurrentCartTotal > @CurrentBalance THEN 1 ELSE 0 END;
DECLARE @ShortfallAmount DECIMAL(18,2) = CASE WHEN @IsShortfall = 1 THEN (@CurrentCartTotal - @CurrentBalance) ELSE 0.00 END;
SET @P_JSON_RESPONSE =
(SELECT
c.CartId,
c.SessionId,
c.CartVersion,
c.LastUpdatedAt,
@CurrentBalance AS WalletBalance,
@CurrentCartTotal AS CartTotal,
@IsShortfall AS IsShortfall,
@ShortfallAmount AS ShortfallAmount,
ISNULL((
SELECT
ci.ProductId,
p.Name AS ProductName,
pi.AiLabel,
ci.Quantity,
(p.PriceGross + (p.PriceGross * p.VAT_Rate)) AS UnitPrice,
(ci.Quantity * (p.PriceGross + (p.PriceGross * p.VAT_Rate))) AS LineTotal
FROM CartItems ci
INNER JOIN Products p ON ci.ProductId = p.ProductId
INNER JOIN ProductAiLabels Pi ON p.ProductId = pi.ProductId AND pi.IsPrimary = 1
WHERE ci.CartId = c.CartId
FOR JSON PATH
), JSON_QUERY('[]')) AS CartItems
FROM Carts c
WHERE c.CartId = @CartId
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER, INCLUDE_NULL_VALUES);
END
GO
--------------------------------------------------------------------------------------------------
GO
USE [GrabAndGoDB]
GO
CREATE OR ALTER PROCEDURE [dbo].[SP_ProcessCheckout]
@P_JSON_REQUEST NVARCHAR(MAX),
@P_JSON_RESPONSE NVARCHAR(MAX) OUTPUT
AS
BEGIN
SET NOCOUNT ON;
-- =========================================================================
-- PHASE 1: PARSE INPUT & RESOLVE DIGITAL IDENTITY
-- =========================================================================
DECLARE @TrackId NVARCHAR(50),
@CameraCode NVARCHAR(50),
@EventTime DATETIME2;
SELECT
@TrackId = [TrackId],
@CameraCode = [CameraCode],
@EventTime = [EventTime]
FROM OPENJSON(@P_JSON_REQUEST)
WITH (
TrackId NVARCHAR(50),
CameraCode NVARCHAR(50),
EventTime DATETIME2
);
-- Find the Active Session tied to this TrackId
DECLARE @SessionId INT, @UserId INT, @StoreId INT, @CartId INT;
SELECT
@SessionId = s.SessionId,
@UserId = s.UserId,
@StoreId = s.StoreId
FROM SessionTrackBindings stb
INNER JOIN Sessions s ON stb.SessionId = s.SessionId
WHERE stb.TrackId = @TrackId
AND stb.IsCurrent = 1
AND s.SessionStatusId = 1; -- 1 = Active
-- If no active session is found for this person, safely reject the gate opening
IF @SessionId IS NULL
BEGIN
SET @P_JSON_RESPONSE = '{"GateAction":"KeepClosed", "Message":"No active shopping session found.", "IsSuccess":false, "ShortfallAmount":0}';
RETURN;
END
-- Find the active Cart
SELECT @CartId = CartId FROM Carts WHERE SessionId = @SessionId;
-- =========================================================================
-- PHASE 2: ATOMIC FINANCIAL CALCULATIONS & TRANSACTIONS
-- =========================================================================
BEGIN TRY
BEGIN TRANSACTION;
-- 1. Lock the Wallet to prevent double-spending race conditions
DECLARE @WalletId INT, @CurrentBalance DECIMAL(18,2);
SELECT @WalletId = WalletId, @CurrentBalance = CurrentBalance
FROM Wallets WITH (UPDLOCK)
WHERE UserId = @UserId;
-- 2. Calculate Final Cart Totals
DECLARE @Subtotal DECIMAL(18,2) = 0.00,
@TaxTotal DECIMAL(18,2) = 0.00,
@GrandTotal DECIMAL(18,2) = 0.00;
SELECT
@Subtotal = ISNULL(SUM(ci.Quantity * p.PriceGross), 0.00),
@TaxTotal = ISNULL(SUM(ci.Quantity * (p.PriceGross * p.VAT_Rate)), 0.00)
FROM CartItems ci
INNER JOIN Products p ON ci.ProductId = p.ProductId
WHERE ci.CartId = @CartId;
SET @GrandTotal = @Subtotal + @TaxTotal;
-- 3. The HARD STOP Check (Insufficient Funds)
IF @CurrentBalance < @GrandTotal
BEGIN
-- User does not have enough money. We COMMIT to release the lock,
-- but we DO NOT process the checkout. Session remains active.
DECLARE @Shortfall DECIMAL(18,2) = @GrandTotal - @CurrentBalance;
SET @P_JSON_RESPONSE = (
SELECT
'KeepClosed' AS GateAction,
'Insufficient Funds. Please Top Up.' AS Message,
CAST(0 AS BIT) AS IsSuccess,
@Shortfall AS ShortfallAmount,
@SessionId AS SessionId
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
);
COMMIT TRANSACTION;
RETURN;
END
-- =====================================================================
-- PHASE 3: CHECKOUT EXECUTION (User has enough funds)
-- =====================================================================
DECLARE @NewBalance DECIMAL(18,2) = @CurrentBalance - @GrandTotal;
-- A. Deduct Wallet Funds
UPDATE Wallets
SET CurrentBalance = @NewBalance, LastUpdatedAt = GETDATE()
WHERE WalletId = @WalletId;
-- B. Create Header Transaction (Status = 2 / Completed)
DECLARE @TransactionId INT;
INSERT INTO Transactions (SessionId, UserId, CartId, Subtotal, Tax, Total, PaymentStatusId, CreatedAt)
VALUES (@SessionId, @UserId,@CartId, @Subtotal,@TaxTotal, @GrandTotal, 2,GETDATE() );
SET @TransactionId = SCOPE_IDENTITY();
-- C. Snapshot CartItems into TransactionItems (The Receipt Lines)
INSERT INTO TransactionItems (TransactionId, ProductId, Quantity, UnitPrice, LineTotal)
SELECT
@TransactionId,
ci.ProductId,
ci.Quantity,
(p.PriceGross + (p.PriceGross * p.VAT_Rate)),
(ci.Quantity * (p.PriceGross + (p.PriceGross * p.VAT_Rate)))
FROM CartItems ci
INNER JOIN Products p ON ci.ProductId = p.ProductId
WHERE ci.CartId = @CartId;
DECLARE @LedgerReference NVARCHAR(100) = 'Checkout_Txn_' + CAST(@TransactionId AS NVARCHAR(50));
-- D. Write Ledger Entry (Immutable Financial Record)
INSERT INTO WalletLedgerEntries (WalletId, LedgerEntryTypeId, Amount, BalanceAfter, RelatedTransactionId, CreatedAt,Reference)
VALUES (@WalletId, 2, -@GrandTotal, @NewBalance, @TransactionId, GETDATE(),@LedgerReference); -- 2 = Debit
-- E. Generate Stub for Invoice Generation (Phase 5 will fill the PDF URL later)
INSERT INTO Invoices (TransactionId, GeneratedAt)
VALUES (@TransactionId, GETDATE());
-- F. Terminate the Digital Session
UPDATE Sessions
SET SessionStatusId = 2, ExitDetectedAt = @EventTime, EndedAt = GETDATE()
WHERE SessionId = @SessionId;
-- G. Unbind the physical Track ID so the camera stops associating them with the session
UPDATE SessionTrackBindings
SET IsCurrent = 0
WHERE SessionId = @SessionId;
-- H. Build Success JSON Response
SET @P_JSON_RESPONSE = (
SELECT
'OpenGate' AS GateAction,
'Checkout Successful' AS Message,
CAST(1 AS BIT) AS IsSuccess,
0.00 AS ShortfallAmount,
@SessionId AS SessionId,
@TransactionId AS TransactionId,
@GrandTotal AS TotalDeducted,
@NewBalance AS RemainingBalance
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
);
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION;
THROW; -- Pass exception back to C#
END CATCH
END
GO
--------------------------------------------------------------------------------------------------
USE [GrabAndGoDB]
GO
-- =============================================================================
-- SP_GetInvoiceData
-- Returns the full invoice payload needed to render a PDF, as a single JSON
-- object, including nested line items.
-- Consumer: InvoiceService (called by InvoiceWorker background service, and by
-- GET /api/invoices/{transactionId} controller endpoint).
-- =============================================================================
CREATE OR ALTER PROCEDURE dbo.SP_GetInvoiceData
@TransactionId INT
AS
BEGIN
SET NOCOUNT ON;
SELECT
i.InvoiceId,
i.TransactionId,
i.PdfUrlOrPath,
i.GeneratedAt,
t.Subtotal,
t.Tax,
t.Total,
t.CreatedAt,
t.CompletedAt,
ps.StatusName AS PaymentStatus,
u.UserId AS CustomerUserId,
u.FirstName AS CustomerFirstName,
u.LastName AS CustomerLastName,
u.Email AS CustomerEmail,
s.StoreId,
s.StoreCode,
s.Name AS StoreName,
s.Timezone AS StoreTimezone,
Items = (
SELECT
ti.TransactionItemId,
ti.ProductId,
p.Name AS ProductName,
p.SKU,
ti.Quantity,
ti.UnitPrice,
ti.LineTotal,
p.VAT_Rate
FROM dbo.TransactionItems ti
INNER JOIN dbo.Products p ON p.ProductId = ti.ProductId
WHERE ti.TransactionId = i.TransactionId
ORDER BY ti.TransactionItemId
FOR JSON PATH, INCLUDE_NULL_VALUES
)
FROM dbo.Invoices i
INNER JOIN dbo.Transactions t ON t.TransactionId = i.TransactionId
INNER JOIN dbo.Sessions ses ON ses.SessionId = t.SessionId
INNER JOIN dbo.Stores s ON s.StoreId = ses.StoreId
INNER JOIN dbo.Users u ON u.UserId = t.UserId
INNER JOIN dbo.PaymentStatuses ps ON ps.PaymentStatusId = t.PaymentStatusId
WHERE i.TransactionId = @TransactionId
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER, INCLUDE_NULL_VALUES;
END
GO
-- =============================================================================
-- SP_UpdateInvoicePath
-- Writes the generated PDF path (or URL) back into Invoices.PdfUrlOrPath.
-- Consumer: InvoiceService, after the PDF file has been persisted to storage.
--
-- Request JSON shape:
-- { "TransactionId": <int>, "PdfUrlOrPath": "<string>" }
--
-- Response JSON shape:
-- { "IsSuccess": <bit>, "Message": "<string>",
-- "TransactionId": <int>, "PdfUrlOrPath": "<string>" }
-- =============================================================================
CREATE OR ALTER PROCEDURE dbo.SP_UpdateInvoicePath
@P_JSON_REQUEST NVARCHAR(MAX),
@P_JSON_RESPONSE NVARCHAR(MAX) = NULL OUTPUT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @TransactionId INT;
DECLARE @PdfUrlOrPath NVARCHAR(500);
SELECT
@TransactionId = TransactionId,
@PdfUrlOrPath = PdfUrlOrPath
FROM OPENJSON(@P_JSON_REQUEST)
WITH (
TransactionId INT '$.TransactionId',
PdfUrlOrPath NVARCHAR(500) '$.PdfUrlOrPath'
);
-- Guard: invoice stub must exist before we can fill in the path.
IF NOT EXISTS (
SELECT 1 FROM dbo.Invoices WHERE TransactionId = @TransactionId
)
BEGIN
SET @P_JSON_RESPONSE = (
SELECT
CAST(0 AS BIT) AS IsSuccess,
'No invoice stub found for the specified TransactionId.' AS Message,
@TransactionId AS TransactionId,
CAST(NULL AS NVARCHAR(500)) AS PdfUrlOrPath
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER, INCLUDE_NULL_VALUES
);
RETURN;
END
-- Guard: don't overwrite an existing PDF path silently. The worker should
-- only pick up rows where PdfUrlOrPath IS NULL, but defending against
-- double-submission here keeps the invariant enforced at the data layer.
IF EXISTS (
SELECT 1 FROM dbo.Invoices
WHERE TransactionId = @TransactionId AND PdfUrlOrPath IS NOT NULL
)
BEGIN
SET @P_JSON_RESPONSE = (
SELECT
CAST(0 AS BIT) AS IsSuccess,
'PDF path is already set for this invoice.' AS Message,
@TransactionId AS TransactionId,
(SELECT PdfUrlOrPath FROM dbo.Invoices
WHERE TransactionId = @TransactionId) AS PdfUrlOrPath
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER, INCLUDE_NULL_VALUES
);
RETURN;
END
UPDATE dbo.Invoices
SET PdfUrlOrPath = @PdfUrlOrPath
WHERE TransactionId = @TransactionId;
SET @P_JSON_RESPONSE = (
SELECT
CAST(1 AS BIT) AS IsSuccess,
'Invoice PDF path stored successfully.' AS Message,
@TransactionId AS TransactionId,
@PdfUrlOrPath AS PdfUrlOrPath
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER, INCLUDE_NULL_VALUES
);
END
GO
CREATE OR ALTER PROCEDURE dbo.SP_GetPendingInvoices
@BatchSize INT = 10
AS
BEGIN
SET NOCOUNT ON;
DECLARE @CompletedStatusId INT = (
SELECT PaymentStatusId FROM dbo.PaymentStatuses WHERE StatusName = 'Completed'
);
SELECT TOP (@BatchSize)
i.TransactionId,
i.GeneratedAt
FROM dbo.Invoices i
INNER JOIN dbo.Transactions t ON t.TransactionId = i.TransactionId
WHERE i.PdfUrlOrPath IS NULL
AND t.PaymentStatusId = @CompletedStatusId
ORDER BY i.GeneratedAt ASC
FOR JSON PATH, INCLUDE_NULL_VALUES;
END
GO
USE [GrabAndGoDB];
GO
CREATE OR ALTER PROCEDURE dbo.SP_GetUserTransactions
@UserId INT,
@PageNumber INT = 1,
@PageSize INT = 20