Skip to content

Commit

Permalink
Fix issue with automatically tracked packages not getting notificatio…
Browse files Browse the repository at this point in the history
…ns and not being unfollowable
  • Loading branch information
alufers committed Sep 21, 2023
1 parent 83141a5 commit 7987ff5
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ deobfuscated
paczkobot.db

.DS_Store
*.db
4 changes: 4 additions & 0 deletions paczkobot/bot_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ func NewBotApp(b *tgbotapi.BotAPI, DB *gorm.DB) (a *BotApp) {
}

func (a *BotApp) Run() {
if err := MigrateBadInpostAccounts(a.DB); err != nil {
log.Fatalf("Failed to migrate bad inpost accounts: %v", err)
}

log.Printf("Flushing enqueued notifications...")
if err := a.NotificationsService.FlushEnqueuedNotifications(); err != nil {
log.Fatalf("Failed to flush enqueued notifications: %v", err)
Expand Down
1 change: 1 addition & 0 deletions paczkobot/inpost_login_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func (f *InpostLoginCommand) Execute(ctx context.Context, args *tghelpers.Comman
}

creds.TelegramUserID = args.FromUserID
creds.TelegramChatID = args.FromUserID
err = f.App.DB.Where("telegram_user_id = ? AND phone_number = ?", args.FromUserID, phoneNumber).FirstOrCreate(&creds).Error
if err != nil {
return fmt.Errorf("failed to delete existing credentials: %v", err)
Expand Down
46 changes: 46 additions & 0 deletions paczkobot/migrate_bad_inpost_accounts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package paczkobot

import (
"log"

"gorm.io/gorm"
)

func MigrateBadInpostAccounts(db *gorm.DB) error {
// Begin a new transaction
tx := db.Begin()
if tx.Error != nil {
return tx.Error
}

// copy telegram_user_id to telegram_chat_id if telegram_chat_id is 0 in inpost_credentials
result := tx.Exec(`
UPDATE inpost_credentials
SET telegram_chat_id = telegram_user_id
WHERE telegram_chat_id = 0
`)
if result.Error != nil {
tx.Rollback()
return result.Error
}
log.Printf("Updated rows in inpost_credentials: %d", result.RowsAffected)

// copy telegram_user_id to chat_id if chat_id is 0 in followed_package_telegram_users
result = tx.Exec(`
UPDATE followed_package_telegram_users
SET chat_id = telegram_user_id
WHERE chat_id = 0
`)
if result.Error != nil {
tx.Rollback()
return result.Error
}
log.Printf("Updated rows in followed_package_telegram_users: %d", result.RowsAffected)

// Commit the transaction
if err := tx.Commit().Error; err != nil {
return err
}

return nil
}

0 comments on commit 7987ff5

Please sign in to comment.