Skip to content

Commit

Permalink
fixup duplicate inpost credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
alufers committed Nov 1, 2022
1 parent 61949ae commit cb6b5df
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions paczkobot/inpost_login_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package paczkobot
import (
"context"
"fmt"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)

type InpostLoginCommand struct {
Expand Down Expand Up @@ -39,11 +41,17 @@ func (f *InpostLoginCommand) Execute(ctx context.Context, args *CommandArguments
if err != nil {
return err
}

creds, err := f.App.InpostService.ConfirmSMSCode(phoneNumber, code)
if err != nil {
return fmt.Errorf("failed to confirm sms code: %v", err)
}

creds.TelegramUserID = 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)
}
if err := f.App.DB.Save(creds).Error; err != nil {
return fmt.Errorf("failed to save credentials: %v", err)
}
Expand All @@ -53,5 +61,9 @@ func (f *InpostLoginCommand) Execute(ctx context.Context, args *CommandArguments
if err != nil {
return fmt.Errorf("failed to scan user inpost packages: %v", err)
}
msg := tgbotapi.NewMessage(args.ChatID, fmt.Sprintf("Successfully logged in to Inpost with phone number %s", phoneNumber))
if _, err := f.App.Bot.Send(msg); err != nil {
return fmt.Errorf("failed to send message: %v", err)
}
return nil
}

0 comments on commit cb6b5df

Please sign in to comment.