user selection compoleted tested

This commit is contained in:
2025-06-15 19:05:48 +03:00
parent a48e560ece
commit 9fb517fa15
24 changed files with 574 additions and 313 deletions

View File

@@ -6,11 +6,13 @@ from .supers_events import (
SuperAccountRecordsUpdateEvent,
SuperAccountRecordsDeleteEvent,
)
from .flat_representative import FlatRepresentativeAccountRecordsListEvent
AccountRecordsRouterCluster = RouterCluster(name="AccountRecordsRouterCluster")
AccountRecordsListEventCluster = EventCluster(name="AccountRecordsListEventCluster", endpoint_uu_id=endpoints_index["AccountRecordsList"])
AccountRecordsListEventCluster.add_event(SuperAccountRecordsListEvent)
AccountRecordsListEventCluster.add_event(FlatRepresentativeAccountRecordsListEvent)
AccountRecordsCreateEventCluster = EventCluster(name="AccountRecordsCreateEventCluster", endpoint_uu_id=endpoints_index["AccountRecordsCreate"])
AccountRecordsCreateEventCluster.add_event(SuperAccountRecordsCreateEvent)

View File

@@ -0,0 +1,60 @@
from typing import Any
from Initializer.event_clusters import Event
from Validations.response import (
PaginateOnly,
Pagination,
PaginationResult,
PostgresResponseSingle,
PostgresResponse,
EndpointResponse
)
from Validations.defaults.validations import CommonHeaders
from Schemas import AccountRecords
from Extensions.Middlewares.token_provider import TokenProvider
# List all account records endpoint Flat Representative
FlatRepresentativeAccountRecordsListEvent = Event(
name="flat_representative_account_records_list",
key="7cb248d3-d75a-4d28-8be5-f147089ce654",
request_validator=None, # TODO: Add request validator
response_validator=None, # TODO: Add response validator
description="Flat Representative Account Records List all flat representative users endpoint",
)
class FlatRepresentativeAccountRecordsListPydantic:
uu_id: str
bank_date: datetime.datetime
currency_value: float
process_name: str
def flat_representative_account_records_list_callable(list_options: PaginateOnly, header: CommonHeaders):
list_options = PaginateOnly(**list_options.model_dump())
token_object = TokenProvider.get_dict_from_redis(token=header.token)
if not token_object.is_occupant:
raise Exception("Forbidden for employees")
pagination_query = {}
if list_options.query:
pagination_query = FlatRepresentativeAccountRecordsListPydantic(**list_options.query)
with AccountRecords.new_session() as db_session:
AccountRecords.set_session(db_session)
if pagination_query:
account_records_list = AccountRecords.query.filter(
*AccountRecords.convert(pagination_query),
AccountRecords.build_parts_id == token_object.selected_occupant.build_id
)
else:
account_records_list = AccountRecords.query.filter(
AccountRecords.build_parts_id == token_object.selected_occupant.build_id
)
pagination = Pagination(data=account_records_list)
pagination.change(**list_options.model_dump())
pagination_result = PaginationResult(data=account_records_list, pagination=pagination, response_model=FlatRepresentativeAccountRecordsListPydantic)
return EndpointResponse(message="MSG0003-LIST", pagination_result=pagination_result).response
FlatRepresentativeAccountRecordsListEvent.event_callable = flat_representative_account_records_list_callable

View File

@@ -7,8 +7,7 @@ from config import api_config
from validations.request.auth.validations import (
RequestLogin,
RequestResetPassword,
RequestSelectLiving,
RequestSelectEmployee,
RequestSelect,
RequestCreatePassword,
RequestChangePassword,
RequestForgotPasswordPhone,
@@ -44,7 +43,7 @@ auth_route_select_living = "AuthSelectLiving"
description="Selection of users company or occupant type",
operation_id=endpoints_index[auth_route_select_living]
)
def select_living(data: Union[RequestSelectLiving, RequestSelectEmployee], headers: CommonHeaders = Depends(CommonHeaders.as_dependency)):
def select_living(data: Union[RequestSelect], headers: CommonHeaders = Depends(CommonHeaders.as_dependency)):
"""Select token object company or occupant type"""
token_object = TokenProvider.get_dict_from_redis(token=headers.token)
return AuthHandlers.LoginHandler.authentication_select_company_or_occupant_type(request=headers.request, data=data)

View File

@@ -5,8 +5,9 @@ from config import api_config
from Schemas import (
Users,
People,
BuildLivingSpace,
Build,
BuildParts,
BuildLivingSpace,
OccupantTypes,
Employees,
Addresses,
@@ -78,6 +79,8 @@ class LoginHandler:
user_handler, other_domains_list, main_domain = UserHandlers(), [], ""
found_user = user_handler.check_user_exists(access_key=data.access_key, db_session=db_session)
redis_handler = RedisHandlers()
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:
@@ -133,15 +136,13 @@ class LoginHandler:
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()
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,
user=found_user, token=model_value,
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):
if access_token := redis_handler.set_object_to_redis(**set_to_redis_dict):
return {
"access_token": access_token,
"user_type": UserType.employee.name,
@@ -166,12 +167,15 @@ class LoginHandler:
language = headers.request.headers.get("language", "tr")
domain = headers.request.headers.get("domain", None)
timezone = headers.request.headers.get("tz", None) or "GMT+3"
BuildLivingSpace.set_session(db_session)
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)
other_domains_list, main_domain = [], ""
redis_handler = RedisHandlers()
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:
@@ -189,6 +193,10 @@ class LoginHandler:
if not living_spaces:
raise ValueError("EYS_0006")
user_dict = found_user.get_dict()
person_dict = found_user.person.get_dict()
for living_space in living_spaces:
build_part = BuildParts.query.filter(BuildParts.id == living_space.build_parts_id).first()
if not build_part:
@@ -197,28 +205,40 @@ class LoginHandler:
build = build_part.buildings
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(), "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(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_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,
person_uu_id=str(person.uu_id), domain_list=other_domains_list, request=dict(headers.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, 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}
raise ValueError("Something went wrong")
set_redis_dict = dict(user=found_user, token=model_value, header_info=dict(language=language, domain=domain, timezone=timezone))
if access_token := redis_handler.set_object_to_redis(**set_redis_dict):
return {
"access_token": access_token,
"user_type": UserType.occupant.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"],
"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"],
},
},
"selection_list": occupants_selection_dict,
}
raise ValueError("Something went wrong")
@classmethod
def authentication_login_with_domain_and_creds(cls, headers: CommonHeaders, data: Any):
@@ -264,10 +284,12 @@ class LoginHandler:
with Users.new_session() as db_session:
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,
Departments.id, Departments.uu_id, Duty.id, Duty.uu_id, Addresses.id, Addresses.letter_address, Staff.id, Staff.uu_id,
Duties.id, Duties.uu_id,
Employees.id, Employees.uu_id, People.id, People.uu_id, Users.id, Users.uu_id, Companies.id, Companies.uu_id, Companies.public_name, Companies.company_type,
Departments.id, Departments.uu_id, Duty.id, Duty.uu_id, Addresses.id, Addresses.letter_address, Staff.id, Staff.uu_id, Duty.duty_name,
Duties.id, Duties.uu_id, BuildParts.id, BuildParts.uu_id, BuildParts.part_name, BuildParts.part_level, BuildParts.part_code,
OccupantTypes.id, OccupantTypes.uu_id, OccupantTypes.occupant_type, OccupantTypes.occupant_description, Addresses.letter_address
)
selected_company_query = db_session.query(*list_of_returns
@@ -298,82 +320,76 @@ class LoginHandler:
filter_endpoints_and_events = db_session.query(EndpointRestriction.operation_uu_id, Events.function_code
).join(EndpointRestriction, EndpointRestriction.id == Events.endpoint_id).filter().all()
reachable_event_codes = {endpoint_name: function_code for endpoint_name, function_code in filter_endpoints_and_events}
# Get reachable applications
reachable_app_codes = Application2Employee.get_application_codes(employee_id=int(result_with_keys_dict['Employees.id']), db=db_session)
company_token = CompanyToken(
company_uu_id=str(result_with_keys_dict['Companies.uu_id']),
company_id=int(result_with_keys_dict['Companies.id']),
department_id=int(result_with_keys_dict['Departments.id']),
department_uu_id=str(result_with_keys_dict['Departments.uu_id']),
duty_id=int(result_with_keys_dict['Duty.id']),
duty_uu_id=str(result_with_keys_dict['Duty.uu_id']),
bulk_duties_id=int(result_with_keys_dict['Duties.id']),
staff_id=int(result_with_keys_dict['Staff.id']),
staff_uu_id=str(result_with_keys_dict['Staff.uu_id']),
employee_id=int(result_with_keys_dict['Employees.id']),
employee_uu_id=str(result_with_keys_dict['Employees.uu_id']),
reachable_event_codes=reachable_event_codes,
reachable_app_codes=reachable_app_codes,
)
add_company = {
"uu_id": str(result_with_keys_dict["Employees.uu_id"]),
"public_name": result_with_keys_dict["Companies.public_name"],
"company_type": result_with_keys_dict["Companies.company_type"],
"company_address": result_with_keys_dict["Addresses.letter_address"],
"duty": result_with_keys_dict["Duty.duty_name"],
"company_uuid": str(result_with_keys_dict["Companies.uu_id"])
}
redis_handler = RedisHandlers()
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'])
)
user_uu_id = Users.query.filter(Users.person_id == result_with_keys_dict['People.id']).first().uu_id
redis_result = redis_handler.update_token_at_redis(token=access_token, add_payload=add_company, user_uuid=user_uu_id)
return {"selected_uu_id": data.uuid}
@classmethod
def handle_occupant_selection(cls, access_token: str, data: Any, token_dict: TokenDictType):
"""Handle occupant type selection"""
with BuildLivingSpace.new_session() as db:
# Get selected occupant type
selected_build_living_space: BuildLivingSpace = BuildLivingSpace.filter_one(BuildLivingSpace.uu_id == data.build_living_space_uu_id, db=db).data
if not selected_build_living_space:
BuildLivingSpace.set_session(db)
BuildParts.set_session(db)
Users.set_session(db)
list_of_returns = (
BuildLivingSpace.id, BuildLivingSpace.uu_id, OccupantTypes.id, OccupantTypes.uu_id, OccupantTypes.occupant_type, BuildParts.id, BuildParts.uu_id, Build.id, Build.uu_id,
BuildParts.id, BuildParts.part_level, OccupantTypes.occupant_description, OccupantTypes.occupant_code, People.id, People.uu_id
)
selected_build_living_space_query = db.query(*list_of_returns
).join(OccupantTypes, OccupantTypes.id == BuildLivingSpace.occupant_type_id
).join(BuildParts, BuildParts.id == BuildLivingSpace.build_parts_id
).join(Build, Build.id == BuildParts.build_id
).join(People, People.id == BuildLivingSpace.person_id
).filter(BuildLivingSpace.uu_id == data.uuid)
selected_build_living_space_first = selected_build_living_space_query.first()
if not selected_build_living_space_first:
raise ValueError("EYS_0012")
result_with_keys_dict = {}
for ix, selected_build_living_space_item in enumerate(selected_build_living_space_first):
result_with_keys_dict[str(list_of_returns[ix])] = selected_build_living_space_item
# Get reachable events
reachable_event_codes = Event2Occupant.get_event_codes(build_living_space_id=selected_build_living_space.id, db=db)
occupant_type = OccupantTypes.filter_one_system(OccupantTypes.id == selected_build_living_space.occupant_type_id, db=db).data
build_part = BuildParts.filter_one(BuildParts.id == selected_build_living_space.build_parts_id, db=db)
build = build_part.buildings
reachable_app_codes = Application2Occupant.get_application_codes(build_living_space_id=selected_build_living_space.id, db=db)
# responsible_employee = Employees.filter_one(
# Employees.id == build_part.responsible_employee_id,
# db=db,
# ).data
# related_company = RelationshipEmployee2Build.filter_one(
# RelationshipEmployee2Build.member_id == build.id,
# db=db,
# ).data
# Get company
# company_related = Companies.filter_one(
# Companies.id == related_company.company_id,
# db=db,
# ).data
filter_endpoints_and_events = db.query(EndpointRestriction.operation_uu_id, Events.function_code
).join(EndpointRestriction, EndpointRestriction.id == Events.endpoint_id).filter().all()
reachable_event_codes = {endpoint_name: function_code for endpoint_name, function_code in filter_endpoints_and_events}
# Create occupant token
occupant_token = OccupantToken(
living_space_id=selected_build_living_space.id,
living_space_uu_id=selected_build_living_space.uu_id.__str__(),
occupant_type_id=occupant_type.id,
occupant_type_uu_id=occupant_type.uu_id.__str__(),
occupant_type=occupant_type.occupant_type,
build_id=build.id,
build_uuid=build.uu_id.__str__(),
build_part_id=build_part.id,
build_part_uuid=build_part.uu_id.__str__(),
# responsible_employee_id=responsible_employee.id,
# responsible_employee_uuid=responsible_employee.uu_id.__str__(),
# responsible_company_id=company_related.id,
# responsible_company_uuid=company_related.uu_id.__str__(),
reachable_event_codes=reachable_event_codes,
reachable_app_codes=reachable_app_codes,
)
# Get reachable applications
reachable_app_codes = Application2Occupant.get_application_codes(build_living_space_id=selected_build_living_space_first.id, db=db)
build_part = BuildParts.query.filter(BuildParts.id == result_with_keys_dict['BuildParts.id']).first()
if not build_part:
raise ValueError("EYS_0013")
add_build_living_space = {
"build_living_space_uu_id": str(data.uuid),
"part_uu_id": str(result_with_keys_dict['BuildParts.uu_id']),
"part_name": build_part.part_name(db=db),
"part_level": result_with_keys_dict['BuildParts.part_level'],
"occupant_uu_id": str(result_with_keys_dict['OccupantTypes.uu_id']),
"description": result_with_keys_dict['OccupantTypes.occupant_description'],
"code": result_with_keys_dict['OccupantTypes.occupant_code']
}
redis_handler = RedisHandlers()
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}
user_uu_id = Users.query.filter(Users.person_id == result_with_keys_dict['People.id']).first().uu_id
redis_handler.update_token_at_redis(token=access_token, add_payload=add_build_living_space, user_uuid=user_uu_id)
return {"selected_uu_id": data.uuid}
@classmethod
def authentication_select_company_or_occupant_type(cls, request: Any, data: Any):
@@ -469,3 +485,30 @@ class AuthHandlers:
LoginHandler: LoginHandler = LoginHandler()
PasswordHandler: PasswordHandler = PasswordHandler()
# Get selected occupant type
# selected_build_living_space: BuildLivingSpace = BuildLivingSpace.filter_one(BuildLivingSpace.uu_id == data.build_living_space_uu_id, db=db).data
# if not selected_build_living_space:
# raise ValueError("EYS_0012")
# occupant_type = OccupantTypes.filter_one_system(OccupantTypes.id == selected_build_living_space.occupant_type_id, db=db).data
# build_part = BuildParts.filter_one(BuildParts.id == selected_build_living_space.build_parts_id, db=db)
# build = build_part.buildings
# reachable_app_codes = Application2Occupant.get_application_codes(build_living_space_id=selected_build_living_space.id, db=db)
# responsible_employee = Employees.filter_one(
# Employees.id == build_part.responsible_employee_id,
# db=db,
# ).data
# related_company = RelationshipEmployee2Build.filter_one(
# RelationshipEmployee2Build.member_id == build.id,
# db=db,
# ).data
# Get company
# company_related = Companies.filter_one(
# Companies.id == related_company.company_id,
# db=db,
# ).data
# Get reachable events
# reachable_event_codes = Event2Occupant.get_event_codes(build_living_space_id=selected_build_living_space.id, db=db)

View File

@@ -14,7 +14,7 @@ class RequestVerifyOTP(BaseModel):
otp: str
class RequestSelectEmployee(BaseModel):
class RequestSelect(BaseModel):
uuid: str
@@ -23,10 +23,6 @@ class RequestResetPassword(BaseModel):
password: str
re_password: str
class RequestSelectLiving(BaseModel):
uuid: str
class RequestCreatePassword(BaseModel):
password_token: str
password: str

View File

@@ -0,0 +1,58 @@
from typing import Any
from Initializer.event_clusters import Event
from Validations.response import (
PaginateOnly,
Pagination,
PaginationResult,
PostgresResponseSingle,
PostgresResponse,
EndpointResponse
)
from Validations.defaults.validations import CommonHeaders
from Schemas import People
from Extensions.Middlewares.token_provider import TokenProvider
# List all people endpoint Flat Tenant
FlatTenantPeopleListEvent = Event(
name="flat_tenant_people_list",
key="7cb248d3-d75a-4d28-8be5-f147089ce654",
request_validator=None, # TODO: Add request validator
response_validator=None, # TODO: Add response validator
description="Flat Tenant People List all flat tenant users endpoint",
)
class FlatTenantPeopleListPydantic:
...
def flat_tenant_people_list_callable(list_options: PaginateOnly, header: CommonHeaders):
list_options = PaginateOnly(**list_options.model_dump())
token_object = TokenProvider.get_dict_from_redis(token=header.token)
if not token_object.is_occupant:
raise Exception("Forbidden for employees")
pagination_query = {}
if list_options.query:
pagination_query = FlatTenantPeopleListPydantic(**list_options.query)
with People.new_session() as db_session:
People.set_session(db_session)
if pagination_query:
people_list = People.query.filter(
*People.convert(pagination_query),
People.build_parts_id == token_object.selected_occupant.build_id
)
else:
people_list = People.query.filter(
People.build_parts_id == token_object.selected_occupant.build_id
)
pagination = Pagination(data=people_list)
pagination.change(**list_options.model_dump())
pagination_result = PaginationResult(data=people_list, pagination=pagination, response_model=FlatTenantPeopleListPydantic)
return EndpointResponse(message="MSG0003-LIST", pagination_result=pagination_result).response
FlatTenantPeopleListEvent.event_callable = flat_tenant_people_list_callable