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