CREATE PROC [dbo].[NotifyCheckUpdate] @RequestedUser INT
AS
IF NOT EXISTS
(
    SELECT UserId
    FROM dbo.RKaraNotifyPermission
    WHERE UserId = @RequestedUser
          AND
          (
              NotifyType = 1
              OR NotifyType = 4
          )
)
    RETURN;

DECLARE @LastUpdate DATETIME;
IF (NOT EXISTS
(
    SELECT Value
    FROM dbo.GeneralSetting
    WHERE Parameter = 'Notify_LastTimeUpdate'
)
   )
BEGIN
    SET @LastUpdate = GETDATE();

    DELETE FROM dbo.GeneralSetting
    WHERE Parameter LIKE 'Notify_%';

    INSERT INTO dbo.GeneralSetting
    VALUES
    ('Notify_RKaraNotifyRequest', '1'),
    ('Notify_WaitingStatus', '1'),
    ('Notify_LastTimeUpdate', CAST(GETDATE() AS NVARCHAR(MAX)));

END;
ELSE
BEGIN
    SELECT @LastUpdate = CAST(Value AS DATETIME)
    FROM dbo.GeneralSetting
    WHERE Parameter = 'Notify_LastTimeUpdate';

    IF (DATEDIFF(MINUTE, @LastUpdate, GETDATE()) >= 60)
    BEGIN
        DELETE FROM dbo.GeneralSetting
        WHERE Parameter LIKE 'Notify_%';
        INSERT INTO dbo.GeneralSetting
        VALUES
        ('Notify_RKaraNotifyRequest', '1'),
        ('Notify_WaitingStatus', '1'),
        ('Notify_LastTimeUpdate', GETDATE());
    END;
END;