~homeworkprod/byceps

ref: 4237b3ec9496efe95dcce82bea3207ab9de4d520 byceps/byceps/services/ticketing/event_service.py -rw-r--r-- 1.1 KiB
4237b3ec — Jochen Kupperschmidt Move ticketing blueprint into `site` subpackage 1 year, 11 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""
byceps.services.ticketing.event_service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2020 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

from datetime import datetime
from typing import List

from ...database import db

from .models.ticket_event import TicketEvent, TicketEventData
from .transfer.models import TicketID


def create_event(
    event_type: str, ticket_id: TicketID, data: TicketEventData
) -> None:
    """Create a ticket event."""
    event = build_event(event_type, ticket_id, data)

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


def build_event(
    event_type: str, ticket_id: TicketID, data: TicketEventData
) -> TicketEvent:
    """Assemble, but not persist, a ticket event."""
    now = datetime.utcnow()

    return TicketEvent(now, event_type, ticket_id, data)


def get_events_for_ticket(ticket_id: TicketID) -> List[TicketEvent]:
    """Return the events for that ticket."""
    return TicketEvent.query \
        .filter_by(ticket_id=ticket_id) \
        .order_by(TicketEvent.occurred_at) \
        .all()