~homeworkprod/byceps

ref: bcef32cd2dc329bcf81057794883008f5c040ac8 byceps/byceps/services/shop/order/actions/create_ticket_bundles.py -rw-r--r-- 2.2 KiB
bcef32cd — Jochen Kupperschmidt Work around Jinja 3.0.0 bug with `for` inside of `set` block 1 year, 1 month 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
byceps.services.shop.order.actions.create_ticket_bundles
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2021 Jochen Kupperschmidt
:License: Revised BSD (see `LICENSE` file for details)
"""

from .....typing import UserID

from ....ticketing.dbmodels.ticket_bundle import TicketBundle
from ....ticketing import (
    category_service as ticket_category_service,
    ticket_bundle_service,
)

from ...article.transfer.models import ArticleNumber

from .. import event_service
from ..dbmodels.order_action import Parameters
from ..transfer.models import Order, OrderID

from ._ticketing import create_tickets_sold_event, send_tickets_sold_event


def create_ticket_bundles(
    order: Order,
    article_number: ArticleNumber,
    bundle_quantity: int,
    initiator_id: UserID,
    parameters: Parameters,
) -> None:
    """Create ticket bundles."""
    category_id = parameters['category_id']
    ticket_quantity = parameters['ticket_quantity']
    owned_by_id = order.placed_by_id
    order_number = order.order_number

    category = ticket_category_service.find_category(category_id)
    if category is None:
        raise ValueError(f'Unknown ticket category ID for order {order_number}')

    for _ in range(bundle_quantity):
        bundle = ticket_bundle_service.create_bundle(
            category.party_id,
            category.id,
            ticket_quantity,
            owned_by_id,
            order_number=order_number,
            used_by_id=owned_by_id,
        )

        _create_order_event(order.id, bundle)

    tickets_sold_event = create_tickets_sold_event(
        order.id, initiator_id, category_id, owned_by_id, ticket_quantity
    )
    send_tickets_sold_event(tickets_sold_event)


def _create_order_event(order_id: OrderID, ticket_bundle: TicketBundle) -> None:
    event_type = 'ticket-bundle-created'

    data = {
        'ticket_bundle_id': str(ticket_bundle.id),
        'ticket_bundle_category_id': str(ticket_bundle.ticket_category_id),
        'ticket_bundle_ticket_quantity': ticket_bundle.ticket_quantity,
        'ticket_bundle_owner_id': str(ticket_bundle.owned_by_id),
    }

    event_service.create_event(event_type, order_id, data)