Skip to content

Commit

Permalink
feat: add response received column
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed Jul 15, 2024
1 parent 2162263 commit 51f4ba9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
15 changes: 8 additions & 7 deletions internal/nostr/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +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
CreatedAt time.Time
UpdatedAt time.Time
ID uint
SubscriptionId *uint
NostrId string `validate:"required"`
Content string
State string
ResponseReceived bool
CreatedAt time.Time
UpdatedAt time.Time
}

type ResponseEvent struct {
Expand Down
4 changes: 4 additions & 0 deletions internal/nostr/nostr.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,10 @@ func (svc *Service) handleResponseEvent(event *nostr.Event, subscription *Subscr
"request_event_id": subscription.RequestEvent.ID,
"wallet_pubkey": svc.getWalletPubkey(subscription.Authors),
}).Info("Received response event")
if (subscription.RequestEvent != nil) {
subscription.RequestEventDB.ResponseReceived = true
svc.db.Save(&subscription.RequestEventDB)
}
responseEvent := ResponseEvent{
NostrId: event.ID,
Content: event.Content,
Expand Down
33 changes: 33 additions & 0 deletions migrations/202407131920_add_response_state_to_request_events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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
},
}
1 change: 1 addition & 0 deletions migrations/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +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,
})

return m.Migrate()
Expand Down

0 comments on commit 51f4ba9

Please sign in to comment.