forked from Support/sp-hydra-veil-gui
72 lines
2.7 KiB
Python
72 lines
2.7 KiB
Python
from core.services.subscriptions.subscriptions_by_protocol import get_profile_subscription, start_new_payment_cycle
|
|
from core.models.enum_models import PaymentCategory
|
|
from core.models.Result import Result, ResultError
|
|
from core.errors.logger import logger
|
|
|
|
from gui.v2.infrastructure.take_plumbing_action import take_action
|
|
|
|
# generic
|
|
from typing import Callable, Optional
|
|
|
|
|
|
def confirm_prereqs(PRE_REQS: list, allow_false: Optional[bool] = False) -> Result:
|
|
for each_prereq in PRE_REQS:
|
|
if allow_false:
|
|
if each_prereq == None:
|
|
return Result(valid=False, error_type=ResultError.INVALID_INPUT, message=f"You are missing {each_prereq}")
|
|
else:
|
|
if not each_prereq:
|
|
return Result(valid=False, error_type=ResultError.INVALID_INPUT, message=f"You are missing {each_prereq}")
|
|
return Result(valid=True)
|
|
|
|
|
|
def filter_subscription(profile_id: int) -> Result:
|
|
"""Evaluate if this profile has a subscription that's ready to connect."""
|
|
|
|
logger.info(f"Checking the subscription on profile id {profile_id}")
|
|
subscription = get_profile_subscription(profile_id=profile_id)
|
|
|
|
if not subscription or not subscription.billing_code:
|
|
# auto check tickets would be here.
|
|
return Result(valid=False, error_type=ResultError.NAVIGATE_TO, navigate_to="id", message="No subscription. Pick a payment method.")
|
|
|
|
if subscription.billing_pending:
|
|
start_result = start_new_payment_cycle(category=PaymentCategory.INDIVIDUAL, profile_id=profile_id)
|
|
if start_result.valid:
|
|
logger.info("creating a new payment cycle was valid, it worked.")
|
|
return Result(valid=False, error_type=ResultError.NAVIGATE_TO, navigate_to="payment_details", message="Finish the subscription payment")
|
|
else:
|
|
logger.info(f"Returning the error of start_result {start_result}")
|
|
return start_result
|
|
|
|
# has a valid sub:
|
|
logger.info(f"Profile id {profile_id} has a valid subscription ready to go..")
|
|
return Result(valid=True)
|
|
|
|
|
|
def _connect_profile(profile_id: int) -> Result:
|
|
# Step 1: Subscription filter:
|
|
sub_result = filter_subscription(profile_id=profile_id)
|
|
if not sub_result.valid:
|
|
return sub_result
|
|
|
|
# fake connection
|
|
logger.info(f"Profile id {profile_id} is ready to connect..")
|
|
return Result(valid=True)
|
|
|
|
|
|
def connect_profile(profile_id: int, ui_caller: Callable) -> Result:
|
|
PRE_REQS = [profile_id, ui_caller]
|
|
|
|
confirmed_result = confirm_prereqs(PRE_REQS)
|
|
if not confirmed_result.valid:
|
|
return confirmed_result
|
|
|
|
take_action(
|
|
action=_connect_profile,
|
|
ui_caller=ui_caller,
|
|
profile_id=profile_id
|
|
)
|
|
return Result(valid=True)
|
|
|
|
|