-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
123 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package paczkobot | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/alufers/paczkobot/commondata" | ||
) | ||
|
||
type ArchiveService struct { | ||
App *BotApp | ||
} | ||
|
||
func NewArchiveService(a *BotApp) *ArchiveService { | ||
return &ArchiveService{ | ||
App: a, | ||
} | ||
} | ||
|
||
func (s *ArchiveService) FetchAndArchivePackagesForUser(telegramUserID int64) error { | ||
followedPackages := []FollowedPackageTelegramUser{} | ||
|
||
if err := s.App.DB.Where("telegram_user_id = ? AND archived = ?", telegramUserID, false). | ||
Preload("FollowedPackage"). | ||
Preload("FollowedPackage.FollowedPackageProviders"). | ||
Find(&followedPackages).Error; err != nil { | ||
return fmt.Errorf("failed to query DB: %w", err) | ||
} | ||
return s.ArchivePackagesIfNeeded(followedPackages) | ||
} | ||
|
||
//ArchivePackagesIfNeeded checks whether the passed packages meet the criteria to be archived | ||
//FollowedPackage and FollowedPackage.FollowedPackageProviders must be preloaded | ||
func (s *ArchiveService) ArchivePackagesIfNeeded(packages []FollowedPackageTelegramUser) error { | ||
for _, p := range packages { | ||
if s.shouldArchivePackage(p) { | ||
if err := s.App.DB.Model(&p).Update("archived", true).Error; err != nil { | ||
return fmt.Errorf("failed to update package: %w", err) | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (s *ArchiveService) shouldArchivePackage(p FollowedPackageTelegramUser) bool { | ||
if p.Archived { | ||
return false | ||
} | ||
if p.FollowedPackage.Inactive { | ||
return true | ||
} | ||
for _, provider := range p.FollowedPackage.FollowedPackageProviders { | ||
for _, s := range commondata.CommonTrackingStepsToArchive { | ||
if s == provider.LastStatusCommonType { | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package paczkobot | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" | ||
) | ||
|
||
type ArchivedCommand struct { | ||
App *BotApp | ||
} | ||
|
||
func (s *ArchivedCommand) Aliases() []string { | ||
return []string{"/archived"} | ||
} | ||
|
||
func (s *ArchivedCommand) Arguments() []*CommandDefArgument { | ||
return []*CommandDefArgument{} | ||
} | ||
|
||
func (s *ArchivedCommand) Help() string { | ||
return "prints your archived packages" | ||
} | ||
|
||
func (s *ArchivedCommand) Execute(ctx context.Context, args *CommandArguments) error { | ||
|
||
if err := s.App.ArchiveService.FetchAndArchivePackagesForUser(args.FromUserID); err != nil { | ||
log.Printf("failed to fetch and archive packages for user %v: %v", args.FromUserID, err) | ||
} | ||
|
||
followedPackages := []FollowedPackageTelegramUser{} | ||
|
||
if err := s.App.DB.Where("telegram_user_id = ? AND archived = ?", args.FromUserID, true). | ||
Preload("FollowedPackage"). | ||
Preload("FollowedPackage.FollowedPackageProviders"). | ||
Find(&followedPackages).Error; err != nil { | ||
return fmt.Errorf("failed to query DB: %w", err) | ||
} | ||
|
||
msg := tgbotapi.NewMessage(args.ChatID, fmt.Sprintf(` | ||
Your archived packages: | ||
%v | ||
`, s.App.PackagePrinterService.PrintPackages(followedPackages))) | ||
msg.ParseMode = "HTML" | ||
_, err := s.App.Bot.Send(msg) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters