alchemy functions updated

This commit is contained in:
berkay 2024-11-10 13:16:45 +03:00
parent 7a7241c71c
commit eb947ecb3d
10 changed files with 140 additions and 92 deletions

View File

@ -6,6 +6,7 @@ from fastapi.requests import Request
from fastapi.exceptions import HTTPException from fastapi.exceptions import HTTPException
from fastapi.responses import JSONResponse from fastapi.responses import JSONResponse
from api_objects import UserType
from databases import ( from databases import (
Companies, Companies,
Staff, Staff,
@ -150,10 +151,10 @@ class AuthenticationSelectEventMethods(MethodToEvent):
staff = Staff.filter_one( staff = Staff.filter_one(
Staff.id == employee.staff_id, *Staff.valid_record_args(Staff) Staff.id == employee.staff_id, *Staff.valid_record_args(Staff)
).data ).data
duties = Duties.find_one( duties = Duties.filter_one(
Duties.id == staff.duties_id, *Duties.valid_record_args(Duties) Duties.id == staff.duties_id, *Duties.valid_record_args(Duties)
).data ).data
department = Departments.find_one( department = Departments.filter_one(
Departments.id == duties.department_id, Departments.id == duties.department_id,
*Departments.valid_record_args(Departments), *Departments.valid_record_args(Departments),
).data ).data
@ -556,8 +557,8 @@ class AuthenticationRefreshTokenEventMethods(MethodToEvent):
def authentication_refresher_token( def authentication_refresher_token(
cls, request: Request, data: Remember, token_dict: dict = None cls, request: Request, data: Remember, token_dict: dict = None
): ):
token_refresher = UsersTokens.find_one( token_refresher = UsersTokens.filter_by_one(
token=data.refresh_token, domain=data.domain token=data.refresh_token, domain=data.domain, *UsersTokens.valid_record_dict
) )
if not token_refresher: if not token_refresher:
return JSONResponse( return JSONResponse(

View File

@ -4,6 +4,7 @@ from typing import Union
from fastapi import status, HTTPException from fastapi import status, HTTPException
from fastapi.responses import JSONResponse from fastapi.responses import JSONResponse
from api_library.date_time_actions.date_functions import system_arrow
from api_validations.validations_request import ( from api_validations.validations_request import (
InsertEmployees, InsertEmployees,
BindEmployees2People, BindEmployees2People,
@ -56,8 +57,14 @@ class EmployeeCreateEventMethods(MethodToEvent):
data: InsertEmployees, data: InsertEmployees,
token_dict: Union[EmployeeTokenObject, OccupantTokenObject], token_dict: Union[EmployeeTokenObject, OccupantTokenObject],
): ):
person = People.find_one(uu_id=data.people_uu_id) person = People.filter_one(
staff = Staff.find_one(uu_id=data.staff_uu_id) People.uu_id==data.people_uu_id,
*People.valid_record_args(People),
).data
staff = Staff.filter_one(
Staff.uu_id==data.staff_uu_id,
*Staff.valid_record_args(Staff),
).data
if not staff: if not staff:
return JSONResponse( return JSONResponse(
content={ content={
@ -67,8 +74,7 @@ class EmployeeCreateEventMethods(MethodToEvent):
}, },
status_code=status.HTTP_404_NOT_FOUND, status_code=status.HTTP_404_NOT_FOUND,
) )
created_employee = Employees.find_or_create(
created_employee = Employees.create(
staff_id=staff.id, staff_id=staff.id,
staff_uu_id=str(staff.uu_id), staff_uu_id=str(staff.uu_id),
people_id=person.id if person else None, people_id=person.id if person else None,
@ -77,7 +83,7 @@ class EmployeeCreateEventMethods(MethodToEvent):
Employees.save() Employees.save()
return JSONResponse( return JSONResponse(
content={ content={
"completed": True if not created_employee.is_found else False, "completed": True,
"message": "Create Employee record", "message": "Create Employee record",
"data": created_employee.get_dict(), "data": created_employee.get_dict(),
}, },
@ -94,7 +100,10 @@ class EmployeeUpdateEventMethods(MethodToEvent):
@classmethod @classmethod
def employee_update(cls, employee_uu_id: str, data: PatchRecord, token_dict): def employee_update(cls, employee_uu_id: str, data: PatchRecord, token_dict):
find_one_employee = Employees.find_one_or_abort(uu_id=employee_uu_id) find_one_employee = Employees.filter_one(
Employees.uu_id==employee_uu_id,
*Employees.valid_record_args(Employees),
).data
access_authorized_employee = Employees.select_action( access_authorized_employee = Employees.select_action(
employee_id=getattr(token_dict, "employee_id", 5), employee_id=getattr(token_dict, "employee_id", 5),
filter_expr=[Employees.id == token_dict.get("")], filter_expr=[Employees.id == token_dict.get("")],
@ -178,8 +187,14 @@ class Employee2PeopleEmployEventMethods(MethodToEvent):
@classmethod @classmethod
def company_employee_employ(cls, data: BindEmployees2People, token_dict): def company_employee_employ(cls, data: BindEmployees2People, token_dict):
selected_staff = Staff.find_one(uu_id=data.staff_uu_id) selected_staff = Staff.filter_one(
selected_people = People.find_one(uu_id=data.people_uu_id) Staff.uu_id==data.staff_uu_id,
*Staff.valid_record_args(Staff),
).data
selected_people = People.filter_one(
People.uu_id==data.people_uu_id,
*People.valid_record_args
).data
if not selected_staff: if not selected_staff:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, status_code=status.HTTP_404_NOT_FOUND,
@ -193,7 +208,7 @@ class Employee2PeopleEmployEventMethods(MethodToEvent):
find_one_employee = Employees.filter_active( find_one_employee = Employees.filter_active(
Employees.staff_id == selected_staff.id, Employees.staff_id == selected_staff.id,
Employees.people_id == None, *Employees.valid_record_args(Employees),
).data ).data
staff_name_upper = str(selected_staff.staff_name).upper() staff_name_upper = str(selected_staff.staff_name).upper()
@ -207,9 +222,10 @@ class Employee2PeopleEmployEventMethods(MethodToEvent):
status_code=status.HTTP_406_NOT_ACCEPTABLE, status_code=status.HTTP_406_NOT_ACCEPTABLE,
) )
if not data.expiry_starts: if not data.expiry_starts:
data.expiry_starts = datetime.now() data.expiry_starts = str(system_arrow.now())
data.expiry_starts = str(system_arrow.get(str(data.expiry_starts)))
find_one_employee = find_one_employee[0].update( find_one_employee = find_one_employee.update(
people_id=selected_people.id, people_id=selected_people.id,
expiry_starts=data.expiry_starts, expiry_starts=data.expiry_starts,
**token_dict.update_creds, **token_dict.update_creds,
@ -234,14 +250,20 @@ class Employee2PeopleFireEventMethods(MethodToEvent):
@classmethod @classmethod
def company_employee_fire(cls, data: BindEmployees2People, token_dict): def company_employee_fire(cls, data: BindEmployees2People, token_dict):
selected_people = People.find_one(uu_id=data.people_uu_id) selected_people = People.filter_one(
People.uu_id==data.people_uu_id,
*People.valid_record_args(People),
).data
if not selected_people: if not selected_people:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, status_code=status.HTTP_404_NOT_FOUND,
detail="People record not found", detail="People record not found",
) )
find_one_employee: Employees = Employees.find_one(people_id=selected_people.id) find_one_employee: Employees = Employees.filter_one(
Employees.people_id==selected_people.id,
*Employees.valid_record_args(Employees),
).data
if not find_one_employee: if not find_one_employee:
return JSONResponse( return JSONResponse(
content={ content={

View File

@ -44,7 +44,10 @@ class StaffCreateEventMethods(MethodToEvent):
@classmethod @classmethod
def staff_create(cls, data: InsertStaff, token_dict: EmployeeTokenObject): def staff_create(cls, data: InsertStaff, token_dict: EmployeeTokenObject):
data_dict = data.excluded_dump() data_dict = data.excluded_dump()
duties = Duties.find_one(uu_id=data.duties_uu_id) duties = Duties.filter_one(
Duties.uu_id==data.duties_uu_id,
*Duties.valid_record_args(Duties),
).data
if not duties: if not duties:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, status_code=status.HTTP_404_NOT_FOUND,
@ -73,9 +76,13 @@ class StaffGetByUUIDEventMethods(MethodToEvent):
@classmethod @classmethod
def staff_get_by_uu_id(cls, data: SelectStaff, token_dict): def staff_get_by_uu_id(cls, data: SelectStaff, token_dict):
if data.duties_uu_id: if data.duties_uu_id:
duties_id = Duties.find_one(uu_id=data.duties_uu_id) duties_id = Duties.filter_one(
selected_staffs = Staff.filter_active( Duties.uu_id==data.duties_uu_id,
*Duties.valid_record_args(Duties)
).data
selected_staffs = Staff.filter_all(
Staff.duties_id == duties_id.id, Staff.duties_id == duties_id.id,
*Staff.valid_record_args(Staff)
) )
return JSONResponse( return JSONResponse(
content={ content={

View File

@ -132,7 +132,7 @@ class DecisionBookCreateEventMethods(MethodToEvent):
), ),
) )
elif isinstance(token_dict, OccupantTokenObject): elif isinstance(token_dict, OccupantTokenObject):
occupant_manager = OccupantTypes.find_one( occupant_manager = OccupantTypes.filter_by_one(
occupant_category_type="BU", occupant_code="BU-MNG" occupant_category_type="BU", occupant_code="BU-MNG"
) )
if not token_dict.selected_occupant.occupant_type_id == occupant_manager.id: if not token_dict.selected_occupant.occupant_type_id == occupant_manager.id:
@ -145,9 +145,9 @@ class DecisionBookCreateEventMethods(MethodToEvent):
Build.id == token_dict.selected_occupant.build_id, Build.id == token_dict.selected_occupant.build_id,
*Build.valid_record_args(Build), *Build.valid_record_args(Build),
).get(1) ).get(1)
occupant_company = Companies.find_one( occupant_company = Companies.filter_one(
Companies.id == token_dict.selected_occupant.responsible_company_id, Companies.id == token_dict.selected_occupant.responsible_company_id,
Companies.active == True, *Companies.valid_record_args(Companies),
).get(1) ).get(1)
data_dict["build_id"] = occupant_build.id data_dict["build_id"] = occupant_build.id
data_dict["build_uu_id"] = str(occupant_build.uu_id) data_dict["build_uu_id"] = str(occupant_build.uu_id)

View File

@ -42,7 +42,10 @@ class DecisionBookDecisionBookItemsListEventMethods(MethodToEvent):
data: ListDecisionBook, data: ListDecisionBook,
token_dict: typing.Union[EmployeeTokenObject, OccupantTokenObject], token_dict: typing.Union[EmployeeTokenObject, OccupantTokenObject],
): ):
decision_book = BuildDecisionBook.find_one(uu_id=data.build_decision_book_uu_id) decision_book = BuildDecisionBook.filter_one(
BuildDecisionBook.uu_id==data.build_decision_book_uu_id,
*BuildDecisionBook.valid_record_args(BuildDecisionBook),
)
if not decision_book: if not decision_book:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, status_code=status.HTTP_404_NOT_FOUND,
@ -227,17 +230,20 @@ class DecisionBookDecisionBookItemsCreateEventMethods(MethodToEvent):
BuildDecisionBookProjects.build_decision_book_id == decision_book.id, BuildDecisionBookProjects.build_decision_book_id == decision_book.id,
BuildDecisionBookProjects.project_type BuildDecisionBookProjects.project_type
== f"{decision_book.decision_type}_{data_info_type.key}", == f"{decision_book.decision_type}_{data_info_type.key}",
) ).data
management_room = BuildParts.find_one( management_room = BuildParts.filter_one(
build_id=build_id, part_no=0, active=True, is_confirmed=True BuildParts.build_id==build_id,
) BuildParts.part_no==0,
occupant_man = OccupantTypes.find_one( *BuildParts.valid_record_args(BuildParts),
).data
occupant_man = OccupantTypes.filter_by_one(
occupant_code="MT-VPR", occupant_category_type="MT" occupant_code="MT-VPR", occupant_category_type="MT"
) ).data
manager_living_space = BuildLivingSpace.filter_by_active( manager_living_space = BuildLivingSpace.filter_one(
build_parts_id=management_room.id, BuildLivingSpace.build_parts_id==management_room.id,
occupant_type=occupant_man.id, BuildLivingSpace.occupant_type==occupant_man.id,
) *BuildLivingSpace.valid_record_args(BuildLivingSpace)
).data
if not manager_living_space.data: if not manager_living_space.data:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, status_code=status.HTTP_400_BAD_REQUEST,
@ -266,9 +272,9 @@ class DecisionBookDecisionBookItemsCreateEventMethods(MethodToEvent):
item_comment=f"{book_project_created.project_no}_{book_project_created.project_name} " item_comment=f"{book_project_created.project_no}_{book_project_created.project_name} "
f"is assigned to {occupant_man.occupant_description}" f"is assigned to {occupant_man.occupant_description}"
) )
project_lead = ApiEnumDropdown.find_one( project_lead = ApiEnumDropdown.filter_by_one(
key="PTT-LDR", enum_class="ProjectTeamTypes" key="PTT-LDR", enum_class="ProjectTeamTypes"
) ).data
project_person = BuildDecisionBookProjectPerson.find_or_create( project_person = BuildDecisionBookProjectPerson.find_or_create(
build_decision_book_project_id=book_project_created.id, build_decision_book_project_id=book_project_created.id,
build_decision_book_project_uu_id=str(book_project_created.uu_id), build_decision_book_project_uu_id=str(book_project_created.uu_id),
@ -305,9 +311,9 @@ class DecisionBookDecisionBookItemsCreateEventMethods(MethodToEvent):
) )
elif isinstance(token_dict, OccupantTokenObject): elif isinstance(token_dict, OccupantTokenObject):
data_dict = data.dump() data_dict = data.dump()
occupant_wrt = OccupantTypes.find_one( occupant_wrt = OccupantTypes.filter_by_one(
occupant_code="MT-WRT", occupant_category_type="MT" occupant_code="MT-WRT", occupant_category_type="MT"
) ).data
if token_dict.selected_occupant.occupant_type_id != occupant_wrt.id: if token_dict.selected_occupant.occupant_type_id != occupant_wrt.id:
raise HTTPException( raise HTTPException(
@ -315,10 +321,14 @@ class DecisionBookDecisionBookItemsCreateEventMethods(MethodToEvent):
detail="Only WRITER can create decision book item. Check your occupant type and try again", detail="Only WRITER can create decision book item. Check your occupant type and try again",
) )
decision_book_person = BuildDecisionBookPerson.find_one(token=data.token) decision_book_person = BuildDecisionBookPerson.filter_one(
decision_book = BuildDecisionBook.find_one( BuildDecisionBookPerson.token==data.token,
id=decision_book_person.build_decision_book_id *BuildDecisionBookPerson.valid_record_args(BuildDecisionBookPerson)
) ).data
decision_book = BuildDecisionBook.filter_one(
BuildDecisionBook.id==decision_book_person.build_decision_book_id,
*BuildDecisionBook.valid_record_args(BuildDecisionBook)
).data
BuildDecisionBookItems.check_meeting_is_valid_to_start_add_attendance( BuildDecisionBookItems.check_meeting_is_valid_to_start_add_attendance(
decision_book=decision_book, decision_book=decision_book,
token_dict=token_dict, token_dict=token_dict,

View File

@ -61,9 +61,10 @@ class DecisionBookPersonAddEventMethods(MethodToEvent):
detail="Employee cannot create decision book invitations", detail="Employee cannot create decision book invitations",
) )
elif isinstance(token_dict, OccupantTokenObject): elif isinstance(token_dict, OccupantTokenObject):
decision_book = BuildDecisionBook.find_one( decision_book = BuildDecisionBook.filter_one(
uu_id=data.build_decision_book_uu_id BuildDecisionBook.uu_id==data.build_decision_book_uu_id,
) *BuildDecisionBook.valid_record_args(BuildDecisionBook)
).data
if not decision_book: if not decision_book:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, status_code=status.HTTP_404_NOT_FOUND,

View File

@ -183,10 +183,11 @@ class BuildDecisionBookInvitationsCreateEventMethods(MethodToEvent):
) )
# Check if the invitation is already created at database # Check if the invitation is already created at database
for build_living_spaces_user in build_living_spaces_people: for build_living_spaces_user in build_living_spaces_people:
if invite := BuildDecisionBookPerson.find_one( if invite := BuildDecisionBookPerson.filter_one(
invite_id=book_invitation.id, BuildDecisionBookPerson.invite_id==book_invitation.id,
build_living_space_id=build_living_spaces_user.id, BuildDecisionBookPerson.build_living_space_id==build_living_spaces_user.id,
): *BuildDecisionBookPerson.valid_record_args(BuildDecisionBookPerson)
).data:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Invitation already send to {build_living_spaces_user.email} " detail=f"Invitation already send to {build_living_spaces_user.email} "

View File

@ -135,14 +135,26 @@ def save_access_token_to_redis(
companies_uu_id_list, companies_id_list = [], [] companies_uu_id_list, companies_id_list = [], []
duty_uu_id_list, duty_id_list = [], [] duty_uu_id_list, duty_id_list = [], []
for employee in list_employee.data: for employee in list_employee.data:
staff = Staff.find_one(id=employee.staff_id) staff = Staff.filter_one(
if duties := Duties.find_one(id=staff.duties_id): Staff.id==employee.staff_id,
if duty_found := Duty.find_one(id=duties.duties_id): *Staff.valid_record_args(Staff)
).data
if duties := Duties.filter_one(
Duties.id == staff.duties_id,
*Duties.valid_record_args(Duties)
).data:
if duty_found := Duty.filter_by_one(id=duties.duties_id).data:
duty_uu_id_list.append(str(duty_found.uu_id)) duty_uu_id_list.append(str(duty_found.uu_id))
duty_id_list.append(duty_found.id) duty_id_list.append(duty_found.id)
department = Departments.find_one(id=duties.department_id) department = Departments.filter_one(
if company := Companies.find_one(id=department.company_id): Departments.id==duties.department_id,
*Departments.valid_record_args(Departments),
)
if company := Companies.filter_one(
Companies.id==department.company_id,
*Companies.valid_record_args(Companies),
).data:
companies_uu_id_list.append(str(company.uu_id)) companies_uu_id_list.append(str(company.uu_id))
companies_id_list.append(company.id) companies_id_list.append(company.id)

View File

@ -10,9 +10,6 @@ from api_events.events.events.events_bind_services import (
) )
def create_all_events_from_actions():
import api_events.events as events
active_confirmed = dict( active_confirmed = dict(
created_by="System", created_by="System",
confirmed_by="System", confirmed_by="System",
@ -21,6 +18,10 @@ def create_all_events_from_actions():
deleted=False, deleted=False,
is_notification_send=True, is_notification_send=True,
) )
def create_all_events_from_actions():
import api_events.events as events
an_empty_list, duplicate_list = [], [] an_empty_list, duplicate_list = [], []
for event in events.__all__: for event in events.__all__:
@ -34,14 +35,14 @@ def create_all_events_from_actions():
if endpoint_match := event_selected.action_match: if endpoint_match := event_selected.action_match:
if event_selected_function: if event_selected_function:
selected_event = Events.find_one( selected_event = Events.filter_one(
event_type=event_selected.event_type, Events.event_type==event_selected.event_type,
function_class=event, Events.function_class==event,
function_code=event_selected_key, Events.function_code==event_selected_key,
endpoint_id=endpoint_match.id, Events.endpoint_id==endpoint_match.id,
endpoint_uu_id=str(endpoint_match.uu_id), Events.endpoint_uu_id==str(endpoint_match.uu_id),
**active_confirmed, *Events.valid_record_args(Events)
) ).data
if not selected_event: if not selected_event:
created_event = Events.create( created_event = Events.create(
event_type=event_selected.event_type, event_type=event_selected.event_type,
@ -49,7 +50,7 @@ def create_all_events_from_actions():
function_code=event_selected_key, function_code=event_selected_key,
endpoint_id=endpoint_match.id, endpoint_id=endpoint_match.id,
endpoint_uu_id=str(endpoint_match.uu_id), endpoint_uu_id=str(endpoint_match.uu_id),
**active_confirmed, **active_confirmed
) )
print(f"Event created: {created_event.uu_id}") print(f"Event created: {created_event.uu_id}")
@ -69,19 +70,13 @@ def create_all_events_from_actions():
def add_events_all_services_and_occupant_types(): def add_events_all_services_and_occupant_types():
import api_events.tasks2events as tasks2events import api_events.tasks2events as tasks2events
active_confirmed = dict(
created_by="System",
confirmed_by="System",
is_confirmed=True,
active=True,
deleted=False,
is_notification_send=True,
)
for event_block in tasks2events.__all__: for event_block in tasks2events.__all__:
event_block_class = getattr(tasks2events, event_block) event_block_class = getattr(tasks2events, event_block)
service_selected = Services.find_one( service_selected = Services.filter_one(
service_code=getattr(event_block_class, "service_code", None), Services.service_code==getattr(event_block_class, "service_code", None),
) *Services.valid_record_args(Services)
).data
if not service_selected: if not service_selected:
raise Exception(f"{event_block_class.service_code} service is not found") raise Exception(f"{event_block_class.service_code} service is not found")
@ -95,21 +90,21 @@ def add_events_all_services_and_occupant_types():
service_uu_id=str(service_selected.uu_id), service_uu_id=str(service_selected.uu_id),
event_id=event_id, event_id=event_id,
event_uu_id=event_uu_id, event_uu_id=event_uu_id,
**active_confirmed, **active_confirmed
) )
def add_events_to_system_super_user(): def add_events_to_system_super_user():
add_service = Services.find_one(service_code="SRE-SUE") add_service = Services.filter_by_one(service_code="SRE-SUE", **Services.valid_record_dict)
if not add_service: if not add_service:
raise Exception("Service not found") raise Exception("Service not found")
find_staff = Staff.find_one(staff_code="SUE") find_staff = Staff.filter_by_one(staff_code="SUE", **Staff.valid_record_dict)
if not find_staff: if not find_staff:
raise Exception("Super User not found") raise Exception("Super User not found")
add_employee = Employees.find_one(staff_id=find_staff.id) add_employee = Employees.filter_by_one(staff_id=find_staff.id, **Employees.valid_record_dict)
if not add_employee: if not add_employee:
raise Exception("Super User Employee not found") raise Exception("Super User Employee not found")

View File

@ -180,9 +180,9 @@ def create_application_defaults():
Duties.init_a_company_default_duties( Duties.init_a_company_default_duties(
company_id=company_management.id, company_uu_id=str(company_management.uu_id) company_id=company_management.id, company_uu_id=str(company_management.uu_id)
) )
bulk_duty = Duty.find_one( bulk_duty = Duty.filter_by_one(
duty_code="BULK", duty_code="BULK",
) ).data
RelationshipDutyCompany.find_or_create( RelationshipDutyCompany.find_or_create(
duties_id=bulk_duty.id, duties_id=bulk_duty.id,
@ -195,14 +195,13 @@ def create_application_defaults():
is_notification_send=True, is_notification_send=True,
) )
it_dept = Departments.find_one( it_dept = Departments.filter_by_one(
department_name="IT Department", department_name="IT Department",
department_code="ITD001", department_code="ITD001",
company_id=company_management.id, company_id=company_management.id,
company_uu_id=str(company_management.uu_id), company_uu_id=str(company_management.uu_id),
**dict( **Departments.valid_record_dict,
is_confirmed=True, active=True, deleted=False, is_notification_send=True is_confirmed=True,
),
) )
Duty.find_or_create( Duty.find_or_create(