Skip to content

Commit e6837b1

Browse files
committed
feat: enhance Voucher API with new use and disuse methods
Update key parameter naming, and add observations to voucher fields
1 parent 4cb39d9 commit e6837b1

4 files changed

Lines changed: 160 additions & 7 deletions

File tree

src/budy/controllers/api/voucher.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ def create(self):
5151
voucher = voucher.map()
5252
return voucher
5353

54-
@appier.route("/api/vouchers/<int:id>", "GET", json=True)
54+
@appier.route("/api/vouchers/<int:key>", "GET", json=True)
5555
@appier.ensure(token="admin")
56-
def show(self, id):
57-
voucher = budy.Voucher.get_e(id=id, map=True)
56+
def show(self, key):
57+
voucher = budy.Voucher.get_e(key=key, map=True)
5858
return voucher
5959

6060
@appier.route("/api/vouchers/value", "POST", json=True)
@@ -64,7 +64,7 @@ def create_value(self):
6464
key = object.get("key", None)
6565
amount = object.get("amount", None)
6666
currency = object.get("currency", None)
67-
unlimited = object.get("unlimited", None)
67+
unlimited = object.get("unlimited", False)
6868
key = self.field("key", key)
6969
amount = self.field("amount", amount, cast=float)
7070
currency = self.field("currency", currency, cast=str)
@@ -84,3 +84,28 @@ def create_percentage(self):
8484
voucher = budy.Voucher.create_percentage_s(key, percentage)
8585
voucher = voucher.map()
8686
return voucher
87+
88+
@appier.route("/api/vouchers/<str:key>/use", "POST", json=True)
89+
@appier.ensure(token="admin")
90+
def use(self, key):
91+
object = appier.get_object()
92+
amount = object.get("amount", None)
93+
currency = object.get("currency", None)
94+
justification = object.get("justification", None)
95+
save_use = object.get("save_use", True)
96+
voucher = budy.Voucher.get_e(key=key)
97+
voucher_use = voucher.use_s(
98+
amount, currency=currency, justification=justification, save_use=save_use
99+
)
100+
voucher = voucher.map()
101+
if voucher_use:
102+
voucher["use"] = voucher_use.map()
103+
return voucher
104+
105+
@appier.route("/api/vouchers/<str:key>/disuse", "POST", json=True)
106+
@appier.ensure(token="admin")
107+
def disuse(self, key):
108+
voucher = budy.Voucher.get_e(key=key)
109+
voucher.disuse_s()
110+
voucher = voucher.map()
111+
return voucher

src/budy/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,6 @@
8484
from .store import Store
8585
from .subscription import Subscription
8686
from .voucher import Voucher
87+
from .voucher_use import VoucherUse
8788
from .wishlist_line import WishlistLine
8889
from .wishlist import Wishlist

src/budy/models/voucher.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,41 @@
3535
import appier_extras
3636

3737
from . import base
38+
from . import voucher_use
3839

3940

4041
class Voucher(base.BudyBase):
41-
key = appier.field(index=True, safe=True, immutable=True)
42+
key = appier.field(
43+
index=True,
44+
safe=True,
45+
immutable=True,
46+
observations="""The (secret) key of the voucher, considered to be
47+
the unique identifier of the voucher, this is the key that will
48+
be used to identify the voucher when it is used""",
49+
)
4250

4351
amount = appier.field(
4452
type=commons.Decimal,
4553
index=True,
4654
initial=commons.Decimal(0.0),
4755
safe=True,
4856
immutable=True,
57+
observations="""The amount of the voucher, meaning the total
58+
amount of the voucher that can be used, this is the amount
59+
that will be deducted from the order total when the voucher
60+
is used, if this value is set to zero the voucher is considered
61+
to be a percentage based voucher""",
4962
)
5063

5164
used_amount = appier.field(
52-
type=commons.Decimal, initial=commons.Decimal(0.0), index=True, safe=True
65+
type=commons.Decimal,
66+
initial=commons.Decimal(0.0),
67+
index=True,
68+
safe=True,
69+
observations="""The amount of the voucher that has been used,
70+
meaning that the remaining amount of the voucher that can be
71+
used can be calculated by subtracting the used amount from the
72+
amount""",
5373
)
5474

5575
percentage = appier.field(
@@ -58,6 +78,8 @@ class Voucher(base.BudyBase):
5878
index=True,
5979
safe=True,
6080
immutable=True,
81+
observations="""The percentage of discount to be applied when
82+
using the voucher, meaning the voucher is percentage based""",
6183
)
6284

6385
currency = appier.field(
@@ -245,13 +267,22 @@ def pre_update(self):
245267
if self.used and not self.is_used():
246268
self.used = False
247269

248-
def use_s(self, amount, currency=None):
270+
def use_s(self, amount, currency=None, justification=None, save_use=True):
249271
amount_l = self.to_local(amount, currency)
250272
appier.verify(self.is_valid(amount=amount, currency=currency))
251273
if self.is_value and not self.unlimited:
252274
self.used_amount += commons.Decimal(amount_l)
253275
self.usage_count += 1
254276
self.save()
277+
if save_use:
278+
voucher_use_ = voucher_use.VoucherUse(
279+
voucher=self,
280+
amount=amount,
281+
currency=currency,
282+
justification=justification,
283+
)
284+
voucher_use_.save()
285+
return voucher_use_
255286

256287
def disuse_s(self, amount, currency=None):
257288
appier.verify(self.usage_count > 0)

src/budy/models/voucher_use.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
# Hive Budy
5+
# Copyright (c) 2008-2024 Hive Solutions Lda.
6+
#
7+
# This file is part of Hive Budy.
8+
#
9+
# Hive Budy is free software: you can redistribute it and/or modify
10+
# it under the terms of the Apache License as published by the Apache
11+
# Foundation, either version 2.0 of the License, or (at your option) any
12+
# later version.
13+
#
14+
# Hive Budy is distributed in the hope that it will be useful,
15+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
# Apache License for more details.
18+
#
19+
# You should have received a copy of the Apache License along with
20+
# Hive Budy. If not, see <http://www.apache.org/licenses/>.
21+
22+
__author__ = "João Magalhães <joamag@hive.pt>"
23+
""" The author(s) of the module """
24+
25+
__copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26+
""" The copyright for the module """
27+
28+
__license__ = "Apache License, Version 2.0"
29+
""" The license for the module """
30+
31+
import appier
32+
33+
from . import base
34+
35+
36+
class VoucherUse(base.BudyBase):
37+
38+
USAGE_TYPE_S = dict(
39+
percentage="percentage",
40+
value="value",
41+
)
42+
43+
usage_type = appier.field(
44+
type=str,
45+
safe=True,
46+
meta="enum",
47+
enum=USAGE_TYPE_S,
48+
observations="""Type of usage, either value or percentage""",
49+
)
50+
51+
amount = appier.field(
52+
type=float,
53+
safe=True,
54+
observations="""The amount used from the voucher""",
55+
)
56+
57+
justification = appier.field(
58+
type=str,
59+
safe=True,
60+
observations="""Justification or reason for the usage of the voucher,m
61+
may contain external ID references""",
62+
)
63+
64+
voucher = appier.field(
65+
type=appier.reference("Voucher", name="id"),
66+
safe=True,
67+
observations="""Reference to the voucher instance that was used.""",
68+
)
69+
70+
account = appier.field(
71+
type=appier.reference("BudyAccount", name="id"),
72+
safe=True,
73+
observations="""Reference to the account that used the voucher (nullable).""",
74+
)
75+
76+
@classmethod
77+
def validate(cls):
78+
return super(VoucherUse, cls).validate() + [
79+
appier.not_null("voucher"),
80+
]
81+
82+
@classmethod
83+
def list_names(cls):
84+
return [
85+
"id",
86+
"voucher",
87+
"account",
88+
"amount",
89+
"usage_type",
90+
"justification",
91+
"created",
92+
]
93+
94+
@classmethod
95+
def order_name(cls):
96+
return ["id", -1]

0 commit comments

Comments
 (0)