aboutsummaryrefslogtreecommitdiff
path: root/jb/managers/thl.py
diff options
context:
space:
mode:
Diffstat (limited to 'jb/managers/thl.py')
-rw-r--r--jb/managers/thl.py71
1 files changed, 66 insertions, 5 deletions
diff --git a/jb/managers/thl.py b/jb/managers/thl.py
index 85e0697..96f624e 100644
--- a/jb/managers/thl.py
+++ b/jb/managers/thl.py
@@ -1,15 +1,16 @@
import requests
+from fastapi import HTTPException, status
from generalresearch.currency import USDCent
from generalresearch.models.thl.definitions import PayoutStatus
from generalresearch.models.thl.payout import UserPayoutEvent
from generalresearch.models.thl.task_status import TaskStatusResponse
from generalresearch.models.thl.wallet.cashout_method import (
+ CashoutMethodOut,
CashoutRequestInfo,
CashoutRequestResponse,
)
from jb.config import settings
-from jb.models.auth import User
def get_task_status(tsid: str) -> TaskStatusResponse | None:
@@ -22,20 +23,30 @@ def get_task_status(tsid: str) -> TaskStatusResponse | None:
def user_cashout_request(
- user: User, amount: USDCent, cashout_method_id: str
+ product_user_id: str, amount: USDCent, cashout_method_id: str
) -> CashoutRequestInfo:
assert isinstance(amount, USDCent)
- assert USDCent(0) < amount < USDCent(10_00)
+ assert USDCent(0) < amount <= USDCent(100_00)
url = f"{settings.fsb_host}{settings.product_id}/cashout/"
body: dict[str, str | int] = {
- "bpuid": user.product_user_id,
+ "bpuid": product_user_id,
"amount": int(amount),
"cashout_method_id": cashout_method_id,
}
res = requests.post(url, json=body)
- d = res.json()
+ if res.status_code == status.HTTP_400_BAD_REQUEST:
+ try:
+ message = res.json().get("msg")
+ except (ValueError, AttributeError):
+ message = None
+ raise HTTPException(
+ status_code=status.HTTP_400_BAD_REQUEST,
+ detail=message or "Cashout request failed",
+ )
+ res.raise_for_status()
+ d = res.json()
return CashoutRequestResponse.model_validate(d).cashout
@@ -53,6 +64,49 @@ def manage_pending_cashout(
return UserPayoutEvent.model_validate(d)
+def get_paypal_cashout_method_if_exists(
+ product_user_id: str, client_ip: str
+) -> str | None:
+ """
+ Make sure a paypal cashout method exists for this user. Use their login email.
+ We check if it exists first, so that once a user can change their paypal email,
+ we do not overwrite it.
+ """
+ url = f"{settings.fsb_host}{settings.product_id}/cashout_methods/"
+ params = {"bpuid": product_user_id, "ip": client_ip}
+ res = requests.get(url, params=params)
+ assert res.status_code == status.HTTP_200_OK, res.text
+ cms = res.json()["cashout_methods"]
+ res = next(filter(lambda x: x["type"] == "PAYPAL", cms), None)
+ if res:
+ return res["id"]
+ return None
+
+
+def create_paypal_cashout_method(product_user_id: str, email: str) -> dict:
+ url = f"{settings.fsb_host}{settings.product_id}/cashout_methods/"
+ body = {"bpuid": product_user_id, "type": "PAYPAL", "email": email}
+ res = requests.post(url, json=body)
+ assert res.status_code == status.HTTP_200_OK, res.text
+ return res.json()["cashout_method"]
+
+
+def create_paypal_cashout_method_if_not_exists(
+ product_user_id: str, email: str, client_ip: str
+) -> None:
+ if not get_paypal_cashout_method_if_exists(product_user_id, client_ip=client_ip):
+ create_paypal_cashout_method(product_user_id, email)
+
+
+def get_cashout_method(cashout_method_id: str) -> CashoutMethodOut:
+ url = (
+ f"{settings.fsb_host}{settings.product_id}/cashout_methods/{cashout_method_id}/"
+ )
+ res = requests.get(url)
+ assert res.status_code == status.HTTP_200_OK, res.text
+ return CashoutMethodOut.model_validate(res.json()["cashout_method"])
+
+
def get_wallet_balance(amt_worker_id: str) -> USDCent:
# This will raise an Exception if wallet balance is negative
url = f"{settings.fsb_host}{settings.product_id}/wallet/"
@@ -67,3 +121,10 @@ def get_wallet_balance_if_non_negative(amt_worker_id: str) -> USDCent | None:
if amt >= 0:
return USDCent(amt)
return None
+
+
+def get_cashout_detail(cashout_id: str) -> CashoutRequestInfo:
+ url = f"{settings.fsb_host}{settings.product_id}/cashout/{cashout_id}/"
+ res = requests.get(url)
+ assert res.status_code == status.HTTP_200_OK, res.text
+ return CashoutRequestInfo.model_validate(res.json()["cashout"])