-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcustomer_button.py
113 lines (94 loc) · 3.06 KB
/
customer_button.py
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
from typing import Dict, List, Optional, Any
from ..business_objects import general
from ..session import session
from ..models import CustomerButton
from ..util import prevent_sql_injection
from .. import enums
from sqlalchemy.orm.attributes import flag_modified
def get(id: str) -> CustomerButton:
return (
session.query(CustomerButton)
.filter(
CustomerButton.id == id,
)
.first()
)
def get_all(filter_visible: Optional[bool] = None) -> List[CustomerButton]:
where_add = ""
if filter_visible is not None:
filter_visible = prevent_sql_injection(
filter_visible, isinstance(filter_visible, bool)
)
where_add = f"WHERE cb.visible = {filter_visible}"
query = f"""
SELECT cb.*, o.name
FROM global.customer_button cb
INNER JOIN public.organization o
ON cb.organization_id = o.id
{where_add}
"""
return general.execute_all(query)
def get_by_org_id(
org_id: str,
filter_visible: Optional[bool] = None,
filter_location: Optional[enums.CustomerButtonLocation] = None,
) -> List[CustomerButton]:
# org name only relevant for admin dashboard, this is for org specific so no need to join
query = session.query(CustomerButton).filter(
CustomerButton.organization_id == org_id,
)
if filter_visible is not None:
query = query.filter(CustomerButton.visible == filter_visible)
if filter_location is not None:
query = query.filter(CustomerButton.location == filter_location.value)
return query.all()
def create(
org_id: str,
type: enums.CustomerButtonType,
location: enums.CustomerButtonLocation,
config: Dict[str, Any],
user_id: str,
visible: bool = False,
with_commit: bool = True,
) -> CustomerButton:
obj = CustomerButton(
organization_id=org_id,
type=type.value,
location=location.value,
config=config,
visible=visible,
created_by=user_id,
)
general.add(obj, with_commit)
return obj
def update(
id: str,
org_id: str,
type: Optional[enums.CustomerButtonType] = None,
location: Optional[enums.CustomerButtonLocation] = None,
config: Optional[Dict[str, Any]] = None,
user_id: Optional[str] = None,
visible: Optional[bool] = None,
with_commit: bool = True,
) -> CustomerButton:
button = get(id)
if not button or str(org_id) != str(button.organization_id):
raise ValueError("Button not found or not part of the organization")
if type:
button.type = type.value
if location:
button.location = location.value
if config:
button.config = config
flag_modified(button, "config")
if user_id:
button.created_by = user_id
if visible is not None:
button.visible = visible
general.flush_or_commit(with_commit)
return button
def delete(id: str, with_commit: bool = True) -> None:
session.query(CustomerButton).filter(
CustomerButton.id == id,
).delete()
general.flush_or_commit(with_commit)