UPDATE TBDD_BASECONFIG SET DB_VERSION = '1.9.0.5'
GO
CREATE TABLE TBDD_GROUPS_USER_HISTORY
(
	GUID			BIGINT,         				
	[USER_ID]		INT										NOT NULL,
	GROUP_ID		INT										NOT NULL,
	COMMENT			VARCHAR(500),
	DELETED_WHO		VARCHAR(50)		DEFAULT 'DEFAULT'	NOT NULL,
	DELETED_WHEN	DATETIME 		DEFAULT GETDATE()
)
GO

-- =============================================
-- Author:			DD MS
-- Creation date:	03.07.2020
-- =============================================
ALTER TRIGGER [dbo].[TBDD_GROUPS_USER_AFT_DEL] ON [dbo].[TBDD_GROUPS_USER]
FOR DELETE
AS
	DECLARE @USER_ID INTEGER,@GROUP_ID INTEGER
	SELECT @USER_ID = deleted.USER_ID,@GROUP_ID = deleted.GROUP_ID FROM deleted
	IF @USER_ID IS NOT NULL
		BEGIN
			if exists (select * from sysobjects where name='TBPMO_USER_RIGHTS_JOBS' and xtype='U')  
				INSERT INTO TBPMO_USER_RIGHTS_JOBS (USER_ID,COMMENT,ADDED_WHO) VALUES (@USER_ID,'USER DELETED FROM GROUP','TBDD_GROUPS_USER_AFT_DEL')
			if exists (select * from sysobjects where name='TBPMO_LOG_ESSENTIALS' and xtype='U')  
				INSERT INTO TBPMO_LOG_ESSENTIALS (REFERENCE_KEY,REFERENCE_STRING,COMMENT,ADDED_WHO) VALUES (@USER_ID,'USER-ID','USER DELETED FROM USER-GROUP',(SELECT USERNAME FROM TBDD_USER WHERE GUID = @USER_ID ))
			if exists (select * from sysobjects where name='TBDD_GROUPS_USER_HISTORY' and xtype='U')
				INSERT INTO TBDD_GROUPS_USER_HISTORY ([USER_ID],GROUP_ID,COMMENT,DELETED_WHO) VALUES (@USER_ID,@GROUP_ID,'USER DELETED FROM USER-GROUP','DD')
		END

