From 4ff342cb43913e31dcdf985168dc09ac353fc604 Mon Sep 17 00:00:00 2001
From: Sven Thelemann <sven.thelemann@t-online.de>
Date: Wed, 11 Dec 2024 22:58:10 +0100
Subject: [PATCH] feat(invitation): provide flattened invitations

A view is created that provides invitation data together with corresponding even and contact data.
---
 schema/schema.definition.sql        | 46 +++++++++++++++++++++++++++++
 src/deploy/view_invitation_flat.sql | 21 +++++++++++++
 src/revert/view_invitation_flat.sql |  7 +++++
 src/sqitch.plan                     |  1 +
 src/verify/view_invitation_flat.sql | 14 +++++++++
 5 files changed, 89 insertions(+)
 create mode 100644 src/deploy/view_invitation_flat.sql
 create mode 100644 src/revert/view_invitation_flat.sql
 create mode 100644 src/verify/view_invitation_flat.sql

diff --git a/schema/schema.definition.sql b/schema/schema.definition.sql
index 3e72a2ff..831088ae 100644
--- a/schema/schema.definition.sql
+++ b/schema/schema.definition.sql
@@ -2006,6 +2006,52 @@ COMMENT ON COLUMN maevsi.invitation.feedback IS 'The invitation''s general feedb
 COMMENT ON COLUMN maevsi.invitation.feedback_paper IS 'The invitation''s paper feedback status.';
 
 
+--
+-- Name: invitation_flat; Type: VIEW; Schema: maevsi; Owner: postgres
+--
+
+CREATE VIEW maevsi.invitation_flat AS
+ SELECT i.id,
+    i.contact_id,
+    i.event_id,
+    i.feedback,
+    i.feedback_paper,
+    c.account_id AS contact_account_id,
+    c.address,
+    c.author_account_id AS contact_author_account_id,
+    c.email_address,
+    c.email_address_hash,
+    c.first_name,
+    c.last_name,
+    c.phone_number,
+    c.url AS contact_url,
+    e.author_account_id AS event_author_account_id,
+    e.description,
+    e.start,
+    e."end",
+    e.invitee_count_maximum,
+    e.is_archived,
+    e.is_in_person,
+    e.is_remote,
+    e.location,
+    e.name,
+    e.slug,
+    e.url AS event_url,
+    e.visibility
+   FROM ((maevsi.invitation i
+     JOIN maevsi.contact c ON ((i.contact_id = c.id)))
+     JOIN maevsi.event e ON ((i.event_id = e.id)));
+
+
+ALTER VIEW maevsi.invitation_flat OWNER TO postgres;
+
+--
+-- Name: VIEW invitation_flat; Type: COMMENT; Schema: maevsi; Owner: postgres
+--
+
+COMMENT ON VIEW maevsi.invitation_flat IS 'View returning flattened invitations.';
+
+
 --
 -- Name: legal_term; Type: TABLE; Schema: maevsi; Owner: postgres
 --
diff --git a/src/deploy/view_invitation_flat.sql b/src/deploy/view_invitation_flat.sql
new file mode 100644
index 00000000..c16f15e1
--- /dev/null
+++ b/src/deploy/view_invitation_flat.sql
@@ -0,0 +1,21 @@
+-- Deploy maevsi:view_invitation_flat to pg
+-- requires: schema_public
+-- requires: table_invitation
+-- requires: table_contact
+
+BEGIN;
+CREATE VIEW maevsi.invitation_flat AS
+SELECT i.*,
+  c.account_id as contact_account_id, c.address, c.author_account_id as contact_author_account_id, c.email_address, c.email_address_hash,
+  c.first_name, c.last_name, c.phone_number, c.url as contact_url,
+  e.author_account_id as event_author_account_id, e.description, e.start, e.end,
+  e.invitee_count_maximum, e.is_archived, e.is_in_person, e.is_remote,
+  e.location, e.name, e.slug, e.url as event_url, e.visibility
+FROM maevsi.invitation i
+  JOIN maevsi.contact c ON i.contact_id = c.id
+  JOIN maevsi.event e ON i.event_id = e.id
+;
+
+COMMENT ON VIEW maevsi.invitation_flat IS 'View returning flattened invitations.';
+
+END;
diff --git a/src/revert/view_invitation_flat.sql b/src/revert/view_invitation_flat.sql
new file mode 100644
index 00000000..6c24164e
--- /dev/null
+++ b/src/revert/view_invitation_flat.sql
@@ -0,0 +1,7 @@
+-- Revert maevsi:view_invitation_flat from pg
+
+BEGIN;
+
+DROP VIEW maevsi.invitation_flat;
+
+COMMIT;
diff --git a/src/sqitch.plan b/src/sqitch.plan
index f4e039ae..6ed2b359 100644
--- a/src/sqitch.plan
+++ b/src/sqitch.plan
@@ -71,3 +71,4 @@ table_report [schema_public table_account_public table_event table_upload] 1970-
 table_report_policy [schema_public table_report role_account] 1970-01-01T00:00:00Z Marlon <marlon@maev.si> # Access policies for reports.
 table_legal_term [schema_public role_account role_anonymous] 1970-01-01T00:00:00Z Jonas Thelemann <e-mail+maevsi/sqitch@jonas-thelemann.de> # Legal terms like privacy policies or terms of service.
 table_legal_term_acceptance [schema_public table_account_public table_legal_term role_account] 1970-01-01T00:00:00Z Jonas Thelemann <e-mail+maevsi/sqitch@jonas-thelemann.de> # Tracks each user account's acceptance of legal terms and conditions.
+view_invitation_flat [schema_public table_invitation table_contact role_account] 1970-01-01T00:00:00Z Sven Thelemann <sven.thelemann@t-online.de> # View returning flattened invitations
diff --git a/src/verify/view_invitation_flat.sql b/src/verify/view_invitation_flat.sql
new file mode 100644
index 00000000..c922b659
--- /dev/null
+++ b/src/verify/view_invitation_flat.sql
@@ -0,0 +1,14 @@
+-- Verify maevsi:view_invitation_flat on pg
+
+BEGIN;
+
+SELECT
+  id, contact_id, event_id, feedback, feedback_paper,
+  contact_account_id, address, contact_author_account_id, email_address, email_address_hash,
+  first_name, last_name, phone_number, contact_url,
+  event_author_account_id, description, "start", "end",
+  invitee_count_maximum, is_archived, is_in_person, is_remote,
+  location, name, slug, event_url, visibility
+FROM maevsi.invitation_flat WHERE FALSE;
+
+ROLLBACK;