161 lines
5.7 KiB
Python
161 lines
5.7 KiB
Python
from fastapi import APIRouter, Depends
|
|
from typing import Any
|
|
|
|
from ApiControllers.abstracts.default_validations import CommonHeaders
|
|
from ApiControllers.providers.token_provider import TokenProvider
|
|
|
|
from Controllers.Postgres.pagination import PaginateOnly, Pagination, PaginationResult
|
|
from Controllers.Postgres.response import EndpointResponse, CreateEndpointResponse
|
|
from Schemas import Applications
|
|
from Validations.application.validations import (
|
|
RequestApplication,
|
|
)
|
|
|
|
# Create API router
|
|
application_route = APIRouter(prefix="/application", tags=["Application Management"])
|
|
|
|
|
|
@application_route.post(
|
|
path="/list",
|
|
description="List applications endpoint",
|
|
operation_id="3189a049-bdb0-49f3-83ff-feb8cb4cb57a",
|
|
)
|
|
def application_list_route(
|
|
list_options: PaginateOnly,
|
|
headers: CommonHeaders = Depends(CommonHeaders.as_dependency),
|
|
):
|
|
"""
|
|
List applications with pagination and filtering options
|
|
"""
|
|
token_object = TokenProvider.get_dict_from_redis(token=headers.token)
|
|
list_options = PaginateOnly(**list_options.model_dump())
|
|
with Applications.new_session() as db_session:
|
|
if list_options.query:
|
|
applications_list = Applications.filter_all(
|
|
*Applications.convert(list_options.query), db=db_session
|
|
)
|
|
else:
|
|
applications_list = Applications.filter_all(db=db_session)
|
|
pagination = Pagination(data=applications_list)
|
|
pagination.change(**list_options.model_dump())
|
|
pagination_result = PaginationResult(
|
|
data=applications_list,
|
|
pagination=pagination,
|
|
)
|
|
return EndpointResponse(
|
|
message="MSG0003-LIST",
|
|
pagination_result=pagination_result,
|
|
).response
|
|
|
|
|
|
@application_route.post(
|
|
path="/create",
|
|
description="Create application endpoint",
|
|
operation_id="5570be78-030a-438e-8674-7e751447608b",
|
|
)
|
|
def application_create_route(
|
|
data: RequestApplication,
|
|
headers: CommonHeaders = Depends(CommonHeaders.as_dependency),
|
|
):
|
|
"""
|
|
Create a new application
|
|
"""
|
|
token_object = TokenProvider.get_dict_from_redis(token=headers.token)
|
|
with Applications.new_session() as db_session:
|
|
created_application_dict = data.model_dump()
|
|
created_application = Applications.find_or_create(
|
|
db=db_session,
|
|
include_args=[
|
|
Applications.application_for,
|
|
Applications.application_code,
|
|
Applications.site_url,
|
|
],
|
|
**created_application_dict,
|
|
)
|
|
if created_application.meta_data.created:
|
|
return CreateEndpointResponse(
|
|
message="MSG0001-INSERT",
|
|
data=created_application,
|
|
).response
|
|
return CreateEndpointResponse(
|
|
message="MSG0002-FOUND",
|
|
data=created_application,
|
|
).response
|
|
|
|
|
|
@application_route.post(
|
|
path="/update/{application_uuid}",
|
|
description="Update application endpoint",
|
|
operation_id="87cd4515-73dd-4d11-a01f-562e221d973c",
|
|
)
|
|
def application_update_route(
|
|
data: RequestApplication,
|
|
application_uuid: str,
|
|
headers: CommonHeaders = Depends(CommonHeaders.as_dependency),
|
|
):
|
|
"""
|
|
Update an existing application
|
|
"""
|
|
token_object = TokenProvider.get_dict_from_redis(token=headers.token)
|
|
with Applications.new_session() as db_session:
|
|
updated_application_dict = data.model_dump(exclude_unset=True, exclude_none=True)
|
|
found_application = Applications.filter_one(
|
|
Applications.uu_id == application_uuid, db=db_session
|
|
).data
|
|
if not found_application:
|
|
return CreateEndpointResponse(
|
|
message="MSG0002-FOUND",
|
|
data=found_application,
|
|
).response
|
|
updated_application = found_application.update(db=db_session,**updated_application_dict)
|
|
updated_application.save(db_session)
|
|
if updated_application.meta_data.updated:
|
|
return CreateEndpointResponse(
|
|
message="MSG0003-UPDATE",
|
|
data=updated_application,
|
|
).response
|
|
return CreateEndpointResponse(
|
|
message="MSG0003-UPDATE",
|
|
data=updated_application,
|
|
).response
|
|
|
|
|
|
@application_route.post(
|
|
path="/bind/employee",
|
|
description="Bind application to employee endpoint",
|
|
operation_id="2bab94fa-becb-4d8e-80f1-f4631119a521",
|
|
)
|
|
def application_bind_employee_route(
|
|
data: Any,
|
|
headers: CommonHeaders,
|
|
):
|
|
"""
|
|
Bind application to employee endpoint
|
|
"""
|
|
token_object = TokenProvider.get_dict_from_redis(token=headers.token)
|
|
event_founder_dict = dict(endpoint_code=headers.operation_id, token=token_object)
|
|
event_key = TokenProvider.retrieve_event_codes(**event_founder_dict)
|
|
FoundCluster = ApplicationRouterCluster.get_event_cluster("ApplicationBindEmployee")
|
|
event_cluster_matched = FoundCluster.match_event(event_key=event_key)
|
|
return event_cluster_matched.event_callable(data=data)
|
|
|
|
|
|
@application_route.post(
|
|
path="/bind/occupant",
|
|
description="Bind application to occupant endpoint",
|
|
operation_id="fccf1a59-0650-4e5c-ba8d-f389dadce01c",
|
|
)
|
|
def application_bind_occupant_route(
|
|
data: Any,
|
|
headers: CommonHeaders,
|
|
):
|
|
"""
|
|
Bind application to occupant endpoint
|
|
"""
|
|
token_object = TokenProvider.get_dict_from_redis(token=headers.token)
|
|
event_founder_dict = dict(endpoint_code=headers.operation_id, token=token_object)
|
|
event_key = TokenProvider.retrieve_event_codes(**event_founder_dict)
|
|
FoundCluster = ApplicationRouterCluster.get_event_cluster("ApplicationBindOccupant")
|
|
event_cluster_matched = FoundCluster.match_event(event_key=event_key)
|
|
return event_cluster_matched.event_callable(data=data)
|
|
|