-- "__ORG_ID__"."terminalChangesHistory" definition

-- Drop table

-- DROP TABLE "__ORG_ID__"."terminalChangesHistory";

CREATE TABLE "__ORG_ID__"."terminalChangesHistory" (
	id uuid NOT NULL,
	"action" int4 NOT NULL,
	"actionT" timestamp NOT NULL,
	"transactionT" timestamp NOT NULL,
	"type" int4 NOT NULL,
	"sessionId" uuid NULL,
	"data" json NULL,
	"deviceId" uuid NOT NULL,
	"sessionExpirationT" timestamp NULL,
	"transactionId" int8 NOT NULL
);
CREATE INDEX terminalchangeshistory_idx ON "__ORG_ID__"."terminalChangesHistory" USING btree (id, action, "transactionT");



-- DROP FUNCTION "__ORG_ID__"."zz_terminalChangesHistory2"();

CREATE OR REPLACE FUNCTION "__ORG_ID__"."zz_terminalChangesHistory"()
 RETURNS trigger
 LANGUAGE plpgsql
 COST 300
AS $function$
		DECLARE
			_now timestamp without time zone;
		BEGIN   
			_now := now();

			IF (TG_OP = 'INSERT') THEN
				INSERT INTO "__ORG_ID__"."terminalChangesHistory"
				(
					id,
					action,
					"actionT",
					"transactionT",
					"type",
					"sessionId",
					"data",
					"deviceId",
					"sessionExpirationT",
					"transactionId"
				)
				SELECT newtab.id,
					1,
					newtab."actionT",
					_now,
					newtab."type",
					newtab."sessionId",
					newtab."data",
					newtab."deviceId",
					newtab."sessionExpirationT",
					txid_current()
					FROM newtab;

				ELSIF (TG_OP = 'UPDATE') THEN
					INSERT INTO "__ORG_ID__"."terminalChangesHistory"
				(
					id,
					action,
					"actionT",
					"transactionT",
					"type",
					"sessionId",
					"data",
					"deviceId",
					"sessionExpirationT",
					"transactionId"
				)
					SELECT newtab.id,
					2,
					newtab."actionT",
					_now,
					newtab."type",
					newtab."sessionId",
					null,
					newtab."deviceId",
					newtab."sessionExpirationT",
					txid_current()
					FROM newtab;
					
					ELSIF (TG_OP = 'DELETE') THEN
									INSERT INTO "__ORG_ID__"."terminalChangesHistory"
				(
					id,
					action,
					"actionT",
					"transactionT",
					"type",
					"sessionId",
					"data",
					"deviceId",
					"sessionExpirationT",
					"transactionId"
				)
					SELECT oldtab.id,
					3,
					oldtab."actionT",
					_now,
					oldtab."type",
					oldtab."sessionId",
					null,
					oldtab."deviceId",
					oldtab."sessionExpirationT",
					txid_current()
					FROM oldtab;
					END IF;    

			
			RETURN NULL;
		END;
	$function$
;


create trigger terminal_change_history_insert after
insert
    on
    "__ORG_ID__"."terminalChanges" referencing NEW TABLE AS newtab
for each statement execute function "__ORG_ID__"."zz_terminalChangesHistory"();

create trigger terminal_change_history_update after
update
    on
    "__ORG_ID__"."terminalChanges" referencing  OLD TABLE AS oldtab NEW TABLE AS newtab
for each statement execute function "__ORG_ID__"."zz_terminalChangesHistory"();

create trigger terminal_change_history_delete after
delete
    on
    "__ORG_ID__"."terminalChanges" referencing OLD TABLE AS oldtab
for each statement execute function "__ORG_ID__"."zz_terminalChangesHistory"();