diff --git a/internal/nostr/models.go b/internal/nostr/models.go index 98b2d3c..5a5ed88 100644 --- a/internal/nostr/models.go +++ b/internal/nostr/models.go @@ -130,14 +130,14 @@ type OnReceiveEOSFunc func(ctx context.Context, subscription *Subscription) type HandleEventFunc func(event *nostr.Event, subscription *Subscription) type RequestEvent struct { - ID uint - SubscriptionId *uint - NostrId string `validate:"required"` - Content string - State string - ResponseReceived bool - CreatedAt time.Time - UpdatedAt time.Time + ID uint + SubscriptionId *uint + NostrId string `validate:"required"` + Content string + State string + ResponseReceivedAt time.Time + CreatedAt time.Time + UpdatedAt time.Time } type ResponseEvent struct { diff --git a/internal/nostr/nostr.go b/internal/nostr/nostr.go index db1c31c..caa42dc 100644 --- a/internal/nostr/nostr.go +++ b/internal/nostr/nostr.go @@ -795,7 +795,7 @@ func (svc *Service) handleResponseEvent(event *nostr.Event, subscription *Subscr "wallet_pubkey": svc.getWalletPubkey(subscription.Authors), }).Info("Received response event") if (subscription.RequestEvent != nil) { - subscription.RequestEventDB.ResponseReceived = true + subscription.RequestEventDB.ResponseReceivedAt = time.Now() svc.db.Save(&subscription.RequestEventDB) } responseEvent := ResponseEvent{ diff --git a/migrations/202407131920_add_response_state_to_request_events.go b/migrations/202407131920_add_response_state_to_request_events.go deleted file mode 100644 index 721cd88..0000000 --- a/migrations/202407131920_add_response_state_to_request_events.go +++ /dev/null @@ -1,33 +0,0 @@ -package migrations - -import ( - "github.com/go-gormigrate/gormigrate/v2" - "gorm.io/gorm" -) - -// Add response_received column to request_events table -var _202407131920_add_response_state_to_request_events = &gormigrate.Migration{ - ID: "202407131920_add_response_state_to_request_events", - Migrate: func(tx *gorm.DB) error { - if err := tx.Exec("ALTER TABLE request_events ADD COLUMN response_received BOOLEAN DEFAULT FALSE").Error; err != nil { - return err - } - - // Update response_received to TRUE if there is a corresponding row in response_events - if err := tx.Exec(` - UPDATE request_events - SET response_received = TRUE - WHERE id IN (SELECT request_id FROM response_events WHERE request_id IS NOT NULL) - `).Error; err != nil { - return err - } - - return nil - }, - Rollback: func(tx *gorm.DB) error { - if err := tx.Exec("ALTER TABLE request_events DROP COLUMN response_received").Error; err != nil { - return err - } - return nil - }, -} diff --git a/migrations/202407171220_add_response_state_to_request_events.go b/migrations/202407171220_add_response_state_to_request_events.go new file mode 100644 index 0000000..9c904a6 --- /dev/null +++ b/migrations/202407171220_add_response_state_to_request_events.go @@ -0,0 +1,33 @@ +package migrations + +import ( + "github.com/go-gormigrate/gormigrate/v2" + "gorm.io/gorm" +) + +// Add response_received_at column to request_events table +var _202407171220_add_response_received_at_to_request_events = &gormigrate.Migration{ + ID: "202407171220_add_response_received_at_to_request_events", + Migrate: func(tx *gorm.DB) error { + if err := tx.Exec("ALTER TABLE request_events ADD COLUMN response_received_at TIMESTAMP DEFAULT '0001-01-01 00:00:00+00'").Error; err != nil { + return err + } + + // Update response_received_at if there is a corresponding row in response_events + if err := tx.Exec(` + UPDATE request_events re + SET response_received_at = (SELECT created_at FROM response_events WHERE request_id = re.id) + WHERE id IN (SELECT request_id FROM response_events WHERE request_id IS NOT NULL) + `).Error; err != nil { + return err + } + + return nil + }, + Rollback: func(tx *gorm.DB) error { + if err := tx.Exec("ALTER TABLE request_events DROP COLUMN response_received_at").Error; err != nil { + return err + } + return nil + }, +} diff --git a/migrations/migrate.go b/migrations/migrate.go index e0280a5..444adee 100644 --- a/migrations/migrate.go +++ b/migrations/migrate.go @@ -11,7 +11,7 @@ func Migrate(db *gorm.DB) error { _202402161653_initial_migration, _202404021628_add_uuid_to_subscriptions, _202404031539_add_indexes, - _202407131920_add_response_state_to_request_events, + _202407171220_add_response_received_at_to_request_events, }) return m.Migrate()