
from datetime import datetime
from typing import Sequence

from ...database import db
from ...typing import UserID

from .models.event import UserEvent, UserEventData


def create_event(event_type: str, user_id: UserID, data: UserEventData) -> None:
    """Create a user event."""
    event = _build_event(event_type, user_id, data)

    db.session.add(event)
    db.session.commit()


def _build_event(event_type: str, user_id: UserID, data: UserEventData
                ) -> UserEvent:
    """Assemble, but not persist, a user event."""
    now = datetime.utcnow()

    return UserEvent(now, event_type, user_id, data)


def get_events_for_user(user_id: UserID) -> Sequence[UserEvent]:
    """Return the events for that user."""
    return UserEvent.query \
        .filter_by(user_id=user_id) \
        .order_by(UserEvent.occurred_at) \
        .all()


from datetime import datetime
from typing import Optional, Sequence

from ...database import db
from ...typing import UserID

from .models.event import UserEvent, UserEventData


def create_event(event_type: str, user_id: UserID, data: UserEventData) -> None:
    """Create a user event."""
    event = _build_event(event_type, user_id, data)

    db.session.add(event)
    db.session.commit()


def _build_event(event_type: str, user_id: UserID, data: UserEventData,
                 occurred_at: Optional[datetime]=None) -> UserEvent:
    """Assemble, but not persist, a user event."""
    if occurred_at is None:
        occurred_at = datetime.utcnow()

    return UserEvent(occurred_at, event_type, user_id, data)


def get_events_for_user(user_id: UserID) -> Sequence[UserEvent]:
    """Return the events for that user."""
    return UserEvent.query \
        .filter_by(user_id=user_id) \
        .order_by(UserEvent.occurred_at) \
        .all()

Allow to provide a custom `occurred_at` value when building a user event
Allow to provide a custom `occurred_at` value when building a user event

  
  from datetime import datetime
- from typing import Sequence
+ from typing import Optional, Sequence
  
  from ...database import db
  from ...typing import UserID
  
  from .models.event import UserEvent, UserEventData
  
  
  def create_event(event_type: str, user_id: UserID, data: UserEventData) -> None:
      """Create a user event."""
      event = _build_event(event_type, user_id, data)
  
      db.session.add(event)
      db.session.commit()
  
  
- def _build_event(event_type: str, user_id: UserID, data: UserEventData
+ def _build_event(event_type: str, user_id: UserID, data: UserEventData,
-                 ) -> UserEvent:
+                  occurred_at: Optional[datetime]=None) -> UserEvent:
      """Assemble, but not persist, a user event."""
+     if occurred_at is None:
-     now = datetime.utcnow()
+         occurred_at = datetime.utcnow()
  
-     return UserEvent(now, event_type, user_id, data)
+     return UserEvent(occurred_at, event_type, user_id, data)
  
  
  def get_events_for_user(user_id: UserID) -> Sequence[UserEvent]:
      """Return the events for that user."""
      return UserEvent.query \
          .filter_by(user_id=user_id) \
          .order_by(UserEvent.occurred_at) \
          .all()
  
Allow to provide a custom `occurred_at` value when building a user event
## Code Before:

from datetime import datetime
from typing import Sequence

from ...database import db
from ...typing import UserID

from .models.event import UserEvent, UserEventData


def create_event(event_type: str, user_id: UserID, data: UserEventData) -> None:
    """Create a user event."""
    event = _build_event(event_type, user_id, data)

    db.session.add(event)
    db.session.commit()


def _build_event(event_type: str, user_id: UserID, data: UserEventData
                ) -> UserEvent:
    """Assemble, but not persist, a user event."""
    now = datetime.utcnow()

    return UserEvent(now, event_type, user_id, data)


def get_events_for_user(user_id: UserID) -> Sequence[UserEvent]:
    """Return the events for that user."""
    return UserEvent.query \
        .filter_by(user_id=user_id) \
        .order_by(UserEvent.occurred_at) \
        .all()

## Instruction:
Allow to provide a custom `occurred_at` value when building a user event
## Code After:

from datetime import datetime
from typing import Optional, Sequence

from ...database import db
from ...typing import UserID

from .models.event import UserEvent, UserEventData


def create_event(event_type: str, user_id: UserID, data: UserEventData) -> None:
    """Create a user event."""
    event = _build_event(event_type, user_id, data)

    db.session.add(event)
    db.session.commit()


def _build_event(event_type: str, user_id: UserID, data: UserEventData,
                 occurred_at: Optional[datetime]=None) -> UserEvent:
    """Assemble, but not persist, a user event."""
    if occurred_at is None:
        occurred_at = datetime.utcnow()

    return UserEvent(occurred_at, event_type, user_id, data)


def get_events_for_user(user_id: UserID) -> Sequence[UserEvent]:
    """Return the events for that user."""
    return UserEvent.query \
        .filter_by(user_id=user_id) \
        .order_by(UserEvent.occurred_at) \
        .all()
