aboutsummaryrefslogtreecommitdiff
path: root/jb/managers/thl.py
blob: b1dcbdeed5471257b3e73f571c7bc7e76490d2d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from decimal import Decimal
from typing import Dict, Optional

import requests
from generalresearchutils.models.thl.payout import UserPayoutEvent
from generalresearchutils.models.thl.task_status import TaskStatusResponse
from generalresearchutils.models.thl.wallet.cashout_method import (
    CashoutRequestResponse,
    CashoutRequestInfo,
)

from jb.config import settings
from jb.models.currency import USDCent
from jb.models.definitions import PayoutStatus

# TODO: Organize this more with other endpoints (offerwall, cashout requests/approvals, etc).


def get_user_profile(amt_worker_id: str) -> Dict:
    url = f"{settings.fsb_host}{settings.product_id}/user/{amt_worker_id}/profile/"
    res = requests.get(url).json()
    if res.get("detail") == "user not found":
        raise ValueError("user not found")
    return res["user_profile"]


def get_user_blocked(amt_worker_id: str) -> bool:
    res = get_user_profile(amt_worker_id=amt_worker_id)
    return res["user"]["blocked"]


def get_user_blocked_or_not_exists(amt_worker_id: str) -> bool:
    try:
        res = get_user_profile(amt_worker_id=amt_worker_id)
        return res["user"]["blocked"]
    except ValueError as e:
        if e.args[0] == "user not found":
            return True


def get_task_status(tsid: str) -> Optional[TaskStatusResponse]:
    url = f"{settings.fsb_host}{settings.product_id}/status/{tsid}/"
    d = requests.get(url).json()
    if d.get("msg") == "invalid tsid":
        return None
    return TaskStatusResponse.model_validate(d)


def user_cashout_request(
    amt_worker_id: str, amount: USDCent, cashout_method_id
) -> CashoutRequestInfo:
    assert cashout_method_id in {
        settings.amt_assignment_cashout_method,
        settings.amt_bonus_cashout_method,
    }
    assert isinstance(amount, USDCent)
    assert USDCent(0) < amount < USDCent(10_00)
    url = f"{settings.fsb_host}{settings.product_id}/cashout/"
    body = {
        "bpuid": amt_worker_id,
        "amount": int(amount),
        "cashout_method_id": cashout_method_id,
    }
    res = requests.post(url, json=body)
    d = res.json()

    return CashoutRequestResponse.model_validate(d).cashout


def manage_pending_cashout(
    cashout_id: str, payout_status: PayoutStatus
) -> UserPayoutEvent:
    url = f"{settings.fsb_host}{settings.fsb_host_private_route}/thl/manage_cashout/"
    body = {
        "payout_id": cashout_id,
        "new_status": payout_status.value,
    }
    res = requests.post(url, json=body)
    d = res.json()

    return UserPayoutEvent.model_validate(d)


def get_wallet_balance(amt_worker_id: str):
    url = f"{settings.fsb_host}{settings.product_id}/wallet/"
    params = {"bpuid": amt_worker_id}
    return USDCent(requests.get(url, params=params).json()["wallet"]["amount"])