Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions database/odca.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2884,3 +2884,67 @@ INSERT INTO odca.schema_migrations(version,name,checksum)
VALUES(26,'Immutable PDF and signature participant preparation','8100e40a0155473f1f8fb70076fc76adb25b9a7ce785bbb94407fb7d3ffd6f7d') ON CONFLICT(version) DO NOTHING;
COMMIT;
-- ODCA-END 026

-- ODCA-MIGRATION 027 CHECKSUM ad707eeb447350489a75d60a36530b0ebe7da857ce6991f7161ca462fc332afa
BEGIN;
SELECT pg_advisory_xact_lock(hashtext('odca.schema.migrations'));

ALTER TABLE odca.signature_preparations
ADD COLUMN composition_revision integer NOT NULL DEFAULT 1 CHECK(composition_revision>0),
ADD COLUMN confirmed_revision integer,
ADD COLUMN confirmed_pdf_storage_key text,
ADD COLUMN confirmed_pdf_sha256 char(64),
ADD COLUMN confirmed_at timestamptz,
ADD COLUMN confirmed_by uuid,
ADD COLUMN confirmation_operation_id uuid,
ADD CONSTRAINT signature_preparations_confirmation_ck CHECK(
(status='confirmed' AND ((confirmed_revision IS NULL AND confirmed_pdf_storage_key IS NULL AND confirmed_pdf_sha256 IS NULL AND confirmed_at IS NULL AND confirmed_by IS NULL) OR (confirmed_revision=composition_revision AND confirmed_pdf_storage_key IS NOT NULL AND confirmed_pdf_sha256 IS NOT NULL AND confirmed_at IS NOT NULL AND confirmed_by IS NOT NULL AND confirmation_operation_id IS NOT NULL)))
OR (status='draft' AND confirmed_revision IS NULL)),
ADD CONSTRAINT signature_preparations_confirmation_operation_uq UNIQUE(tenant_id,confirmation_operation_id),
ADD CONSTRAINT signature_preparations_confirmed_by_fk FOREIGN KEY(tenant_id,confirmed_by) REFERENCES odca.memberships(tenant_id,user_id);

ALTER TABLE odca.signature_participants
DROP CONSTRAINT signature_participants_tenant_id_preparation_id_position_key,
ADD COLUMN client_id uuid,
ADD COLUMN composition_revision integer NOT NULL DEFAULT 1 CHECK(composition_revision>0),
ADD COLUMN recorded_by uuid,
ADD COLUMN recorded_at timestamptz NOT NULL DEFAULT now();
UPDATE odca.signature_participants p SET client_id=p.id,recorded_by=s.updated_by
FROM odca.signature_preparations s WHERE (s.tenant_id,s.id)=(p.tenant_id,p.preparation_id);
ALTER TABLE odca.signature_participants
ALTER COLUMN client_id SET NOT NULL,
ALTER COLUMN recorded_by SET NOT NULL,
ADD CONSTRAINT signature_participants_recorder_fk FOREIGN KEY(tenant_id,recorded_by) REFERENCES odca.memberships(tenant_id,user_id),
ADD CONSTRAINT signature_participants_revision_fk FOREIGN KEY(tenant_id,preparation_id) REFERENCES odca.signature_preparations(tenant_id,id),
ADD CONSTRAINT signature_participants_revision_position_uq UNIQUE(tenant_id,preparation_id,composition_revision,position),
ADD CONSTRAINT signature_participants_revision_client_uq UNIQUE(tenant_id,preparation_id,composition_revision,client_id);
CREATE INDEX signature_participants_current_ix ON odca.signature_participants(tenant_id,preparation_id,composition_revision,position);

CREATE TABLE odca.signature_preparation_events(
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, tenant_id uuid NOT NULL, preparation_id uuid NOT NULL,
composition_revision integer NOT NULL CHECK(composition_revision>0), actor_user_id uuid NOT NULL,
event_type text NOT NULL CHECK(event_type IN('created','updated','participant_added','participant_changed','participant_removed','participant_reordered','confirmed','reopened')),
details jsonb NOT NULL DEFAULT '{}'::jsonb, occurred_at timestamptz NOT NULL DEFAULT now(),
FOREIGN KEY(tenant_id,preparation_id) REFERENCES odca.signature_preparations(tenant_id,id),
FOREIGN KEY(tenant_id,actor_user_id) REFERENCES odca.memberships(tenant_id,user_id));
CREATE INDEX signature_preparation_events_history_ix ON odca.signature_preparation_events(tenant_id,preparation_id,occurred_at,id);
ALTER TABLE odca.signature_preparation_events ENABLE ROW LEVEL SECURITY; ALTER TABLE odca.signature_preparation_events FORCE ROW LEVEL SECURITY;
CREATE POLICY tenant_isolation ON odca.signature_preparation_events TO odca_app USING(tenant_id=nullif(current_setting('odca.tenant_id',true),'')::uuid) WITH CHECK(tenant_id=nullif(current_setting('odca.tenant_id',true),'')::uuid);
GRANT SELECT,INSERT ON odca.signature_preparation_events TO odca_app;

CREATE OR REPLACE FUNCTION odca.patient_document_snapshot(p_tenant_id uuid,p_patient_id uuid)
RETURNS jsonb LANGUAGE sql STABLE SET search_path=pg_catalog,odca AS $function$
SELECT CASE WHEN p.id IS NULL THEN NULL ELSE jsonb_build_object(
'id',p.id,'fullName',p.full_name,'preferredName',p.preferred_name,'birthDate',p.birth_date,
'email',p.email,'phone',p.phone,'address',p.address,'identifierType',p.identifier_type,
'identifierValue',p.identifier_value,'representative',CASE WHEN r.id IS NULL THEN NULL ELSE jsonb_build_object(
'id',r.id,'fullName',r.full_name,'identifierType',r.identifier_type,'identifierValue',r.identifier_value,'relationship',r.relationship) END)
END
FROM (SELECT p.* FROM odca.patients p WHERE p.tenant_id=p_tenant_id AND p.id=p_patient_id) p
LEFT JOIN odca.patient_representatives r ON (r.tenant_id,r.id)=(p.tenant_id,p.representative_id)
$function$;

INSERT INTO odca.schema_migrations(version,name,checksum)
VALUES(27,'Traceable signature preparation and confirmation evidence','ad707eeb447350489a75d60a36530b0ebe7da857ce6991f7161ca462fc332afa') ON CONFLICT(version) DO NOTHING;
COMMIT;
-- ODCA-END 027
Loading
Loading