Skip to content

Commit

Permalink
feat(events): add event's discount system
Browse files Browse the repository at this point in the history
  • Loading branch information
nmluci committed Dec 15, 2021
1 parent 54ff1f7 commit 1d1b13d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 13 deletions.
20 changes: 18 additions & 2 deletions app/order/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from flask.helpers import make_response
from datetime import datetime
from app.baseModel import SuccessResponse, FailedResponse
from app.order.models import Order, UserOrder
from app.order.services import generateTodaySpecialty, generateTodayEvents, processCheckout
from app.order.models import EventData, Order, UserOrder
from app.order.services import generateTodaySpecialty, generateTodayEvents, insertEvents, processCheckout
from app.userdata.models import User, UserData

order_bp = Blueprint("order_bp", __name__)
Expand Down Expand Up @@ -50,6 +50,22 @@ def todayEvents():
except Exception as e:
return make_response(FailedResponse(errorMessage=str(e)).toDict(), 500)

@order_bp.route("/api/order/events", methods=["POST"])
def addEventCoupon():
try:
res = request.get_json()
event = EventData(
name=res.get("name"),
coupon=res.get("code"),
date_start=datetime.strptime(res.get("date_start"), "%Y-%m-%d"),
date_end=datetime.strptime(res.get("date_end"), "%Y-%m-%d"),
amount=res.get("amount")
)
insertEvents(event)
return make_response(SuccessResponse(data=[event]).toDict())
except Exception as e:
return make_response(FailedResponse(errorMessage=str(e)).toDict(), 500)

@order_bp.route("/api/order/<uid>/checkout", methods=["POST"])
def checkout(uid):
try:
Expand Down
31 changes: 26 additions & 5 deletions app/order/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,16 @@ class Events(db.Model):
__tablename__ = "Events"

id = db.Column(db.Integer, primary_key=True, unique=True)
name = db.Column(db.Text, unique=True)
item_name = db.Column(db.Text, db.ForeignKey("Inventory.name", ondelete="SET NULL", onupdate="CASCADE"))
name = db.Column(db.Text)
coupon_code = db.Column(db.Text, unique=True)
date_start = db.Column(db.Date, nullable=False)
date_end = db.Column(db.Date, nullable=False)
amount = db.Column(db.Integer, nullable=False)
amount_type = db.Column(db.Text, nullable=False)

def insert(self):
db.session.add(self)
db.session.commit()

return self.id

def delete(self):
db.session.delete(self)
Expand All @@ -71,6 +70,25 @@ def delete(self):
def update(self):
db.session.commit()

@dataclass
class EventData:
name: str
coupon: str
date_start: datetime
date_end: datetime
amount: int
id: int = None

def toDict(cls):
return {
"id": cls.id,
"name": cls.name,
"coupon_code": cls.coupon,
"date_start": datetime.strftime(cls.date_start,"%Y-%m-%d"),
"date_end": datetime.strftime(cls.date_end, "%Y-%m-%d"),
"amount": cls.amount
}

@dataclass
class Order:
uid: int
Expand All @@ -92,6 +110,7 @@ def toDict(cls):
res["price"] = cls.price
res["total_price"] = cls.total_price
return res

@dataclass
class UserOrder:
uid: int
Expand Down Expand Up @@ -136,11 +155,13 @@ def toDict(cls):
class TodayEventData:
event_id: int
name: str
coupon_code: str
disc_amount: int

def toDict(cls):
return {
"event_id": cls.event_ide,
"event_id": cls.event_id,
"coupon_code": cls.coupon_code,
"title": cls.name,
"disc_amount": cls.disc_amount
}
31 changes: 25 additions & 6 deletions app/order/services.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from __future__ import annotations
from os import name
from typing import List

from datetime import datetime

from sqlalchemy.sql.operators import notbetween_op
from app.inventory.models import Inventory

from app.order.models import Events, OrderItems, Orders, TodayEventData, TodaySpecialData, UserOrder, Order
from app.order.models import EventData, Events, OrderItems, Orders, TodayEventData, TodaySpecialData, UserOrder, Order
from app.userdata.models import User
from app.baseModel import db

Expand All @@ -22,13 +23,14 @@ def generateTodaySpecialty() -> List[TodaySpecialData]:

def generateTodayEvents() -> List[TodayEventData]:
todayDate = datetime.now().date()
todayEvent = db.session.query(Events).filter((Events.date_start >= todayDate) & (Events.date_end <= todayDate)).all()
todayEvent = db.session.query(Events).filter((Events.date_start <= todayDate) & (Events.date_end >= todayDate)).all()
if not todayEvent:
raise Exception("no events available today")
return list(TodayEventData(
event.id,
event.name,
f"IDR {event.amount}" if event.amount_type == "cash" else f"{event.amount}%").toDict() for event in todayEvent)
event_id=event.id,
name=event.name,
coupon_code=event.coupon_code,
disc_amount=f"IDR {event.amount}").toDict() for event in todayEvent)

def isItemExists(metadata: Order):
itemList = db.session.query(Inventory).all()
Expand Down Expand Up @@ -81,4 +83,21 @@ def processCheckout(metadata: UserOrder):

usr.point += grandTotal/1000
usr.update()
metadata.done = True
metadata.done = True

def insertEvents(metadata: EventData):
events = db.session.query(Events).filter(Events.coupon_code==metadata.coupon).all()
if events:
print(list(x.coupon_code for x in events))
raise Exception("event already listed")

newEvent = Events(
name=metadata.name,
amount=metadata.amount,
coupon_code=metadata.coupon,
date_start=metadata.date_start,
date_end=metadata.date_end,
)
newEvent.insert()

metadata.id = newEvent.id

0 comments on commit 1d1b13d

Please sign in to comment.