refactor(commands): use core services instead of storage module #33

Merged
shoko merged 2 commits from fix/issue-13 into main 2026-04-03 16:14:24 +02:00
Owner

Summary

Refactor commands.py to be thin Telegram wrappers around core services.

Changes

  • Replace import storage with imports from core.services and adapters.storage
  • Create module-level service instances (BountyService, TrackingService)
  • Update format_bounty() to work with Bounty dataclass instead of dict
  • Add get_room_id() helper for unified group/DM handling

Commands Updated

Command Service Method
/bounty BountyService.list_bounties()
/my BountyService.list_bounties() or TrackingService.get_tracked_bounties()
/add BountyService.add_bounty()
/update BountyService.update_bounty()
/delete BountyService.delete_bounty()
/track TrackingService.track_bounty() (groups only)
/untrack TrackingService.untrack_bounty() (groups only)

Kept from Original

  • parse_args()
  • format_bounty()
  • extract_args()

Acceptance Criteria

  • All commands work as before
  • commands.py imports and uses core services
  • No business logic in commands.py

Fixes #13

## Summary Refactor commands.py to be thin Telegram wrappers around core services. ## Changes - Replace `import storage` with imports from `core.services` and `adapters.storage` - Create module-level service instances (BountyService, TrackingService) - Update `format_bounty()` to work with Bounty dataclass instead of dict - Add `get_room_id()` helper for unified group/DM handling ## Commands Updated | Command | Service Method | |---------|---------------| | `/bounty` | `BountyService.list_bounties()` | | `/my` | `BountyService.list_bounties()` or `TrackingService.get_tracked_bounties()` | | `/add` | `BountyService.add_bounty()` | | `/update` | `BountyService.update_bounty()` | | `/delete` | `BountyService.delete_bounty()` | | `/track` | `TrackingService.track_bounty()` (groups only) | | `/untrack` | `TrackingService.untrack_bounty()` (groups only) | ## Kept from Original - `parse_args()` - `format_bounty()` - `extract_args()` ## Acceptance Criteria - ✅ All commands work as before - ✅ commands.py imports and uses core services - ✅ No business logic in commands.py Fixes #13
shoko added 1 commit 2026-04-03 14:39:40 +02:00
Refactor commands.py to be thin Telegram wrappers around core services.

Changes:
- Replace 'import storage' with imports from core.services and adapters.storage
- Create module-level service instances (BountyService, TrackingService)
- Update format_bounty() to work with Bounty dataclass instead of dict
- Add get_room_id() helper for unified group/DM handling
- Each command handler is now a thin wrapper that:
  1. Extracts Telegram types (update, user_id, room_id)
  2. Calls appropriate core service
  3. Formats and sends response

Kept from original:
- parse_args()
- format_bounty()
- extract_args()

Commands now use services:
- cmd_bounty: BOUNTY_SERVICE.list_bounties()
- cmd_my: BOUNTY_SERVICE.list_bounties() or TRACKING_SERVICE.get_tracked_bounties()
- cmd_add: BOUNTY_SERVICE.add_bounty()
- cmd_update: BOUNTY_SERVICE.update_bounty()
- cmd_delete: BOUNTY_SERVICE.delete_bounty()
- cmd_track: TRACKING_SERVICE.track_bounty() (groups only)
- cmd_untrack: TRACKING_SERVICE.untrack_bounty() (groups only)

Fixes #13
Collaborator

is it possible for commands.py to have unit tests? so we can be sure that all these commands are exists and calling the right function (by mocking), handling the right params, and returning the right output

is it possible for commands.py to have unit tests? so we can be sure that all these commands are exists and calling the right function (by mocking), handling the right params, and returning the right output
shoko added 1 commit 2026-04-03 15:11:32 +02:00
Add comprehensive unit tests for all command handlers:
- TestHelperFunctions: is_group, get_group_id, get_user_id, get_room_id
- TestCmdBounty: lists bounties, handles empty
- TestCmdMy: shows tracked in groups, personal in DM
- TestCmdAdd: add bounty success, validation
- TestCmdUpdate: update bounty, permission denied, invalid ID
- TestCmdDelete: delete bounty, invalid ID
- TestCmdTrack: track in group, reject in DM
- TestCmdUntrack: untrack in group, reject in DM
- TestCmdStart: group vs DM behavior
- TestCmdHelp: shows all commands

Also fix conftest.py to remove obsolete fresh_db fixture
that referenced non-existent db module.

All 55 tests pass.

Addresses han's feedback on PR #33
han approved these changes 2026-04-03 16:13:07 +02:00
han left a comment
Collaborator

lgtm

lgtm
shoko merged commit f5cb28d45c into main 2026-04-03 16:14:24 +02:00
Sign in to join this conversation.
No Reviewers
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: shoko/jigaido#33