notification

This commit is contained in:
2026-08-07 08:52:51 +07:00
parent 26094ee4d6
commit 302d262d33
7 changed files with 1112 additions and 8 deletions

View File

@@ -875,6 +875,53 @@ BEGIN
PRINT 'Table AssetDamageDisposalHistory created successfully.';
END
-- In-app notifications: one message, reusable audiences, per-user read state.
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'Notifications')
BEGIN
CREATE TABLE Notifications (
NotificationId BIGINT PRIMARY KEY IDENTITY(1,1),
Source NVARCHAR(20) NOT NULL,
Severity NVARCHAR(20) NOT NULL CONSTRAINT DF_Notifications_Severity DEFAULT('info'),
Title NVARCHAR(160) NOT NULL,
Message NVARCHAR(2000) NOT NULL,
ActionUrl NVARCHAR(500) NULL,
CreatedBy INT NULL,
CreatedDate DATETIME NOT NULL CONSTRAINT DF_Notifications_CreatedDate DEFAULT(DATEADD(HOUR, 7, SYSUTCDATETIME())),
ExpiresAt DATETIME NULL,
IsActive BIT NOT NULL CONSTRAINT DF_Notifications_IsActive DEFAULT(1),
CONSTRAINT CK_Notifications_Source CHECK (Source IN ('admin', 'system')),
CONSTRAINT CK_Notifications_Severity CHECK (Severity IN ('info', 'success', 'warning', 'error')),
CONSTRAINT FK_Notifications_CreatedBy FOREIGN KEY (CreatedBy) REFERENCES Users(UserId) ON DELETE SET NULL
);
PRINT 'Table Notifications created successfully.';
END
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'NotificationAudiences')
BEGIN
CREATE TABLE NotificationAudiences (
AudienceId BIGINT PRIMARY KEY IDENTITY(1,1),
NotificationId BIGINT NOT NULL,
AudienceType NVARCHAR(20) NOT NULL,
AudienceValue NVARCHAR(100) NULL,
CONSTRAINT CK_NotificationAudiences_Type CHECK (AudienceType IN ('all', 'role', 'user')),
CONSTRAINT FK_NotificationAudiences_Notification FOREIGN KEY (NotificationId) REFERENCES Notifications(NotificationId) ON DELETE CASCADE
);
PRINT 'Table NotificationAudiences created successfully.';
END
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'NotificationReceipts')
BEGIN
CREATE TABLE NotificationReceipts (
NotificationId BIGINT NOT NULL,
UserId INT NOT NULL,
ReadAt DATETIME NOT NULL CONSTRAINT DF_NotificationReceipts_ReadAt DEFAULT(DATEADD(HOUR, 7, SYSUTCDATETIME())),
CONSTRAINT PK_NotificationReceipts PRIMARY KEY (NotificationId, UserId),
CONSTRAINT FK_NotificationReceipts_Notification FOREIGN KEY (NotificationId) REFERENCES Notifications(NotificationId) ON DELETE CASCADE,
CONSTRAINT FK_NotificationReceipts_User FOREIGN KEY (UserId) REFERENCES Users(UserId) ON DELETE CASCADE
);
PRINT 'Table NotificationReceipts created successfully.';
END
-- ===========================================
-- 12. CREATE AUDIT LOG TABLE
-- ===========================================
@@ -1072,6 +1119,21 @@ BEGIN
CREATE INDEX IX_AssetDamageDisposalHistory_ActionType ON AssetDamageDisposalHistory(ActionType);
END
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_Notifications_ActiveCreated')
BEGIN
CREATE INDEX IX_Notifications_ActiveCreated ON Notifications(IsActive, CreatedDate DESC);
END
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_NotificationAudiences_Lookup')
BEGIN
CREATE INDEX IX_NotificationAudiences_Lookup ON NotificationAudiences(NotificationId, AudienceType, AudienceValue);
END
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_NotificationReceipts_User')
BEGIN
CREATE INDEX IX_NotificationReceipts_User ON NotificationReceipts(UserId, NotificationId);
END
PRINT 'Indexes created successfully.';
-- ===========================================