Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs #190

Merged
merged 6 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions common/models/voucher.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import uuid

from sqlalchemy import Column, Text, Integer, ForeignKey, UUID, LargeBinary
from sqlalchemy.orm import relationship, backref
from sqlalchemy.orm import relationship, backref, Mapped

from common.models.base import Base, TimeStampMixin, AutoIdMixin
from common.models.product import Product
from common.models.receipt import Receipt
from common.utils.receipt import PlanetID


class VoucherRequest(AutoIdMixin, TimeStampMixin, Base):
__tablename__ = "voucher_request"
receipt_id = Column(Integer, ForeignKey("receipt.id"), nullable=False)
receipt = relationship("Receipt", foreign_keys=[receipt_id], uselist=False, backref=backref("voucher_request"))
receipt: Mapped["Receipt"] = relationship("Receipt", foreign_keys=[receipt_id], uselist=False,
backref=backref("voucher_request"))

# Copy all required data to view all info solely with this table
uuid = Column(UUID(as_uuid=True), nullable=False, index=True, default=uuid.uuid4,
Expand All @@ -20,7 +23,7 @@ class VoucherRequest(AutoIdMixin, TimeStampMixin, Base):
planet_id = Column(LargeBinary(length=12), nullable=False, default=PlanetID.ODIN.value,
doc="An identifier of planets")
product_id = Column(Integer, ForeignKey("product.id"), nullable=False)
product = relationship("Product", foreign_keys=[product_id], uselist=False)
product: Mapped["Product"] = relationship("Product", foreign_keys=[product_id], uselist=False)
product_name = Column(Text, nullable=False)

# Voucher request result
Expand Down
1 change: 1 addition & 0 deletions iap/api/purchase.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ def request_product(receipt_data: ReceiptSchema, sess=Depends(session)):
MessageBody=json.dumps({
"id": receipt.id,
"uuid": str(receipt.uuid),
"receipt_id": receipt.id,
"product_id": receipt.product_id,
"product_name": receipt.product.name,
"agent_addr": receipt.agent_addr,
Expand Down
8 changes: 7 additions & 1 deletion worker/worker/voucher.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from common import logger
from common.models.voucher import VoucherRequest
from common.utils.aws import fetch_secrets, fetch_parameter
from common.utils.receipt import PlanetID
from schemas.aws import SQSMessage

DB_URI = os.environ.get("DB_URI")
Expand Down Expand Up @@ -66,15 +67,20 @@ def handle(event, context):
message = SQSMessage(Records=event.get("Records", {}))
logger.info(f"SQS Message: {message}")

with scoped_session(sessionmaker(bind=engine)) as sess:
sess = scoped_session(sessionmaker(bind=engine))
try:
uuid_list = [x.body.get("uuid") for x in message.Records if x.body.get("uuid")]
voucher_list = sess.scalars(select(VoucherRequest.uuid).where(VoucherRequest.uuid.in_(uuid_list))).fetchall()
target_message_list = [x.body for x in message.Records if
x.body.get("force", False) is True or x.body.get("uuid") not in voucher_list]

for msg in target_message_list:
voucher = VoucherRequest(**msg)
voucher.planet_id = PlanetID(voucher.planet_id.encode())
sess.add(voucher)
sess.commit()
sess.refresh(voucher)
request(sess, voucher)
finally:
if sess is not None:
sess.close()