updated login/select

This commit is contained in:
2025-01-27 21:43:36 +03:00
parent b88f910a43
commit c0bd9c1685
18 changed files with 257 additions and 275 deletions

View File

@@ -1,4 +1,4 @@
from typing import Any
from typing import Any, Union
from fastapi import Request
from ApiLayers.ApiLibrary.common.line_number import get_line_number_for_error
@@ -30,7 +30,6 @@ class Handlers:
@classmethod # Requires no auth context
def handle_employee_selection(cls, request: Request, data: Any, token_dict: TokenDictType):
Users.set_user_define_properties(token=token_dict)
db = Users.new_session()
if data.company_uu_id not in token_dict.companies_uu_id_list:
raise HTTPExceptionApi(
@@ -40,8 +39,7 @@ class Handlers:
sys_msg="Company not found in token",
)
selected_company: Companies = Companies.filter_one(
Companies.uu_id == data.company_uu_id,
db=db,
Companies.uu_id == data.company_uu_id, db=db
).data
if not selected_company:
raise HTTPExceptionApi(
@@ -51,15 +49,6 @@ class Handlers:
sys_msg="Company not found in token",
)
# Get department IDs for the company
department_ids = [
dept.id
for dept in Departments.filter_all(
Departments.company_id == selected_company.id,
db=db,
).data
]
# Get duties IDs for the company
duties_ids = [
duty.id
@@ -77,8 +66,7 @@ class Handlers:
# Get employee
employee: Employees = Employees.filter_one(
Employees.people_id == token_dict.person_id,
Employees.staff_id.in_(staff_ids),
db=db,
Employees.staff_id.in_(staff_ids), db=db
).data
if not employee:
@@ -91,9 +79,6 @@ class Handlers:
# Get reachable events
reachable_event_codes = Event2Employee.get_event_codes(employee_id=employee.id)
reachable_event_endpoints = Event2Employee.get_event_endpoints(
employee_id=employee.id
)
# Get staff and duties
staff = Staff.filter_one(Staff.id == employee.staff_id, db=db).data
@@ -107,7 +92,6 @@ class Handlers:
bulk_duty_id = Duties.filter_by_one(
company_id=selected_company.id,
duties_id=bulk_id.id,
**Duties.valid_record_dict,
db=db,
).data
@@ -125,7 +109,6 @@ class Handlers:
employee_id=employee.id,
employee_uu_id=employee.uu_id.__str__(),
reachable_event_codes=reachable_event_codes,
reachable_event_endpoints=reachable_event_endpoints,
)
try: # Update Redis
@@ -143,7 +126,6 @@ class Handlers:
@classmethod # Requires no auth context
def handle_occupant_selection(cls, request: Request, data: Any, token_dict: TokenDictType):
"""Handle occupant type selection"""
Users.set_user_define_properties(token=token_dict)
db = BuildLivingSpace.new_session()
# Get selected occupant type
selected_build_living_space: BuildLivingSpace = BuildLivingSpace.filter_one(
@@ -162,9 +144,6 @@ class Handlers:
reachable_event_codes = Event2Occupant.get_event_codes(
build_living_space_id=selected_build_living_space.id
)
reachable_event_endpoints = Event2Occupant.get_event_endpoints(
build_living_space_id=selected_build_living_space.id
)
occupant_type = OccupantTypes.filter_one(
OccupantTypes.id == selected_build_living_space.occupant_type_id,
db=db,
@@ -208,7 +187,6 @@ class Handlers:
responsible_company_id=company_related.id,
responsible_company_uuid=company_related.uu_id.__str__(),
reachable_event_codes=reachable_event_codes,
reachable_event_endpoints=reachable_event_endpoints,
)
try: # Update Redis
@@ -227,25 +205,7 @@ class Handlers:
class AuthenticationFunctions:
"""Class for handling authentication functions"""
@classmethod # Requires auth context
def authentication_select_company_or_occupant_type(cls, data: Any):
"""Handle selection of company or occupant type"""
context_retriever = ContextRetrievers(func=cls.authentication_select_company_or_occupant_type)
if context_retriever.token.is_employee:
if Handlers.handle_employee_selection(
request=context_retriever.request, data=data, token_dict=context_retriever.token
):
return {
"completed": True, "selected": data.company_uu_id, **context_retriever.base,
}
elif context_retriever.token.is_occupant:
if Handlers.handle_occupant_selection(
request=context_retriever.request, data=data, token_dict=context_retriever.token
):
return {
"completed": True, "selected": data.build_living_space_uu_id, **context_retriever.base,
}
return {"completed": False, "selected": None, **context_retriever.base}
context_retriever: Union[ContextRetrievers] = None
@classmethod # Requires no auth context
def authentication_login_with_domain_and_creds(cls, request: Request, data: Any):
@@ -275,6 +235,32 @@ class AuthenticationFunctions:
code="LOGIN_SUCCESS", lang=user_login_module.language
).as_dict(data=user_login_module.as_dict)
@classmethod # Requires auth context
def authentication_select_company_or_occupant_type(cls, data: Any):
"""
Handle selection of company or occupant type
{"data": {"build_living_space_uu_id": ""}} | {"data": {"company_uu_id": ""}}
"""
if cls.context_retriever.token.is_employee:
if Handlers.handle_employee_selection(
request=cls.context_retriever.request, data=data, token_dict=cls.context_retriever.token
):
return EndpointSuccessResponse(
code="LOGIN_SELECT", lang=cls.context_retriever.token.lang
).as_dict(data={
"selected": data.company_uu_id, **cls.context_retriever.base
})
elif cls.context_retriever.token.is_occupant:
if Handlers.handle_occupant_selection(
request=cls.context_retriever.request, data=data, token_dict=cls.context_retriever.token
):
return EndpointSuccessResponse(
code="LOGIN_SELECT", lang=cls.context_retriever.token.lang
).as_dict(data={
"selected": data.build_living_space_uu_id, **cls.context_retriever.base
})
return {"completed": False, "selected": None, **cls.context_retriever.base}
@classmethod # Requires not auth context
def authentication_check_token_is_valid(cls, data: Any):
"""Check if token is valid for user"""