Skip to content

Commit

Permalink
Merge pull request #203 from planetarium/release/0.8.0
Browse files Browse the repository at this point in the history
Backmerge 0.8.0 to develotment
  • Loading branch information
U-lis authored Dec 13, 2023
2 parents d20c557 + ecccca4 commit 5008f96
Show file tree
Hide file tree
Showing 23 changed files with 435 additions and 322 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@ on:
required: true
IAP_GARAGE_WEBHOOK_URL:
required: true
IAP_ALERT_WEBHOOK_URL:
required: true
GOLDEN_DUST_REQUEST_SHEET_ID:
required: true
GOLDEN_DUST_WORK_SHEET_ID:
required: true
SEASON_PASS_JWT_SECRET:
required: true
VOUCHER_URL:
required: true
VOUCHER_JWT_SECRET:
required: true
BRIDGE_DATA:
Expand Down Expand Up @@ -135,12 +139,14 @@ jobs:
APPLE_KEY_ID: ${{ secrets.APPLE_KEY_ID }}
APPLE_ISSUER_ID: ${{ secrets.APPLE_ISSUER_ID }}
IAP_GARAGE_WEBHOOK_URL: ${{ secrets.IAP_GARAGE_WEBHOOK_URL }}
IAP_ALERT_WEBHOOK_URL: ${{ secrets.IAP_ALERT_WEBHOOK_URL }}
GOLDEN_DUST_REQUEST_SHEET_ID: ${{ secrets.GOLDEN_DUST_REQUEST_SHEET_ID }}
GOLDEN_DUST_WORK_SHEET_ID: ${{ secrets.GOLDEN_DUST_WORK_SHEET_ID }}
FORM_SHEET: ${{ vars.FORM_SHEET }}
CDN_HOST: ${{ vars.CDN_HOST }}
PLANET_URL: ${{ vars.PLANET_URL }}
SEASON_PASS_JWT_SECRET: ${{ secrets.SEASON_PASS_JWT_SECRET }}
VOUCHER_URL: ${{ secrets.VOUCHER_URL }}
VOUCHER_JWT_SECRET: ${{ secrets.VOUCHER_JWT_SECRET }}
BRIDGE_DATA: ${{ secrets.BRIDGE_DATA }}
REFUND_SHEET_ID : ${{ secrets.REFUND_SHEET_ID }}
Expand All @@ -166,12 +172,14 @@ jobs:
APPLE_KEY_ID: ${{ secrets.APPLE_KEY_ID }}
APPLE_ISSUER_ID: ${{ secrets.APPLE_ISSUER_ID }}
IAP_GARAGE_WEBHOOK_URL: ${{ secrets.IAP_GARAGE_WEBHOOK_URL }}
IAP_ALERT_WEBHOOK_URL: ${{ secrets.IAP_ALERT_WEBHOOK_URL }}
GOLDEN_DUST_REQUEST_SHEET_ID: ${{ secrets.GOLDEN_DUST_REQUEST_SHEET_ID }}
GOLDEN_DUST_WORK_SHEET_ID: ${{ secrets.GOLDEN_DUST_WORK_SHEET_ID }}
FORM_SHEET: ${{ vars.FORM_SHEET }}
CDN_HOST: ${{ vars.CDN_HOST }}
PLANET_URL: ${{ vars.PLANET_URL }}
SEASON_PASS_JWT_SECRET: ${{ secrets.SEASON_PASS_JWT_SECRET }}
VOUCHER_URL: ${{ secrets.VOUCHER_URL }}
VOUCHER_JWT_SECRET: ${{ secrets.VOUCHER_JWT_SECRET }}
BRIDGE_DATA: ${{ secrets.BRIDGE_DATA }}
REFUND_SHEET_ID : ${{ secrets.REFUND_SHEET_ID }}
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ jobs:
APPLE_ISSUER_ID: ${{ secrets.APPLE_ISSUER_ID }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
IAP_GARAGE_WEBHOOK_URL: ${{ secrets.IAP_GARAGE_WEBHOOK_URL }}
IAP_ALERT_WEBHOOK_URL: ${{ secrets.IAP_ALERT_WEBHOOK_URL }}
GOLDEN_DUST_REQUEST_SHEET_ID: ${{ secrets.GOLDEN_DUST_REQUEST_SHEET_ID }}
GOLDEN_DUST_WORK_SHEET_ID: ${{ secrets.GOLDEN_DUST_WORK_SHEET_ID }}
SEASON_PASS_JWT_SECRET: ${{ secrets.SEASON_PASS_JWT_SECRET }}
VOUCHER_URL: ${{ secrets.VOUCHER_URL }}
VOUCHER_JWT_SECRET: ${{ secrets.VOUCHER_JWT_SECRET }}
BRIDGE_DATA: ${{ secrets.BRIDGE_DATA }}
REFUND_SHEET_ID : ${{ secrets.REFUND_SHEET_ID }}
Expand All @@ -67,9 +69,11 @@ jobs:
APPLE_ISSUER_ID: ${{ secrets.APPLE_ISSUER_ID }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
IAP_GARAGE_WEBHOOK_URL: ${{ secrets.IAP_GARAGE_WEBHOOK_URL }}
IAP_ALERT_WEBHOOK_URL: ${{ secrets.IAP_ALERT_WEBHOOK_URL }}
GOLDEN_DUST_REQUEST_SHEET_ID: ${{ secrets.GOLDEN_DUST_REQUEST_SHEET_ID }}
GOLDEN_DUST_WORK_SHEET_ID: ${{ secrets.GOLDEN_DUST_WORK_SHEET_ID }}
SEASON_PASS_JWT_SECRET: ${{ secrets.SEASON_PASS_JWT_SECRET }}
VOUCHER_URL: ${{ secrets.VOUCHER_URL }}
VOUCHER_JWT_SECRET: ${{ secrets.VOUCHER_JWT_SECRET }}
BRIDGE_DATA: ${{ secrets.BRIDGE_DATA }}
REFUND_SHEET_ID : ${{ secrets.REFUND_SHEET_ID }}
Expand Down Expand Up @@ -104,9 +108,11 @@ jobs:
APPLE_ISSUER_ID: ${{ secrets.APPLE_ISSUER_ID }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
IAP_GARAGE_WEBHOOK_URL: ${{ secrets.IAP_GARAGE_WEBHOOK_URL }}
IAP_ALERT_WEBHOOK_URL: ${{ secrets.IAP_ALERT_WEBHOOK_URL }}
GOLDEN_DUST_REQUEST_SHEET_ID: ${{ secrets.GOLDEN_DUST_REQUEST_SHEET_ID }}
GOLDEN_DUST_WORK_SHEET_ID: ${{ secrets.GOLDEN_DUST_WORK_SHEET_ID }}
SEASON_PASS_JWT_SECRET: ${{ secrets.SEASON_PASS_JWT_SECRET }}
VOUCHER_URL: ${{ secrets.VOUCHER_URL }}
VOUCHER_JWT_SECRET: ${{ secrets.VOUCHER_JWT_SECRET }}
BRIDGE_DATA: ${{ secrets.BRIDGE_DATA }}
REFUND_SHEET_ID : ${{ secrets.REFUND_SHEET_ID }}
6 changes: 6 additions & 0 deletions .github/workflows/synth.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ on:
required: true
IAP_GARAGE_WEBHOOK_URL:
required: true
IAP_ALERT_WEBHOOK_URL:
required: true
GOLDEN_DUST_REQUEST_SHEET_ID:
required: true
GOLDEN_DUST_WORK_SHEET_ID:
required: true
SEASON_PASS_JWT_SECRET:
required: true
VOUCHER_URL:
required: true
VOUCHER_JWT_SECRET:
required: true
BRIDGE_DATA:
Expand Down Expand Up @@ -122,12 +126,14 @@ jobs:
APPLE_KEY_ID: ${{ secrets.APPLE_KEY_ID }}
APPLE_ISSUER_ID: ${{ secrets.APPLE_ISSUER_ID }}
IAP_GARAGE_WEBHOOK_URL: ${{ secrets.IAP_GARAGE_WEBHOOK_URL }}
IAP_ALERT_WEBHOOK_URL: ${{ secrets.IAP_ALERT_WEBHOOK_URL }}
GOLDEN_DUST_REQUEST_SHEET_ID: ${{ secrets.GOLDEN_DUST_REQUEST_SHEET_ID }}
GOLDEN_DUST_WORK_SHEET_ID: ${{ secrets.GOLDEN_DUST_WORK_SHEET_ID }}
FORM_SHEET: ${{ vars.FORM_SHEET }}
CDN_HOST: ${{ vars.CDN_HOST }}
PLANET_URL: ${{ vars.PLANET_URL }}
SEASON_PASS_JWT_SECRET: ${{ secrets.SEASON_PASS_JWT_SECRET }}
VOUCHER_URL: ${{ secrets.VOUCHER_URL }}
VOUCHER_JWT_SECRET: ${{ secrets.VOUCHER_JWT_SECRET }}
BRIDGE_DATA: ${{ secrets.BRIDGE_DATA }}
REFUND_SHEET_ID : ${{ secrets.REFUND_SHEET_ID }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ jobs:
- name: Run test
run: |
poetry run pytest tests/utils
poetry run pytest tests
slack_notification:
uses: ./.github/workflows/slack_message.yml
Expand Down
5 changes: 5 additions & 0 deletions common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,13 @@ class Config:
golden_dust_work_sheet_id: Optional[str] = None
form_sheet: Optional[str] = None

# Status monitor
iap_garage_webhook_url: str = None
iap_alert_webhook_url: str = None

# SeasonPass
season_pass_jwt_secret: str = None

# Voucher
voucher_url: str = None
voucher_jwt_secret: str = None
28 changes: 28 additions & 0 deletions common/alembic/versions/2822c456abc3_add_unique_constraint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Add unique constraint
Revision ID: 2822c456abc3
Revises: 5bf8ab4f3b13
Create Date: 2023-12-05 01:11:38.405536
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = '2822c456abc3'
down_revision = '5bf8ab4f3b13'
branch_labels = None
depends_on = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_unique_constraint("voucher_request_receipt_id_unique", 'voucher_request', ['receipt_id'])
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint("voucher_request_receipt_id_unique", 'voucher_request', type_='unique')
# ### end Alembic commands ###
2 changes: 1 addition & 1 deletion common/models/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Category(AutoIdMixin, TimeStampMixin, Base):
# FIXME: Update to nullable=False
l10n_key = Column(Text, doc="L10N Key")

product_list: Mapped[List["Product"]] = relationship("Product", secondary=category_product_table)
product_list: Mapped[List["Product"]] = relationship("Product", secondary=category_product_table, order_by="Product.order")

@property
def path(self):
Expand Down
11 changes: 7 additions & 4 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_id = Column(Integer, ForeignKey("receipt.id"), nullable=False, unique=True)
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
4 changes: 2 additions & 2 deletions common/shared_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
self.voucher_dlq = _sqs.Queue(self, f"{config.stage}-9c-iap-voucher-dlq")
self.voucher_q = _sqs.Queue(
self, f"{config.stage}-9c-iap-voucher-queue",
dead_letter_queue=_sqs.DeadLetterQueue(max_receive_count=12, queue=self.voucher_dlq),
visibility_timeout=cdk_core.Duration.seconds(10)
dead_letter_queue=_sqs.DeadLetterQueue(max_receive_count=10, queue=self.voucher_dlq),
visibility_timeout=cdk_core.Duration.seconds(30),
)

# RDS
Expand Down
32 changes: 6 additions & 26 deletions iap/api/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from common.models.product import Product, Category
from common.utils.address import format_addr
from common.utils.garage import get_iap_garage
from common.utils.receipt import PlanetID
from iap import settings
from iap.dependencies import session
Expand Down Expand Up @@ -38,17 +37,6 @@ def product_list(agent_addr: str,
.order_by(Category.order, Product.order)
).all()

iap_garage = {x.fungible_id: x.amount for x in get_iap_garage(sess)}
garage = {}
for category in all_category_list:
if ((category.open_timestamp and category.open_timestamp > datetime.now()) or
(category.close_timestamp and category.close_timestamp <= datetime.now())):
continue

for product in category.product_list:
for fungible_item in product.fungible_item_list:
garage[fungible_item.fungible_item_id] = iap_garage.get(fungible_item.fungible_item_id, 0)

category_schema_list = []
for category in all_category_list:
cat_schema = CategorySchema.model_validate(category)
Expand All @@ -59,21 +47,8 @@ def product_list(agent_addr: str,
(product.close_timestamp and product.close_timestamp <= datetime.now())):
continue

schema_dict[product.id] = ProductSchema.model_validate(product)
# FIXME: Pinpoint get product buyability
product_buyable = True
# Check fungible item stock in garage
for item in product.fungible_item_list:
if garage.get(item.fungible_item_id, 0) < item.amount:
schema_dict[product.id].buyable = False
product_buyable = False
break

if not product_buyable:
continue

# Check purchase history
schema = schema_dict[product.id]
schema = ProductSchema.model_validate(product)
if product.daily_limit:
schema.purchase_count = get_purchase_count(
sess, product.id, planet_id=planet_id, agent_addr=agent_addr, daily_limit=True
Expand All @@ -89,6 +64,11 @@ def product_list(agent_addr: str,
sess, product.id, planet_id=planet_id, agent_addr=agent_addr
)
schema.buyable = schema.purchase_count < product.account_limit
else: # Product with no limitation
schema.buyable = True

schema_dict[product.id] = schema

cat_schema.product_list = list(schema_dict.values())
category_schema_list.append(cat_schema)

Expand Down
24 changes: 12 additions & 12 deletions iap/api/purchase.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,6 @@ def request_product(receipt_data: ReceiptSchema, sess=Depends(session)):
)
sess.add(receipt)
sess.commit()
logger.info(f"Send voucher request: {receipt.uuid}")
resp = sqs.send_message(QueueUrl=VOUCHER_SQS_URL,
MessageBody=json.dumps({
"id": receipt.id,
"uuid": receipt.uuid,
"product_id": receipt.product_id,
"product_name": receipt.product.name,
"agent_addr": receipt.agent_addr,
"avatar_addr": receipt.avatar_addr,
"planet_id": receipt_data.planetId.decode(),
}))
logger.info(f"Voucher message: {resp['MessageId']}")
sess.refresh(receipt)

if receipt_data.store not in (Store.APPLE, Store.APPLE_TEST) and not product:
Expand Down Expand Up @@ -238,6 +226,18 @@ def request_product(receipt_data: ReceiptSchema, sess=Depends(session)):
raise_error(sess, receipt, ValueError(f"Receipt validation failed: {msg}"))

receipt.status = ReceiptStatus.VALID
logger.info(f"Send voucher request: {receipt.uuid}")
resp = sqs.send_message(QueueUrl=VOUCHER_SQS_URL,
MessageBody=json.dumps({
"receipt_id": receipt.id,
"uuid": str(receipt.uuid),
"product_id": receipt.product_id,
"product_name": receipt.product.name,
"agent_addr": receipt.agent_addr,
"avatar_addr": receipt.avatar_addr,
"planet_id": receipt_data.planetId.decode(),
}))
logger.info(f"Voucher message: {resp['MessageId']}")

now = datetime.now()
if ((product.open_timestamp and product.open_timestamp > now) or
Expand Down
2 changes: 1 addition & 1 deletion iap/schemas/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class FungibleAssetValueSchema(BaseSchema):

@model_validator(mode="after")
def make_ticker_to_name(self):
self.ticker = self.ticker.split("_")[-1]
self.ticker = self.ticker.split("__")[-1]
return self

class Config:
Expand Down
Loading

0 comments on commit 5008f96

Please sign in to comment.