-- ==============================================================
-- GNUmed database schema change script
--
-- License: GPL v2 or later
-- Author: Karsten Hilbert
-- 
-- ==============================================================
\set ON_ERROR_STOP 1
set check_function_bodies to 'on';

--set default_transaction_read_only to off;

-- --------------------------------------------------------------
-- clin.test_result
select gm.add_table_for_notifies('clin', 'test_result');


comment on column clin.test_result.note_test_org is
	'A comment on the test result provided by the tester or testing entity.';


drop function if exists clin.trf_invalidate_review_on_result_change() cascade;

create function clin.trf_invalidate_review_on_result_change()
	returns trigger
	language 'plpgsql'
	as '
DECLARE
	is_modified bool;
BEGIN
	is_modified := False;

	-- change of test type
	if NEW.fk_type != OLD.fk_type then
		is_modified := True;
	end if;

	-- change of numeric value
	if NEW.val_num != OLD.val_num then
		is_modified := True;
	end if;

	-- change of alpha value
	if NEW.val_alpha != OLD.val_alpha then
		is_modified := True;
	end if;

	-- change of unit
	if NEW.val_unit != OLD.val_unit then
		is_modified := True;
	end if;

	if is_modified is True then
		delete from clin.reviewed_test_results where fk_reviewed_row = OLD.pk;
	end if;

	return NEW;
END;';

create trigger tr_invalidate_review_on_result_change
	after update on clin.test_result
	for each row execute procedure clin.trf_invalidate_review_on_result_change()
;

-- --------------------------------------------------------------
-- clin.reviewed_test_results
select gm.add_table_for_notifies('clin', 'reviewed_test_results');


alter table clin.reviewed_test_results
	add constraint unique_review_per_row unique(fk_reviewed_row);

-- create rule r_no_del_clin_reviewed_test_results as
--	on delete to clin.reviewed_test_results do instead
--		nothing;

-- comment on rule r_no_del_clin_reviewed_test_results on clin.reviewed_test_results is
-- 'Once a review exists it cannot be deleted anymore.';


revoke delete on clin.reviewed_test_results from public, "gm-doctors", "gm-public" cascade;
grant delete on clin.reviewed_test_results to "gm-dbo";


drop function if exists clin.f_fk_reviewer_default() cascade;

create function clin.f_fk_reviewer_default()
	returns integer
	language 'plpgsql'
	as '
declare
	_pk_staff integer;
begin
	select pk into _pk_staff from dem.staff where db_user = current_user;
	return _pk_staff;
end;';

alter table clin.reviewed_test_results
	alter column fk_reviewer
		set default clin.f_fk_reviewer_default();


drop function if exists clin.trf_notify_reviewer_of_review_change() cascade;

create function clin.trf_notify_reviewer_of_review_change()
	returns trigger
	language 'plpgsql'
	as '
declare
	_pk_patient integer;
	_pk_type integer;
begin
	-- disallow change of referenced row
	-- for cleanliness this really *should* be in another trigger
	if NEW.fk_reviewed_row <> OLD.fk_reviewed_row then
		raise exception ''Attaching an existing review to another test result is not allowed (fk_reviewed_row change).'';
		return NEW;
	end if;

	-- change of last reviewer ?
	if NEW.fk_reviewer = OLD.fk_reviewer then
		return NEW;
	end if;

	-- review change ?
	if (NEW.is_technically_abnormal <> OLD.is_technically_abnormal) or
	   (NEW.clinically_relevant <> OLD.clinically_relevant) then

		-- find patient for test result
		select pk_patient into _pk_patient
			from clin.v_test_results
			where pk_test_result = OLD.fk_reviewed_row;

		-- find inbox item type
		select pk_type into _pk_type
			from dem.v_inbox_item_type where
			type = ''results review change'';
		-- create it if necessary
		if not found then
			insert into dem.inbox_item_type (
				fk_inbox_item_category,
				description
			) values (
				(select pk from dem.item_inbox_category where description = ''clinical''),
				''results review change''
			);
			select pk_type into _pk_type
				from dem.v_inbox_item_type where
				type = ''results review change'';
		end if;

		-- already notified ?
		perform 1 from dem.provider_inbox where
			fk_staff = OLD.fk_reviewer
			and fk_inbox_item_type = _pk_type
			and ufk_context = _pk_patient;
		-- nope, so notify now
		if not found then
			insert into dem.provider_inbox (
				fk_staff, fk_inbox_item_type, comment, ufk_context
			) values (
				OLD.fk_reviewer,
				_pk_type,
				(select
					_(''results review changed for patient'') || '' ['' || vpb.lastnames || '', '' || vbp.firstnames || '']''
					from dem.v_basic_person vbp
					where vpb.pk_identity = _pk_patient
				),
				_pk_patient
			);
		end if;
	end if;

	return NEW;
end;';

create trigger tr_notify_reviewer_of_review_change
	before update on clin.reviewed_test_results
	for each row execute procedure clin.trf_notify_reviewer_of_review_change()
;


select i18n.i18n('results review change');
select i18n.i18n('results review changed for patient');

select i18n.upd_tx('de_DE', 'results review change', 'Ergebnisbewertung geändert');
select i18n.upd_tx('de_DE', 'results review changed for patient', 'Bewertung von Testergebnissen änderte sich beim Patienten');

-- --------------------------------------------------------------
select gm.log_script_insertion('$RCSfile: v9-clin-test_result-dynamic.sql,v $', '$Revision: 1.8 $');
