247 lines
7.6 KiB
Python
247 lines
7.6 KiB
Python
from typing import Union
|
|
|
|
from fastapi.exceptions import HTTPException
|
|
|
|
from databases import (
|
|
Events,
|
|
Employees,
|
|
Staff,
|
|
Duties,
|
|
Event2Occupant,
|
|
Event2Employee,
|
|
BuildLivingSpace,
|
|
)
|
|
from api_validations.validations_request import (
|
|
RegisterEvents2Employee,
|
|
RegisterEvents2Occupant,
|
|
CreateEvents,
|
|
ListOptions,
|
|
)
|
|
|
|
from api_events.events.abstract_class import MethodToEvent, ActionsSchema
|
|
from api_objects.auth.token_objects import EmployeeTokenObject, OccupantTokenObject
|
|
from api_validations.core_response import AlchemyJsonResponse
|
|
|
|
|
|
class EventsListEventMethods(MethodToEvent):
|
|
|
|
event_type = "SELECT"
|
|
__event_keys__ = {
|
|
"9fa01bef-c0e8-4fe1-b9ed-2ff1c4f35faa": "events_list",
|
|
}
|
|
|
|
@classmethod
|
|
def events_list(
|
|
cls,
|
|
list_options: ListOptions,
|
|
token_dict: Union[EmployeeTokenObject, OccupantTokenObject],
|
|
):
|
|
Events.filter_attr = list_options
|
|
records = Events.filter_all(
|
|
*Events.valid_record_args(Events)
|
|
)
|
|
return AlchemyJsonResponse(
|
|
completed=True,
|
|
message="DecisionBook are listed successfully",
|
|
result=records,
|
|
)
|
|
|
|
|
|
class EventsCreateEventMethods(MethodToEvent):
|
|
|
|
event_type = "CREATE"
|
|
__event_keys__ = {
|
|
"514a9f8f-e5e5-4e10-9d0b-2de8f461fc1b": "events_create",
|
|
}
|
|
|
|
@classmethod
|
|
def events_create(cls, data: CreateEvents, token_dict):
|
|
event = Events.find_or_create(
|
|
**token_dict.user_creds,
|
|
event_name=data.event_name,
|
|
event_description=data.event_description,
|
|
event_date=data.event_date,
|
|
event_location=data.event_location,
|
|
active=True,
|
|
deleted=False,
|
|
)
|
|
Events.save()
|
|
return {
|
|
"status": "success",
|
|
"message": "Event created successfully.",
|
|
"event": event.uu_id,
|
|
}
|
|
|
|
|
|
class EventsUpdateEventMethods(MethodToEvent):
|
|
|
|
event_type = "UPDATE"
|
|
__event_keys__ = {
|
|
"f94e7b79-2369-4840-bf2b-244934ca3136": "events_update",
|
|
}
|
|
|
|
@classmethod
|
|
def events_update(cls, data: CreateEvents, token_dict):
|
|
event = Events.filter_by_one(uu_id=data.uu_id, **Events.valid_record_dict).data
|
|
if not event:
|
|
raise HTTPException(
|
|
status_code=404,
|
|
detail="No event found. Please contact your responsible company.",
|
|
)
|
|
event.update(
|
|
**token_dict.user_creds,
|
|
event_name=data.event_name,
|
|
event_description=data.event_description,
|
|
event_date=data.event_date,
|
|
event_location=data.event_location,
|
|
)
|
|
Events.save()
|
|
return {
|
|
"status": "success",
|
|
"message": "Event updated successfully.",
|
|
"event": event.uu_id,
|
|
}
|
|
|
|
|
|
class EventsPatchEventMethods(MethodToEvent):
|
|
|
|
event_type = "PATCH"
|
|
__event_keys__ = {
|
|
"41944c63-22d3-4866-affd-34bcd49da58b": "events_patch",
|
|
}
|
|
|
|
@classmethod
|
|
def events_patch(cls, data: CreateEvents, token_dict):
|
|
event = Events.filter_by_one(uu_id=data.uu_id, **Events.valid_record_dict).data
|
|
if not event:
|
|
raise HTTPException(
|
|
status_code=404,
|
|
detail="No event found. Please contact your responsible company.",
|
|
)
|
|
event.update(
|
|
**token_dict.user_creds,
|
|
event_name=data.event_name,
|
|
event_description=data.event_description,
|
|
event_date=data.event_date,
|
|
event_location=data.event_location,
|
|
)
|
|
return {
|
|
"status": "success",
|
|
"message": "Event patched successfully.",
|
|
"event": event.uu_id,
|
|
}
|
|
|
|
|
|
class EventsBindEventToOccupantMethods(MethodToEvent):
|
|
|
|
event_type = "UPDATE"
|
|
__event_keys__ = {
|
|
"d9aa58aa-37f7-4c27-861d-3105f76f5cdc": "bind_events_employee",
|
|
}
|
|
|
|
@classmethod
|
|
def bind_events_employee(cls, data: RegisterEvents2Employee, token_dict):
|
|
events = Events.filter_all(
|
|
Events.uu_id.in_(data.event_id),
|
|
*Events.valid_record_args(Events),
|
|
).data
|
|
if not events:
|
|
raise HTTPException(
|
|
status_code=401,
|
|
detail="No event found. Please contact your super user.",
|
|
)
|
|
employee_is_not_valid = False
|
|
employee = Employees.filter_one(
|
|
Employees.employee_uu_id == data.employee_uu_id,
|
|
*Employees.valid_record_args(Employees),
|
|
).data
|
|
if employee:
|
|
staff = Staff.filter_one(
|
|
Staff.id == employee.staff_id,
|
|
*Staff.valid_record_args(Staff),
|
|
).data
|
|
duties = Duties.filter_one(
|
|
Duties.id == staff.duties_id,
|
|
*Duties.valid_record_args(Duties),
|
|
).data
|
|
if duties.company_id not in token_dict.companies_id_list:
|
|
employee_is_not_valid = True
|
|
|
|
if employee_is_not_valid:
|
|
raise HTTPException(
|
|
status_code=401,
|
|
detail="This employee can not be reached by this user. Please contact your super user.",
|
|
)
|
|
|
|
for event in events:
|
|
employee = Event2Employee.find_or_create(
|
|
**token_dict.user_creds, employee_id=employee.id, event_id=event.id
|
|
)
|
|
Events.save()
|
|
return {
|
|
"status": "success",
|
|
"message": "Events registered successfully.",
|
|
"events": [event.uu_id for event in events.data],
|
|
}
|
|
|
|
|
|
class EventsBindEventToEmployeeMethods(MethodToEvent):
|
|
|
|
event_type = "UPDATE"
|
|
__event_keys__ = {
|
|
"8bb4f4fc-b474-427e-90b3-d8681f308bb5": "bind_events_occupant",
|
|
}
|
|
|
|
@classmethod
|
|
def bind_events_occupant(cls, data: RegisterEvents2Occupant, token_dict):
|
|
events = Events.filter_all(
|
|
Events.uu_id.in_(data.event_id),
|
|
*Events.valid_record_args(Events),
|
|
).data
|
|
if not events:
|
|
raise HTTPException(
|
|
status_code=401,
|
|
detail="No event found. Please contact your super user.",
|
|
)
|
|
occupant = BuildLivingSpace.filter_one(
|
|
BuildLivingSpace.uu_id == data.build_living_space_uu_id,
|
|
*BuildLivingSpace.valid_record_args(BuildLivingSpace),
|
|
).data
|
|
if not occupant:
|
|
raise HTTPException(
|
|
status_code=401,
|
|
detail="This occupant can not be reached by this user. Please contact your super user.",
|
|
)
|
|
for event in events:
|
|
occupant = Event2Occupant.find_or_create(
|
|
**token_dict.user_creds,
|
|
build_living_space_id=occupant.id,
|
|
event_id=event.id,
|
|
)
|
|
Events.save()
|
|
return {
|
|
"status": "success",
|
|
"message": "Events registered successfully.",
|
|
"events": [event.uu_id for event in events.data],
|
|
}
|
|
|
|
|
|
EventsListEventMethod = EventsListEventMethods(
|
|
action=ActionsSchema(endpoint="/event/list")
|
|
)
|
|
EventsCreateEventMethod = EventsCreateEventMethods(
|
|
action=ActionsSchema(endpoint="/event/create")
|
|
)
|
|
EventsUpdateEventMethod = EventsUpdateEventMethods(
|
|
action=ActionsSchema(endpoint="/event/update")
|
|
)
|
|
EventsPatchEventMethod = EventsPatchEventMethods(
|
|
action=ActionsSchema(endpoint="/event/patch")
|
|
)
|
|
EventsBindEventToOccupantMethod = EventsBindEventToOccupantMethods(
|
|
action=ActionsSchema(endpoint="/bind/events/occupant")
|
|
)
|
|
EventsBindEventToEmployeeMethod = EventsBindEventToEmployeeMethods(
|
|
action=ActionsSchema(endpoint="/bind/events/employee")
|
|
)
|