Spaces:
Sleeping
Sleeping
File size: 6,689 Bytes
bf2bf0e |
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
from typing import List, Dict, Optional
from api.sources.agent.request_params.auth import UpdateBrandRequest
from core.constants import Tables
from core.interface.supabase_client import async_supabase_client, supabase_config, create_supabase_client
from core.schemas.agent import AgentModel
from core.schemas.wallet import WalletModel
class AuthDataSource:
@staticmethod
async def login(email: str, password: str) -> Dict:
supabase = create_supabase_client(supabase_config)
response = supabase.auth.sign_in_with_password({
"email": email,
"password": password
})
res = {'refresh_token': response.session.refresh_token, 'access_token': response.session.access_token,
'uuid': response.user.id}
return res
@staticmethod
async def login_passwordless(phone: str):
supabase = create_supabase_client(supabase_config)
supabase.auth.sign_in_with_otp({
"phone": phone
})
@staticmethod
async def verify_passwordless(phone: str, token: str):
supabase = create_supabase_client(supabase_config)
response = supabase.auth.verify_otp({
"phone": phone,
"token": token,
"type": "sms"
})
print(response)
is_new_user = False
supabase = await async_supabase_client(supabase_config)
db_response = await supabase.table(Tables.AGENT_TABLE) \
.select('*') \
.eq('phone', phone) \
.execute()
if not db_response.data:
is_new_user = True
res = {'refresh_token': response.session.refresh_token, 'access_token': response.session.access_token,
'uuid': response.user.id}
return res, is_new_user
@staticmethod
async def register(email: str, password: str) -> str:
supabase = create_supabase_client(supabase_config)
response = supabase.auth.sign_up({
"email": email,
"password": password,
})
uuid = response.user.id
return uuid
@staticmethod
async def register_in_db(**kwargs):
data = kwargs
# data['wallet'] = WalletModel(amount=0)
# data['setup_completed'] = False
user: AgentModel = AgentModel(**data)
supabase = await async_supabase_client(supabase_config)
response = await supabase.table(Tables.AGENT_TABLE) \
.insert(user.to_json()) \
.execute()
return response.data
@staticmethod
async def verify(uuid: str) -> Optional[dict]:
supabase = await async_supabase_client(supabase_config)
response = await supabase.table(Tables.AGENT_TABLE) \
.select('*') \
.eq('uuid', uuid) \
.execute()
if response.data:
res = response.data[0]
res.pop('password_hash')
return res
else:
return None
@staticmethod
async def refresh_auth_state(refresh_token: str):
supabase = create_supabase_client(supabase_config)
response = supabase.auth.refresh_session(refresh_token)
res = {'refresh_token': response.session.refresh_token, 'access_token': response.session.access_token,
'uuid': response.user.id}
return res
@staticmethod
async def does_user_exists(email: Optional[str], phone: Optional[str]):
supabase = await async_supabase_client(supabase_config)
response = await supabase.table(Tables.AGENT_TABLE) \
.select('*') \
.eq('email' if email else 'phone', email if email else phone) \
.execute()
if bool(response.data):
return response.data[0], bool(response.data[0]['setup_completed'])
else:
return None, False
@staticmethod
async def setup_completed(uuid: str):
supabase = await async_supabase_client(supabase_config)
data, count = await supabase.table(Tables.AGENT_TABLE) \
.update({'setup_completed': True}) \
.eq('uuid', uuid) \
.execute()
@staticmethod
async def setup_brand(brand_request: UpdateBrandRequest, agent_uuid: str):
supabase = await async_supabase_client(supabase_config)
# Step 1: Create or get the brand in the "brands" table
brand_data = {
"brand_name": brand_request.brand_name,
"logo_url": brand_request.logo_url,
"proof_id_url": brand_request.proof_id_url,
}
db_response = await supabase.table(Tables.BRAND_TABLE) \
.select('*') \
.eq("brand_name", brand_request.brand_name) \
.execute()
if not db_response.data:
brand_response = await supabase.table(Tables.BRAND_TABLE) \
.insert(brand_data) \
.execute()
else:
brand_response = await supabase.table(Tables.BRAND_TABLE) \
.update(brand_data) \
.eq('brand_name', brand_request.brand_name) \
.execute()
brand_id = brand_response.data[0]['brand_id']
# Step 2: Add brand location in the "brand_locations" table
location_data = {
"brand_id": brand_id,
"location_lat": brand_request.lat,
"location_long": brand_request.long,
}
await supabase.table(Tables.BRAND_LOCATIONS_TABLE) \
.upsert(location_data) \
.execute()
# Step 3: Link brand_id with agent in the "agents" table
agent_data = {
"brand_id": brand_id,
}
await supabase.table(Tables.AGENT_TABLE) \
.update(agent_data) \
.eq('uuid', agent_uuid) \
.execute()
@staticmethod
async def update_wallet_amount(uuid: str, amount: float):
supabase = await async_supabase_client(supabase_config)
await supabase.table(Tables.AGENT_TABLE) \
.update({"wallet": WalletModel(amount=amount).to_json()}) \
.eq('uuid', uuid) \
.execute()
@staticmethod
async def fetch_categories() -> List[dict]:
return [
{"id": 1, "category": "General"},
{"id": 2, "category": "Restaurant Reservations"},
{"id": 3, "category": "Transportation"},
{"id": 4, "category": "Entertainment"},
{"id": 5, "category": "Tour Bookings"},
{"id": 6, "category": "Event Tickets"},
{"id": 7, "category": "Spa and Wellness"},
{"id": 8, "category": "Shopping Assistance"},
{"id": 9, "category": "Travel Planning"},
]
|