From 91d32585338265c8f94abcd2b8ad479caaa7fcf1 Mon Sep 17 00:00:00 2001 From: devthejo Date: Sun, 25 May 2025 09:25:04 +0200 Subject: [PATCH] feat(follow-location): init --- .../v1/operations/alert/send-alert.patch.js | 5 +- .../alert/send-alert.patch.spec.yaml | 2 + .../default/tables/public_alert.yaml | 16 ++++++ .../down.sql | 4 ++ .../up.sql | 2 + .../down.sql | 4 ++ .../up.sql | 2 + .../down.sql | 4 ++ .../up.sql | 2 + .../watchers/alert-table-follow-location.js | 54 +++++++++++++++++++ 10 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 services/hasura/migrations/default/1747985127916_alter_table_public_alert_add_column_initial_location/down.sql create mode 100644 services/hasura/migrations/default/1747985127916_alter_table_public_alert_add_column_initial_location/up.sql create mode 100644 services/hasura/migrations/default/1748092711434_alter_table_public_alert_add_column_follow_location/down.sql create mode 100644 services/hasura/migrations/default/1748092711434_alter_table_public_alert_add_column_follow_location/up.sql create mode 100644 services/hasura/migrations/default/1748092761961_alter_table_public_alert_add_column_follow_location_ran/down.sql create mode 100644 services/hasura/migrations/default/1748092761961_alter_table_public_alert_add_column_follow_location_ran/up.sql create mode 100644 services/watchers/src/watchers/alert-table-follow-location.js diff --git a/services/api/src/api/v1/operations/alert/send-alert.patch.js b/services/api/src/api/v1/operations/alert/send-alert.patch.js index db23716..f50ced6 100644 --- a/services/api/src/api/v1/operations/alert/send-alert.patch.js +++ b/services/api/src/api/v1/operations/alert/send-alert.patch.js @@ -30,6 +30,7 @@ module.exports = function () { callEmergency, notifyAround, notifyRelatives, + followLocation, level, subject, accuracy, @@ -54,8 +55,8 @@ module.exports = function () { let alertId await sql.begin(async () => { const [{ id }] = await sql` - INSERT INTO "alert" ("uuid", "device_id", "user_id", "call_emergency", "notify_around", "notify_relatives", "level", "subject", "location", "accuracy", "altitude", "altitude_accuracy", "heading", "speed", "code", "access_code") - VALUES (${uuid}, ${deviceId}, ${userId}, ${callEmergency}, ${notifyAround}, ${notifyRelatives}, ${level}, ${subject}, ST_GeomFromGeoJSON (${locationJSON}), ${accuracy}, ${altitude}, ${altitudeAccuracy}, ${heading}, ${speed}, ${code}, ${accessCode}) + INSERT INTO "alert" ("uuid", "device_id", "user_id", "call_emergency", "notify_around", "notify_relatives", "follow_location", "level", "subject", "location", "initial_location", "accuracy", "altitude", "altitude_accuracy", "heading", "speed", "code", "access_code") + VALUES (${uuid}, ${deviceId}, ${userId}, ${callEmergency}, ${notifyAround}, ${notifyRelatives}, ${followLocation}, ${level}, ${subject}, ST_GeomFromGeoJSON (${locationJSON}), ST_GeomFromGeoJSON (${locationJSON}), ${accuracy}, ${altitude}, ${altitudeAccuracy}, ${heading}, ${speed}, ${code}, ${accessCode}) ON CONFLICT ("uuid") DO UPDATE SET "uuid" = EXCLUDED. "uuid" diff --git a/services/api/src/api/v1/operations/alert/send-alert.patch.spec.yaml b/services/api/src/api/v1/operations/alert/send-alert.patch.spec.yaml index 51f1b02..d32ecd2 100644 --- a/services/api/src/api/v1/operations/alert/send-alert.patch.spec.yaml +++ b/services/api/src/api/v1/operations/alert/send-alert.patch.spec.yaml @@ -14,6 +14,8 @@ requestBody: type: boolean notifyRelatives: type: boolean + followLocation: + type: boolean uuid: type: string format: uuid diff --git a/services/hasura/metadata/databases/default/tables/public_alert.yaml b/services/hasura/metadata/databases/default/tables/public_alert.yaml index def9f62..d6f3990 100644 --- a/services/hasura/metadata/databases/default/tables/public_alert.yaml +++ b/services/hasura/metadata/databases/default/tables/public_alert.yaml @@ -33,6 +33,12 @@ configuration: custom_name: deviceId emergency_calling_notification_sent: custom_name: emergencyCallingNotificationSent + follow_location: + custom_name: followLocation + follow_location_ran: + custom_name: followLocationRan + initial_location: + custom_name: initialLocation keep_open_at: custom_name: keepOpenAt nearest_place: @@ -71,6 +77,9 @@ configuration: created_at: createdAt device_id: deviceId emergency_calling_notification_sent: emergencyCallingNotificationSent + follow_location: followLocation + follow_location_ran: followLocationRan + initial_location: initialLocation keep_open_at: keepOpenAt nearest_place: nearestPlace notified_count: notifiedCount @@ -128,8 +137,11 @@ select_permissions: - closed_at - code - created_at + - follow_location + - follow_location_ran - heading - id + - initial_location - keep_open_at - level - location @@ -166,8 +178,11 @@ select_permissions: - closed_at - code - created_at + - follow_location + - follow_location_ran - heading - id + - initial_location - keep_open_at - level - location @@ -193,6 +208,7 @@ update_permissions: permission: columns: - alert_tag + - follow_location - level - subject filter: diff --git a/services/hasura/migrations/default/1747985127916_alter_table_public_alert_add_column_initial_location/down.sql b/services/hasura/migrations/default/1747985127916_alter_table_public_alert_add_column_initial_location/down.sql new file mode 100644 index 0000000..406e9eb --- /dev/null +++ b/services/hasura/migrations/default/1747985127916_alter_table_public_alert_add_column_initial_location/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"."alert" add column "initial_location" geography +-- null; diff --git a/services/hasura/migrations/default/1747985127916_alter_table_public_alert_add_column_initial_location/up.sql b/services/hasura/migrations/default/1747985127916_alter_table_public_alert_add_column_initial_location/up.sql new file mode 100644 index 0000000..de52958 --- /dev/null +++ b/services/hasura/migrations/default/1747985127916_alter_table_public_alert_add_column_initial_location/up.sql @@ -0,0 +1,2 @@ +alter table "public"."alert" add column "initial_location" geography + null; diff --git a/services/hasura/migrations/default/1748092711434_alter_table_public_alert_add_column_follow_location/down.sql b/services/hasura/migrations/default/1748092711434_alter_table_public_alert_add_column_follow_location/down.sql new file mode 100644 index 0000000..52dca78 --- /dev/null +++ b/services/hasura/migrations/default/1748092711434_alter_table_public_alert_add_column_follow_location/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"."alert" add column "follow_location" boolean +-- not null default 'false'; diff --git a/services/hasura/migrations/default/1748092711434_alter_table_public_alert_add_column_follow_location/up.sql b/services/hasura/migrations/default/1748092711434_alter_table_public_alert_add_column_follow_location/up.sql new file mode 100644 index 0000000..3bafe5f --- /dev/null +++ b/services/hasura/migrations/default/1748092711434_alter_table_public_alert_add_column_follow_location/up.sql @@ -0,0 +1,2 @@ +alter table "public"."alert" add column "follow_location" boolean + not null default 'false'; diff --git a/services/hasura/migrations/default/1748092761961_alter_table_public_alert_add_column_follow_location_ran/down.sql b/services/hasura/migrations/default/1748092761961_alter_table_public_alert_add_column_follow_location_ran/down.sql new file mode 100644 index 0000000..20cd441 --- /dev/null +++ b/services/hasura/migrations/default/1748092761961_alter_table_public_alert_add_column_follow_location_ran/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"."alert" add column "follow_location_ran" boolean +-- not null default 'false'; diff --git a/services/hasura/migrations/default/1748092761961_alter_table_public_alert_add_column_follow_location_ran/up.sql b/services/hasura/migrations/default/1748092761961_alter_table_public_alert_add_column_follow_location_ran/up.sql new file mode 100644 index 0000000..4eeee2d --- /dev/null +++ b/services/hasura/migrations/default/1748092761961_alter_table_public_alert_add_column_follow_location_ran/up.sql @@ -0,0 +1,2 @@ +alter table "public"."alert" add column "follow_location_ran" boolean + not null default 'false'; diff --git a/services/watchers/src/watchers/alert-table-follow-location.js b/services/watchers/src/watchers/alert-table-follow-location.js new file mode 100644 index 0000000..65094b6 --- /dev/null +++ b/services/watchers/src/watchers/alert-table-follow-location.js @@ -0,0 +1,54 @@ +const async = require("async") + +const { gql } = require("@apollo/client/core") +const { ctx } = require("@modjo/core") +// const watcherCtx = require("modjo-plugins/core/ctx/watcher") +// const tasks = require("~/tasks") + +const ALERT_FOLLOW_LOCATION_SUBSCRIPTION = gql` + subscription onAlertFollowLocation($limit: Int) { + selectManyAlert( + where: { _and: { state: { _eq: "open" }, followLocation: { _eq: true } } } + limit: $limit + order_by: { id: asc } + ) { + id + } + } +` + +module.exports = async function () { + const logger = ctx.require("logger") + const apolloClient = ctx.require("apolloClient") + // const { addTask } = ctx.require("amqp") + + // const QUEUE_BATCH_SIZE = null + const QUEUE_BATCH_SIZE = 1000 + + return async function alertTableFollowLocation() { + logger.info("watcher alertTableFollowLocation: daemon started") + + const observable = apolloClient.subscribe({ + query: ALERT_FOLLOW_LOCATION_SUBSCRIPTION, + variables: { + limit: QUEUE_BATCH_SIZE, + }, + }) + + observable.subscribe({ + next: async ({ data }) => { + const { selectManyAlert } = data + await async.eachOf(selectManyAlert, async ({ id: _id }) => { + // TODO + }) + }, + error: (error) => { + logger.error( + { error, watcher: "alertTableFollowLocation" }, + `subscription error` + ) + throw error + }, + }) + } +}