150 lines
4.6 KiB
Python
150 lines
4.6 KiB
Python
from typing import Union
|
|
|
|
from fastapi import status
|
|
from fastapi.responses import JSONResponse
|
|
|
|
from databases import (
|
|
People,
|
|
Users,
|
|
Companies,
|
|
)
|
|
|
|
from api_validations.validations_request import InsertPerson, UpdateUsers
|
|
|
|
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 PeopleListEventMethods(MethodToEvent):
|
|
|
|
event_type = "SELECT"
|
|
__event_keys__ = {
|
|
"0a05f03c-6ed8-4230-a4ff-6e7cf886909b": "super_users_people_list",
|
|
"b5612538-0445-4a4a-ab13-d2a06037f7a5": "sales_users_people_list",
|
|
"c81c2cec-d32c-4cf2-9727-d4493e11ee1f": "human_resources_users_people_list",
|
|
"d1b1b1b1-1b1b-1b1b-1b1b-1b1b1b1b1b1b": "people_list_only_occupant_tenant_or_owner",
|
|
}
|
|
|
|
@classmethod
|
|
def super_users_people_list(cls, list_options, token_dict):
|
|
records = People.filter_active(
|
|
*People.get_smart_query(smart_query=list_options.query)
|
|
)
|
|
return return_json_response_from_alchemy(
|
|
response=records, pagination=list_options
|
|
)
|
|
|
|
@classmethod
|
|
def sales_users_people_list(cls, data, token_dict):
|
|
|
|
records = People.filter_active(*People.get_smart_query(smart_query=data.query))
|
|
# records = [model_class(**record) for record in records.data]
|
|
return return_json_response_from_alchemy(response=records, pagination=data)
|
|
|
|
@classmethod
|
|
def human_resources_users_people_list(cls, data, token_dict):
|
|
|
|
records = People.filter_active(*People.get_smart_query(smart_query=data.query))
|
|
# records = [model_class(**record) for record in records.data]
|
|
return return_json_response_from_alchemy(response=records, pagination=data)
|
|
|
|
|
|
class PeopleCreateEventMethods(MethodToEvent):
|
|
|
|
event_type = "CREATE"
|
|
__event_keys__ = {
|
|
"2d1513f4-44ed-4fa3-84d1-dfbd0eadf9a1": "people_create",
|
|
}
|
|
|
|
@classmethod
|
|
def people_create(
|
|
cls,
|
|
data: InsertPerson,
|
|
token_dict: Union[EmployeeTokenObject, OccupantTokenObject],
|
|
):
|
|
created_user = People.create_action(data=data, token=token_dict)
|
|
People.save()
|
|
return JSONResponse(
|
|
content={
|
|
"completed": True,
|
|
"message": "Create User record",
|
|
"data": created_user.get_dict(),
|
|
},
|
|
status_code=status.HTTP_200_OK,
|
|
)
|
|
|
|
|
|
class PeopleUpdateEventMethods(MethodToEvent):
|
|
|
|
event_type = "UPDATE"
|
|
__event_keys__ = {
|
|
"e05cf22c-16c4-450b-86c8-417896a26afc": "people_update",
|
|
}
|
|
|
|
@classmethod
|
|
def people_update(
|
|
cls,
|
|
data: UpdateUsers,
|
|
user_uu_id: str,
|
|
token_dict: Union[EmployeeTokenObject, OccupantTokenObject],
|
|
):
|
|
find_one_user = Users.find_one_or_abort(uu_id=user_uu_id)
|
|
access_authorized_company = Companies.select_action(
|
|
duty_id=getattr(token_dict, "duty_id", 5),
|
|
filter_expr=[Companies.id == find_one_user.id],
|
|
)
|
|
if access_authorized_company.count:
|
|
data_dict = data.excluded_dump()
|
|
updated_user = find_one_user.update(**data_dict)
|
|
Users.save()
|
|
return JSONResponse(
|
|
content={
|
|
"completed": True,
|
|
"message": "Update User record",
|
|
"data": updated_user,
|
|
},
|
|
status_code=status.HTTP_200_OK,
|
|
)
|
|
return JSONResponse(
|
|
content={"completed": True, "message": "Update User record", "data": {}},
|
|
status_code=status.HTTP_200_OK,
|
|
)
|
|
|
|
|
|
class PeoplePatchEventMethods(MethodToEvent):
|
|
|
|
event_type = "PATCH"
|
|
__event_keys__ = {
|
|
"3ae16d66-090b-4d27-b567-cce1b10a1c3b": "people_patch",
|
|
}
|
|
|
|
@classmethod
|
|
def people_patch(cls):
|
|
return
|
|
|
|
|
|
class PeopleDeleteEventMethods(MethodToEvent):
|
|
|
|
event_type = "DELETE"
|
|
__event_keys__ = {
|
|
"7f84c7a2-a120-4867-90d4-6767a41320db": "people_delete",
|
|
}
|
|
|
|
|
|
PeopleListEventMethod = PeopleListEventMethods(
|
|
action=ActionsSchema(endpoint="/people/list")
|
|
)
|
|
PeopleCreateEventMethod = PeopleCreateEventMethods(
|
|
action=ActionsSchema(endpoint="/people/create")
|
|
)
|
|
PeopleUpdateEventMethod = PeopleUpdateEventMethods(
|
|
action=ActionsSchema(endpoint="/people/update")
|
|
)
|
|
PeoplePatchEventMethod = PeoplePatchEventMethods(
|
|
action=ActionsSchema(endpoint="/people/patch")
|
|
)
|
|
PeopleDeleteEventMethod = PeopleDeleteEventMethods(
|
|
action=ActionsSchema(endpoint="/people/delete")
|
|
)
|