136 lines
4.4 KiB
Python
136 lines
4.4 KiB
Python
from fastapi import status
|
|
from fastapi.responses import JSONResponse
|
|
|
|
from validations import InsertCompanyDuty
|
|
from validations.root_validates import PatchRecord, ListOptions
|
|
|
|
from databases import Duty
|
|
|
|
from api_events.events.abstract_class import MethodToEvent, ActionsSchema
|
|
from api_objects.auth.token_objects import EmployeeTokenObject, OccupantTokenObject
|
|
from api_validations.core_response import return_json_response_from_alchemy
|
|
|
|
|
|
class DutyListEventMethods(MethodToEvent):
|
|
|
|
event_type = "SELECT"
|
|
__event_keys__ = {
|
|
"23231c7d-4ff2-4b39-b71b-ea350d31fadf": "duty_list",
|
|
}
|
|
|
|
@classmethod
|
|
def duty_list(cls, list_options: ListOptions, token_dict):
|
|
records = Duty.filter_active(
|
|
*Duty.get_smart_query(list_options.query),
|
|
)
|
|
return return_json_response_from_alchemy(
|
|
response=records, pagination=list_options
|
|
)
|
|
|
|
|
|
class DutyCreateEventMethods(MethodToEvent):
|
|
|
|
event_type = "CREATE"
|
|
__event_keys__ = {
|
|
"c6ea200e-fa17-4393-b390-37f5337c9c65": "duty_create",
|
|
}
|
|
|
|
@classmethod
|
|
def duty_create(cls, data: InsertCompanyDuty, token_dict):
|
|
created_duty = Duty.find_or_create(**data.excluded_dump())
|
|
return JSONResponse(
|
|
content={
|
|
"completed": True,
|
|
"message": "Create Company record",
|
|
"data": created_duty.get_dict(),
|
|
},
|
|
status_code=status.HTTP_200_OK,
|
|
)
|
|
|
|
|
|
class DutyUpdateEventMethods(MethodToEvent):
|
|
|
|
event_type = "UPDATE"
|
|
__event_keys__ = {
|
|
"ad952647-bcf8-482d-9e05-b2ee8086483f": "duty_update",
|
|
}
|
|
|
|
@classmethod
|
|
def duty_update(cls, company_uu_id: str, data, token_dict):
|
|
find_one_company = Duty.find_one_or_abort(uu_id=company_uu_id)
|
|
access_authorized_company = Duty.select_action(
|
|
duty_id=getattr(token_dict, "duty_id", 5), # ?
|
|
filter_expr=[Duty.id == token_dict.get("")],
|
|
)
|
|
if access_authorized_company.count:
|
|
data_dict = data.excluded_dump()
|
|
updated_company = find_one_company.update(**data_dict)
|
|
return JSONResponse(
|
|
content={
|
|
"completed": True,
|
|
"message": "Update Company record",
|
|
"data": updated_company,
|
|
},
|
|
status_code=status.HTTP_200_OK,
|
|
)
|
|
return JSONResponse(
|
|
content={"completed": True, "message": "Update Company record", "data": {}},
|
|
status_code=status.HTTP_200_OK,
|
|
)
|
|
|
|
|
|
class DutyPatchEventMethods(MethodToEvent):
|
|
|
|
event_type = "PATCH"
|
|
__event_keys__ = {
|
|
"d5c7b5c4-7b4e-4d5b-8e3b-2b9c5f5d0c0b": "duty_patch",
|
|
}
|
|
|
|
@classmethod
|
|
def duty_patch(cls, company_uu_id: str, data: PatchRecord, token_dict):
|
|
find_one_company = Duty.find_one_or_abort(uu_id=company_uu_id)
|
|
access_authorized_company = Duty.select_action(
|
|
duty_id=getattr(token_dict, "duty_id", 5),
|
|
filter_expr=[Duty.id == find_one_company.id],
|
|
)
|
|
if access_authorized_company.count:
|
|
action = data.excluded_dump()
|
|
find_one_company.active = bool(
|
|
action.get("active", find_one_company.active)
|
|
)
|
|
find_one_company.is_confirmed = bool(
|
|
action.get("confirm", find_one_company.is_confirmed)
|
|
)
|
|
find_one_company.deleted = bool(
|
|
action.get("delete", find_one_company.deleted)
|
|
)
|
|
find_one_company.save()
|
|
return JSONResponse(
|
|
content={
|
|
"completed": True,
|
|
"message": "Patch Company record completed",
|
|
"data": find_one_company.get_dict(),
|
|
},
|
|
status_code=status.HTTP_200_OK,
|
|
)
|
|
return JSONResponse(
|
|
content={
|
|
"completed": False,
|
|
"message": "Patch Company record failed",
|
|
"data": {},
|
|
},
|
|
status_code=status.HTTP_200_OK,
|
|
)
|
|
|
|
|
|
DutyListEventMethod = DutyListEventMethods(action=ActionsSchema(endpoint="/duty/list"))
|
|
DutyCreateEventMethod = DutyCreateEventMethods(
|
|
action=ActionsSchema(endpoint="/duty/create")
|
|
)
|
|
DutyUpdateEventMethod = DutyUpdateEventMethods(
|
|
action=ActionsSchema(endpoint="/duty/update")
|
|
)
|
|
DutyPatchEventMethod = DutyPatchEventMethods(
|
|
action=ActionsSchema(endpoint="/duty/patch")
|
|
)
|