Skip to content

Commit

Permalink
Refactor patron aggregate into separate modules
Browse files Browse the repository at this point in the history
  • Loading branch information
subhashb committed Jul 22, 2024
1 parent 56f9393 commit fbc628d
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 254 deletions.
254 changes: 0 additions & 254 deletions src/lending/model/patron.py

This file was deleted.

25 changes: 25 additions & 0 deletions src/lending/model/patron/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from .checkout import (
BookCheckedOut,
BookOverdue,
BookReturned,
Checkout,
CheckoutStatus,
)
from .hold import Hold, HoldCancelled, HoldExpired, HoldPlaced, HoldStatus, HoldType
from .patron import Patron, PatronType

__all__ = [
Patron,
PatronType,
Hold,
HoldStatus,
HoldType,
HoldExpired,
HoldPlaced,
HoldCancelled,
Checkout,
CheckoutStatus,
BookCheckedOut,
BookReturned,
BookOverdue,
]
90 changes: 90 additions & 0 deletions src/lending/model/patron/checkout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
from datetime import date, datetime, timedelta
from enum import Enum

from protean.fields import Date, DateTime, Identifier, String

from lending.domain import lending
from lending.utils import utc_now


class CheckoutStatus(Enum):
ACTIVE = "ACTIVE"
RETURNED = "RETURNED"
OVERDUE = "OVERDUE"


@lending.event(part_of="Patron", abstract=True)
class CheckoutEvent:
checkout_id = Identifier(required=True)
patron_id = Identifier(required=True)
patron_type = String(required=True)
book_id = Identifier(required=True)
branch_id = Identifier(required=True)
checked_out_at = DateTime(required=True)
due_on = Date(required=True)


@lending.event(part_of="Patron")
class BookCheckedOut(CheckoutEvent):
"""Event raised when a patron checks out a book"""


@lending.event(part_of="Patron")
class BookReturned(CheckoutEvent):
"""Event raised when a patron returns a book"""

returned_at = DateTime(required=True)


@lending.event(part_of="Patron")
class BookOverdue(CheckoutEvent):
"""Event raised when a book is marked overdue"""


@lending.entity(part_of="Patron")
class Checkout:
"""The action of a patron borrowing a book from the library
for a period of up to 60 days."""

book_id = Identifier(required=True)
branch_id = Identifier(required=True)
checked_out_at = DateTime(required=True, default=utc_now)
status = String(max_length=11, default=CheckoutStatus.ACTIVE.value)
due_on = Date(
required=True,
default=lambda: date.today()
+ timedelta(days=lending.config["custom"]["CHECKOUT_PERIOD"]),
)
returned_at = DateTime()

def return_(self):
self.status = CheckoutStatus.RETURNED.value
self.returned_at = datetime.now()

self.raise_(
BookReturned(
checkout_id=self.id,
patron_id=self._owner.id,
patron_type=self._owner.patron_type,
book_id=self.book_id,
branch_id=self.branch_id,
checked_out_at=self.checked_out_at,
due_on=self.due_on,
returned_at=self.returned_at,
)
)

def overdue(self):
self.status = CheckoutStatus.OVERDUE.value

self.raise_(
BookOverdue(
checkout_id=self.id,
patron_id=self._owner.id,
patron_type=self._owner.patron_type,
book_id=self.book_id,
branch_id=self.branch_id,
checked_out_at=self.checked_out_at,
due_on=self.due_on,
)
)
Loading

0 comments on commit fbc628d

Please sign in to comment.