first commit
This commit is contained in:
0
api_events/events/building/__init__.py
Normal file
0
api_events/events/building/__init__.py
Normal file
252
api_events/events/building/building_build.py
Normal file
252
api_events/events/building/building_build.py
Normal file
@@ -0,0 +1,252 @@
|
||||
import typing
|
||||
|
||||
from fastapi import status, HTTPException
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from databases import (
|
||||
Build,
|
||||
# RelationshipEmployee2Build,
|
||||
Addresses,
|
||||
BuildParts,
|
||||
BuildTypes,
|
||||
ApiEnumDropdown,
|
||||
)
|
||||
|
||||
from api_validations import (
|
||||
InsertBuild,
|
||||
UpdateBuild,
|
||||
PatchRecord,
|
||||
ListOptions,
|
||||
|
||||
)
|
||||
|
||||
from api_validations.core_response import return_json_response_from_alchemy
|
||||
|
||||
from api_events.events.abstract_class import MethodToEvent, ActionsSchema
|
||||
from api_objects.auth.token_objects import EmployeeTokenObject, OccupantTokenObject
|
||||
|
||||
|
||||
class BuildListEventMethods(MethodToEvent):
|
||||
|
||||
event_type = "SELECT"
|
||||
__event_keys__ = {
|
||||
"68b3b5ed-b74c-4a27-820f-3959214e94e9": "build_list",
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def build_list(
|
||||
cls,
|
||||
list_options: ListOptions,
|
||||
token_dict: typing.Union[EmployeeTokenObject, OccupantTokenObject],
|
||||
):
|
||||
if isinstance(token_dict, OccupantTokenObject):
|
||||
Build.pre_query = Build.filter_active(
|
||||
Build.id == token_dict.selected_occupant.build_id
|
||||
).query
|
||||
elif isinstance(token_dict, EmployeeTokenObject):
|
||||
Build.pre_query = Build.select_action(
|
||||
employee_id=token_dict.selected_company.employee_id
|
||||
)
|
||||
Build.filter_attr = list_options
|
||||
records = Build.filter_active(
|
||||
*Build.get_smart_query(smart_query=list_options.query)
|
||||
)
|
||||
return return_json_response_from_alchemy(
|
||||
response=records, pagination=list_options
|
||||
)
|
||||
|
||||
|
||||
class BuildCreateEventMethods(MethodToEvent):
|
||||
|
||||
event_type = "CREATE"
|
||||
__event_keys__ = {
|
||||
"a2271854-6b90-43da-a440-a62b70d90528": "build_create",
|
||||
"b67ee709-0992-4604-9f90-fb1da10d5cf9": "create_building_employee",
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def build_create(cls, data: InsertBuild, token_dict: EmployeeTokenObject):
|
||||
if not token_dict.selected_company.employee_id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail=f"Employee id is not found for {token_dict.selected_company.employee_uu_id}",
|
||||
)
|
||||
|
||||
if not token_dict.selected_company.company_id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail=f"Company id is not found for {token_dict.selected_company.company_uu_id}",
|
||||
)
|
||||
|
||||
created_build = Build.create_action(data=data, token=token_dict)
|
||||
if not created_build.is_found:
|
||||
build_type = BuildTypes.find_one(type_code="APT_YNT")
|
||||
if not build_type:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Build type APT_YNT is not found. Please contact with your system administrator.",
|
||||
)
|
||||
api_enum = ApiEnumDropdown.find_one(enum_class="Directions", key="NN")
|
||||
if not api_enum:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Api Enum NN is not found. Please contact with your system administrator.",
|
||||
)
|
||||
build_parts = dict(
|
||||
address_gov_code=f"{data.gov_address_code}-M",
|
||||
build_id=int(created_build.id),
|
||||
build_uu_id=str(created_build.uu_id),
|
||||
part_no="0",
|
||||
part_type_id=int(build_type.id),
|
||||
part_type_uu_id=str(build_type.uu_id),
|
||||
part_direction_id=int(api_enum.id),
|
||||
part_direction_uu_id=str(api_enum.uu_id),
|
||||
part_code="MAN-ROOM",
|
||||
human_livable=False,
|
||||
is_confirmed=True,
|
||||
)
|
||||
man_build_part = BuildParts.find_or_create(**build_parts)
|
||||
if not man_build_part.is_found:
|
||||
created_build.update(management_room_id=man_build_part.id)
|
||||
return JSONResponse(
|
||||
content={
|
||||
"completed": True,
|
||||
"message": "Create Build record completed. This build is assigned to you.",
|
||||
"data": created_build.get_dict(),
|
||||
},
|
||||
status_code=status.HTTP_200_OK,
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_406_NOT_ACCEPTABLE,
|
||||
detail=f"Build record create not successful for Employee UUID : {token_dict.selected_company.employee_uu_id}",
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def create_building_employee(
|
||||
cls, data: InsertBuild, token_dict: EmployeeTokenObject
|
||||
):
|
||||
records = Addresses.list_via_employee(
|
||||
token_dict=token_dict, filter_expr=[Addresses.uu_id == data.address_uu_id]
|
||||
)
|
||||
if not records.data:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=f"This address {data.address_uu_id} is not found in the user's address list.",
|
||||
)
|
||||
|
||||
if not token_dict.selected_company.employee_id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail=f"Employee id is not found for {token_dict.selected_company.employee_uu_id}",
|
||||
)
|
||||
|
||||
if not token_dict.selected_company.company_id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail=f"Company id is not found for {token_dict.selected_company.company_uu_id}",
|
||||
)
|
||||
|
||||
created_build = Build.create_action(data=data, token=token_dict)
|
||||
if not created_build.is_found:
|
||||
RelationshipEmployee2Build.find_or_create(
|
||||
company_id=token_dict.selected_company.company_id,
|
||||
member_id=created_build.id,
|
||||
employee_id=token_dict.selected_company.employee_id,
|
||||
is_confirmed=True,
|
||||
)
|
||||
return JSONResponse(
|
||||
content={
|
||||
"completed": True,
|
||||
"message": "Create Build record completed. This build is assigned to you.",
|
||||
"data": created_build.get_dict(),
|
||||
},
|
||||
status_code=status.HTTP_200_OK,
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail=f"Build record create not successful for {token_dict.selected_company.employee_uu_id}",
|
||||
)
|
||||
|
||||
|
||||
class BuildUpdateEventMethods(MethodToEvent):
|
||||
|
||||
event_type = "UPDATE"
|
||||
__event_keys__ = {
|
||||
"5ad38a66-1189-451e-babb-77de2d63d757": "build_update",
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def build_update(cls, build_uu_id: str, data: UpdateBuild, token_dict):
|
||||
Build.pre_query = Build.select_action(
|
||||
employee_id=token_dict.selected_company.employee_id
|
||||
)
|
||||
if Build.filter_active(Build.person_id == token_dict.person_id):
|
||||
if updated_build := Build.update_action(
|
||||
data=data, token=token_dict, build_uu_id=build_uu_id
|
||||
):
|
||||
return JSONResponse(
|
||||
content={
|
||||
"completed": True,
|
||||
"message": "Update Build record",
|
||||
"data": updated_build,
|
||||
},
|
||||
status_code=status.HTTP_200_OK,
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail=f"This user can not modify {build_uu_id} - building.",
|
||||
)
|
||||
|
||||
|
||||
class BuildPatchEventMethods(MethodToEvent):
|
||||
|
||||
event_type = "PATCH"
|
||||
__event_keys__ = {
|
||||
"e3876bfe-8847-4dea-ae36-e709f7431930": "build_patch",
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def build_patch(cls, build_uu_id: str, data: PatchRecord, token_dict):
|
||||
find_one_build = Build.find_one_or_abort(uu_id=build_uu_id)
|
||||
access_authorized_build = Build.select_action(
|
||||
employee_id=token_dict.selected_company.employee_id,
|
||||
filter_expr=[Build.id == find_one_build.id],
|
||||
)
|
||||
if access_authorized_build.count:
|
||||
action = data.excluded_dump()
|
||||
find_one_build.active = bool(action.get("active", find_one_build.active))
|
||||
find_one_build.is_confirmed = bool(
|
||||
action.get("confirm", find_one_build.is_confirmed)
|
||||
)
|
||||
find_one_build.deleted = bool(action.get("delete", find_one_build.deleted))
|
||||
find_one_build.save()
|
||||
return JSONResponse(
|
||||
content={
|
||||
"completed": True,
|
||||
"message": "Patch Build record completed",
|
||||
"data": find_one_build.get_dict(),
|
||||
},
|
||||
status_code=status.HTTP_200_OK,
|
||||
)
|
||||
return JSONResponse(
|
||||
content={
|
||||
"completed": False,
|
||||
"message": "Patch Build record failed",
|
||||
"data": {},
|
||||
},
|
||||
status_code=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
|
||||
BuildListEventMethod = BuildListEventMethods(
|
||||
action=ActionsSchema(endpoint="/building/build/list")
|
||||
)
|
||||
BuildCreateEventMethod = BuildCreateEventMethods(
|
||||
action=ActionsSchema(endpoint="/building/build/create")
|
||||
)
|
||||
BuildUpdateEventMethod = BuildUpdateEventMethods(
|
||||
action=ActionsSchema(endpoint="/building/build/update")
|
||||
)
|
||||
BuildPatchEventMethod = BuildPatchEventMethods(
|
||||
action=ActionsSchema(endpoint="/building/build/patch")
|
||||
)
|
||||
5
api_events/events/building/building_build_area.py
Normal file
5
api_events/events/building/building_build_area.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from api_events.events.abstract_class import MethodToEvent, ActionsSchema
|
||||
from api_objects.auth.token_objects import EmployeeTokenObject, OccupantTokenObject
|
||||
|
||||
|
||||
class BuildingBuildAreaEvents(MethodToEvent): ...
|
||||
150
api_events/events/building/building_build_parts.py
Normal file
150
api_events/events/building/building_build_parts.py
Normal file
@@ -0,0 +1,150 @@
|
||||
from fastapi.responses import JSONResponse
|
||||
from fastapi import status
|
||||
|
||||
from databases import (
|
||||
Build,
|
||||
BuildParts,
|
||||
)
|
||||
from api_events.events.abstract_class import MethodToEvent, ActionsSchema
|
||||
from api_objects.auth.token_objects import EmployeeTokenObject, OccupantTokenObject
|
||||
from api_validations.core_response import return_json_response_from_alchemy
|
||||
|
||||
from api_validations import (
|
||||
InsertBuildParts,
|
||||
ListOptions,
|
||||
)
|
||||
|
||||
|
||||
class BuildingBuildPartsListEventMethods(MethodToEvent):
|
||||
|
||||
event_type = "SELECT"
|
||||
__event_keys__ = {
|
||||
"b860e37a-e19b-4c45-9543-461241f7587c": "building_build_parts_list"
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def building_build_parts_list(cls, list_options: ListOptions, token_dict):
|
||||
build_list_query = Build.select_action(
|
||||
employee_id=token_dict.selected_company.employee_id,
|
||||
)
|
||||
build_list_ids = [build.id for build in build_list_query.all()]
|
||||
BuildParts.pre_query = BuildParts.query.filter(
|
||||
BuildParts.build_id.in_(build_list_ids)
|
||||
)
|
||||
records = BuildParts.filter_active(
|
||||
*BuildParts.get_smart_query(list_options.query)
|
||||
)
|
||||
return return_json_response_from_alchemy(
|
||||
response=records, pagination=list_options
|
||||
)
|
||||
|
||||
|
||||
class BuildingBuildPartsCreateEventMethods(MethodToEvent):
|
||||
|
||||
event_type = "CREATE"
|
||||
__event_keys__ = {
|
||||
"fb403f69-11ed-4f4f-ad71-5e6fb4a793d2": "building_build_parts_create"
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def building_build_parts_create(cls, data: InsertBuildParts, token_dict: dict):
|
||||
created_build = BuildParts.create_action(data=data, token=token_dict)
|
||||
if not created_build:
|
||||
return JSONResponse(
|
||||
content={
|
||||
"completed": False,
|
||||
"message": "Create Build Parts is not completed",
|
||||
"data": {},
|
||||
},
|
||||
status_code=status.HTTP_406_NOT_ACCEPTABLE,
|
||||
)
|
||||
return JSONResponse(
|
||||
content={
|
||||
"completed": True,
|
||||
"message": "Create Build Parts record",
|
||||
"data": created_build.get_dict(),
|
||||
},
|
||||
status_code=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
|
||||
class BuildingBuildPartsUpdateEventMethods(MethodToEvent):
|
||||
|
||||
event_type = "UPDATE"
|
||||
__event_keys__ = {
|
||||
"58fdf95e-2110-4ed6-9c26-95f4be87eaee": "building_build_parts_update"
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def building_build_parts_update(cls, data: InsertBuildParts, token_dict: dict):
|
||||
if updated_build := BuildParts.update_action(data=data, token=token_dict):
|
||||
return JSONResponse(
|
||||
content={
|
||||
"completed": True,
|
||||
"message": "Update Build Parts record",
|
||||
"data": updated_build,
|
||||
},
|
||||
status_code=status.HTTP_200_OK,
|
||||
)
|
||||
return JSONResponse(
|
||||
content={
|
||||
"completed": True,
|
||||
"message": "Update Build Parts record",
|
||||
"data": {},
|
||||
},
|
||||
status_code=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
|
||||
class BuildingBuildPartsPatchEventMethods(MethodToEvent):
|
||||
|
||||
event_type = "PATCH"
|
||||
__event_keys__ = {
|
||||
"87a15ade-3474-4206-b574-bbf8580cbb14": "building_build_parts_patch"
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def building_build_parts_patch(cls, data, token_dict):
|
||||
find_one_build = BuildParts.find_one(uu_id=data.uu_id)
|
||||
access_authorized_build = BuildParts.select_action(
|
||||
duty_id=token_dict.selected_company.duty_id,
|
||||
filter_expr=[BuildParts.id == find_one_build.id],
|
||||
)
|
||||
if access_authorized_build.count:
|
||||
action = data.excluded_dump()
|
||||
find_one_build.active = bool(action.get("active", find_one_build.active))
|
||||
find_one_build.is_confirmed = bool(
|
||||
action.get("confirm", find_one_build.is_confirmed)
|
||||
)
|
||||
find_one_build.deleted = bool(action.get("delete", find_one_build.deleted))
|
||||
find_one_build.save()
|
||||
return JSONResponse(
|
||||
content={
|
||||
"completed": True,
|
||||
"message": "Update Build Parts record",
|
||||
"data": find_one_build.get_dict(),
|
||||
},
|
||||
status_code=status.HTTP_200_OK,
|
||||
)
|
||||
return JSONResponse(
|
||||
content={
|
||||
"completed": False,
|
||||
"message": "Update Build Parts record",
|
||||
"data": {},
|
||||
},
|
||||
status_code=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
|
||||
BuildingBuildPartsListEventMethod = BuildingBuildPartsListEventMethods(
|
||||
action=ActionsSchema(endpoint="/building/parts/list")
|
||||
)
|
||||
BuildingBuildPartsCreateEventMethod = BuildingBuildPartsCreateEventMethods(
|
||||
action=ActionsSchema(endpoint="/building/parts/create")
|
||||
)
|
||||
BuildingBuildPartsUpdateEventMethod = BuildingBuildPartsUpdateEventMethods(
|
||||
action=ActionsSchema(endpoint="/building/parts/update")
|
||||
)
|
||||
BuildingBuildPartsPatchEventMethod = BuildingBuildPartsPatchEventMethods(
|
||||
action=ActionsSchema(endpoint="/building/parts/patch")
|
||||
)
|
||||
5
api_events/events/building/building_build_sites.py
Normal file
5
api_events/events/building/building_build_sites.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from api_events.events.abstract_class import MethodToEvent, ActionsSchema
|
||||
from api_objects.auth.token_objects import EmployeeTokenObject, OccupantTokenObject
|
||||
|
||||
|
||||
class BuildingBuildSitesEvents(MethodToEvent): ...
|
||||
5
api_events/events/building/building_build_types.py
Normal file
5
api_events/events/building/building_build_types.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from api_events.events.abstract_class import MethodToEvent, ActionsSchema
|
||||
from api_objects.auth.token_objects import EmployeeTokenObject, OccupantTokenObject
|
||||
|
||||
|
||||
class BuildingBuildTypesEvents(MethodToEvent): ...
|
||||
240
api_events/events/building/building_living_spaces.py
Normal file
240
api_events/events/building/building_living_spaces.py
Normal file
@@ -0,0 +1,240 @@
|
||||
import typing
|
||||
|
||||
from fastapi import status, HTTPException
|
||||
|
||||
from databases import (
|
||||
BuildParts,
|
||||
Build,
|
||||
BuildLivingSpace,
|
||||
OccupantTypes,
|
||||
|
||||
)
|
||||
from api_events.events.abstract_class import MethodToEvent, ActionsSchema
|
||||
from api_objects.auth.token_objects import EmployeeTokenObject, OccupantTokenObject
|
||||
|
||||
from api_validations.core_response import return_json_response_from_alchemy
|
||||
from api_validations import (
|
||||
InsertBuildLivingSpace,
|
||||
ListOptions,
|
||||
)
|
||||
|
||||
class BuildingLivingSpacesPartsListEventMethods(MethodToEvent):
|
||||
|
||||
event_type = "SELECT"
|
||||
__event_keys__ = {
|
||||
"8fd04d94-68fb-4a07-9549-8b47aee3a870": "building_build_parts_list",
|
||||
"2f3041a9-6184-44c2-ac38-8ea934297ed1": "building_build_parts_list_with_expired",
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def building_build_parts_list(cls, list_options: ListOptions, token_dict):
|
||||
build_id_list_query = Build.select_action(
|
||||
employee_id=token_dict.selected_company.employee_id
|
||||
)
|
||||
Build.filter_attr = list_options
|
||||
build_part_id_list_query = BuildParts.filter_active(
|
||||
BuildParts.build_id.in_([build.id for build in build_id_list_query.all()]),
|
||||
)
|
||||
|
||||
list_options.query.pop("expiry_starts", None)
|
||||
list_options.query.pop("expiry_ends", None)
|
||||
|
||||
records = BuildLivingSpace.filter_active(
|
||||
BuildLivingSpace.build_parts_id.in_(
|
||||
[build_part.id for build_part in build_part_id_list_query.data]
|
||||
),
|
||||
*BuildLivingSpace.get_smart_query(smart_query=list_options.query),
|
||||
)
|
||||
return return_json_response_from_alchemy(
|
||||
response=records, pagination=list_options
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def building_build_parts_list_with_expired(
|
||||
cls, list_options: ListOptions, token_dict
|
||||
):
|
||||
build_id_list_query = Build.select_action(
|
||||
employee_id=token_dict.selected_company.employee_id
|
||||
)
|
||||
Build.filter_attr = list_options
|
||||
build_part_id_list_query = BuildParts.filter_active(
|
||||
BuildParts.build_id.in_([build.id for build in build_id_list_query.all()]),
|
||||
)
|
||||
|
||||
records = BuildLivingSpace.filter_active(
|
||||
BuildLivingSpace.build_parts_id.in_(
|
||||
[build_part.id for build_part in build_part_id_list_query.data]
|
||||
),
|
||||
*BuildLivingSpace.get_smart_query(smart_query=list_options.query),
|
||||
expired=False,
|
||||
)
|
||||
return return_json_response_from_alchemy(
|
||||
response=records, pagination=list_options
|
||||
)
|
||||
|
||||
|
||||
class BuildingLivingSpacesPartsCreateEventMethods(MethodToEvent):
|
||||
|
||||
event_type = "CREATE"
|
||||
__event_keys__ = {
|
||||
"b78ca45c-b9f4-41f6-9ddb-2c6f2faa2570": "building_live_space_create"
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def building_live_space_create(
|
||||
cls,
|
||||
data: InsertBuildLivingSpace,
|
||||
token_dict: typing.Union[EmployeeTokenObject, OccupantTokenObject],
|
||||
):
|
||||
from service_app.application import DateTimeLocal
|
||||
from database_sql_models import People
|
||||
from sqlalchemy.sql import select
|
||||
|
||||
data_dict = data.dump()
|
||||
build_id_list_query = Build.select_action(
|
||||
employee_id=token_dict.selected_company.employee_id
|
||||
)
|
||||
build_part = BuildParts.filter_active(
|
||||
BuildParts.uu_id == data.build_parts_uu_id,
|
||||
BuildParts.build_id.in_([build.id for build in build_id_list_query.all()]),
|
||||
)
|
||||
if not build_part.get(1):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_418_IM_A_TEAPOT,
|
||||
detail=f"{data.build_parts_uu_id} - Build Part is not found in database. Check build part uu_id",
|
||||
)
|
||||
build_part = build_part.get(1)
|
||||
|
||||
life_person = People.find_one(uu_id=data.person_uu_id or "")
|
||||
if not life_person:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_418_IM_A_TEAPOT,
|
||||
detail=f"{data.life_person_uu_id} - Living Person is not found in database. "
|
||||
f"Check build live person uu_id",
|
||||
)
|
||||
|
||||
occupant_type = OccupantTypes.find_one(uu_id=data.occupant_type_uu_id)
|
||||
if not occupant_type:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_418_IM_A_TEAPOT,
|
||||
detail=f"{data.occupant_type_uu_id} - Occupant Type is not found in database. "
|
||||
f"Check occupant type uu_id",
|
||||
)
|
||||
|
||||
data_dict["occupant_type"] = occupant_type.id
|
||||
data_dict["occupant_type_uu_id"] = str(occupant_type.uu_id)
|
||||
data_dict["build_parts_id"] = build_part.id
|
||||
data_dict["build_parts_uu_id"] = str(build_part.uu_id)
|
||||
data_dict["person_id"] = life_person.id
|
||||
data_dict["person_uu_id"] = str(life_person.uu_id)
|
||||
|
||||
living_space_id = BuildLivingSpace.session.execute(
|
||||
select(BuildLivingSpace.id)
|
||||
.where(
|
||||
*[
|
||||
BuildLivingSpace.build_parts_id == build_part.id,
|
||||
BuildLivingSpace.person_id == life_person.id,
|
||||
BuildLivingSpace.occupant_type == occupant_type.id,
|
||||
BuildLivingSpace.active == True,
|
||||
BuildLivingSpace.is_confirmed == True,
|
||||
str(DateTimeLocal.now()) < BuildLivingSpace.expiry_ends,
|
||||
str(DateTimeLocal.now()) >= BuildLivingSpace.expiry_starts,
|
||||
]
|
||||
)
|
||||
.order_by(BuildLivingSpace.expiry_starts.desc())
|
||||
).first()
|
||||
|
||||
last_living_space = BuildLivingSpace.find_one(
|
||||
id=living_space_id[0] if living_space_id else None
|
||||
)
|
||||
|
||||
data_dict["expiry_starts"] = str(DateTimeLocal.now())
|
||||
created_living_space = BuildLivingSpace.create_action(
|
||||
data=data_dict, token_dict=token_dict
|
||||
)
|
||||
|
||||
if last_living_space:
|
||||
if last_living_space.expiry_ends > DateTimeLocal.now():
|
||||
last_living_space.expiry_ends = str(DateTimeLocal.shift(minutes=-10))
|
||||
last_living_space.save()
|
||||
return created_living_space
|
||||
|
||||
|
||||
class BuildingLivingSpacesPartsUpdateEventMethods(MethodToEvent):
|
||||
|
||||
event_type = "UPDATE"
|
||||
__event_keys__ = {
|
||||
"70b4666f-4ceb-46ec-b89e-24be8712f0e7": "building_build_parts_update"
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def building_build_parts_update(cls, build_uu_id: str, data, token_dict):
|
||||
from service_app.application import DateTimeLocal
|
||||
from database_sql_models import People
|
||||
from sqlalchemy.sql import select
|
||||
|
||||
data_dict = data.dump()
|
||||
build_id_list_query = Build.select_action(
|
||||
employee_id=token_dict.selected_company.employee_id
|
||||
)
|
||||
build_part = BuildParts.filter_active(
|
||||
BuildParts.uu_id == data.build_parts_uu_id,
|
||||
BuildParts.build_id.in_([build.id for build in build_id_list_query.all()]),
|
||||
)
|
||||
if not build_part.get(1):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_418_IM_A_TEAPOT,
|
||||
detail=f"{data.build_parts_uu_id} - Build Part is not found in database. Check build part uu_id",
|
||||
)
|
||||
build_part = build_part.get(1)
|
||||
|
||||
life_person = People.find_one(uu_id=data.life_person_uu_id or "")
|
||||
if not life_person:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_418_IM_A_TEAPOT,
|
||||
detail=f"{data.life_person_uu_id} - Living Person is not found in database. "
|
||||
f"Check build live person uu_id",
|
||||
)
|
||||
|
||||
living_spaces = select(BuildLivingSpace.id).order_by(
|
||||
BuildLivingSpace.expiry_starts.desc()
|
||||
)
|
||||
living_space_id = BuildLivingSpace.session.execute(living_spaces).first()
|
||||
last_living_space = BuildLivingSpace.find_one(
|
||||
id=getattr(living_space_id[0], "id", None) if living_space_id else None
|
||||
)
|
||||
|
||||
data_dict["expiry_starts"] = DateTimeLocal.now()
|
||||
data_dict["is_tenant_live"] = bool(data.is_tenant_live)
|
||||
data_dict["build_parts_id"] = build_part.id
|
||||
if data_dict["is_tenant_live"]:
|
||||
owner_person = getattr(last_living_space, "owner_person_id", None)
|
||||
if not owner_person:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_418_IM_A_TEAPOT,
|
||||
detail=dict(
|
||||
message="Owner person of build part is not defined. Please register owner of part first.",
|
||||
data=build_part.get_dict(),
|
||||
),
|
||||
)
|
||||
data_dict["life_person_id"] = life_person.id
|
||||
data_dict["owner_person_id"] = owner_person
|
||||
else:
|
||||
data_dict["life_person_id"] = life_person.id
|
||||
data_dict["owner_person_id"] = life_person.id
|
||||
del data_dict["build_parts_uu_id"], data_dict["life_person_uu_id"]
|
||||
|
||||
|
||||
BuildingLivingSpacesPartsListEventMethod = BuildingLivingSpacesPartsListEventMethods(
|
||||
action=ActionsSchema(endpoint="/building/living_space/list")
|
||||
)
|
||||
BuildingLivingSpacesPartsCreateEventMethod = (
|
||||
BuildingLivingSpacesPartsCreateEventMethods(
|
||||
action=ActionsSchema(endpoint="/building/living_space/create")
|
||||
)
|
||||
)
|
||||
BuildingLivingSpacesPartsUpdateEventMethod = (
|
||||
BuildingLivingSpacesPartsUpdateEventMethods(
|
||||
action=ActionsSchema(endpoint="/building/living_space/update")
|
||||
)
|
||||
)
|
||||
Reference in New Issue
Block a user