alcehmy and event functions updated
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
from typing import Union
|
||||
|
||||
from fastapi.exceptions import HTTPException
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
@@ -31,7 +33,11 @@ class AddressListEventMethods(MethodToEvent):
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def address_list_super_user(cls, list_options: ListOptions, token_dict):
|
||||
def address_list_super_user(
|
||||
cls,
|
||||
list_options: ListOptions,
|
||||
token_dict: Union[EmployeeTokenObject, OccupantTokenObject],
|
||||
):
|
||||
from sqlalchemy import select
|
||||
|
||||
post_code_list = RelationshipEmployee2PostCode.filter_all(
|
||||
@@ -45,17 +51,29 @@ class AddressListEventMethods(MethodToEvent):
|
||||
detail="User has no post code registered. User can not list addresses.",
|
||||
)
|
||||
|
||||
get_street_ids = AddressPostcode.session.execute(
|
||||
select(AddressPostcode.street_id).where(
|
||||
AddressPostcode.id.in_(post_code_id_list)
|
||||
get_street_ids = [
|
||||
street_id[0]
|
||||
for street_id in AddressPostcode.select_only(
|
||||
AddressPostcode.id.in_(post_code_id_list),
|
||||
AddressPostcode.active == True,
|
||||
select_args=[AddressPostcode.street_id],
|
||||
order_by=AddressPostcode.street_id.desc(),
|
||||
).data
|
||||
]
|
||||
if not get_street_ids:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail="User has no street registered. User can not list addresses.",
|
||||
)
|
||||
).all()
|
||||
Addresses.pre_query = Addresses.filter_active(
|
||||
Addresses.street_id.in_(*get_street_ids) if get_street_ids else None
|
||||
|
||||
Addresses.pre_query = Addresses.filter_all(
|
||||
Addresses.street_id.in_(*get_street_ids),
|
||||
Addresses.active == True,
|
||||
).query
|
||||
Addresses.filter_attr = list_options
|
||||
records = Addresses.filter_active(
|
||||
*Addresses.get_smart_query(list_options.query)
|
||||
records = Addresses.filter_all(
|
||||
*Addresses.get_smart_query(list_options.query),
|
||||
Addresses.active == True,
|
||||
)
|
||||
return AlchemyJsonResponse(
|
||||
completed=True,
|
||||
@@ -64,7 +82,11 @@ class AddressListEventMethods(MethodToEvent):
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def address_list_employee(cls, list_options: ListOptions, token_dict):
|
||||
def address_list_employee(
|
||||
cls,
|
||||
list_options: ListOptions,
|
||||
token_dict: Union[EmployeeTokenObject, OccupantTokenObject],
|
||||
):
|
||||
Addresses.filter_attr = list_options
|
||||
records = Addresses.list_via_employee(
|
||||
token_dict=token_dict,
|
||||
@@ -76,6 +98,7 @@ class AddressListEventMethods(MethodToEvent):
|
||||
result=records,
|
||||
)
|
||||
|
||||
|
||||
class AddressCreateEventMethods(MethodToEvent):
|
||||
|
||||
event_type = "CREATE"
|
||||
@@ -84,8 +107,15 @@ class AddressCreateEventMethods(MethodToEvent):
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def create_address(cls, data: InsertAddress, token_dict):
|
||||
post_code = AddressPostcode.find_one(uu_id=data.post_code_uu_id)
|
||||
def create_address(
|
||||
cls,
|
||||
data: InsertAddress,
|
||||
token_dict: Union[EmployeeTokenObject, OccupantTokenObject],
|
||||
):
|
||||
post_code = AddressPostcode.filter_one(
|
||||
AddressPostcode.uu_id==data.post_code_uu_id,
|
||||
AddressPostcode.active == True,
|
||||
).data
|
||||
if not post_code:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
@@ -95,15 +125,14 @@ class AddressCreateEventMethods(MethodToEvent):
|
||||
data_dict = data.excluded_dump()
|
||||
data_dict["street_id"] = post_code.street_id
|
||||
del data_dict["post_code_uu_id"]
|
||||
|
||||
data_dict["is_confirmed"] = True
|
||||
address = Addresses.find_or_create(**data_dict)
|
||||
if not address.is_found:
|
||||
RelationshipEmployee2PostCode.find_or_create(
|
||||
employee_id=token_dict.selected_company.employee_id,
|
||||
member_id=post_code.id,
|
||||
company_id=token_dict.selected_company.company_id,
|
||||
is_confirmed=True,
|
||||
)
|
||||
RelationshipEmployee2PostCode.find_or_create(
|
||||
employee_id=token_dict.selected_company.employee_id,
|
||||
member_id=post_code.id,
|
||||
company_id=token_dict.selected_company.company_id,
|
||||
is_confirmed=True,
|
||||
)
|
||||
Addresses.save()
|
||||
return JSONResponse(
|
||||
content={
|
||||
@@ -123,7 +152,11 @@ class AddressSearchEventMethods(MethodToEvent):
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def search_address(cls, data: SearchAddress, token_dict):
|
||||
def search_address(
|
||||
cls,
|
||||
data: SearchAddress,
|
||||
token_dict: Union[EmployeeTokenObject, OccupantTokenObject],
|
||||
):
|
||||
import databases as database_sql_models
|
||||
from time import perf_counter
|
||||
|
||||
@@ -191,11 +224,16 @@ class AddressUpdateEventMethods(MethodToEvent):
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def update_address(cls, address_uu_id: str, data: InsertAddress, token_dict):
|
||||
def update_address(
|
||||
cls,
|
||||
address_uu_id: str,
|
||||
data: InsertAddress,
|
||||
token_dict: Union[EmployeeTokenObject, OccupantTokenObject],
|
||||
):
|
||||
address = Addresses.find_one_or_abort(uu_id=address_uu_id)
|
||||
post_code = RelationshipEmployee2PostCode.postcode.find_one(
|
||||
member_id=address.post_code_id
|
||||
)
|
||||
post_code = RelationshipEmployee2PostCode.filter_one(
|
||||
RelationshipEmployee2PostCode.member_id==address.post_code_id
|
||||
).data
|
||||
if not post_code:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
@@ -205,7 +243,7 @@ class AddressUpdateEventMethods(MethodToEvent):
|
||||
data_dict = data.excluded_dump()
|
||||
data_dict["post_code_id"] = post_code.id
|
||||
del data_dict["post_code_uu_id"]
|
||||
|
||||
data_dict["is_confirmed"] = True
|
||||
updated_address = address.update(**data_dict)
|
||||
updated_address.save()
|
||||
return JSONResponse(
|
||||
@@ -226,7 +264,12 @@ class AddressPatchEventMethods(MethodToEvent):
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def patch_address(cls, address_uu_id: str, data: InsertAddress, token_dict):
|
||||
def patch_address(
|
||||
cls,
|
||||
address_uu_id: str,
|
||||
data: InsertAddress,
|
||||
token_dict: Union[EmployeeTokenObject, OccupantTokenObject],
|
||||
):
|
||||
address = Addresses.find_one_or_abort(uu_id=address_uu_id)
|
||||
post_code = RelationshipEmployee2PostCode.filter_one(
|
||||
RelationshipEmployee2PostCode.member_id == address.post_code_id
|
||||
@@ -260,9 +303,13 @@ class AddressPostCodeCreateEventMethods(MethodToEvent):
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def create_post_code_address(cls, data: InsertPostCode, token_dict):
|
||||
def create_post_code_address(
|
||||
cls,
|
||||
data: InsertPostCode,
|
||||
token_dict: Union[EmployeeTokenObject, OccupantTokenObject],
|
||||
):
|
||||
data_dump = data.excluded_dump()
|
||||
street = AddressStreet.find_one(uu_id=data.street_uu_id)
|
||||
street = AddressStreet.filter_one(AddressStreet.uu_id==data.street_uu_id).data
|
||||
if not street:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
@@ -273,13 +320,12 @@ class AddressPostCodeCreateEventMethods(MethodToEvent):
|
||||
del data_dump["street_uu_id"], data_dump["post_code"]
|
||||
|
||||
post_code = AddressPostcode.find_or_create(**data_dump)
|
||||
if not post_code.is_found:
|
||||
AddressPostcode.__many__table__.find_or_create(
|
||||
member_id=post_code.id,
|
||||
employee_id=token_dict.selected_company.employee_id,
|
||||
company_id=token_dict.selected_company.company_id,
|
||||
is_confirmed=True,
|
||||
)
|
||||
AddressPostcode.__many__table__.find_or_create(
|
||||
member_id=post_code.id,
|
||||
employee_id=token_dict.selected_company.employee_id,
|
||||
company_id=token_dict.selected_company.company_id,
|
||||
is_confirmed=True,
|
||||
)
|
||||
AddressStreet.save()
|
||||
return JSONResponse(
|
||||
content={
|
||||
@@ -300,10 +346,13 @@ class AddressPostCodeUpdateEventMethods(MethodToEvent):
|
||||
|
||||
@classmethod
|
||||
def update_post_code_address(
|
||||
cls, post_code_uu_id: str, data: InsertPostCode, token_dict
|
||||
cls,
|
||||
post_code_uu_id: str,
|
||||
data: InsertPostCode,
|
||||
token_dict: Union[EmployeeTokenObject, OccupantTokenObject],
|
||||
):
|
||||
post_code = AddressPostcode.find_one_or_abort(uu_id=post_code_uu_id)
|
||||
street = AddressStreet.find_one(uu_id=data.street_uu_id)
|
||||
post_code = AddressPostcode.filter_one(AddressPostcode.uu_id==post_code_uu_id).data
|
||||
street = AddressStreet.filter_one(AddressPostcode.uu_id==data.street_uu_id).data
|
||||
if not street:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
@@ -330,8 +379,12 @@ class AddressPostCodeListEventMethods(MethodToEvent):
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def list_post_code_address(cls, list_options: ListOptions, token_dict):
|
||||
post_code_list = AddressPostcode.__many__table__.filter_active(
|
||||
def list_post_code_address(
|
||||
cls,
|
||||
list_options: ListOptions,
|
||||
token_dict: Union[EmployeeTokenObject, OccupantTokenObject],
|
||||
):
|
||||
post_code_list = AddressPostcode.__many__table__.filter_all(
|
||||
AddressPostcode.__many__table__.company_id
|
||||
== token_dict.selected_company.company_id
|
||||
).data
|
||||
@@ -341,13 +394,14 @@ class AddressPostCodeListEventMethods(MethodToEvent):
|
||||
detail="User has no post code registered or not yet any post code created.",
|
||||
)
|
||||
|
||||
AddressPostcode.pre_query = AddressPostcode.filter_active(
|
||||
AddressPostcode.pre_query = AddressPostcode.filter_all(
|
||||
AddressPostcode.id.in_(
|
||||
[post_code.member_id for post_code in post_code_list]
|
||||
)
|
||||
),
|
||||
AddressPostcode.active == True,
|
||||
).query
|
||||
AddressPostcode.filter_attr = list_options
|
||||
records = AddressPostcode.filter_active(
|
||||
records = AddressPostcode.filter_all(
|
||||
*Addresses.get_smart_query(list_options.query)
|
||||
)
|
||||
return AlchemyJsonResponse(
|
||||
|
||||
@@ -37,7 +37,7 @@ from api_services import (
|
||||
|
||||
from api_events.events.abstract_class import MethodToEvent, ActionsSchema
|
||||
from api_objects.auth.token_objects import EmployeeTokenObject, OccupantTokenObject
|
||||
from api_library.date_time_actions.date_functions import DateTimeLocal
|
||||
from api_library.date_time_actions.date_functions import DateTimeLocal, system_arrow
|
||||
from api_configs import ApiStatic, Auth
|
||||
from databases.no_sql_models.login_handlers import load_user_with_erp_details
|
||||
|
||||
@@ -51,6 +51,7 @@ from api_validations.validations_request import (
|
||||
OccupantSelection,
|
||||
EmployeeSelection,
|
||||
)
|
||||
from databases.sql_models.building.build import RelationshipEmployee2Build
|
||||
|
||||
|
||||
class AuthenticationLoginEventMethods(MethodToEvent):
|
||||
@@ -98,8 +99,8 @@ class AuthenticationSelectEventMethods(MethodToEvent):
|
||||
def authentication_select_company_or_occupant_type(
|
||||
cls,
|
||||
request: Request,
|
||||
data: typing.Union[EmployeeSelection, OccupantSelection],
|
||||
token_dict: dict = None,
|
||||
data,
|
||||
token_dict: typing.Union[EmployeeSelection, OccupantSelection],
|
||||
):
|
||||
from api_objects import OccupantToken, CompanyToken
|
||||
|
||||
@@ -116,38 +117,39 @@ class AuthenticationSelectEventMethods(MethodToEvent):
|
||||
if selected_company := Companies.find_one(uu_id=data.company_uu_id):
|
||||
department_ids = [
|
||||
department.id
|
||||
for department in Departments.filter_by_active(
|
||||
company_id=selected_company.id
|
||||
for department in Departments.filter_all(
|
||||
Departments.company_id==selected_company.id
|
||||
).data
|
||||
]
|
||||
duties_ids = [
|
||||
duties.id
|
||||
for duties in Duties.filter_active(
|
||||
for duties in Duties.filter_all(
|
||||
Duties.company_id == selected_company.id,
|
||||
Duties.department_id.in_(department_ids),
|
||||
).data
|
||||
]
|
||||
staff_ids = [
|
||||
staff.id
|
||||
for staff in Staff.filter_active(
|
||||
for staff in Staff.filter_all(
|
||||
Staff.duties_id.in_(duties_ids)
|
||||
).data
|
||||
]
|
||||
employee = Employees.filter_active(
|
||||
employee = Employees.filter_one(
|
||||
Employees.people_id == token_user.person_id,
|
||||
Employees.staff_id.in_(staff_ids),
|
||||
).data[0]
|
||||
).data
|
||||
|
||||
reachable_event_list_id, reachable_event_list_uu_id = (
|
||||
Event2Employee.get_event_id_by_employee_id(employee_id=employee.id)
|
||||
)
|
||||
staff = Staff.find_one(id=employee.staff_id)
|
||||
duties = Duties.find_one(id=staff.duties_id)
|
||||
department = Departments.find_one(id=duties.department_id)
|
||||
bulk_id = Duty.find_one(duty_code="BULK")
|
||||
bulk_duty_id = Duties.find_one(
|
||||
company_id=selected_company.id, duties_id=bulk_id.id
|
||||
)
|
||||
staff = Staff.filter_one(Staff.id==employee.staff_id).data
|
||||
duties = Duties.find_one(Duties.id==staff.duties_id).data
|
||||
department = Departments.find_one(Departments.id==duties.department_id).data
|
||||
bulk_id = Duty.filter_one(Duty.duty_code=="BULK").data
|
||||
bulk_duty_id = Duties.filter_one(
|
||||
Duties.company_id==selected_company.id,
|
||||
Duties.duties_id==bulk_id.id
|
||||
).data
|
||||
update_selected_to_redis(
|
||||
request=request,
|
||||
add_payload=CompanyToken(
|
||||
@@ -174,30 +176,36 @@ class AuthenticationSelectEventMethods(MethodToEvent):
|
||||
status_code=status.HTTP_200_OK,
|
||||
)
|
||||
elif token_user.user_type == 2:
|
||||
occupant_type = OccupantTypes.find_one(uu_id=data.occupant_uu_id)
|
||||
occupant_type = OccupantTypes.filter_one(OccupantTypes.uu_id==data.occupant_uu_id).data
|
||||
if not occupant_type:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Occupant Type is not found",
|
||||
)
|
||||
build_part = BuildParts.find_one(uu_id=data.build_part_uu_id)
|
||||
build_part = BuildParts.filter_one(BuildParts.uu_id==data.build_part_uu_id).data
|
||||
if not build_part:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Build Part is not found",
|
||||
)
|
||||
build = Build.find_one(id=build_part.build_id)
|
||||
related_company = RelationshipEmployee2Build.find_one(member_id=build.id)
|
||||
company_related = Companies.find_one(id=related_company.company_id)
|
||||
responsible_employee = Employees.find_one(id=related_company.employee_id)
|
||||
if selected_occupant_type := BuildLivingSpace.find_one(
|
||||
occupant_type=occupant_type.id,
|
||||
person_id=token_user.person_id,
|
||||
build_parts_id=build_part.id,
|
||||
):
|
||||
build = Build.filter_one(Build.id==build_part.build_id).data
|
||||
related_company = RelationshipEmployee2Build.filter_one(
|
||||
RelationshipEmployee2Build.member_id==build.id
|
||||
).data
|
||||
company_related = Companies.filter_one(
|
||||
Companies.id==related_company.company_id
|
||||
).data
|
||||
responsible_employee = Employees.filter_one(
|
||||
Employees.id==related_company.employee_id
|
||||
).data
|
||||
if selected_occupant_type := BuildLivingSpace.filter_one(
|
||||
BuildLivingSpace.occupant_type==occupant_type.id,
|
||||
BuildLivingSpace.person_id==token_user.person_id,
|
||||
BuildLivingSpace.build_parts_id==build_part.id,
|
||||
).data:
|
||||
reachable_event_list_id, reachable_event_list_uu_id = (
|
||||
Event2Occupant.get_event_id_by_build_living_space_id(
|
||||
build_living_space_id=selected_occupant_type.id
|
||||
Event2Occupant.build_living_space_id==selected_occupant_type.id
|
||||
)
|
||||
)
|
||||
update_selected_to_redis(
|
||||
@@ -242,7 +250,7 @@ class AuthenticationCheckTokenEventMethods(MethodToEvent):
|
||||
|
||||
@classmethod
|
||||
def authentication_login_with_domain_and_creds(
|
||||
cls, request, token_dict: dict = None
|
||||
cls, request, token_dict: typing.Union[EmployeeSelection, OccupantSelection],
|
||||
):
|
||||
if get_object_via_access_key(request=request):
|
||||
return JSONResponse(
|
||||
@@ -263,15 +271,17 @@ class AuthenticationRefreshEventMethods(MethodToEvent):
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def authentication_refresh_user_info(cls, request, token_dict: dict = None):
|
||||
def authentication_refresh_user_info(
|
||||
cls, request, token_dict: typing.Union[EmployeeSelection, OccupantSelection],
|
||||
):
|
||||
access_token = str(request.headers.get(Auth.ACCESS_TOKEN_TAG))
|
||||
if token_user := get_object_via_access_key(request=request):
|
||||
if found_user := Users.find_one(uu_id=token_user.get("uu_id")):
|
||||
user_token = UsersTokens.find_one(
|
||||
domain=found_user.domain_name,
|
||||
user_id=found_user.id,
|
||||
token_type="RememberMe",
|
||||
)
|
||||
if found_user := Users.filter_one(Users.uu_id==token_user.get("uu_id")).data:
|
||||
user_token = UsersTokens.filter_one(
|
||||
UsersTokens.domain==found_user.domain_name,
|
||||
UsersTokens.user_id==found_user.id,
|
||||
UsersTokens.token_type=="RememberMe",
|
||||
).data
|
||||
access_dict = {
|
||||
"access_token": access_token,
|
||||
"refresh_token": getattr(user_token, "token", None),
|
||||
@@ -299,11 +309,11 @@ class AuthenticationChangePasswordEventMethods(MethodToEvent):
|
||||
|
||||
@classmethod
|
||||
def authentication_change_password(
|
||||
cls, request, data: ChangePassword, token_dict: dict = None
|
||||
cls, request, data: ChangePassword, token_dict: typing.Union[EmployeeSelection, OccupantSelection],
|
||||
):
|
||||
token_user = get_object_via_access_key(request=request)
|
||||
if token_user.user_type == 1:
|
||||
if found_user := Users.find_one(uu_id=token_user.uu_id):
|
||||
if found_user := Users.filter_one(Users.uu_id==token_user.uu_id).data:
|
||||
if found_user.check_password(data.old_password):
|
||||
found_user.set_password(data.new_password)
|
||||
return JSONResponse(
|
||||
@@ -342,10 +352,12 @@ class AuthenticationCreatePasswordEventMethods(MethodToEvent):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_406_NOT_ACCEPTABLE, detail="Password must match"
|
||||
)
|
||||
if found_user := Users.find_one(password_token=data.password_token):
|
||||
if found_user := Users.filter_one(
|
||||
Users.password_token==data.password_token
|
||||
).data:
|
||||
found_user.create_password(password=data.password)
|
||||
found_user.password_token = None
|
||||
found_user.save()
|
||||
Users.save()
|
||||
send_email_completed = send_email(
|
||||
subject=f"Dear {found_user.user_tag}, your password has been changed.",
|
||||
receivers=[str(found_user.email)],
|
||||
@@ -386,7 +398,9 @@ class AuthenticationDisconnectUserEventMethods(MethodToEvent):
|
||||
):
|
||||
|
||||
if token_user := get_object_via_access_key(request=request):
|
||||
found_user = Users.find_one(uu_id=token_user.get("uu_id"))
|
||||
found_user = Users.filter_one(
|
||||
Users.uu_id==token_user.get("uu_id")
|
||||
).data
|
||||
if not found_user:
|
||||
return JSONResponse(
|
||||
content={
|
||||
@@ -400,7 +414,9 @@ class AuthenticationDisconnectUserEventMethods(MethodToEvent):
|
||||
for key in already_tokens:
|
||||
token_user = json.loads(redis_cli.get(key) or {})
|
||||
redis_cli.delete(key)
|
||||
selected_user = Users.find_one(uu_id=token_user.get("uu_id"))
|
||||
selected_user = Users.filter_one(
|
||||
Users.uu_id==token_user.get("uu_id")
|
||||
).data
|
||||
selected_user.remove_refresher_token(
|
||||
domain=data.domain, disconnect=True
|
||||
)
|
||||
@@ -449,7 +465,9 @@ class AuthenticationLogoutEventMethods(MethodToEvent):
|
||||
token_user = json.loads(redis_cli.get(key) or {})
|
||||
if token_user.get("domain") == data.domain:
|
||||
redis_cli.delete(key)
|
||||
selected_user = Users.find_one(uu_id=token_user.get("uu_id"))
|
||||
selected_user = Users.filter_one(
|
||||
Users.uu_id==token_user.get("uu_id")
|
||||
).data
|
||||
selected_user.remove_refresher_token(domain=data.domain)
|
||||
# UserLogger.log_error(
|
||||
# str(
|
||||
@@ -503,7 +521,9 @@ class AuthenticationRefreshTokenEventMethods(MethodToEvent):
|
||||
content={"completed": False, "message": "Invalid data", "data": {}},
|
||||
status_code=status.HTTP_202_ACCEPTED,
|
||||
)
|
||||
if found_user := Users.find_one(id=token_refresher.user_id):
|
||||
if found_user := Users.filter_one(
|
||||
Users.id==token_refresher.user_id
|
||||
).data:
|
||||
found_user: Users = found_user
|
||||
access_key = save_access_token_to_redis(
|
||||
request=request, found_user=found_user, domain=data.domain
|
||||
@@ -513,7 +533,7 @@ class AuthenticationRefreshTokenEventMethods(MethodToEvent):
|
||||
found_user.last_remote_addr = getattr(
|
||||
request, "remote_addr", None
|
||||
) or request.headers.get("X-Forwarded-For", None)
|
||||
found_user.last_seen = str(DateTimeLocal.now())
|
||||
found_user.last_seen = str(system_arrow.now())
|
||||
# UserLogger.log_error(
|
||||
# str(
|
||||
# dict(
|
||||
@@ -595,7 +615,7 @@ class AuthenticationForgotPasswordEventMethods(MethodToEvent):
|
||||
)
|
||||
|
||||
found_user.password_token = forgot_key
|
||||
found_user.password_token_is_valid = str(DateTimeLocal.shift(days=1))
|
||||
found_user.password_token_is_valid = str(system_arrow.shift(days=1))
|
||||
found_user.save()
|
||||
|
||||
return JSONResponse(
|
||||
@@ -632,13 +652,13 @@ class AuthenticationDownloadAvatarEventMethods(MethodToEvent):
|
||||
"remember_me": found_user.remember_me,
|
||||
"expiry_ends": str(found_user.expiry_ends),
|
||||
"expired_str": str(
|
||||
DateTimeLocal.now()
|
||||
- DateTimeLocal.get(str(found_user.expiry_ends))
|
||||
system_arrow.now()
|
||||
- system_arrow.get(str(found_user.expiry_ends))
|
||||
),
|
||||
"expired_int": int(
|
||||
(
|
||||
DateTimeLocal.now()
|
||||
- DateTimeLocal.get(str(found_user.expiry_ends))
|
||||
system_arrow.now()
|
||||
- system_arrow.get(str(found_user.expiry_ends))
|
||||
).days
|
||||
),
|
||||
},
|
||||
|
||||
@@ -39,16 +39,18 @@ class BuildListEventMethods(MethodToEvent):
|
||||
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
|
||||
Build.pre_query = Build.filter_all(
|
||||
Build.id == token_dict.selected_occupant.build_id,
|
||||
Build.active == True,
|
||||
).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)
|
||||
records = Build.filter_all(
|
||||
*Build.get_smart_query(smart_query=list_options.query),
|
||||
Build.active == True,
|
||||
)
|
||||
return AlchemyJsonResponse(
|
||||
completed=True,
|
||||
@@ -81,13 +83,13 @@ class BuildCreateEventMethods(MethodToEvent):
|
||||
|
||||
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")
|
||||
build_type = BuildTypes.filter_by_one(type_code="APT_YNT").data
|
||||
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")
|
||||
api_enum = ApiEnumDropdown.filter_by_one(enum_class="Directions", key="NN").data
|
||||
if not api_enum:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
@@ -107,8 +109,8 @@ class BuildCreateEventMethods(MethodToEvent):
|
||||
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)
|
||||
created_build.update(management_room_id=man_build_part.id)
|
||||
BuildParts.save()
|
||||
return JSONResponse(
|
||||
content={
|
||||
"completed": True,
|
||||
@@ -148,24 +150,19 @@ class BuildCreateEventMethods(MethodToEvent):
|
||||
)
|
||||
|
||||
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}",
|
||||
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,
|
||||
)
|
||||
|
||||
|
||||
@@ -181,10 +178,12 @@ class BuildUpdateEventMethods(MethodToEvent):
|
||||
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 Build.filter_all(Build.person_id == token_dict.person_id):
|
||||
Build.pre_query = None
|
||||
if updated_build := Build.update_action(
|
||||
data=data, token=token_dict, build_uu_id=build_uu_id
|
||||
):
|
||||
Build.save()
|
||||
return JSONResponse(
|
||||
content={
|
||||
"completed": True,
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
from typing import Union
|
||||
|
||||
from fastapi.responses import JSONResponse
|
||||
from fastapi import status
|
||||
|
||||
@@ -23,7 +25,9 @@ class BuildingBuildPartsListEventMethods(MethodToEvent):
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def building_build_parts_list(cls, list_options: ListOptions, token_dict):
|
||||
def building_build_parts_list(
|
||||
cls, list_options: ListOptions, token_dict: Union[EmployeeTokenObject, OccupantTokenObject]
|
||||
):
|
||||
build_list_query = Build.select_action(
|
||||
employee_id=token_dict.selected_company.employee_id,
|
||||
)
|
||||
@@ -49,7 +53,9 @@ class BuildingBuildPartsCreateEventMethods(MethodToEvent):
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def building_build_parts_create(cls, data: InsertBuildParts, token_dict: dict):
|
||||
def building_build_parts_create(
|
||||
cls, data: InsertBuildParts, token_dict: Union[EmployeeTokenObject, OccupantTokenObject]
|
||||
):
|
||||
created_build = BuildParts.create_action(data=data, token=token_dict)
|
||||
if not created_build:
|
||||
return JSONResponse(
|
||||
@@ -79,7 +85,9 @@ class BuildingBuildPartsUpdateEventMethods(MethodToEvent):
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def building_build_parts_update(cls, data: InsertBuildParts, token_dict: dict):
|
||||
def building_build_parts_update(
|
||||
cls, data: InsertBuildParts, token_dict: Union[EmployeeTokenObject, OccupantTokenObject]
|
||||
):
|
||||
if updated_build := BuildParts.update_action(data=data, token=token_dict):
|
||||
updated_build.save()
|
||||
return JSONResponse(
|
||||
@@ -109,7 +117,7 @@ class BuildingBuildPartsPatchEventMethods(MethodToEvent):
|
||||
|
||||
@classmethod
|
||||
def building_build_parts_patch(cls, data, token_dict):
|
||||
find_one_build = BuildParts.find_one(uu_id=data.uu_id)
|
||||
find_one_build = BuildParts.filter_one(BuildParts.uu_id==data.uu_id).data
|
||||
access_authorized_build = BuildParts.select_action(
|
||||
duty_id=token_dict.selected_company.duty_id,
|
||||
filter_expr=[BuildParts.id == find_one_build.id],
|
||||
|
||||
@@ -35,17 +35,19 @@ class BuildingLivingSpacesPartsListEventMethods(MethodToEvent):
|
||||
employee_id=token_dict.selected_company.employee_id
|
||||
)
|
||||
Build.filter_attr = list_options
|
||||
build_part_id_list_query = BuildParts.filter_active(
|
||||
build_part_id_list_query = BuildParts.filter_all(
|
||||
BuildParts.build_id.in_([build.id for build in build_id_list_query.all()]),
|
||||
)
|
||||
BuildParts.active == True,
|
||||
).data
|
||||
|
||||
list_options.query.pop("expiry_starts", None)
|
||||
list_options.query.pop("expiry_ends", None)
|
||||
|
||||
records = BuildLivingSpace.filter_active(
|
||||
records = BuildLivingSpace.filter_all(
|
||||
BuildLivingSpace.build_parts_id.in_(
|
||||
[build_part.id for build_part in build_part_id_list_query.data]
|
||||
[build_part.id for build_part in build_part_id_list_query]
|
||||
),
|
||||
BuildLivingSpace.active == True,
|
||||
*BuildLivingSpace.get_smart_query(smart_query=list_options.query),
|
||||
)
|
||||
return AlchemyJsonResponse(
|
||||
@@ -62,17 +64,17 @@ class BuildingLivingSpacesPartsListEventMethods(MethodToEvent):
|
||||
employee_id=token_dict.selected_company.employee_id
|
||||
)
|
||||
Build.filter_attr = list_options
|
||||
build_part_id_list_query = BuildParts.filter_active(
|
||||
build_part_id_list_query = BuildParts.filter_all(
|
||||
BuildParts.build_id.in_([build.id for build in build_id_list_query.all()]),
|
||||
)
|
||||
BuildParts.active == True,
|
||||
).data
|
||||
|
||||
records = BuildLivingSpace.filter_active(
|
||||
records = BuildLivingSpace.filter_all(
|
||||
BuildLivingSpace.build_parts_id.in_(
|
||||
[build_part.id for build_part in build_part_id_list_query.data]
|
||||
[build_part.id for build_part in build_part_id_list_query]
|
||||
),
|
||||
*BuildLivingSpace.get_smart_query(smart_query=list_options.query),
|
||||
expired=False,
|
||||
)
|
||||
).data
|
||||
return AlchemyJsonResponse(
|
||||
completed=True,
|
||||
message="Building Living Spaces are listed successfully",
|
||||
@@ -103,15 +105,14 @@ class BuildingLivingSpacesPartsCreateEventMethods(MethodToEvent):
|
||||
build_part = BuildParts.filter_one(
|
||||
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.data:
|
||||
).data
|
||||
if not build_part:
|
||||
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.data
|
||||
|
||||
life_person = People.find_one(uu_id=data.person_uu_id or "")
|
||||
life_person = People.filter_one(People.uu_id==data.person_uu_id or "").data
|
||||
if not life_person:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_418_IM_A_TEAPOT,
|
||||
@@ -119,7 +120,7 @@ class BuildingLivingSpacesPartsCreateEventMethods(MethodToEvent):
|
||||
f"Check build live person uu_id",
|
||||
)
|
||||
|
||||
occupant_type = OccupantTypes.find_one(uu_id=data.occupant_type_uu_id)
|
||||
occupant_type = OccupantTypes.filter_by_one(uu_id=data.occupant_type_uu_id).data
|
||||
if not occupant_type:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_418_IM_A_TEAPOT,
|
||||
@@ -134,21 +135,18 @@ class BuildingLivingSpacesPartsCreateEventMethods(MethodToEvent):
|
||||
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(system_arrow.now()) < BuildLivingSpace.expiry_ends,
|
||||
str(system_arrow.now()) >= BuildLivingSpace.expiry_starts,
|
||||
]
|
||||
)
|
||||
.order_by(BuildLivingSpace.expiry_starts.desc())
|
||||
).first()
|
||||
living_space_id = BuildLivingSpace.select_only(
|
||||
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(system_arrow.now()) < BuildLivingSpace.expiry_ends,
|
||||
str(system_arrow.now()) >= BuildLivingSpace.expiry_starts,
|
||||
select_args=[BuildLivingSpace.id],
|
||||
order_by=BuildLivingSpace.expiry_starts.desc(),
|
||||
limit=1
|
||||
).data
|
||||
|
||||
last_living_space = BuildLivingSpace.filter_one(
|
||||
BuildLivingSpace.id == living_space_id[0] if living_space_id else None
|
||||
@@ -160,11 +158,11 @@ class BuildingLivingSpacesPartsCreateEventMethods(MethodToEvent):
|
||||
)
|
||||
|
||||
if last_living_space:
|
||||
if last_living_space.expiry_ends > system_arrow.now():
|
||||
if last_living_space.expiry_ends > str(system_arrow.now()):
|
||||
last_living_space.expiry_ends = str(system_arrow.shift(minutes=-10))
|
||||
last_living_space.save()
|
||||
|
||||
user_module = Modules.filter_one(Modules.module_code == "USR-PUB")
|
||||
user_module = Modules.filter_one(Modules.module_code == "USR-PUB", system=True).data
|
||||
ModulesBindOccupantEventMethods.modules_bind_occupant_system(
|
||||
build_living_space_id=created_living_space.id,
|
||||
modules_id=user_module.id,
|
||||
@@ -189,18 +187,18 @@ class BuildingLivingSpacesPartsUpdateEventMethods(MethodToEvent):
|
||||
build_id_list_query = Build.select_action(
|
||||
employee_id=token_dict.selected_company.employee_id
|
||||
)
|
||||
build_part = BuildParts.filter_active(
|
||||
build_part = BuildParts.filter_one(
|
||||
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):
|
||||
BuildParts.active == True,
|
||||
).data
|
||||
if not build_part:
|
||||
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 "")
|
||||
life_person = People.filter_one(People.uu_id==data.life_person_uu_id or "").data
|
||||
if not life_person:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_418_IM_A_TEAPOT,
|
||||
@@ -208,15 +206,15 @@ class BuildingLivingSpacesPartsUpdateEventMethods(MethodToEvent):
|
||||
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()
|
||||
living_space_id = BuildLivingSpace.select_only(
|
||||
select_args=[BuildLivingSpace.id],
|
||||
order_by=BuildLivingSpace.expiry_starts.desc(),
|
||||
limit=1
|
||||
).get(1)
|
||||
|
||||
last_living_space = BuildLivingSpace.filter_one(
|
||||
BuildLivingSpace.id == getattr(living_space_id[0], "id", None)
|
||||
if living_space_id
|
||||
else None
|
||||
)
|
||||
BuildLivingSpace.id == living_space_id if living_space_id else None
|
||||
).data
|
||||
|
||||
data_dict["expiry_starts"] = str(system_arrow.now())
|
||||
data_dict["is_tenant_live"] = bool(data.is_tenant_live)
|
||||
|
||||
@@ -18,7 +18,7 @@ from api_validations.validations_request import (
|
||||
from api_events.events.abstract_class import MethodToEvent, ActionsSchema
|
||||
from api_objects.auth.token_objects import EmployeeTokenObject, OccupantTokenObject
|
||||
from api_validations.core_response import AlchemyJsonResponse
|
||||
from api_library.date_time_actions.date_functions import DateTimeLocal
|
||||
from api_library.date_time_actions.date_functions import DateTimeLocal, system_arrow
|
||||
|
||||
|
||||
class DecisionBookListEventMethods(MethodToEvent):
|
||||
@@ -46,15 +46,17 @@ class DecisionBookListEventMethods(MethodToEvent):
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"No building is match with given Employee UUID {token_dict.selected_company.employee_uu_id}",
|
||||
)
|
||||
records = BuildDecisionBook.filter_active(
|
||||
records = BuildDecisionBook.filter_all(
|
||||
BuildDecisionBook.build_id.in_([build.id for build in build_id_list]),
|
||||
*BuildDecisionBook.get_smart_query(list_options.query),
|
||||
)
|
||||
BuildDecisionBook.active == True,
|
||||
).data
|
||||
elif isinstance(token_dict, OccupantTokenObject):
|
||||
records = BuildDecisionBook.filter_active(
|
||||
records = BuildDecisionBook.filter_all(
|
||||
BuildDecisionBook.build_id == token_dict.selected_occupant.build_id,
|
||||
*BuildDecisionBook.get_smart_query(list_options.query),
|
||||
)
|
||||
BuildDecisionBook.active == True,
|
||||
).data
|
||||
return AlchemyJsonResponse(
|
||||
completed=True,
|
||||
message="DecisionBook are listed successfully",
|
||||
@@ -80,15 +82,16 @@ class DecisionBookCreateEventMethods(MethodToEvent):
|
||||
Build.pre_query = Build.select_action(
|
||||
employee_id=token_dict.selected_company.employee_id
|
||||
)
|
||||
build = Build.filter_active(
|
||||
build = Build.filter_one(
|
||||
Build.uu_id == data.build_uu_id,
|
||||
)
|
||||
if not build.data:
|
||||
Build.active == True,
|
||||
).get(1)
|
||||
if not build:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Emloyee UUID {token_dict.selected_company.employee_uu_id} has no build with given UUID {data_dict.get('build_uu_id')}",
|
||||
)
|
||||
data_dict["build_id"] = build.data[0].id
|
||||
data_dict["build_id"] = build.id
|
||||
if data.resp_company_uu_id:
|
||||
Companies.pre_query = Companies.select_action(
|
||||
duty_id_list=[
|
||||
@@ -96,21 +99,22 @@ class DecisionBookCreateEventMethods(MethodToEvent):
|
||||
token_dict.selected_company.bulk_duties_id,
|
||||
]
|
||||
)
|
||||
company = Companies.filter_active(
|
||||
Companies.uu_id == data.resp_company_uu_id
|
||||
)
|
||||
if not company.data:
|
||||
company = Companies.filter_one(
|
||||
Companies.uu_id == data.resp_company_uu_id,
|
||||
Companies.active == True,
|
||||
).get(1)
|
||||
if not company:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Duty UUID {token_dict.selected_company.duty_uu_id} has no company with given UUID {data_dict.get('resp_company_uu_id')}",
|
||||
)
|
||||
data_dict["resp_company_id"] = company.data[0].id
|
||||
data_dict["resp_company_uu_id"] = str(company.data[0].uu_id)
|
||||
data_dict["resp_company_id"] = company.id
|
||||
data_dict["resp_company_uu_id"] = str(company.uu_id)
|
||||
|
||||
build_object = build.data[0]
|
||||
decision_period_date = DateTimeLocal.get(build_object.decision_period_date)
|
||||
|
||||
decision_period_date = DateTimeLocal.get(build.decision_period_date)
|
||||
data_dict["expiry_starts"] = DateTimeLocal.get(
|
||||
DateTimeLocal.now().date().year,
|
||||
system_arrow.now().date().year,
|
||||
int(decision_period_date.date().month),
|
||||
int(decision_period_date.date().day),
|
||||
)
|
||||
@@ -138,20 +142,24 @@ class DecisionBookCreateEventMethods(MethodToEvent):
|
||||
detail="Only Build Manager can create decision book",
|
||||
)
|
||||
|
||||
occupant_build = Build.find_one(id=token_dict.selected_occupant.build_id)
|
||||
occupant_build = Build.filter_one(
|
||||
Build.id==token_dict.selected_occupant.build_id,
|
||||
Build.active == True,
|
||||
).get(1)
|
||||
occupant_company = Companies.find_one(
|
||||
id=token_dict.selected_occupant.responsible_company_id
|
||||
)
|
||||
Companies.id==token_dict.selected_occupant.responsible_company_id,
|
||||
Companies.active == True,
|
||||
).get(1)
|
||||
data_dict["build_id"] = occupant_build.id
|
||||
data_dict["build_uu_id"] = str(occupant_build.uu_id)
|
||||
data_dict["resp_company_id"] = occupant_company.id
|
||||
data_dict["resp_company_uu_id"] = str(occupant_company.uu_id)
|
||||
|
||||
decision_period_date = DateTimeLocal.get(
|
||||
decision_period_date = system_arrow.get(
|
||||
occupant_build.decision_period_date
|
||||
)
|
||||
data_dict["expiry_starts"] = DateTimeLocal.get(
|
||||
DateTimeLocal.now().date().year,
|
||||
system_arrow.now().date().year,
|
||||
int(decision_period_date.date().month),
|
||||
int(decision_period_date.date().day),
|
||||
)
|
||||
|
||||
@@ -75,7 +75,9 @@ class DecisionBookDecisionBookItemsListEventMethods(MethodToEvent):
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"No company is match with given Employee UUID {token_dict.selected_company.employee_uu_id}",
|
||||
)
|
||||
BuildDecisionBookItems.filter_attr = BuildDecisionBookItems.FilterModel(**data.dump())
|
||||
BuildDecisionBookItems.filter_attr = BuildDecisionBookItems.FilterModel(
|
||||
**data.dump()
|
||||
)
|
||||
records = BuildDecisionBookItems.filter_active(
|
||||
BuildDecisionBookItems.build_decision_book_id == decision_book.id
|
||||
)
|
||||
|
||||
@@ -5,7 +5,6 @@ from databases import (
|
||||
BuildParts,
|
||||
BuildDecisionBook,
|
||||
BuildDecisionBookItems,
|
||||
|
||||
BuildDecisionBookPerson,
|
||||
BuildDecisionBookPayments,
|
||||
BuildDecisionBookProjects,
|
||||
@@ -21,6 +20,7 @@ from api_objects.auth.token_objects import EmployeeTokenObject, OccupantTokenObj
|
||||
from api_validations.core_response import AlchemyJsonResponse
|
||||
from api_library.date_time_actions.date_functions import system_arrow, client_arrow
|
||||
|
||||
|
||||
class DecisionBookDecisionBookItemsDebitsListEventMethods(MethodToEvent):
|
||||
|
||||
event_type = "SELECT"
|
||||
@@ -47,6 +47,7 @@ class DecisionBookDecisionBookItemsDebitsListEventMethods(MethodToEvent):
|
||||
result=records,
|
||||
)
|
||||
|
||||
|
||||
class DecisionBookDecisionBookItemsDebitsCreateEventMethods(MethodToEvent):
|
||||
|
||||
event_type = "CREATE"
|
||||
@@ -100,4 +101,3 @@ class DecisionBookDecisionBookItemsDebitsCreateEventMethods(MethodToEvent):
|
||||
message="Decision Book Items Debits are listed",
|
||||
result=records,
|
||||
)
|
||||
|
||||
|
||||
@@ -69,9 +69,9 @@ class DecisionBookPersonAddEventMethods(MethodToEvent):
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"No Decision Book is match with given UUID {data.build_decision_book_uu_id}",
|
||||
)
|
||||
manager_occupant_type = OccupantTypes.find_or_abort(
|
||||
manager_occupant_type = OccupantTypes.filter_by_one(
|
||||
occupant_code="BU-MNG", occupant_category_type="BU"
|
||||
)
|
||||
).get(1)
|
||||
if (
|
||||
not manager_occupant_type.uu_id
|
||||
== token_dict.selected_occupant.occupant_type_uu_id
|
||||
@@ -81,42 +81,42 @@ class DecisionBookPersonAddEventMethods(MethodToEvent):
|
||||
detail="Only Build Manager can update the invitation",
|
||||
)
|
||||
|
||||
assign_occupant_type = OccupantTypes.find_or_abort(
|
||||
assign_occupant_type = OccupantTypes.filter_by_one(
|
||||
uu_id=data.occupant_type_uu_id,
|
||||
occupant_category_type="MT",
|
||||
)
|
||||
).get(1)
|
||||
if not assign_occupant_type:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Occupant type must be a Meeting Type {data.occupant_type_uu_id} is not a MT type occupant",
|
||||
)
|
||||
|
||||
manger_book_person = BuildDecisionBookPerson.find_one(
|
||||
token=data.token,
|
||||
build_decision_book_uu_id=data.build_decision_book_uu_id,
|
||||
is_confirmed=True,
|
||||
active=True,
|
||||
)
|
||||
manger_book_person = BuildDecisionBookPerson.filter_one(
|
||||
BuildDecisionBookPerson.token==data.token,
|
||||
BuildDecisionBookPerson.build_decision_book_uu_id==data.build_decision_book_uu_id,
|
||||
BuildDecisionBookPerson.is_confirmed==True,
|
||||
BuildDecisionBookPerson.active==True,
|
||||
).get(1)
|
||||
if not manger_book_person:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Manager person not found. Please check token",
|
||||
)
|
||||
book_invite = BuildDecisionBookInvitations.find_one(
|
||||
id=manger_book_person.invite_id,
|
||||
build_id=token_dict.selected_occupant.build_id,
|
||||
)
|
||||
book_invite = BuildDecisionBookInvitations.filter_one(
|
||||
BuildDecisionBookInvitations.id==manger_book_person.invite_id,
|
||||
BuildDecisionBookInvitations.build_id==token_dict.selected_occupant.build_id,
|
||||
).get(1)
|
||||
if not book_invite:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Invitation not found. Please check token",
|
||||
)
|
||||
selected_book_person = BuildDecisionBookPerson.find_one(
|
||||
invite_id=book_invite.id,
|
||||
person_uu_id=data.person_uu_id,
|
||||
is_confirmed=True,
|
||||
active=True,
|
||||
)
|
||||
selected_book_person = BuildDecisionBookPerson.filter_one(
|
||||
BuildDecisionBookPerson.invite_id==book_invite.id,
|
||||
BuildDecisionBookPerson.person_uu_id==data.person_uu_id,
|
||||
BuildDecisionBookPerson.is_confirmed==True,
|
||||
BuildDecisionBookPerson.active==True,
|
||||
).get(1)
|
||||
if not selected_book_person:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
@@ -171,10 +171,12 @@ class DecisionBookPersonAttendEventMethods(MethodToEvent):
|
||||
detail="Employee cannot create decision book invitations",
|
||||
)
|
||||
|
||||
token_user = Users.find_one(id=token_dict.user_id)
|
||||
invitation_person = BuildDecisionBookPerson.find_one(
|
||||
token=data.token, active=True, is_confirmed=True
|
||||
)
|
||||
token_user = Users.filter_one(Users.id==token_dict.user_id).get(1)
|
||||
invitation_person = BuildDecisionBookPerson.filter_one(
|
||||
BuildDecisionBookPerson.token==data.token,
|
||||
BuildDecisionBookPerson.active==True,
|
||||
BuildDecisionBookPerson.is_confirmed==True
|
||||
).get(1)
|
||||
if not invitation_person:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
@@ -189,10 +191,11 @@ class DecisionBookPersonAttendEventMethods(MethodToEvent):
|
||||
# detail=f"Invitation for the user {token_user.email} is not valid. Please check token",
|
||||
# )
|
||||
# todo check if vicarious person is valid
|
||||
invitation = BuildDecisionBookInvitations.find_one(
|
||||
id=invitation_person.invite_id,
|
||||
build_id=token_dict.selected_occupant.build_id,
|
||||
)
|
||||
invitation = BuildDecisionBookInvitations.filter_one(
|
||||
BuildDecisionBookInvitations.id==invitation_person.invite_id,
|
||||
BuildDecisionBookInvitations.build_id==token_dict.selected_occupant.build_id,
|
||||
BuildDecisionBookInvitations.active==True,
|
||||
).get(1)
|
||||
if not invitation:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
@@ -236,66 +239,60 @@ class DecisionBookPersonAssignOccupantEventMethods(MethodToEvent):
|
||||
detail="Employee cannot create decision book invitations",
|
||||
)
|
||||
|
||||
book_person_manager = BuildDecisionBookPerson.find_one(
|
||||
token=data.token,
|
||||
build_living_space_id=token_dict.selected_occupant.living_space_id,
|
||||
active=True,
|
||||
is_confirmed=True,
|
||||
)
|
||||
manager_occupant_type = OccupantTypes.find_or_abort(
|
||||
book_person_manager = BuildDecisionBookPerson.filter_one(
|
||||
BuildDecisionBookPerson.token==data.token,
|
||||
BuildDecisionBookPerson.build_living_space_id==token_dict.selected_occupant.living_space_id,
|
||||
BuildDecisionBookPerson.active==True,
|
||||
BuildDecisionBookPerson.is_confirmed==True,
|
||||
).get(1)
|
||||
manager_occupant_type = OccupantTypes.filter_by_one(
|
||||
occupant_code="BU-MNG", occupant_category_type="BU"
|
||||
)
|
||||
).get(1)
|
||||
book_person_manager.check_occupant_type(manager_occupant_type)
|
||||
|
||||
# supervisor_occupant_type = OccupantTypes.find_or_abort(occupant_code="BU-SPV", occupant_category_type="BU")
|
||||
# book_person_supervisor.check_occupant_type(supervisor_occupant_type)
|
||||
|
||||
invitation = BuildDecisionBookInvitations.find_one(
|
||||
id=book_person_manager.invite_id,
|
||||
build_id=token_dict.selected_occupant.build_id,
|
||||
active=True,
|
||||
is_confirmed=True,
|
||||
)
|
||||
invitation = BuildDecisionBookInvitations.filter_one(
|
||||
BuildDecisionBookInvitations.id==book_person_manager.invite_id,
|
||||
BuildDecisionBookInvitations.build_id==token_dict.selected_occupant.build_id,
|
||||
BuildDecisionBookInvitations.active==True,
|
||||
).get(1)
|
||||
if not invitation:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Invitation not found. Please check token",
|
||||
)
|
||||
|
||||
assign_occupant_type = OccupantTypes.find_or_abort(
|
||||
uu_id=data.occupant_type_uu_id, is_confirmed=True, active=True
|
||||
)
|
||||
assign_occupant_type = OccupantTypes.filter_by_one(
|
||||
uu_id=data.occupant_type_uu_id, active=True
|
||||
).get(1)
|
||||
if not assign_occupant_type:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Occupant type must be a Meeting Type {data.occupant_type_uu_id} is not a MT type occupant",
|
||||
)
|
||||
|
||||
build_parts_of_token = BuildParts.filter_active(
|
||||
build_parts_of_token = BuildParts.filter_all(
|
||||
BuildParts.build_id == token_dict.selected_occupant.build_id,
|
||||
)
|
||||
selected_living_space = BuildLivingSpace.filter_active(
|
||||
).data
|
||||
selected_living_space = BuildLivingSpace.filter_one(
|
||||
BuildLivingSpace.uu_id == data.build_living_space_uu_id,
|
||||
BuildLivingSpace.build_parts_id.in_(
|
||||
[build.id for build in build_parts_of_token.data]
|
||||
[build.id for build in build_parts_of_token]
|
||||
),
|
||||
)
|
||||
|
||||
if not selected_living_space.data:
|
||||
).get(1)
|
||||
if not selected_living_space:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Person not found. Please check person uuid",
|
||||
)
|
||||
|
||||
selected_living_space = selected_living_space.get(1)
|
||||
book_person_to_assign: BuildDecisionBookPerson = (
|
||||
BuildDecisionBookPerson.find_one(
|
||||
build_living_space_id=selected_living_space.id,
|
||||
invite_id=invitation.id,
|
||||
active=True,
|
||||
is_confirmed=True,
|
||||
)
|
||||
)
|
||||
book_person_to_assign: BuildDecisionBookPerson = BuildDecisionBookPerson.filter_one(
|
||||
BuildDecisionBookPerson.build_living_space_id==selected_living_space.id,
|
||||
BuildDecisionBookPerson.invite_id==invitation.id,
|
||||
BuildDecisionBookPerson.active==True
|
||||
).get(1)
|
||||
if not book_person_to_assign:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
@@ -309,16 +306,16 @@ class DecisionBookPersonAssignOccupantEventMethods(MethodToEvent):
|
||||
)
|
||||
|
||||
if assign_occupant_type.occupant_code in ("MT-PRS", "MT-WRT"):
|
||||
occupant_type_unique = OccupantTypes.find_or_abort(
|
||||
occupant_type_unique = OccupantTypes.filter_by_one(
|
||||
occupant_code=assign_occupant_type.occupant_code,
|
||||
occupant_category_type="MT",
|
||||
)
|
||||
if assigned_book_person_occupant := BuildDecisionBookPersonOccupants.find_one(
|
||||
invite_id=invitation.id,
|
||||
occupant_type_id=occupant_type_unique.id,
|
||||
active=True,
|
||||
is_confirmed=True,
|
||||
):
|
||||
).get(1)
|
||||
if assigned_book_person_occupant := BuildDecisionBookPersonOccupants.filter_one(
|
||||
BuildDecisionBookPersonOccupants.invite_id==invitation.id,
|
||||
BuildDecisionBookPersonOccupants.occupant_type_id==occupant_type_unique.id,
|
||||
BuildDecisionBookPersonOccupants.active==True,
|
||||
BuildDecisionBookPersonOccupants.is_confirmed==True,
|
||||
).get(1):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=f"Only one person can be assigned to {assign_occupant_type.occupant_code} type"
|
||||
@@ -326,13 +323,13 @@ class DecisionBookPersonAssignOccupantEventMethods(MethodToEvent):
|
||||
)
|
||||
|
||||
if assign_occupant_type.occupant_code == "BU-MNG":
|
||||
person_occupant_manager = BuildDecisionBookPersonOccupants.find_one(
|
||||
invite_id=invitation.id,
|
||||
occupant_type_id=manager_occupant_type.id,
|
||||
active=True,
|
||||
is_confirmed=True,
|
||||
person_occupant_manager = BuildDecisionBookPersonOccupants.filter_one(
|
||||
BuildDecisionBookPersonOccupants.invite_id==invitation.id,
|
||||
BuildDecisionBookPersonOccupants.occupant_type_id==manager_occupant_type.id,
|
||||
BuildDecisionBookPersonOccupants.active==True,
|
||||
BuildDecisionBookPersonOccupants.is_confirmed==True,
|
||||
)
|
||||
person_occupant_manager.delete(destroy=True)
|
||||
person_occupant_manager.query.delete()
|
||||
|
||||
book_person_to_assign.add_occupant_type(
|
||||
occupant_type=assign_occupant_type,
|
||||
|
||||
@@ -62,9 +62,9 @@ class BuildDecisionBookInvitationsCreateEventMethods(MethodToEvent):
|
||||
detail="Employee cannot create decision book invitations",
|
||||
)
|
||||
# Check token posses the occupant type of Build Manager
|
||||
occupant_manager = OccupantTypes.find_one(
|
||||
occupant_manager = OccupantTypes.filter_by_one(
|
||||
occupant_category_type="BU", occupant_code="BU-MNG"
|
||||
)
|
||||
).get(1)
|
||||
if not token_dict.selected_occupant.occupant_type_id == occupant_manager.id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
@@ -72,21 +72,24 @@ class BuildDecisionBookInvitationsCreateEventMethods(MethodToEvent):
|
||||
)
|
||||
|
||||
# Check decision book is valid for this token and building
|
||||
decision_book = BuildDecisionBook.find_one(
|
||||
uu_id=data.build_decision_book_uu_id,
|
||||
build_id=token_dict.selected_occupant.build_id,
|
||||
)
|
||||
decision_book = BuildDecisionBook.filter_one(
|
||||
BuildDecisionBook.uu_id==data.build_decision_book_uu_id,
|
||||
BuildDecisionBook.build_id==token_dict.selected_occupant.build_id,
|
||||
BuildDecisionBook.active==True,
|
||||
).get(1)
|
||||
if not decision_book:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Decision book not found. Please create decision book first",
|
||||
)
|
||||
occupant_building = Build.find_one(id=token_dict.selected_occupant.build_id)
|
||||
occupant_building = Build.filter_one(
|
||||
Build.id==token_dict.selected_occupant.build_id
|
||||
).get(1)
|
||||
|
||||
# Check meeting type is valid
|
||||
meeting_type = ApiEnumDropdown.find_one(
|
||||
meeting_type = ApiEnumDropdown.filter_by_one(
|
||||
enum_class="MeetingTypes",
|
||||
)
|
||||
).get(1)
|
||||
if not meeting_type:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
@@ -104,6 +107,9 @@ class BuildDecisionBookInvitationsCreateEventMethods(MethodToEvent):
|
||||
)
|
||||
|
||||
# Create an invitation for specific invitation type to start invite sending process
|
||||
planned_date_expires = str(
|
||||
system_arrow.get(data.planned_date).shift(days=15).date()
|
||||
),
|
||||
book_invitation = BuildDecisionBookInvitations.find_or_create(
|
||||
build_id=token_dict.selected_occupant.build_id,
|
||||
build_uu_id=token_dict.selected_occupant.build_uuid,
|
||||
@@ -114,9 +120,7 @@ class BuildDecisionBookInvitationsCreateEventMethods(MethodToEvent):
|
||||
living_part_percentage=0.51,
|
||||
message=data.message,
|
||||
planned_date=data.planned_date,
|
||||
planned_date_expires=str(
|
||||
system_arrow.get(data.planned_date).shift(days=15).date()
|
||||
),
|
||||
planned_date_expires=planned_date_expires,
|
||||
expiry_ends=str(system_arrow.get(data.planned_date).shift(days=15).date()),
|
||||
is_confirmed=True,
|
||||
)
|
||||
@@ -131,25 +135,22 @@ class BuildDecisionBookInvitationsCreateEventMethods(MethodToEvent):
|
||||
)
|
||||
|
||||
# Get all the parts of the building that is occupant in token
|
||||
build_parts = BuildParts.filter_active(
|
||||
BuildParts.build_id == occupant_building.id
|
||||
)
|
||||
build_parts = BuildParts.filter_all(
|
||||
BuildParts.build_id == occupant_building.id,
|
||||
BuildParts.active == True
|
||||
).data
|
||||
|
||||
# Get all build living spaces that is found in building with distinct person id
|
||||
occupants = OccupantTypes.filter_all()
|
||||
build_living_spaces_people = (
|
||||
BuildLivingSpace.filter_active(
|
||||
BuildLivingSpace.build_parts_id.in_(
|
||||
[build_part.id for build_part in build_parts.data]
|
||||
),
|
||||
BuildLivingSpace.occupant_type.in_(
|
||||
[occupant.id for occupant in occupants.data]
|
||||
),
|
||||
filter_records=False,
|
||||
)
|
||||
.query.distinct(BuildLivingSpace.person_id)
|
||||
.all()
|
||||
)
|
||||
occupants = OccupantTypes.filter_all(system=True)
|
||||
BuildLivingSpace.filter_attr = None
|
||||
build_living_spaces_people = BuildLivingSpace.filter_all(
|
||||
BuildLivingSpace.build_parts_id.in_(
|
||||
[build_part.id for build_part in build_parts.data]
|
||||
),
|
||||
BuildLivingSpace.occupant_type.in_(
|
||||
[occupant.id for occupant in occupants.data]
|
||||
),
|
||||
).query.distinct(BuildLivingSpace.person_id).all()
|
||||
if not build_living_spaces_people:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
@@ -199,21 +200,22 @@ class BuildDecisionBookInvitationsCreateEventMethods(MethodToEvent):
|
||||
invitations_person.add_occupant_type(occupant_type=attendance_occupant_type)
|
||||
if invitations_person and not invitations_person.is_found:
|
||||
print(f'"{invitations_person.token}",')
|
||||
spaces_user = Users.find_one(
|
||||
active=True,
|
||||
is_confirmed=True,
|
||||
person_id=build_living_spaces_user.person_id,
|
||||
)
|
||||
spaces_user = Users.filter_one(
|
||||
Users.active==True,
|
||||
Users.is_confirmed==True,
|
||||
Users.person_id==build_living_spaces_user.person_id,
|
||||
).data
|
||||
# print(
|
||||
# f"Invitation is send : {spaces_user.email} "
|
||||
# f"Token : {invitations_person.token} "
|
||||
# f"Send Date : {str(invitations_person.send_date.date())}"
|
||||
# )
|
||||
|
||||
manager_living_spaces = BuildLivingSpace.filter_active(
|
||||
manager_living_spaces = BuildLivingSpace.filter_all(
|
||||
BuildLivingSpace.person_id == token_dict.person_id,
|
||||
)
|
||||
manager_people = BuildDecisionBookPerson.filter_active(
|
||||
BuildLivingSpace.active == True,
|
||||
).data
|
||||
manager_people = BuildDecisionBookPerson.filter_all(
|
||||
BuildDecisionBookPerson.invite_id == book_invitation.id,
|
||||
BuildDecisionBookPerson.build_living_space_id.in_(
|
||||
[
|
||||
@@ -221,16 +223,15 @@ class BuildDecisionBookInvitationsCreateEventMethods(MethodToEvent):
|
||||
for manager_living_space in manager_living_spaces.data
|
||||
]
|
||||
),
|
||||
BuildLivingSpace.active == True,
|
||||
)
|
||||
manager_people_occupants = BuildDecisionBookPersonOccupants.filter_active(
|
||||
manager_people_occupants = BuildDecisionBookPersonOccupants.filter_all(
|
||||
BuildDecisionBookPersonOccupants.build_decision_book_person_id
|
||||
== manager_people.get(1).id
|
||||
== manager_people.get(1).id,
|
||||
BuildDecisionBookPersonOccupants.active == True,
|
||||
)
|
||||
dlt = [
|
||||
occupants.delete(destroy=True)
|
||||
for occupants in manager_people_occupants.data
|
||||
]
|
||||
dlt = [occupants.delete(destroy=True) for occupants in manager_people.data]
|
||||
manager_people_occupants.query.delete()
|
||||
manager_people.query.delete()
|
||||
|
||||
if book_person_manager := BuildDecisionBookPerson.find_or_create(
|
||||
**build_decision_book_person_dict,
|
||||
|
||||
Reference in New Issue
Block a user