From 880bdfb4b7fa5752a94ad1a7b69fd6f679a92a90 Mon Sep 17 00:00:00 2001 From: Yared Yemane Date: Sun, 16 Aug 2026 18:52:09 +0300 Subject: [PATCH] Fix scheduled notification creator FK and default app timezone to EAT. Point scheduled_notifications.created_by at team_members so admin JWT IDs validate, and set TZ=Africa/Addis_Ababa for the Docker app service. Co-authored-by: Cursor --- ...ifications_created_by_team_member.down.sql | 6 +++++ ...otifications_created_by_team_member.up.sql | 22 +++++++++++++++++++ docker-compose.yml | 1 + internal/domain/scheduled_notification.go | 2 +- 4 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 db/migrations/000107_scheduled_notifications_created_by_team_member.down.sql create mode 100644 db/migrations/000107_scheduled_notifications_created_by_team_member.up.sql diff --git a/db/migrations/000107_scheduled_notifications_created_by_team_member.down.sql b/db/migrations/000107_scheduled_notifications_created_by_team_member.down.sql new file mode 100644 index 00000000..c143bf3c --- /dev/null +++ b/db/migrations/000107_scheduled_notifications_created_by_team_member.down.sql @@ -0,0 +1,6 @@ +ALTER TABLE scheduled_notifications + DROP CONSTRAINT IF EXISTS scheduled_notifications_created_by_fkey; + +ALTER TABLE scheduled_notifications + ADD CONSTRAINT scheduled_notifications_created_by_fkey + FOREIGN KEY (created_by) REFERENCES users(id); diff --git a/db/migrations/000107_scheduled_notifications_created_by_team_member.up.sql b/db/migrations/000107_scheduled_notifications_created_by_team_member.up.sql new file mode 100644 index 00000000..f74f0d69 --- /dev/null +++ b/db/migrations/000107_scheduled_notifications_created_by_team_member.up.sql @@ -0,0 +1,22 @@ +-- scheduled_notifications.created_by is the admin actor who scheduled the job. +-- Admin panel auth (POST /team/login) JWT user_id is team_members.id, not users.id. + +ALTER TABLE scheduled_notifications + DROP CONSTRAINT IF EXISTS scheduled_notifications_created_by_fkey; + +-- Remap legacy rows that stored platform users.id to a valid team member when possible. +UPDATE scheduled_notifications sn +SET created_by = tm.id +FROM users u +JOIN team_members tm ON LOWER(TRIM(tm.email)) = LOWER(TRIM(u.email)) +WHERE sn.created_by = u.id + AND NOT EXISTS (SELECT 1 FROM team_members existing WHERE existing.id = sn.created_by); + +-- Fallback: assign orphaned created_by values to the earliest team member. +UPDATE scheduled_notifications sn +SET created_by = (SELECT id FROM team_members ORDER BY id LIMIT 1) +WHERE NOT EXISTS (SELECT 1 FROM team_members tm WHERE tm.id = sn.created_by); + +ALTER TABLE scheduled_notifications + ADD CONSTRAINT scheduled_notifications_created_by_fkey + FOREIGN KEY (created_by) REFERENCES team_members(id); diff --git a/docker-compose.yml b/docker-compose.yml index 831bb743..79758d8c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -106,6 +106,7 @@ services: environment: - DB_URL=postgresql://root:secret@postgres:5432/gh?sslmode=disable - MONGO_URI=mongodb://root:secret@mongo:27017 + - TZ=Africa/Addis_Ababa depends_on: migrate: condition: service_completed_successfully diff --git a/internal/domain/scheduled_notification.go b/internal/domain/scheduled_notification.go index 7112e497..7f572795 100644 --- a/internal/domain/scheduled_notification.go +++ b/internal/domain/scheduled_notification.go @@ -74,7 +74,7 @@ type ScheduledNotification struct { SentAt *time.Time `json:"sent_at,omitempty"` CancelledAt *time.Time `json:"cancelled_at,omitempty"` - CreatedBy int64 `json:"created_by"` + CreatedBy int64 `json:"created_by"` // team_members.id of the staff actor who scheduled the job CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` }