From b1423717d5d27220d3d485a7b78caddb8e5d9cf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dafydd=20Ll=C5=B7r=20Pearson?= Date: Thu, 25 Aug 2022 12:08:34 +0100 Subject: [PATCH] feat: Cleanup Save & Return scheduled events when lowcal_session record is deleted (#1126) --- .env | 3 ++- api.planx.uk/saveAndReturn/utils.js | 2 +- api.planx.uk/webhooks/hardDeleteSessions.js | 4 ++++ .../1661380924586_run_sql_migration/down.sql | 2 ++ .../1661380924586_run_sql_migration/up.sql | 20 +++++++++++++++++++ 5 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 hasura.planx.uk/migrations/1661380924586_run_sql_migration/down.sql create mode 100644 hasura.planx.uk/migrations/1661380924586_run_sql_migration/up.sql diff --git a/.env b/.env index 08bfad1996..17427b5ac3 100644 --- a/.env +++ b/.env @@ -7,7 +7,8 @@ API_URL_EXT=http://localhost:${API_PORT} HASURA_GRAPHQL_ADMIN_SECRET=TODO HASURA_PROXY_PORT=7000 HASURA_GRAPHQL_URL=http://localhost:${HASURA_PROXY_PORT}/v1/graphql -HASURA_GRAPHQL_CORS_DOMAIN="http://localhost:${API_PORT}, http://localhost:3000, http://api:${API_PORT}, http://localhost:${HASURA_PROXY_PORT}/v1/graphql, https://*.planx.uk, https://*.planx.dev, https://*.planx.pizza, https://*.gov.uk" +HASURA_CONSOLE_PORT=9695 +HASURA_GRAPHQL_CORS_DOMAIN="http://localhost:${API_PORT}, http://localhost:${HASURA_CONSOLE_PORT}, http://localhost:3000, http://api:${API_PORT}, http://localhost:${HASURA_PROXY_PORT}/v1/graphql, https://*.planx.uk, https://*.planx.dev, https://*.planx.pizza, https://*.gov.uk" HASURA_PLANX_API_URL=http://api:${API_PORT} HASURA_PLANX_API_KEY=REPLACE diff --git a/api.planx.uk/saveAndReturn/utils.js b/api.planx.uk/saveAndReturn/utils.js index 5c80ba5c7e..1596562ea0 100644 --- a/api.planx.uk/saveAndReturn/utils.js +++ b/api.planx.uk/saveAndReturn/utils.js @@ -145,7 +145,7 @@ const validateSingleSessionRequest = async (email, sessionId) => { const headers = getSaveAndReturnPublicHeaders(sessionId, email); const { lowcal_sessions: [session] } = await client.request(query, null, headers); - if (!session) throw Error("Unable to find session"); + if (!session) throw Error(`Unable to find session: ${sessionId}`); return { flowSlug: session.flow.slug, diff --git a/api.planx.uk/webhooks/hardDeleteSessions.js b/api.planx.uk/webhooks/hardDeleteSessions.js index 231b479ff6..81e7b9146d 100644 --- a/api.planx.uk/webhooks/hardDeleteSessions.js +++ b/api.planx.uk/webhooks/hardDeleteSessions.js @@ -2,6 +2,10 @@ const { subDays } = require("date-fns"); const { gql } = require("graphql-request"); const { adminGraphQLClient } = require("../hasura"); +/** + * Called by Hasura cron job `delete_expired_sessions` on a nightly basis + * See hasura.planx.uk/metadata/cron_triggers.yaml + */ const hardDeleteSessions = async (_req, res, next) => { try { const mutation = gql` diff --git a/hasura.planx.uk/migrations/1661380924586_run_sql_migration/down.sql b/hasura.planx.uk/migrations/1661380924586_run_sql_migration/down.sql new file mode 100644 index 0000000000..cf4bcac1e7 --- /dev/null +++ b/hasura.planx.uk/migrations/1661380924586_run_sql_migration/down.sql @@ -0,0 +1,2 @@ +DROP TRIGGER IF EXISTS delete_lowcal_session_scheduled_events_trigger ON lowcal_sessions; +DROP FUNCTION IF EXISTS delete_lowcal_session_scheduled_events; \ No newline at end of file diff --git a/hasura.planx.uk/migrations/1661380924586_run_sql_migration/up.sql b/hasura.planx.uk/migrations/1661380924586_run_sql_migration/up.sql new file mode 100644 index 0000000000..47bfa753e0 --- /dev/null +++ b/hasura.planx.uk/migrations/1661380924586_run_sql_migration/up.sql @@ -0,0 +1,20 @@ +CREATE OR REPLACE FUNCTION + delete_lowcal_session_scheduled_events() +RETURNS TRIGGER AS $$ +BEGIN + DELETE FROM hdb_catalog.hdb_scheduled_events + WHERE comment IN ( + 'reminder_' || OLD.id, + 'expiry_' || OLD.id + ); + RETURN NULL; +END +$$ LANGUAGE plpgsql VOLATILE; + +DROP TRIGGER IF EXISTS delete_lowcal_session_scheduled_events_trigger ON lowcal_sessions; +CREATE TRIGGER delete_lowcal_session_scheduled_events_trigger +AFTER DELETE ON lowcal_sessions FOR EACH ROW +EXECUTE PROCEDURE delete_lowcal_session_scheduled_events(); + +COMMENT ON TRIGGER delete_lowcal_session_scheduled_events_trigger ON lowcal_sessions +IS 'Delete linked scheduled events (reminder and expiry emails) when deleting a lowcal_session';