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

server: fix protocol for Refillings and implements refillings cancellation

parent 74f30be2
......@@ -44,7 +44,7 @@ message Product {
google.protobuf.Timestamp end = 2;
Money price = 3;
}
int64 id = 1;
uint64 id = 1;
string name = 2; //pretty print in trees
string code = 3; //code for fast search, not necessarily unique
Money default_price = 4;
......@@ -53,18 +53,19 @@ message Product {
}
message BasketItem {
int64 product_id = 1;
uint64 product_id = 1;
int64 quantity = 2;
Money unit_price = 3; // Price at buying time, used in History, not mandatory when it's a BuyingRequest
}
message Refilling {
string customer_id = 1;
int64 counter_id = 2;
uint64 counter_id = 2;
string device_uuid = 3;
PaymentMethod payment_method = 4;
Money amount = 5;
google.protobuf.Timestamp date = 6;
bool cancelled = 6;
google.protobuf.Timestamp date = 7;
}
message Payment { //Allow us to handle multiple user buyings, describe who pays and how much.
......@@ -73,18 +74,18 @@ message Payment { //Allow us to handle multiple user buyings, describe who pays
}
message Buying {
int64 id = 1;
uint64 id = 1;
string label = 2; // description of the buying, ui purpose only
Money price = 3; // price the customer(s) actually paid at 'date', sum of Payments
bool refounded = 4;
int64 counter_id = 5;
uint64 counter_id = 5;
google.protobuf.Timestamp date = 6;
repeated Payment payments = 7; // Several users can pay the same thing during multi-payments
repeated BasketItem items = 8;
}
message BuyingRequest {
int64 counter_id = 1;
uint64 counter_id = 1;
string device_uuid = 2;
repeated Payment payments = 3; // Payment repartition between users
repeated BasketItem basket = 4; //List of all products,their qty, their unit price when it's a reply...
......@@ -116,7 +117,7 @@ message BuyingReply {
message RefillingRequest {
string customer_id = 1;
int64 counter_id = 2;
uint64 counter_id = 2;
string device_uuid = 3;
PaymentMethod payment_method = 4;
Money amount = 5;
......@@ -141,11 +142,12 @@ message RefillingReply {
Status status = 1;
google.protobuf.Timestamp now = 2;
Money customer_balance = 3;
Refilling refilling = 4;
}
message RefoundBuyingRequest {
int64 transaction_id = 1;
int64 counter_id = 3;
uint64 transaction_id = 1;
uint64 counter_id = 3;
string device_uuid = 4;
}
......@@ -164,13 +166,12 @@ message RefoundBuyingReply {
Status status = 1;
google.protobuf.Timestamp now = 2;
repeated Money customer_id = 3; // Multiple customer could be refounded at the same time since multi-payment exist
repeated string customer_id = 3; // Multiple customer could be refounded at the same time since multi-payment exist
repeated Money customer_balance = 4; //So we can have multiple new balances...
}
message CancelRefillingRequest {
int64 refilling_id = 1;
int64 counter_id = 3;
uint64 refilling_id = 1;
string device_uuid = 4;
}
......@@ -180,16 +181,14 @@ message CancelRefillingReply {
INTERNAL_SERVER_ERROR = 1;
MISSING_TRANSACTION = 2;
MISSING_COUNTER = 4;
MISSING_DEVICE_UUID = 5;
MISSING_DEVICE_UUID = 4;
TRANSACTION_NOT_FOUND = 6;
COUNTER_NOT_FOUND = 7;
TRANSACTION_NOT_FOUND = 5;
}
Status status = 1;
google.protobuf.Timestamp now = 2;
Money customer_id = 3;
string customer_id = 3;
Money customer_balance = 4;
}
......@@ -197,7 +196,7 @@ message TransfertRequest {
string origin_id = 1;
string destination_id = 2;
Money amount = 3;
int64 counter_id = 4;
uint64 counter_id = 4;
string device_uuid = 5;
}
......@@ -246,7 +245,7 @@ message HistoryRequest {
HistoryType type = 1; // Only one required
// Optional fields
int64 counter = 2;
uint64 counter = 2;
string customer_id = 3;
string device_uuid = 4;
uint64 max_history_size = 5;
......@@ -274,7 +273,7 @@ message CounterListReply {
INTERNAL_SERVER_ERROR = 1;
}
message Counter {
int64 id = 1;
uint64 id = 1;
string name = 2;
}
......@@ -284,7 +283,7 @@ message CounterListReply {
}
message ProductsRequest {
int64 counter_id = 1;
uint64 counter_id = 1;
}
message ProductsReply {
......
......@@ -129,13 +129,13 @@ class Refilling(Model):
__tablename__ = "refillings"
id = Column(Integer, primary_key=True, unique=True, autoincrement=True)
customer_id = Column(Integer, ForeignKey("customers.id"))
customer_id = Column(String, ForeignKey("customers.id"))
customer = relationship(
"Customer", backref=backref("refillings", lazy=True), lazy=True
)
payment_method_id = Column(Integer, ForeignKey("payment_methods.id"))
customer = relationship(
payment_method = relationship(
"PaymentMethod", backref=backref("refillings", lazy=True), lazy=True
)
......@@ -150,6 +150,7 @@ class Refilling(Model):
)
amount = Column(Money)
date = Column(DateTime(timezone=True), default=datetime.utcnow)
cancelled = Column(Boolean, default=False)
......@@ -203,7 +204,7 @@ class Payment(Model):
__tablename__ = "payments"
id = Column(Integer, primary_key=True, unique=True, autoincrement=True)
customer_id = Column(Integer, ForeignKey("customers.id"))
customer_id = Column(String, ForeignKey("customers.id"))
customer = relationship(
"Customer", backref=backref("payments", lazy=True), lazy=True
)
......
......@@ -17,6 +17,12 @@ def pb_now() -> Timestamp:
return timestamp
def date_to_pb(date) -> Timestamp:
timestamp = Timestamp()
timestamp.FromDatetime(date)
return timestamp
def decimal_to_pb_money(dec: decimal.Decimal) -> com_pb2.Money:
tup = dec.as_tuple()
return com_pb2.Money(sign=tup.sign, exponent=tup.exponent, digits=tup.digits)
......@@ -88,21 +94,29 @@ class PaymentServicer(com_pb2_grpc.PaymentProtocolServicer):
customer.balance += amount
db.add(customer)
db.add(
models.Refilling(
customer_id=customer.id,
payment_method_id=request.payment_method,
counter_id=counter.id,
machine_id=machine.uuid,
amount=amount,
cancelled=False,
)
refilling = models.Refilling(
customer_id=customer.id,
payment_method_id=request.payment_method,
counter_id=counter.id,
machine_id=machine.uuid,
amount=amount,
cancelled=False,
)
db.add(refilling)
db.commit()
return com_pb2.RefillingReply(
now=pb_now(),
status=com_pb2.RefillingReply.SUCCESS,
customer_balance=decimal_to_pb_money(customer.balance),
refilling=com_pb2.Refilling(
customer_id=refilling.customer_id,
counter_id=refilling.counter_id,
device_uuid=refilling.machine_id,
payment_method=refilling.payment_method.id,
amount=decimal_to_pb_money(refilling.amount),
cancelled=refilling.cancelled,
date=date_to_pb(refilling.date),
),
)
def RefoundBuying(self, request, context):
......@@ -112,10 +126,36 @@ class PaymentServicer(com_pb2_grpc.PaymentProtocolServicer):
raise NotImplementedError("Method not implemented!")
def CancelRefilling(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!")
"""
Cancel a refilling
"""
if not request.refilling_id:
return com_pb2.CancelRefillingReply(
now=pb_now(), status=com_pb2.CancelRefillingReply.MISSING_TRANSACTION
)
if not request.device_uuid:
return com_pb2.CancelRefillingReply(
now=pb_now(), status=com_pb2.CancelRefillingReply.MISSING_DEVICE_UUID
)
refilling = db.query(models.Refilling).get(request.refilling_id)
if refilling is None:
return com_pb2.CancelRefillingReply(
now=pb_now(), status=com_pb2.CancelRefillingReply.TRANSACTION_NOT_FOUND
)
refilling.cancelled = True
customer = refilling.customer
customer.balance -= refilling.amount
db.add(refilling)
db.commit()
return com_pb2.CancelRefillingReply(
now=pb_now(),
status=com_pb2.CancelRefillingReply.SUCCESS,
customer_id=customer.id,
customer_balance=decimal_to_pb_money(customer.balance),
)
def Transfert(self, request, context):
"""Missing associated documentation comment in .proto file"""
......@@ -163,7 +203,7 @@ class PaymentServicer(com_pb2_grpc.PaymentProtocolServicer):
"""
Return the list of products available on a given counter
"""
if request.counter_id < 1:
if not request.counter_id:
return com_pb2.ProductsReply(
status=com_pb2.ProductsReply.MISSING_COUNTER, now=pb_now()
)
......@@ -188,14 +228,10 @@ class PaymentServicer(com_pb2_grpc.PaymentProtocolServicer):
# Add happy hours
for happy_hour in product.happy_hours:
start = Timestamp()
start.FromDatetime(happy_hour.start)
end = Timestamp()
end.FromDatetime(happy_hour.end)
p.happy_hours.append(
com_pb2.Product.HappyHour(
start=start,
end=end,
start=date_to_pb(happy_hour.start),
end=date_to_pb(happy_hour.end),
price=decimal_to_pb_money(happy_hour.price),
)
)
......
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