updated Query options
This commit is contained in:
@@ -9,7 +9,7 @@ from ApiLayers.ApiLibrary.common.line_number import get_line_number_for_error
|
||||
from ApiLayers.ApiLibrary.date_time_actions.date_functions import DateTimeLocal
|
||||
from ApiLayers.ApiLibrary.token.password_module import PasswordModule
|
||||
from ApiLayers.ErrorHandlers import HTTPExceptionApi
|
||||
from ApiLayers.Schemas.identity.identity import UsersTokens, People
|
||||
|
||||
from ApiLayers.ApiValidations.Custom.token_objects import (
|
||||
EmployeeTokenObject,
|
||||
OccupantTokenObject,
|
||||
@@ -37,6 +37,7 @@ from Services.Redis import RedisActions, AccessToken
|
||||
if TYPE_CHECKING:
|
||||
from fastapi import Request
|
||||
|
||||
|
||||
T = TypeVar("T", EmployeeTokenObject, OccupantTokenObject)
|
||||
|
||||
|
||||
@@ -55,6 +56,85 @@ class TokenService:
|
||||
"""Get all tokens for a user from Redis."""
|
||||
return RedisActions.get_json(list_keys=[f"*:{str(user.uu_id)}"])
|
||||
|
||||
@classmethod
|
||||
def do_employee_login(
|
||||
cls, request: "Request", user: Users, domain: str
|
||||
) -> Dict[str, Any]:
|
||||
"""Handle employee login process and return login information."""
|
||||
from ApiLayers.Schemas.identity.identity import UsersTokens, People
|
||||
db_session = Employees.new_session()
|
||||
list_employee = Employees.filter_all(
|
||||
Employees.people_id == user.person_id, db=db_session
|
||||
).data
|
||||
|
||||
companies_uu_id_list: List[str] = []
|
||||
companies_id_list: List[int] = []
|
||||
companies_list: List[Dict[str, Any]] = []
|
||||
duty_uu_id_list: List[str] = []
|
||||
duty_id_list: List[int] = []
|
||||
|
||||
for employee in list_employee:
|
||||
staff = Staff.filter_one(Staff.id == employee.staff_id, db=db_session).data
|
||||
if duties := Duties.filter_one(
|
||||
Duties.id == staff.duties_id, db=db_session
|
||||
).data:
|
||||
if duty_found := Duty.filter_by_one(
|
||||
id=duties.duties_id, db=db_session
|
||||
).data:
|
||||
duty_uu_id_list.append(str(duty_found.uu_id))
|
||||
duty_id_list.append(duty_found.id)
|
||||
|
||||
department = Departments.filter_one(
|
||||
Departments.id == duties.department_id, db=db_session
|
||||
).data
|
||||
|
||||
if company := Companies.filter_one(
|
||||
Companies.id == department.company_id, db=db_session
|
||||
).data:
|
||||
companies_uu_id_list.append(str(company.uu_id))
|
||||
companies_id_list.append(company.id)
|
||||
company_address = Addresses.filter_by_one(
|
||||
id=company.official_address_id, db=db_session
|
||||
).data
|
||||
companies_list.append(
|
||||
{
|
||||
"uu_id": str(company.uu_id),
|
||||
"public_name": company.public_name,
|
||||
"company_type": company.company_type,
|
||||
"company_address": company_address,
|
||||
}
|
||||
)
|
||||
person = People.filter_one(People.id == user.person_id, db=db_session).data
|
||||
model_value = EmployeeTokenObject(
|
||||
domain=domain,
|
||||
user_type=UserType.employee.value,
|
||||
user_uu_id=str(user.uu_id),
|
||||
credentials=user.credentials(),
|
||||
user_id=user.id,
|
||||
person_id=person.id,
|
||||
person_uu_id=str(person.uu_id),
|
||||
full_name=person.full_name,
|
||||
request=dict(request.headers),
|
||||
companies_uu_id_list=companies_uu_id_list,
|
||||
companies_id_list=companies_id_list,
|
||||
duty_uu_id_list=duty_uu_id_list,
|
||||
duty_id_list=duty_id_list,
|
||||
timezone=user.local_timezone or "GMT+0",
|
||||
lang="tr",
|
||||
).model_dump()
|
||||
if access_token := cls.set_object_to_redis(user, model_value):
|
||||
return {
|
||||
"access_token": access_token,
|
||||
"user_type": UserType.employee.name,
|
||||
"selection_list": companies_list,
|
||||
}
|
||||
raise HTTPExceptionApi(
|
||||
error_code="",
|
||||
lang="en",
|
||||
loc=get_line_number_for_error(),
|
||||
sys_msg="Creating Token failed...",
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def do_occupant_login(
|
||||
cls, request: "Request", user: Users, domain: str
|
||||
@@ -113,14 +193,17 @@ class TokenService:
|
||||
}
|
||||
else:
|
||||
occupants_selection_dict[build_key]["occupants"].append(occupant_data)
|
||||
|
||||
person = user.person
|
||||
model_value = OccupantTokenObject(
|
||||
domain=domain,
|
||||
user_type=UserType.occupant.value,
|
||||
user_uu_id=str(user.uu_id),
|
||||
credentials=user.credentials(),
|
||||
user_id=user.id,
|
||||
person_id=user.person_id,
|
||||
person_uu_id=str(user.person.uu_id),
|
||||
person_id=person.id,
|
||||
person_uu_id=str(person.uu_id),
|
||||
full_name=person.full_name,
|
||||
request=dict(request.headers),
|
||||
available_occupants=occupants_selection_dict,
|
||||
timezone=user.local_timezone or "GMT+0",
|
||||
@@ -180,84 +263,6 @@ class TokenService:
|
||||
sys_msg="Saving Token failed...",
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def do_employee_login(
|
||||
cls, request: "Request", user: Users, domain: str
|
||||
) -> Dict[str, Any]:
|
||||
"""Handle employee login process and return login information."""
|
||||
db_session = Employees.new_session()
|
||||
list_employee = Employees.filter_all(
|
||||
Employees.people_id == user.person_id, db=db_session
|
||||
).data
|
||||
|
||||
companies_uu_id_list: List[str] = []
|
||||
companies_id_list: List[int] = []
|
||||
companies_list: List[Dict[str, Any]] = []
|
||||
duty_uu_id_list: List[str] = []
|
||||
duty_id_list: List[int] = []
|
||||
|
||||
for employee in list_employee:
|
||||
staff = Staff.filter_one(Staff.id == employee.staff_id, db=db_session).data
|
||||
if duties := Duties.filter_one(
|
||||
Duties.id == staff.duties_id, db=db_session
|
||||
).data:
|
||||
if duty_found := Duty.filter_by_one(
|
||||
id=duties.duties_id, db=db_session
|
||||
).data:
|
||||
duty_uu_id_list.append(str(duty_found.uu_id))
|
||||
duty_id_list.append(duty_found.id)
|
||||
|
||||
department = Departments.filter_one(
|
||||
Departments.id == duties.department_id, db=db_session
|
||||
).data
|
||||
|
||||
if company := Companies.filter_one(
|
||||
Companies.id == department.company_id, db=db_session
|
||||
).data:
|
||||
companies_uu_id_list.append(str(company.uu_id))
|
||||
companies_id_list.append(company.id)
|
||||
company_address = Addresses.filter_by_one(
|
||||
id=company.official_address_id, db=db_session
|
||||
).data
|
||||
companies_list.append(
|
||||
{
|
||||
"uu_id": str(company.uu_id),
|
||||
"public_name": company.public_name,
|
||||
"company_type": company.company_type,
|
||||
"company_address": company_address,
|
||||
}
|
||||
)
|
||||
person = People.filter_one(People.id == user.person_id, db=db_session).data
|
||||
|
||||
model_value = EmployeeTokenObject(
|
||||
domain=domain,
|
||||
user_type=UserType.employee.value,
|
||||
user_uu_id=str(user.uu_id),
|
||||
credentials=user.credentials(),
|
||||
user_id=user.id,
|
||||
person_id=person.id,
|
||||
person_uu_id=str(person.uu_id),
|
||||
request=dict(request.headers),
|
||||
companies_uu_id_list=companies_uu_id_list,
|
||||
companies_id_list=companies_id_list,
|
||||
duty_uu_id_list=duty_uu_id_list,
|
||||
duty_id_list=duty_id_list,
|
||||
timezone=user.local_timezone or "GMT+0",
|
||||
lang="tr",
|
||||
).model_dump()
|
||||
if access_token := cls.set_object_to_redis(user, model_value):
|
||||
return {
|
||||
"access_token": access_token,
|
||||
"user_type": UserType.employee.name,
|
||||
"selection_list": companies_list,
|
||||
}
|
||||
raise HTTPExceptionApi(
|
||||
error_code="",
|
||||
lang="en",
|
||||
loc=get_line_number_for_error(),
|
||||
sys_msg="Creating Token failed...",
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def remove_token_with_domain(cls, user: Users, domain: str) -> None:
|
||||
"""Remove all tokens for a user with specific domain."""
|
||||
@@ -282,6 +287,8 @@ class TokenService:
|
||||
) -> Dict[str, Any]:
|
||||
"""Set access token to redis and handle user session."""
|
||||
from ApiLayers.AllConfigs.Token.config import Auth
|
||||
from ApiLayers.Schemas.identity.identity import UsersTokens, People
|
||||
|
||||
cls.remove_token_with_domain(user=user, domain=domain)
|
||||
# Users.client_arrow = DateTimeLocal(is_client=True, timezone=user.local_timezone)
|
||||
login_dict, db_session = {}, UsersTokens.new_session()
|
||||
@@ -309,9 +316,13 @@ class TokenService:
|
||||
users_token.token = users_token_created
|
||||
users_token.save(db=db_session)
|
||||
else:
|
||||
if arrow.now() > arrow.get(str(users_token.expires_at)): # Check if token is expired
|
||||
if arrow.now() > arrow.get(
|
||||
str(users_token.expires_at)
|
||||
): # Check if token is expired
|
||||
users_token.token = users_token_created
|
||||
users_token.expires_at = str(arrow.now().datetime + Auth.TOKEN_EXPIRE_DAY_1)
|
||||
users_token.expires_at = str(
|
||||
arrow.now().datetime + Auth.TOKEN_EXPIRE_DAY_1
|
||||
)
|
||||
users_token.save(db=db_session)
|
||||
else:
|
||||
login_dict["refresh_token"] = users_token.token
|
||||
|
||||
Reference in New Issue
Block a user