~homeworkprod/byceps

ref: 4237b3ec9496efe95dcce82bea3207ab9de4d520 byceps/byceps/services/ticketing/ticket_user_management_service.py -rw-r--r-- 3.2 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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"""
byceps.services.ticketing.ticket_user_management_service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

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

from ..user import service as user_service

from . import event_service
from .exceptions import (
    TicketIsRevoked,
    UserAccountSuspended,
    UserAlreadyCheckedIn,
    UserIdUnknown,
)
from . import ticket_service
from .transfer.models import TicketID


def appoint_user_manager(
    ticket_id: TicketID, manager_id: UserID, initiator_id: UserID
) -> None:
    """Appoint the user as the ticket's user manager."""
    ticket = ticket_service.find_ticket(ticket_id)

    if ticket.revoked:
        raise TicketIsRevoked(f'Ticket {ticket_id} has been revoked.')

    ticket.user_managed_by_id = manager_id

    event = event_service.build_event(
        'user-manager-appointed',
        ticket.id,
        {
            'appointed_user_manager_id': str(manager_id),
            'initiator_id': str(initiator_id),
        },
    )
    db.session.add(event)

    db.session.commit()


def withdraw_user_manager(ticket_id: TicketID, initiator_id: UserID) -> None:
    """Withdraw the ticket's custom user manager."""
    ticket = ticket_service.find_ticket(ticket_id)

    if ticket.revoked:
        raise TicketIsRevoked(f'Ticket {ticket_id} has been revoked.')

    ticket.user_managed_by_id = None

    event = event_service.build_event(
        'user-manager-withdrawn',
        ticket.id,
        {
            'initiator_id': str(initiator_id),
        },
    )
    db.session.add(event)

    db.session.commit()


def appoint_user(
    ticket_id: TicketID, user_id: UserID, initiator_id: UserID
) -> None:
    """Appoint the user as the ticket's user."""
    ticket = ticket_service.find_ticket(ticket_id)

    if ticket.revoked:
        raise TicketIsRevoked(f'Ticket {ticket_id} has been revoked.')

    if ticket.user_checked_in:
        raise UserAlreadyCheckedIn('Ticket user has already been checked in.')

    user = user_service.find_user(user_id)
    if user is None:
        raise UserIdUnknown(f"Unknown user ID '{user_id}'")

    if user.suspended:
        raise UserAccountSuspended(
            f'User account {user.screen_name} is suspended.'
        )

    ticket.used_by_id = user_id

    event = event_service.build_event(
        'user-appointed',
        ticket.id,
        {
            'appointed_user_id': str(user_id),
            'initiator_id': str(initiator_id),
        },
    )
    db.session.add(event)

    db.session.commit()


def withdraw_user(ticket_id: TicketID, initiator_id: UserID) -> None:
    """Withdraw the ticket's user."""
    ticket = ticket_service.find_ticket(ticket_id)

    if ticket.revoked:
        raise TicketIsRevoked(f'Ticket {ticket_id} has been revoked.')

    if ticket.user_checked_in:
        raise UserAlreadyCheckedIn('Ticket user has already been checked in.')

    ticket.used_by_id = None

    event = event_service.build_event(
        'user-withdrawn',
        ticket.id,
        {
            'initiator_id': str(initiator_id),
        },
    )
    db.session.add(event)

    db.session.commit()