Verified Commit f145ec60 authored by Sli's avatar Sli
Browse files

server: implements history endpoint

parent 23755415
......@@ -233,13 +233,19 @@ message HistoryRequest {
BUYINGS = 0;
REFILLINGS = 1;
}
enum RefoundStatus {
NOT_SPECIFIED = 0;
NOT_REFOUNDED = 1;
REFOUNDED = 2;
}
HistoryType type = 1; // Only one required
// Optional fields
uint64 counter = 2;
uint64 counter_id = 2;
string customer_id = 3;
string device_uuid = 4;
uint64 max_history_size = 5;
uint64 max_history_size = 5; // 0 means no limit
RefoundStatus refounded = 6; // To know if refounded or not
}
message HistoryReply {
......
......@@ -166,6 +166,21 @@ class Refilling(Model):
date = Column(DateTime(timezone=True), default=datetime.utcnow)
cancelled = Column(Boolean, default=False)
def __str__(self):
return (
"id: %d, cancelled: %d, customer: %s, payment_method: %s, counter: %s, machine: %s, amount: %s, date: %s"
% (
self.id,
self.cancelled,
self.customer_id,
self.payment_method,
self.counter.name,
self.machine_id,
self.amount,
self.date,
)
)
class Buying(Model):
"""
......@@ -194,6 +209,20 @@ class Buying(Model):
]
)
def __str__(self):
return (
"id: %d, refounded: %d, counter: %s, machine: %s, label: %s, payments: %s, date: %s"
% (
self.id,
self.refounded,
self.counter.name,
self.machine_id,
self.label,
[str(payment) for payment in self.payments],
self.date,
)
)
class BasketItem(Model):
"""
......@@ -234,3 +263,10 @@ class Payment(Model):
buying = relationship("Buying", backref=backref("payments", lazy=True), lazy=True)
amount = Column(Money) # Amount spent by this user in the Buying
def __str__(self):
return "id: %d, customer: %s, amount: %s" % (
self.id,
self.customer_id,
self.amount,
)
......@@ -408,10 +408,105 @@ class PaymentServicer(com_pb2_grpc.PaymentProtocolServicer):
)
def History(self, request, context):
"""Missing associated documentation comment in .proto file"""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details("Method not implemented!")
raise NotImplementedError("Method not implemented!")
"""
Retrieve history
"""
# Prepare default values
counter = db.query(models.Counter).get(request.counter_id)
customer = None
device = None
history_size = request.max_history_size # Default is 0
refounded = None
if request.customer_id:
customer = get_or_create_customer(request.customer_id)
if request.device_uuid:
device = get_or_create_machine(request.device_uuid)
if request.refounded == com_pb2.HistoryRequest.NOT_REFOUNDED:
refounded = False
if request.refounded == com_pb2.HistoryRequest.REFOUNDED:
refounded = True
m = None
if request.type == com_pb2.HistoryRequest.BUYINGS:
m = models.Buying
if request.type == com_pb2.HistoryRequest.REFILLINGS:
m = models.Refilling
if m is None:
return com_pb2.HistoryReply(
now=pb_now(), status=com_pb2.HistoryReply.MISSING_TYPE
)
# Create request and apply filters
req = db.query(m)
if counter is not None:
req = req.filter(m.counter_id == counter.id)
if device is not None:
req = req.filter(m.machine_id == device.uuid)
if customer is not None:
if request.type == com_pb2.HistoryRequest.BUYINGS:
req = req.join(models.Payment).filter(
models.Payment.customer_id == customer.id
)
if request.type == com_pb2.HistoryRequest.REFILLINGS:
req = req.filter(m.customer_id == customer.id)
if refounded is not None:
if request.type == com_pb2.HistoryRequest.BUYINGS:
req = req.filter(m.refounded == refounded)
if request.type == com_pb2.HistoryRequest.REFILLINGS:
req = req.filter(m.cancelled == refounded)
if history_size > 0:
req = req.limit(history_size)
refillings = []
buyings = []
for element in req.all():
if request.type == com_pb2.HistoryRequest.BUYINGS:
total_price = decimal.Decimal(0)
payments = []
basket = []
for payment in element.payments:
total_price += payment.amount
payments.append(
com_pb2.Payment(
customer_id=payment.customer_id,
amount=decimal_to_pb_money(payment.amount),
)
)
for item in element.basket_items:
basket.append(
com_pb2.BasketItem(
product_id=item.product_id,
quantity=item.quantity,
unit_price=decimal_to_pb_money(item.unit_price),
)
)
buyings.append(
com_pb2.Buying(
id=element.id,
label=element.label,
refounded=element.refounded,
date=date_to_pb(element.date),
price=decimal_to_pb_money(total_price),
payments=payments,
items=basket,
)
)
if request.type == com_pb2.HistoryRequest.REFILLINGS:
refillings.append(refilling_to_pb(element))
return com_pb2.HistoryReply(
now=pb_now(),
status=com_pb2.HistoryReply.SUCCESS,
refillings=refillings,
buyings=buyings,
)
def CounterList(self, request, context):
"""
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment