From e9887b29ae2091f3c5c5dfd973afa2679517563a Mon Sep 17 00:00:00 2001 From: devthejo Date: Sun, 4 May 2025 11:43:48 +0200 Subject: [PATCH] fix: archive_alert function --- .../down.sql | 4 + .../up.sql | 2 + .../down.sql | 4 + .../up.sql | 2 + .../down.sql | 4 + .../up.sql | 2 + .../down.sql | 4 + .../up.sql | 2 + .../down.sql | 4 + .../up.sql | 2 + .../down.sql | 4 + .../up.sql | 2 + .../down.sql | 4 + .../up.sql | 2 + .../down.sql | 165 ++++++++++++++++++ .../up.sql | 163 +++++++++++++++++ 16 files changed, 370 insertions(+) create mode 100644 services/hasura/migrations/default/1746351546952_alter_table_public_archived_alert_add_column_emergency_calling_notification_sent/down.sql create mode 100644 services/hasura/migrations/default/1746351546952_alter_table_public_archived_alert_add_column_emergency_calling_notification_sent/up.sql create mode 100644 services/hasura/migrations/default/1746351567604_alter_table_public_archived_alert_add_column_acknowledged_relative_count/down.sql create mode 100644 services/hasura/migrations/default/1746351567604_alter_table_public_archived_alert_add_column_acknowledged_relative_count/up.sql create mode 100644 services/hasura/migrations/default/1746351577786_alter_table_public_archived_alert_add_column_acknowledged_around_count/down.sql create mode 100644 services/hasura/migrations/default/1746351577786_alter_table_public_archived_alert_add_column_acknowledged_around_count/up.sql create mode 100644 services/hasura/migrations/default/1746351588513_alter_table_public_archived_alert_add_column_acknowledged_connect_count/down.sql create mode 100644 services/hasura/migrations/default/1746351588513_alter_table_public_archived_alert_add_column_acknowledged_connect_count/up.sql create mode 100644 services/hasura/migrations/default/1746351603197_alter_table_public_archived_alert_add_column_alerting_relative_count/down.sql create mode 100644 services/hasura/migrations/default/1746351603197_alter_table_public_archived_alert_add_column_alerting_relative_count/up.sql create mode 100644 services/hasura/migrations/default/1746351612691_alter_table_public_archived_alert_add_column_alerting_around_count/down.sql create mode 100644 services/hasura/migrations/default/1746351612691_alter_table_public_archived_alert_add_column_alerting_around_count/up.sql create mode 100644 services/hasura/migrations/default/1746351621876_alter_table_public_archived_alert_add_column_alerting_connect_count/down.sql create mode 100644 services/hasura/migrations/default/1746351621876_alter_table_public_archived_alert_add_column_alerting_connect_count/up.sql create mode 100644 services/hasura/migrations/default/1746351814777_fix_function_archive_alert/down.sql create mode 100644 services/hasura/migrations/default/1746351814777_fix_function_archive_alert/up.sql diff --git a/services/hasura/migrations/default/1746351546952_alter_table_public_archived_alert_add_column_emergency_calling_notification_sent/down.sql b/services/hasura/migrations/default/1746351546952_alter_table_public_archived_alert_add_column_emergency_calling_notification_sent/down.sql new file mode 100644 index 0000000..9550b59 --- /dev/null +++ b/services/hasura/migrations/default/1746351546952_alter_table_public_archived_alert_add_column_emergency_calling_notification_sent/down.sql @@ -0,0 +1,4 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- alter table "public"."archived_alert" add column "emergency_calling_notification_sent" boolean +-- null; diff --git a/services/hasura/migrations/default/1746351546952_alter_table_public_archived_alert_add_column_emergency_calling_notification_sent/up.sql b/services/hasura/migrations/default/1746351546952_alter_table_public_archived_alert_add_column_emergency_calling_notification_sent/up.sql new file mode 100644 index 0000000..6a18b67 --- /dev/null +++ b/services/hasura/migrations/default/1746351546952_alter_table_public_archived_alert_add_column_emergency_calling_notification_sent/up.sql @@ -0,0 +1,2 @@ +alter table "public"."archived_alert" add column "emergency_calling_notification_sent" boolean + null; diff --git a/services/hasura/migrations/default/1746351567604_alter_table_public_archived_alert_add_column_acknowledged_relative_count/down.sql b/services/hasura/migrations/default/1746351567604_alter_table_public_archived_alert_add_column_acknowledged_relative_count/down.sql new file mode 100644 index 0000000..e001121 --- /dev/null +++ b/services/hasura/migrations/default/1746351567604_alter_table_public_archived_alert_add_column_acknowledged_relative_count/down.sql @@ -0,0 +1,4 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- alter table "public"."archived_alert" add column "acknowledged_relative_count" integer +-- not null default '0'; diff --git a/services/hasura/migrations/default/1746351567604_alter_table_public_archived_alert_add_column_acknowledged_relative_count/up.sql b/services/hasura/migrations/default/1746351567604_alter_table_public_archived_alert_add_column_acknowledged_relative_count/up.sql new file mode 100644 index 0000000..83e877e --- /dev/null +++ b/services/hasura/migrations/default/1746351567604_alter_table_public_archived_alert_add_column_acknowledged_relative_count/up.sql @@ -0,0 +1,2 @@ +alter table "public"."archived_alert" add column "acknowledged_relative_count" integer + not null default '0'; diff --git a/services/hasura/migrations/default/1746351577786_alter_table_public_archived_alert_add_column_acknowledged_around_count/down.sql b/services/hasura/migrations/default/1746351577786_alter_table_public_archived_alert_add_column_acknowledged_around_count/down.sql new file mode 100644 index 0000000..6b94f35 --- /dev/null +++ b/services/hasura/migrations/default/1746351577786_alter_table_public_archived_alert_add_column_acknowledged_around_count/down.sql @@ -0,0 +1,4 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- alter table "public"."archived_alert" add column "acknowledged_around_count" integer +-- not null default '0'; diff --git a/services/hasura/migrations/default/1746351577786_alter_table_public_archived_alert_add_column_acknowledged_around_count/up.sql b/services/hasura/migrations/default/1746351577786_alter_table_public_archived_alert_add_column_acknowledged_around_count/up.sql new file mode 100644 index 0000000..2b15a07 --- /dev/null +++ b/services/hasura/migrations/default/1746351577786_alter_table_public_archived_alert_add_column_acknowledged_around_count/up.sql @@ -0,0 +1,2 @@ +alter table "public"."archived_alert" add column "acknowledged_around_count" integer + not null default '0'; diff --git a/services/hasura/migrations/default/1746351588513_alter_table_public_archived_alert_add_column_acknowledged_connect_count/down.sql b/services/hasura/migrations/default/1746351588513_alter_table_public_archived_alert_add_column_acknowledged_connect_count/down.sql new file mode 100644 index 0000000..10e1372 --- /dev/null +++ b/services/hasura/migrations/default/1746351588513_alter_table_public_archived_alert_add_column_acknowledged_connect_count/down.sql @@ -0,0 +1,4 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- alter table "public"."archived_alert" add column "acknowledged_connect_count" integer +-- not null default '0'; diff --git a/services/hasura/migrations/default/1746351588513_alter_table_public_archived_alert_add_column_acknowledged_connect_count/up.sql b/services/hasura/migrations/default/1746351588513_alter_table_public_archived_alert_add_column_acknowledged_connect_count/up.sql new file mode 100644 index 0000000..49fa9ee --- /dev/null +++ b/services/hasura/migrations/default/1746351588513_alter_table_public_archived_alert_add_column_acknowledged_connect_count/up.sql @@ -0,0 +1,2 @@ +alter table "public"."archived_alert" add column "acknowledged_connect_count" integer + not null default '0'; diff --git a/services/hasura/migrations/default/1746351603197_alter_table_public_archived_alert_add_column_alerting_relative_count/down.sql b/services/hasura/migrations/default/1746351603197_alter_table_public_archived_alert_add_column_alerting_relative_count/down.sql new file mode 100644 index 0000000..7d9ff6d --- /dev/null +++ b/services/hasura/migrations/default/1746351603197_alter_table_public_archived_alert_add_column_alerting_relative_count/down.sql @@ -0,0 +1,4 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- alter table "public"."archived_alert" add column "alerting_relative_count" integer +-- not null default '0'; diff --git a/services/hasura/migrations/default/1746351603197_alter_table_public_archived_alert_add_column_alerting_relative_count/up.sql b/services/hasura/migrations/default/1746351603197_alter_table_public_archived_alert_add_column_alerting_relative_count/up.sql new file mode 100644 index 0000000..6fd03de --- /dev/null +++ b/services/hasura/migrations/default/1746351603197_alter_table_public_archived_alert_add_column_alerting_relative_count/up.sql @@ -0,0 +1,2 @@ +alter table "public"."archived_alert" add column "alerting_relative_count" integer + not null default '0'; diff --git a/services/hasura/migrations/default/1746351612691_alter_table_public_archived_alert_add_column_alerting_around_count/down.sql b/services/hasura/migrations/default/1746351612691_alter_table_public_archived_alert_add_column_alerting_around_count/down.sql new file mode 100644 index 0000000..0c0798b --- /dev/null +++ b/services/hasura/migrations/default/1746351612691_alter_table_public_archived_alert_add_column_alerting_around_count/down.sql @@ -0,0 +1,4 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- alter table "public"."archived_alert" add column "alerting_around_count" integer +-- not null default '0'; diff --git a/services/hasura/migrations/default/1746351612691_alter_table_public_archived_alert_add_column_alerting_around_count/up.sql b/services/hasura/migrations/default/1746351612691_alter_table_public_archived_alert_add_column_alerting_around_count/up.sql new file mode 100644 index 0000000..7d8287c --- /dev/null +++ b/services/hasura/migrations/default/1746351612691_alter_table_public_archived_alert_add_column_alerting_around_count/up.sql @@ -0,0 +1,2 @@ +alter table "public"."archived_alert" add column "alerting_around_count" integer + not null default '0'; diff --git a/services/hasura/migrations/default/1746351621876_alter_table_public_archived_alert_add_column_alerting_connect_count/down.sql b/services/hasura/migrations/default/1746351621876_alter_table_public_archived_alert_add_column_alerting_connect_count/down.sql new file mode 100644 index 0000000..ac552eb --- /dev/null +++ b/services/hasura/migrations/default/1746351621876_alter_table_public_archived_alert_add_column_alerting_connect_count/down.sql @@ -0,0 +1,4 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- alter table "public"."archived_alert" add column "alerting_connect_count" integer +-- not null default '0'; diff --git a/services/hasura/migrations/default/1746351621876_alter_table_public_archived_alert_add_column_alerting_connect_count/up.sql b/services/hasura/migrations/default/1746351621876_alter_table_public_archived_alert_add_column_alerting_connect_count/up.sql new file mode 100644 index 0000000..98cba24 --- /dev/null +++ b/services/hasura/migrations/default/1746351621876_alter_table_public_archived_alert_add_column_alerting_connect_count/up.sql @@ -0,0 +1,2 @@ +alter table "public"."archived_alert" add column "alerting_connect_count" integer + not null default '0'; diff --git a/services/hasura/migrations/default/1746351814777_fix_function_archive_alert/down.sql b/services/hasura/migrations/default/1746351814777_fix_function_archive_alert/down.sql new file mode 100644 index 0000000..d7636a7 --- /dev/null +++ b/services/hasura/migrations/default/1746351814777_fix_function_archive_alert/down.sql @@ -0,0 +1,165 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- CREATE OR REPLACE FUNCTION public.archive_alert(p_id integer) +-- RETURNS void +-- LANGUAGE plpgsql +-- AS $function$ +-- DECLARE +-- v_archived_alert_id INTEGER; +-- BEGIN +-- -- Insert the selected alert into the archived_alert table +-- INSERT INTO archived_alert ( +-- "alert_id", +-- "user_id", +-- "phone_number_id", +-- "device_id", +-- "location", +-- "alert_tag", +-- "created_at", +-- "call_emergency", +-- "notify_around", +-- "notify_relatives", +-- "level", +-- "subject", +-- "accuracy", +-- "altitude", +-- "altitude_accuracy", +-- "heading", +-- "speed", +-- "radius", +-- "address", +-- "what3words", +-- "nearest_place", +-- "uuid", +-- "code", +-- "notified_count", +-- "closed_at", +-- "keep_open_at", +-- "updated_at", +-- "suggest_close_sent", +-- "suggest_keep_open_sent", +-- "closed_by", +-- "alerting_relative_count", +-- "alerting_around_count", +-- "alerting_connect_count", +-- "acknowledged_relative_count", +-- "acknowledged_around_count", +-- "acknowledged_connect_count", +-- "emergency_calling_notification_sent" +-- +-- ) +-- SELECT +-- "id", +-- "user_id", +-- "phone_number_id", +-- "device_id", +-- "location", +-- "alert_tag", +-- "created_at", +-- "call_emergency", +-- "notify_around", +-- "notify_relatives", +-- "level", +-- "subject", +-- "accuracy", +-- "altitude", +-- "altitude_accuracy", +-- "heading", +-- "speed", +-- "radius", +-- "address", +-- "what3words", +-- "nearest_place", +-- "uuid", +-- "code", +-- "notified_count", +-- "closed_at", +-- "keep_open_at", +-- "updated_at", +-- "suggest_close_sent", +-- "suggest_keep_open_sent", +-- "closed_by", +-- "alerting_relative_count", +-- "alerting_around_count", +-- "alerting_connect_count", +-- "acknowledged_relative_count", +-- "acknowledged_around_count", +-- "acknowledged_connect_count", +-- "emergency_calling_notification_sent" +-- FROM "alert" +-- WHERE "id" = p_id +-- RETURNING "id" INTO v_archived_alert_id; +-- +-- -- Insert related alerted records into the archived_alerted table +-- INSERT INTO +-- "alerted" ( +-- "archived_alert_id", +-- "user_id", +-- "opened_once", +-- "created_at", +-- "near_location", +-- "opened", +-- "device_id", +-- "initial_location", +-- "notification_sent", +-- "notification_sent_at", +-- "initial_distance", +-- "geomatch_method", +-- "reason", +-- "updated_at", +-- "coming_help", +-- "relative_user_id" +-- ) +-- SELECT +-- v_archived_alert_id, +-- "user_id", +-- "opened_once", +-- "created_at", +-- "near_location", +-- "opened", +-- "device_id", +-- "initial_location", +-- "notification_sent", +-- "notification_sent_at", +-- "initial_distance", +-- "geomatch_method", +-- "reason", +-- "updated_at", +-- "coming_help", +-- "relative_user_id" +-- FROM "alerting" +-- WHERE "alert_id" = p_id; +-- +-- -- Insert related messages into the archived_message table +-- INSERT INTO archived_message ( +-- "archived_alert_id", +-- "user_id", +-- "device_id", +-- "content_type", +-- "text", +-- "audio_file_uuid", +-- "location", +-- "created_at" +-- ) +-- SELECT +-- v_archived_alert_id, +-- "user_id", +-- "device_id", +-- "content_type", +-- "text", +-- "audio_file_uuid", +-- "location", +-- "created_at" +-- FROM "message" +-- WHERE "alert_id" = p_id; +-- +-- -- Delete the messages from the message table +-- DELETE FROM "message" +-- WHERE "alert_id" = p_id; +-- +-- -- Delete the alert from the alert table +-- DELETE FROM "alert" +-- WHERE id = p_id; +-- +-- END; +-- $function$; diff --git a/services/hasura/migrations/default/1746351814777_fix_function_archive_alert/up.sql b/services/hasura/migrations/default/1746351814777_fix_function_archive_alert/up.sql new file mode 100644 index 0000000..e60ed35 --- /dev/null +++ b/services/hasura/migrations/default/1746351814777_fix_function_archive_alert/up.sql @@ -0,0 +1,163 @@ +CREATE OR REPLACE FUNCTION public.archive_alert(p_id integer) + RETURNS void + LANGUAGE plpgsql +AS $function$ +DECLARE + v_archived_alert_id INTEGER; +BEGIN + -- Insert the selected alert into the archived_alert table + INSERT INTO archived_alert ( + "alert_id", + "user_id", + "phone_number_id", + "device_id", + "location", + "alert_tag", + "created_at", + "call_emergency", + "notify_around", + "notify_relatives", + "level", + "subject", + "accuracy", + "altitude", + "altitude_accuracy", + "heading", + "speed", + "radius", + "address", + "what3words", + "nearest_place", + "uuid", + "code", + "notified_count", + "closed_at", + "keep_open_at", + "updated_at", + "suggest_close_sent", + "suggest_keep_open_sent", + "closed_by", + "alerting_relative_count", + "alerting_around_count", + "alerting_connect_count", + "acknowledged_relative_count", + "acknowledged_around_count", + "acknowledged_connect_count", + "emergency_calling_notification_sent" + + ) + SELECT + "id", + "user_id", + "phone_number_id", + "device_id", + "location", + "alert_tag", + "created_at", + "call_emergency", + "notify_around", + "notify_relatives", + "level", + "subject", + "accuracy", + "altitude", + "altitude_accuracy", + "heading", + "speed", + "radius", + "address", + "what3words", + "nearest_place", + "uuid", + "code", + "notified_count", + "closed_at", + "keep_open_at", + "updated_at", + "suggest_close_sent", + "suggest_keep_open_sent", + "closed_by", + "alerting_relative_count", + "alerting_around_count", + "alerting_connect_count", + "acknowledged_relative_count", + "acknowledged_around_count", + "acknowledged_connect_count", + "emergency_calling_notification_sent" + FROM "alert" + WHERE "id" = p_id + RETURNING "id" INTO v_archived_alert_id; + + -- Insert related alerted records into the archived_alerted table + INSERT INTO + "alerted" ( + "archived_alert_id", + "user_id", + "opened_once", + "created_at", + "near_location", + "opened", + "device_id", + "initial_location", + "notification_sent", + "notification_sent_at", + "initial_distance", + "geomatch_method", + "reason", + "updated_at", + "coming_help", + "relative_user_id" + ) + SELECT + v_archived_alert_id, + "user_id", + "opened_once", + "created_at", + "near_location", + "opened", + "device_id", + "initial_location", + "notification_sent", + "notification_sent_at", + "initial_distance", + "geomatch_method", + "reason", + "updated_at", + "coming_help", + "relative_user_id" + FROM "alerting" + WHERE "alert_id" = p_id; + + -- Insert related messages into the archived_message table + INSERT INTO archived_message ( + "archived_alert_id", + "user_id", + "device_id", + "content_type", + "text", + "audio_file_uuid", + "location", + "created_at" + ) + SELECT + v_archived_alert_id, + "user_id", + "device_id", + "content_type", + "text", + "audio_file_uuid", + "location", + "created_at" + FROM "message" + WHERE "alert_id" = p_id; + + -- Delete the messages from the message table + DELETE FROM "message" + WHERE "alert_id" = p_id; + + -- Delete the alert from the alert table + DELETE FROM "alert" + WHERE id = p_id; + +END; +$function$;