﻿-- =============================================
-- Author:		Dr. Stefan Ott
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[fis_widgets_setCache]
	@account varchar(50)
	,@short_name varchar(50)
	,@login_guid varchar(36)
	,@data nvarchar(max)
AS
BEGIN
	SET NOCOUNT ON;

	DECLARE @IsCacheable bit, @Personalized bit;
	SELECT @IsCacheable = [daycache], @personalized = [personalized] 
		FROM [dbo].[fis_widgets] 
		WHERE ([account] = @account OR [account] = '*') AND [short_name] = @short_name;

	DELETE FROM [dbo].[fis_widgets_cache]
		WHERE [date] <> CAST(GETUTCDATE() as date)
			OR ([account] = @account AND [short_name] = @short_name AND @Personalized = 0)
			OR ([account] = @account AND [short_name] = @short_name AND @Personalized = 1 AND [person_guid] = @login_guid);

	IF @IsCacheable = 1
		BEGIN
			INSERT INTO [dbo].[fis_widgets_cache]
				VALUES( @account, @short_name, @login_guid, CAST(GETUTCDATE() as date), @data );
		END
END