alchemy functions updated
This commit is contained in:
parent
7a7241c71c
commit
eb947ecb3d
|
|
@ -6,6 +6,7 @@ from fastapi.requests import Request
|
|||
from fastapi.exceptions import HTTPException
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from api_objects import UserType
|
||||
from databases import (
|
||||
Companies,
|
||||
Staff,
|
||||
|
|
@ -150,10 +151,10 @@ class AuthenticationSelectEventMethods(MethodToEvent):
|
|||
staff = Staff.filter_one(
|
||||
Staff.id == employee.staff_id, *Staff.valid_record_args(Staff)
|
||||
).data
|
||||
duties = Duties.find_one(
|
||||
duties = Duties.filter_one(
|
||||
Duties.id == staff.duties_id, *Duties.valid_record_args(Duties)
|
||||
).data
|
||||
department = Departments.find_one(
|
||||
department = Departments.filter_one(
|
||||
Departments.id == duties.department_id,
|
||||
*Departments.valid_record_args(Departments),
|
||||
).data
|
||||
|
|
@ -556,8 +557,8 @@ class AuthenticationRefreshTokenEventMethods(MethodToEvent):
|
|||
def authentication_refresher_token(
|
||||
cls, request: Request, data: Remember, token_dict: dict = None
|
||||
):
|
||||
token_refresher = UsersTokens.find_one(
|
||||
token=data.refresh_token, domain=data.domain
|
||||
token_refresher = UsersTokens.filter_by_one(
|
||||
token=data.refresh_token, domain=data.domain, *UsersTokens.valid_record_dict
|
||||
)
|
||||
if not token_refresher:
|
||||
return JSONResponse(
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ from typing import Union
|
|||
from fastapi import status, HTTPException
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from api_library.date_time_actions.date_functions import system_arrow
|
||||
from api_validations.validations_request import (
|
||||
InsertEmployees,
|
||||
BindEmployees2People,
|
||||
|
|
@ -56,8 +57,14 @@ class EmployeeCreateEventMethods(MethodToEvent):
|
|||
data: InsertEmployees,
|
||||
token_dict: Union[EmployeeTokenObject, OccupantTokenObject],
|
||||
):
|
||||
person = People.find_one(uu_id=data.people_uu_id)
|
||||
staff = Staff.find_one(uu_id=data.staff_uu_id)
|
||||
person = People.filter_one(
|
||||
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:
|
||||
return JSONResponse(
|
||||
content={
|
||||
|
|
@ -67,8 +74,7 @@ class EmployeeCreateEventMethods(MethodToEvent):
|
|||
},
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
)
|
||||
|
||||
created_employee = Employees.create(
|
||||
created_employee = Employees.find_or_create(
|
||||
staff_id=staff.id,
|
||||
staff_uu_id=str(staff.uu_id),
|
||||
people_id=person.id if person else None,
|
||||
|
|
@ -77,7 +83,7 @@ class EmployeeCreateEventMethods(MethodToEvent):
|
|||
Employees.save()
|
||||
return JSONResponse(
|
||||
content={
|
||||
"completed": True if not created_employee.is_found else False,
|
||||
"completed": True,
|
||||
"message": "Create Employee record",
|
||||
"data": created_employee.get_dict(),
|
||||
},
|
||||
|
|
@ -94,7 +100,10 @@ class EmployeeUpdateEventMethods(MethodToEvent):
|
|||
|
||||
@classmethod
|
||||
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(
|
||||
employee_id=getattr(token_dict, "employee_id", 5),
|
||||
filter_expr=[Employees.id == token_dict.get("")],
|
||||
|
|
@ -178,8 +187,14 @@ class Employee2PeopleEmployEventMethods(MethodToEvent):
|
|||
|
||||
@classmethod
|
||||
def company_employee_employ(cls, data: BindEmployees2People, token_dict):
|
||||
selected_staff = Staff.find_one(uu_id=data.staff_uu_id)
|
||||
selected_people = People.find_one(uu_id=data.people_uu_id)
|
||||
selected_staff = Staff.filter_one(
|
||||
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:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
|
|
@ -193,7 +208,7 @@ class Employee2PeopleEmployEventMethods(MethodToEvent):
|
|||
|
||||
find_one_employee = Employees.filter_active(
|
||||
Employees.staff_id == selected_staff.id,
|
||||
Employees.people_id == None,
|
||||
*Employees.valid_record_args(Employees),
|
||||
).data
|
||||
|
||||
staff_name_upper = str(selected_staff.staff_name).upper()
|
||||
|
|
@ -207,9 +222,10 @@ class Employee2PeopleEmployEventMethods(MethodToEvent):
|
|||
status_code=status.HTTP_406_NOT_ACCEPTABLE,
|
||||
)
|
||||
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,
|
||||
expiry_starts=data.expiry_starts,
|
||||
**token_dict.update_creds,
|
||||
|
|
@ -234,14 +250,20 @@ class Employee2PeopleFireEventMethods(MethodToEvent):
|
|||
|
||||
@classmethod
|
||||
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:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_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:
|
||||
return JSONResponse(
|
||||
content={
|
||||
|
|
|
|||
|
|
@ -44,7 +44,10 @@ class StaffCreateEventMethods(MethodToEvent):
|
|||
@classmethod
|
||||
def staff_create(cls, data: InsertStaff, token_dict: EmployeeTokenObject):
|
||||
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:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
|
|
@ -73,9 +76,13 @@ class StaffGetByUUIDEventMethods(MethodToEvent):
|
|||
@classmethod
|
||||
def staff_get_by_uu_id(cls, data: SelectStaff, token_dict):
|
||||
if data.duties_uu_id:
|
||||
duties_id = Duties.find_one(uu_id=data.duties_uu_id)
|
||||
selected_staffs = Staff.filter_active(
|
||||
duties_id = Duties.filter_one(
|
||||
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.valid_record_args(Staff)
|
||||
)
|
||||
return JSONResponse(
|
||||
content={
|
||||
|
|
|
|||
|
|
@ -132,7 +132,7 @@ class DecisionBookCreateEventMethods(MethodToEvent):
|
|||
),
|
||||
)
|
||||
elif isinstance(token_dict, OccupantTokenObject):
|
||||
occupant_manager = OccupantTypes.find_one(
|
||||
occupant_manager = OccupantTypes.filter_by_one(
|
||||
occupant_category_type="BU", occupant_code="BU-MNG"
|
||||
)
|
||||
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.valid_record_args(Build),
|
||||
).get(1)
|
||||
occupant_company = Companies.find_one(
|
||||
occupant_company = Companies.filter_one(
|
||||
Companies.id == token_dict.selected_occupant.responsible_company_id,
|
||||
Companies.active == True,
|
||||
*Companies.valid_record_args(Companies),
|
||||
).get(1)
|
||||
data_dict["build_id"] = occupant_build.id
|
||||
data_dict["build_uu_id"] = str(occupant_build.uu_id)
|
||||
|
|
|
|||
|
|
@ -42,7 +42,10 @@ class DecisionBookDecisionBookItemsListEventMethods(MethodToEvent):
|
|||
data: ListDecisionBook,
|
||||
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:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
|
|
@ -227,17 +230,20 @@ class DecisionBookDecisionBookItemsCreateEventMethods(MethodToEvent):
|
|||
BuildDecisionBookProjects.build_decision_book_id == decision_book.id,
|
||||
BuildDecisionBookProjects.project_type
|
||||
== f"{decision_book.decision_type}_{data_info_type.key}",
|
||||
)
|
||||
management_room = BuildParts.find_one(
|
||||
build_id=build_id, part_no=0, active=True, is_confirmed=True
|
||||
)
|
||||
occupant_man = OccupantTypes.find_one(
|
||||
).data
|
||||
management_room = BuildParts.filter_one(
|
||||
BuildParts.build_id==build_id,
|
||||
BuildParts.part_no==0,
|
||||
*BuildParts.valid_record_args(BuildParts),
|
||||
).data
|
||||
occupant_man = OccupantTypes.filter_by_one(
|
||||
occupant_code="MT-VPR", occupant_category_type="MT"
|
||||
)
|
||||
manager_living_space = BuildLivingSpace.filter_by_active(
|
||||
build_parts_id=management_room.id,
|
||||
occupant_type=occupant_man.id,
|
||||
)
|
||||
).data
|
||||
manager_living_space = BuildLivingSpace.filter_one(
|
||||
BuildLivingSpace.build_parts_id==management_room.id,
|
||||
BuildLivingSpace.occupant_type==occupant_man.id,
|
||||
*BuildLivingSpace.valid_record_args(BuildLivingSpace)
|
||||
).data
|
||||
if not manager_living_space.data:
|
||||
raise HTTPException(
|
||||
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} "
|
||||
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"
|
||||
)
|
||||
).data
|
||||
project_person = BuildDecisionBookProjectPerson.find_or_create(
|
||||
build_decision_book_project_id=book_project_created.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):
|
||||
data_dict = data.dump()
|
||||
occupant_wrt = OccupantTypes.find_one(
|
||||
occupant_wrt = OccupantTypes.filter_by_one(
|
||||
occupant_code="MT-WRT", occupant_category_type="MT"
|
||||
)
|
||||
).data
|
||||
|
||||
if token_dict.selected_occupant.occupant_type_id != occupant_wrt.id:
|
||||
raise HTTPException(
|
||||
|
|
@ -315,10 +321,14 @@ class DecisionBookDecisionBookItemsCreateEventMethods(MethodToEvent):
|
|||
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 = BuildDecisionBook.find_one(
|
||||
id=decision_book_person.build_decision_book_id
|
||||
)
|
||||
decision_book_person = BuildDecisionBookPerson.filter_one(
|
||||
BuildDecisionBookPerson.token==data.token,
|
||||
*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(
|
||||
decision_book=decision_book,
|
||||
token_dict=token_dict,
|
||||
|
|
|
|||
|
|
@ -61,9 +61,10 @@ class DecisionBookPersonAddEventMethods(MethodToEvent):
|
|||
detail="Employee cannot create decision book invitations",
|
||||
)
|
||||
elif isinstance(token_dict, 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)
|
||||
).data
|
||||
if not decision_book:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
|
|
|
|||
|
|
@ -183,10 +183,11 @@ class BuildDecisionBookInvitationsCreateEventMethods(MethodToEvent):
|
|||
)
|
||||
# Check if the invitation is already created at database
|
||||
for build_living_spaces_user in build_living_spaces_people:
|
||||
if invite := BuildDecisionBookPerson.find_one(
|
||||
invite_id=book_invitation.id,
|
||||
build_living_space_id=build_living_spaces_user.id,
|
||||
):
|
||||
if invite := BuildDecisionBookPerson.filter_one(
|
||||
BuildDecisionBookPerson.invite_id==book_invitation.id,
|
||||
BuildDecisionBookPerson.build_living_space_id==build_living_spaces_user.id,
|
||||
*BuildDecisionBookPerson.valid_record_args(BuildDecisionBookPerson)
|
||||
).data:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=f"Invitation already send to {build_living_spaces_user.email} "
|
||||
|
|
|
|||
|
|
@ -135,14 +135,26 @@ def save_access_token_to_redis(
|
|||
companies_uu_id_list, companies_id_list = [], []
|
||||
duty_uu_id_list, duty_id_list = [], []
|
||||
for employee in list_employee.data:
|
||||
staff = Staff.find_one(id=employee.staff_id)
|
||||
if duties := Duties.find_one(id=staff.duties_id):
|
||||
if duty_found := Duty.find_one(id=duties.duties_id):
|
||||
staff = Staff.filter_one(
|
||||
Staff.id==employee.staff_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_id_list.append(duty_found.id)
|
||||
|
||||
department = Departments.find_one(id=duties.department_id)
|
||||
if company := Companies.find_one(id=department.company_id):
|
||||
department = Departments.filter_one(
|
||||
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_id_list.append(company.id)
|
||||
|
||||
|
|
|
|||
|
|
@ -10,17 +10,18 @@ from api_events.events.events.events_bind_services import (
|
|||
)
|
||||
|
||||
|
||||
active_confirmed = dict(
|
||||
created_by="System",
|
||||
confirmed_by="System",
|
||||
is_confirmed=True,
|
||||
active=True,
|
||||
deleted=False,
|
||||
is_notification_send=True,
|
||||
)
|
||||
|
||||
def create_all_events_from_actions():
|
||||
import api_events.events as events
|
||||
|
||||
active_confirmed = dict(
|
||||
created_by="System",
|
||||
confirmed_by="System",
|
||||
is_confirmed=True,
|
||||
active=True,
|
||||
deleted=False,
|
||||
is_notification_send=True,
|
||||
)
|
||||
an_empty_list, duplicate_list = [], []
|
||||
|
||||
for event in events.__all__:
|
||||
|
|
@ -34,14 +35,14 @@ def create_all_events_from_actions():
|
|||
|
||||
if endpoint_match := event_selected.action_match:
|
||||
if event_selected_function:
|
||||
selected_event = Events.find_one(
|
||||
event_type=event_selected.event_type,
|
||||
function_class=event,
|
||||
function_code=event_selected_key,
|
||||
endpoint_id=endpoint_match.id,
|
||||
endpoint_uu_id=str(endpoint_match.uu_id),
|
||||
**active_confirmed,
|
||||
)
|
||||
selected_event = Events.filter_one(
|
||||
Events.event_type==event_selected.event_type,
|
||||
Events.function_class==event,
|
||||
Events.function_code==event_selected_key,
|
||||
Events.endpoint_id==endpoint_match.id,
|
||||
Events.endpoint_uu_id==str(endpoint_match.uu_id),
|
||||
*Events.valid_record_args(Events)
|
||||
).data
|
||||
if not selected_event:
|
||||
created_event = Events.create(
|
||||
event_type=event_selected.event_type,
|
||||
|
|
@ -49,7 +50,7 @@ def create_all_events_from_actions():
|
|||
function_code=event_selected_key,
|
||||
endpoint_id=endpoint_match.id,
|
||||
endpoint_uu_id=str(endpoint_match.uu_id),
|
||||
**active_confirmed,
|
||||
**active_confirmed
|
||||
)
|
||||
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():
|
||||
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__:
|
||||
event_block_class = getattr(tasks2events, event_block)
|
||||
service_selected = Services.find_one(
|
||||
service_code=getattr(event_block_class, "service_code", None),
|
||||
)
|
||||
service_selected = Services.filter_one(
|
||||
Services.service_code==getattr(event_block_class, "service_code", None),
|
||||
*Services.valid_record_args(Services)
|
||||
).data
|
||||
if not service_selected:
|
||||
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),
|
||||
event_id=event_id,
|
||||
event_uu_id=event_uu_id,
|
||||
**active_confirmed,
|
||||
**active_confirmed
|
||||
)
|
||||
|
||||
|
||||
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:
|
||||
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:
|
||||
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:
|
||||
raise Exception("Super User Employee not found")
|
||||
|
||||
|
|
|
|||
|
|
@ -180,9 +180,9 @@ def create_application_defaults():
|
|||
Duties.init_a_company_default_duties(
|
||||
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",
|
||||
)
|
||||
).data
|
||||
|
||||
RelationshipDutyCompany.find_or_create(
|
||||
duties_id=bulk_duty.id,
|
||||
|
|
@ -195,14 +195,13 @@ def create_application_defaults():
|
|||
is_notification_send=True,
|
||||
)
|
||||
|
||||
it_dept = Departments.find_one(
|
||||
it_dept = Departments.filter_by_one(
|
||||
department_name="IT Department",
|
||||
department_code="ITD001",
|
||||
company_id=company_management.id,
|
||||
company_uu_id=str(company_management.uu_id),
|
||||
**dict(
|
||||
is_confirmed=True, active=True, deleted=False, is_notification_send=True
|
||||
),
|
||||
**Departments.valid_record_dict,
|
||||
is_confirmed=True,
|
||||
)
|
||||
|
||||
Duty.find_or_create(
|
||||
|
|
|
|||
Loading…
Reference in New Issue