Skip to content

Commit

Permalink
hack together a minibc_cmd_find_missing_shipping deal right quick
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffwecan committed Mar 6, 2024
1 parent 23111fc commit 4e70c31
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
17 changes: 16 additions & 1 deletion member_card/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from member_card.db import db
from member_card.gcp import get_bucket, publish_message
from member_card.image import generate_card_image
from member_card.minibc import Minibc, parse_subscriptions
from member_card.minibc import Minibc, parse_subscriptions, find_missing_shipping
from member_card.models import AnnualMembership, User
from member_card.models.membership_card import get_or_create_membership_card
from member_card.models.user import add_role_to_user_by_email, edit_user_name
Expand Down Expand Up @@ -254,6 +254,21 @@ def minibc():
pass


@minibc.command("find-missing-shipping")
def minibc_cmd_find_missing_shipping():
minibc_client = Minibc(api_key=app.config["MINIBC_API_KEY"])
missing_shipping_subs = find_missing_shipping(

Check warning on line 260 in member_card/commands.py

View check run for this annotation

Codecov / codecov/patch

member_card/commands.py#L259-L260

Added lines #L259 - L260 were not covered by tests
minibc_client=minibc_client,
skus=app.config["MINIBC_MEMBERSHIP_SKUS"],
)
print(f"{len(missing_shipping_subs)}=")
from pprint import pprint

Check warning on line 265 in member_card/commands.py

View check run for this annotation

Codecov / codecov/patch

member_card/commands.py#L264-L265

Added lines #L264 - L265 were not covered by tests

breakpoint()
pprint({sub["id"]: sub["customer"] for sub in missing_shipping_subs})
breakpoint()

Check warning on line 269 in member_card/commands.py

View check run for this annotation

Codecov / codecov/patch

member_card/commands.py#L267-L269

Added lines #L267 - L269 were not covered by tests


@minibc.command("list-incoming-webhooks")
def list_incoming_webhooks():
minibc = Minibc(api_key=app.config["MINIBC_API_KEY"])
Expand Down
56 changes: 56 additions & 0 deletions member_card/minibc.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,62 @@ def parse_subscriptions(skus, subscriptions):
return memberships


def find_missing_shipping(minibc_client: Minibc, skus):
start_page_num = 1
max_pages = 1000

Check warning on line 272 in member_card/minibc.py

View check run for this annotation

Codecov / codecov/patch

member_card/minibc.py#L271-L272

Added lines #L271 - L272 were not covered by tests

missing_shipping_subs = list()
inactive_missing_shipping_subs = list()

Check warning on line 275 in member_card/minibc.py

View check run for this annotation

Codecov / codecov/patch

member_card/minibc.py#L274-L275

Added lines #L274 - L275 were not covered by tests

last_page_num = start_page_num
end_page_num = start_page_num + max_pages + 1

Check warning on line 278 in member_card/minibc.py

View check run for this annotation

Codecov / codecov/patch

member_card/minibc.py#L277-L278

Added lines #L277 - L278 were not covered by tests

logger.debug(

Check warning on line 280 in member_card/minibc.py

View check run for this annotation

Codecov / codecov/patch

member_card/minibc.py#L280

Added line #L280 was not covered by tests
f"find_missing_shipping() => starting to paginate subscriptions and such: {start_page_num=} {end_page_num=}"
)
total_subs_num = 0
total_subs_missing_shipping = 0
total_inactive_subs_missing_shipping = 0
for page_num in range(start_page_num, end_page_num):
logger.info(f"Sync at {page_num=}")
subscriptions = minibc_client.search_subscriptions(

Check warning on line 288 in member_card/minibc.py

View check run for this annotation

Codecov / codecov/patch

member_card/minibc.py#L283-L288

Added lines #L283 - L288 were not covered by tests
product_sku=skus[0],
page_num=page_num,
)
if subscriptions is None:
logger.debug(

Check warning on line 293 in member_card/minibc.py

View check run for this annotation

Codecov / codecov/patch

member_card/minibc.py#L292-L293

Added lines #L292 - L293 were not covered by tests
f"find_missing_shipping() => {last_page_num=} returned no results!. Setting `last_page_num` back to 1"
)
last_page_num = 1
break

Check warning on line 297 in member_card/minibc.py

View check run for this annotation

Codecov / codecov/patch

member_card/minibc.py#L296-L297

Added lines #L296 - L297 were not covered by tests

last_page_num = page_num
for subscription in subscriptions:
if subscription["shipping_address"]["street_1"] == "":
logger.debug(

Check warning on line 302 in member_card/minibc.py

View check run for this annotation

Codecov / codecov/patch

member_card/minibc.py#L299-L302

Added lines #L299 - L302 were not covered by tests
f"{subscription['customer']['email']} has no shipping address set!"
)
if subscription["status"] == "inactive":
inactive_missing_shipping_subs.append(subscriptions)
missing_shipping_subs.append(subscription)

Check warning on line 307 in member_card/minibc.py

View check run for this annotation

Codecov / codecov/patch

member_card/minibc.py#L305-L307

Added lines #L305 - L307 were not covered by tests

logger.debug(

Check warning on line 309 in member_card/minibc.py

View check run for this annotation

Codecov / codecov/patch

member_card/minibc.py#L309

Added line #L309 was not covered by tests
f"find_missing_shipping() => after {page_num=} sleeping for 1 second..."
)
total_subs_num += len(subscriptions)
total_subs_missing_shipping = len(missing_shipping_subs)
total_inactive_subs_missing_shipping = len(inactive_missing_shipping_subs)
logger.debug(

Check warning on line 315 in member_card/minibc.py

View check run for this annotation

Codecov / codecov/patch

member_card/minibc.py#L312-L315

Added lines #L312 - L315 were not covered by tests
f"{total_subs_num=}:: {total_subs_missing_shipping=} ({total_inactive_subs_missing_shipping=})"
)
sleep(1)

Check warning on line 318 in member_card/minibc.py

View check run for this annotation

Codecov / codecov/patch

member_card/minibc.py#L318

Added line #L318 was not covered by tests

logger.debug(

Check warning on line 320 in member_card/minibc.py

View check run for this annotation

Codecov / codecov/patch

member_card/minibc.py#L320

Added line #L320 was not covered by tests
f"{total_subs_num=}:: {total_subs_missing_shipping=} ({total_inactive_subs_missing_shipping=})"
)
return missing_shipping_subs

Check warning on line 323 in member_card/minibc.py

View check run for this annotation

Codecov / codecov/patch

member_card/minibc.py#L323

Added line #L323 was not covered by tests


def minibc_orders_etl(minibc_client: Minibc, skus, load_all):
from member_card import models

Expand Down

0 comments on commit 4e70c31

Please sign in to comment.