client frontend added

This commit is contained in:
2025-05-19 23:12:23 +03:00
parent 5f7cb35ccc
commit fdf9d2edb8
507 changed files with 40655 additions and 510 deletions

View File

@@ -68,8 +68,14 @@ class LoginHandler:
return str(email).split("@")[1] == api_config.ACCESS_EMAIL_EXT
@classmethod
# headers: CommonHeaders
def do_employee_login(cls, headers: CommonHeaders, data: Any, db_session):
"""Handle employee login."""
language = headers.request.headers.get("language", "tr")
domain = headers.request.headers.get("domain", None)
timezone = headers.request.headers.get("tz", None) or "GMT+3"
user_handler, other_domains_list, main_domain = UserHandlers(), [], ""
found_user = user_handler.check_user_exists(access_key=data.access_key, db_session=db_session)
with mongo_handler.collection(f"{str(found_user.related_company)}*Domain") as collection:
@@ -78,7 +84,7 @@ class LoginHandler:
raise ValueError("EYS_00087")
other_domains_list = result.get("other_domains_list", [])
main_domain = result.get("main_domain", None)
if headers.domain not in other_domains_list or not main_domain:
if domain not in other_domains_list or not main_domain:
raise ValueError("EYS_00088")
if not user_handler.check_password_valid(domain=main_domain, id_=str(found_user.uu_id), password=data.password, password_hashed=found_user.hash_password):
@@ -115,24 +121,24 @@ class LoginHandler:
duty_uu_id_list.append(str(list_employee["Duty.uu_id"]))
duty_id_list.append(int(list_employee["Duty.id"]))
companies_list.append({
"uu_id": str(list_employee["Companies.uu_id"]), "public_name": list_employee["Companies.public_name"],
"company_type": list_employee["Companies.company_type"], "company_address": list_employee["Addresses.letter_address"],
"duty": list_employee["Duty.duty_name"]
"uu_id": str(list_employee["Employees.uu_id"]),
"public_name": list_employee["Companies.public_name"],
"company_type": list_employee["Companies.company_type"],
"company_address": list_employee["Addresses.letter_address"],
"duty": list_employee["Duty.duty_name"],
"company_uuid": str(list_employee["Companies.uu_id"])
})
model_value = EmployeeTokenObject(
user_type=UserType.employee.value,
user_uu_id=str(found_user.uu_id),
user_id=found_user.id,
person_id=found_user.person_id,
person_uu_id=str(list_employees[0]["People.uu_id"]),
request=dict(headers.request.headers),
domain_list=other_domains_list,
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,
user_type=UserType.employee.value, user_uu_id=str(found_user.uu_id), user_id=found_user.id, person_id=found_user.person_id,
person_uu_id=str(list_employees[0]["People.uu_id"]), request=dict(headers.request.headers), domain_list=other_domains_list,
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,
).model_dump()
set_to_redis_dict = dict(user=found_user, token=model_value, header_info=dict(language=headers.language, domain=headers.domain, timezone=headers.timezone))
employee_uuid = str(companies_list[0]["uu_id"])
print('employee_uuid', employee_uuid)
set_to_redis_dict = dict(
user=found_user, token=model_value, add_uuid=employee_uuid,
header_info=dict(language=headers.language, domain=headers.domain, timezone=headers.timezone),
)
user_dict = found_user.get_dict()
person_dict = found_user.person.get_dict()
if access_token := RedisHandlers().set_object_to_redis(**set_to_redis_dict):
@@ -140,21 +146,12 @@ class LoginHandler:
"access_token": access_token,
"user_type": UserType.employee.name,
"user": {
"uuid": user_dict["uu_id"],
"avatar": user_dict["avatar"],
"email": user_dict["email"],
"phone_number": user_dict["phone_number"],
"user_tag": user_dict["user_tag"],
"uuid": user_dict["uu_id"], "avatar": user_dict["avatar"], "email": user_dict["email"], "phone_number": user_dict["phone_number"], "user_tag": user_dict["user_tag"],
"password_expiry_begins": str(arrow.get(user_dict["password_expiry_begins"]).shift(days=int(user_dict["password_expires_day"]))),
"person": {
"uuid": person_dict["uu_id"],
"firstname": person_dict["firstname"],
"surname": person_dict["surname"],
"middle_name": person_dict["middle_name"],
"sex_code": person_dict["sex_code"],
"person_tag": person_dict["person_tag"],
"country_code": person_dict["country_code"],
"birth_date": person_dict["birth_date"],
"uuid": person_dict["uu_id"], "firstname": person_dict["firstname"], "surname": person_dict["surname"],
"middle_name": person_dict["middle_name"], "sex_code": person_dict["sex_code"], "person_tag": person_dict["person_tag"],
"country_code": person_dict["country_code"], "birth_date": person_dict["birth_date"],
},
},
"selection_list": companies_list,
@@ -162,22 +159,21 @@ class LoginHandler:
raise ValueError("Something went wrong")
@classmethod
def do_occupant_login(cls, request: Any, data: Any, db_session, extra_dict: Optional[Dict[str, Any]] = None):
# headers=headers, data=data, db_session=db_session
def do_occupant_login(cls, headers: CommonHeaders, data: Any, db_session):
"""
Handle occupant login.
"""
language = extra_dict.get("language", "tr")
domain = extra_dict.get("domain", None)
timezone = extra_dict.get("tz", None) or "GMT+3"
language = headers.request.headers.get("language", "tr")
domain = headers.request.headers.get("domain", None)
timezone = headers.request.headers.get("tz", None) or "GMT+3"
BuildParts.set_session(db_session)
OccupantTypes.set_session(db_session)
user_handler = UserHandlers()
found_user = user_handler.check_user_exists(
access_key=data.access_key, db_session=db_session
)
found_user = user_handler.check_user_exists(access_key=data.access_key, db_session=db_session)
other_domains_list, main_domain = [], ""
with mongo_handler.collection(
f"{str(found_user.related_company)}*Domain"
) as collection:
with mongo_handler.collection(f"{str(found_user.related_company)}*Domain") as collection:
result = collection.find_one({"user_uu_id": str(found_user.uu_id)})
if not result:
raise ValueError("EYS_00087")
@@ -186,78 +182,43 @@ class LoginHandler:
if domain not in other_domains_list or not main_domain:
raise ValueError("EYS_00088")
if not user_handler.check_password_valid(
domain=main_domain,
id_=str(found_user.uu_id),
password=data.password,
password_hashed=found_user.hash_password,
):
if not user_handler.check_password_valid(domain=main_domain, id_=str(found_user.uu_id), password=data.password, password_hashed=found_user.hash_password):
raise ValueError("EYS_0005")
occupants_selection_dict: Dict[str, Any] = {}
living_spaces: list[BuildLivingSpace] = BuildLivingSpace.filter_all(
BuildLivingSpace.person_id == found_user.person_id, db=db_session
).data
living_spaces: list[BuildLivingSpace] = BuildLivingSpace.query.filter(BuildLivingSpace.person_id == found_user.person_id).all()
if not living_spaces:
raise ValueError("EYS_0006")
for living_space in living_spaces:
build_part = BuildParts.filter_one(
BuildParts.id == living_space.build_parts_id,
db=db_session,
).data
build_part = BuildParts.query.filter(BuildParts.id == living_space.build_parts_id).first()
if not build_part:
raise ValueError("EYS_0007")
build = build_part.buildings
occupant_type = OccupantTypes.filter_by_one(
id=living_space.occupant_type_id,
db=db_session,
system=True,
).data
occupant_type = OccupantTypes.query.filter(OccupantTypes.id == living_space.occupant_type_id).first()
occupant_data = {
"build_living_space_uu_id": str(living_space.uu_id),
"part_uu_id": str(build_part.uu_id),
"part_name": build_part.part_name(db=db_session),
"part_level": build_part.part_level,
"occupant_uu_id": str(occupant_type.uu_id),
"description": occupant_type.occupant_description,
"code": occupant_type.occupant_code,
"build_living_space_uu_id": str(living_space.uu_id), "part_uu_id": str(build_part.uu_id), "part_name": build_part.part_name(), "part_level": build_part.part_level,
"occupant_uu_id": str(occupant_type.uu_id), "description": occupant_type.occupant_description, "code": occupant_type.occupant_code,
}
build_key = str(build.uu_id)
if build_key not in occupants_selection_dict:
occupants_selection_dict[build_key] = {
"build_uu_id": build_key,
"build_name": build.build_name,
"build_no": build.build_no,
"occupants": [occupant_data],
}
occupants_selection_dict[build_key] = {"build_uu_id": build_key, "build_name": build.build_name, "build_no": build.build_no, "occupants": [occupant_data],}
else:
occupants_selection_dict[build_key]["occupants"].append(occupant_data)
person = found_user.person
model_value = OccupantTokenObject(
user_type=UserType.occupant.value,
user_uu_id=str(found_user.uu_id),
user_id=found_user.id,
person_id=person.id,
person_uu_id=str(person.uu_id),
domain_list=other_domains_list,
request=dict(request.headers),
available_occupants=occupants_selection_dict,
user_type=UserType.occupant.value, user_uu_id=str(found_user.uu_id), user_id=found_user.id, person_id=person.id,
person_uu_id=str(person.uu_id), domain_list=other_domains_list, request=dict(request.headers), available_occupants=occupants_selection_dict,
).model_dump()
redis_handler = RedisHandlers()
if access_token := redis_handler.set_object_to_redis(
user=found_user,
token=model_value,
header_info=dict(language=language, domain=domain, timezone=timezone),
user=found_user, token=model_value, add_uuid=living_space.uu_id,
header_info=dict(language=language, domain=domain, timezone=timezone)
):
return {
"access_token": access_token,
"user_type": UserType.occupant.name,
"selection_list": occupants_selection_dict,
}
return {"access_token": access_token, "user_type": UserType.occupant.name, "selection_list": occupants_selection_dict}
raise ValueError("Something went wrong")
@classmethod
@@ -302,7 +263,7 @@ class LoginHandler:
@classmethod
def handle_employee_selection(cls, access_token: str, data: Any, token_dict: TokenDictType):
with Users.new_session() as db_session:
if data.company_uu_id not in token_dict.companies_uu_id_list:
if data.uuid not in token_dict.companies_uu_id_list:
ValueError("EYS_0011")
list_of_returns = (
Employees.id, Employees.uu_id, People.id, People.uu_id, Users.id, Users.uu_id, Companies.id, Companies.uu_id,
@@ -319,7 +280,7 @@ class LoginHandler:
).join(Companies, Companies.id == Departments.company_id
).join(Users, Users.person_id == People.id
).outerjoin(Addresses, Addresses.id == Companies.official_address_id
).filter(Companies.uu_id == data.company_uu_id, Users.id == token_dict.user_id)
).filter(Employees.uu_id == data.uuid, Users.id == token_dict.user_id)
selected_company_first = selected_company_query.first()
if not selected_company_first:
@@ -357,8 +318,10 @@ class LoginHandler:
reachable_app_codes=reachable_app_codes,
)
redis_handler = RedisHandlers()
redis_result = redis_handler.update_token_at_redis(token=access_token, add_payload=company_token)
return {"selected_uu_id": data.company_uu_id}
redis_result = redis_handler.update_token_at_redis(
token=access_token, add_payload=company_token, add_uuid=str(result_with_keys_dict['Employees.uu_id'])
)
return {"selected_uu_id": data.uuid}
@classmethod
def handle_occupant_selection(cls, access_token: str, data: Any, token_dict: TokenDictType):
@@ -408,7 +371,9 @@ class LoginHandler:
reachable_app_codes=reachable_app_codes,
)
redis_handler = RedisHandlers()
redis_handler.update_token_at_redis(token=access_token, add_payload=occupant_token)
redis_handler.update_token_at_redis(
token=access_token, add_payload=occupant_token, add_uuid=occupant_token.living_space_uu_id
)
return {"selected_uu_id": occupant_token.living_space_uu_id}
@classmethod # Requires auth context