167 lines
5.3 KiB
Python
167 lines
5.3 KiB
Python
import typing
|
|
|
|
from api_validations.validations_request import (
|
|
InsertAccountRecord,
|
|
UpdateAccountRecord,
|
|
ListOptions,
|
|
)
|
|
|
|
from api_validations.core_response import AlchemyJsonResponse
|
|
from api_events.events.abstract_class import MethodToEvent, ActionsSchema
|
|
from api_objects.auth.token_objects import EmployeeTokenObject, OccupantTokenObject
|
|
from databases import (
|
|
AccountRecords,
|
|
BuildIbans,
|
|
)
|
|
|
|
|
|
class AccountRecordsListEventMethods(MethodToEvent):
|
|
|
|
event_type = "SELECT"
|
|
__event_keys__ = {
|
|
"": "account_records_list",
|
|
}
|
|
|
|
@classmethod
|
|
def account_records_list(
|
|
cls,
|
|
list_options: ListOptions,
|
|
token_dict: typing.Union[EmployeeTokenObject, OccupantTokenObject],
|
|
):
|
|
if isinstance(token_dict, OccupantTokenObject):
|
|
AccountRecords.pre_query = AccountRecords.filter_all(
|
|
AccountRecords.company_id
|
|
== token_dict.selected_occupant.responsible_company_id,
|
|
*AccountRecords.valid_record_args(AccountRecords),
|
|
)
|
|
elif isinstance(token_dict, EmployeeTokenObject):
|
|
AccountRecords.pre_query = AccountRecords.filter_all(
|
|
AccountRecords.company_id == token_dict.selected_company.company_id,
|
|
*AccountRecords.valid_record_args(AccountRecords),
|
|
)
|
|
records = AccountRecords.filter_all(
|
|
*AccountRecords.smart_query(list_options.query),
|
|
*AccountRecords.valid_record_args(AccountRecords),
|
|
)
|
|
return AlchemyJsonResponse(
|
|
completed=True, message="Update Build record", result=records
|
|
)
|
|
|
|
|
|
class AccountRecordsCreateEventMethods(MethodToEvent):
|
|
|
|
event_type = "CREATE"
|
|
__event_keys__ = {
|
|
"": "account_records_create",
|
|
}
|
|
|
|
@classmethod
|
|
def account_records_create(
|
|
cls,
|
|
data: InsertAccountRecord,
|
|
token_dict: typing.Union[EmployeeTokenObject, OccupantTokenObject],
|
|
):
|
|
|
|
if isinstance(token_dict, OccupantTokenObject):
|
|
build_iban = BuildIbans.filter_one(
|
|
BuildIbans.iban == data.iban,
|
|
BuildIbans.company_id
|
|
== token_dict.selected_occupant.responsible_company_id,
|
|
).data
|
|
if not build_iban:
|
|
BuildIbans.raise_http_exception(
|
|
status_code="HTTP_404_NOT_FOUND",
|
|
error_case="UNAUTHORIZED",
|
|
message=f"{data.iban} is not found in company related to your organization",
|
|
data={
|
|
"iban": data.iban,
|
|
},
|
|
)
|
|
account_record = AccountRecords.find_or_create(**data.excluded_dump())
|
|
return AlchemyJsonResponse(
|
|
completed=True,
|
|
message="Update Build record",
|
|
result=account_record.get_dict(),
|
|
)
|
|
elif isinstance(token_dict, EmployeeTokenObject):
|
|
build_iban = BuildIbans.filter_one(
|
|
BuildIbans.iban == data.iban,
|
|
BuildIbans.company_id == token_dict.selected_company.company_id,
|
|
).data
|
|
if not build_iban:
|
|
BuildIbans.raise_http_exception(
|
|
status_code="HTTP_404_NOT_FOUND",
|
|
error_case="UNAUTHORIZED",
|
|
message=f"{data.iban} is not found in company related to your organization",
|
|
data={
|
|
"iban": data.iban,
|
|
},
|
|
)
|
|
account_record = AccountRecords.find_or_create(**data.excluded_dump())
|
|
return AlchemyJsonResponse(
|
|
completed=True,
|
|
message="Update Build record",
|
|
result=account_record.get_dict(),
|
|
)
|
|
|
|
|
|
class AccountRecordsUpdateEventMethods(MethodToEvent):
|
|
|
|
event_type = "UPDATE"
|
|
__event_keys__ = {
|
|
"": "account_records_update",
|
|
}
|
|
|
|
@classmethod
|
|
def build_area_update(
|
|
cls,
|
|
build_uu_id: str,
|
|
data: UpdateAccountRecord,
|
|
token_dict: typing.Union[EmployeeTokenObject, OccupantTokenObject],
|
|
):
|
|
if isinstance(token_dict, OccupantTokenObject):
|
|
pass
|
|
elif isinstance(token_dict, EmployeeTokenObject):
|
|
pass
|
|
|
|
return AlchemyJsonResponse(
|
|
completed=False,
|
|
message="Update Build record",
|
|
result=None,
|
|
)
|
|
|
|
|
|
class AccountRecordsPatchEventMethods(MethodToEvent):
|
|
|
|
event_type = "PATCH"
|
|
__event_keys__ = {
|
|
"": "account_records_patch",
|
|
}
|
|
|
|
@classmethod
|
|
def build_area_patch(
|
|
cls,
|
|
build_uu_id: str,
|
|
data,
|
|
token_dict: typing.Union[EmployeeTokenObject, OccupantTokenObject],
|
|
):
|
|
return AlchemyJsonResponse(
|
|
completed=False,
|
|
message="Patch Build record",
|
|
result=None,
|
|
)
|
|
|
|
|
|
AccountRecordsListEventMethod = AccountRecordsListEventMethods(
|
|
action=ActionsSchema(endpoint="/account/records/list")
|
|
)
|
|
AccountRecordsCreateEventMethod = AccountRecordsCreateEventMethods(
|
|
action=ActionsSchema(endpoint="/account/records/create")
|
|
)
|
|
AccountRecordsUpdateEventMethod = AccountRecordsUpdateEventMethods(
|
|
action=ActionsSchema(endpoint="/account/records/update")
|
|
)
|
|
AccountRecordsPatchEventMethod = AccountRecordsPatchEventMethods(
|
|
action=ActionsSchema(endpoint="/account/records/patch")
|
|
)
|