diff --git a/.gitignore b/.gitignore index 9bd1d68..18e538a 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,4 @@ deobfuscated paczkobot.db .DS_Store +*.db diff --git a/paczkobot/bot_app.go b/paczkobot/bot_app.go index eed0b5a..fe45d76 100644 --- a/paczkobot/bot_app.go +++ b/paczkobot/bot_app.go @@ -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) diff --git a/paczkobot/inpost_login_cmd.go b/paczkobot/inpost_login_cmd.go index 362a286..db7921c 100644 --- a/paczkobot/inpost_login_cmd.go +++ b/paczkobot/inpost_login_cmd.go @@ -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) diff --git a/paczkobot/migrate_bad_inpost_accounts.go b/paczkobot/migrate_bad_inpost_accounts.go new file mode 100644 index 0000000..c8b6131 --- /dev/null +++ b/paczkobot/migrate_bad_inpost_accounts.go @@ -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 +}