Skip to content

Commit

Permalink
feat: Cleanup Save & Return scheduled events when lowcal_session reco…
Browse files Browse the repository at this point in the history
…rd is deleted (#1126)
  • Loading branch information
DafyddLlyr authored Aug 25, 2022
1 parent a729019 commit b142371
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion api.planx.uk/saveAndReturn/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions api.planx.uk/webhooks/hardDeleteSessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
20 changes: 20 additions & 0 deletions hasura.planx.uk/migrations/1661380924586_run_sql_migration/up.sql
Original file line number Diff line number Diff line change
@@ -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';

0 comments on commit b142371

Please sign in to comment.