from fastapi import status from fastapi.responses import JSONResponse from fastapi.exceptions import HTTPException from api_validations.validations_request import ( InsertStaff, SelectStaff, PatchRecord, ListOptions, ) from databases import Staff, Duties 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 StaffListEventMethods(MethodToEvent): event_type = "SELECT" __event_keys__ = { "8984a519-99bf-4f25-8f34-2e1aebba468c": "staff_list", } @classmethod def staff_list(cls, list_options: ListOptions, token_dict): Staff.filter_attr = list_options records = Staff.filter_active( *Staff.get_smart_query(smart_query=list_options.query), ) return AlchemyJsonResponse( completed=True, message="Staff are listed successfully", result=records, ) class StaffCreateEventMethods(MethodToEvent): event_type = "CREATE" __event_keys__ = { "8f619257-19fd-404f-b713-7392c588dc36": "staff_create", } @classmethod def staff_create(cls, data: InsertStaff, token_dict: EmployeeTokenObject): data_dict = data.excluded_dump() duties = Duties.filter_one( Duties.uu_id == data.duties_uu_id, *Duties.valid_record_args(Duties), ).data if not duties: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="Duties not found", ) data_dict["duties_id"] = duties.id created_duty = Staff.find_or_create(**data_dict) Staff.save() return JSONResponse( content={ "completed": True, "message": "Create Staff record", "data": created_duty.get_dict(), }, status_code=status.HTTP_200_OK, ) class StaffGetByUUIDEventMethods(MethodToEvent): event_type = "GET" __event_keys__ = { "7724cfbb-c0ee-4261-959b-61b84e88a34f": "staff_get_by_uu_id", } @classmethod def staff_get_by_uu_id(cls, data: SelectStaff, token_dict): if data.duties_uu_id: duties_id = Duties.filter_one( Duties.uu_id == data.duties_uu_id, *Duties.valid_record_args(Duties) ).data selected_staffs = Staff.filter_all( Staff.duties_id == duties_id.id, *Staff.valid_record_args(Staff) ) return JSONResponse( content={ "completed": True, "message": "Staff records are listed", "data": [ selected_staff.get_dict() for selected_staff in selected_staffs.data ], }, status_code=status.HTTP_200_OK, ) return JSONResponse( content={ "completed": False, "message": "Get Staff record failed", "data": {}, }, status_code=status.HTTP_202_ACCEPTED, ) class StaffUpdateEventMethods(MethodToEvent): event_type = "UPDATE" __event_keys__ = { "5329f35d-ff9d-4656-a831-ba9c8204e483": "staff_update", } @classmethod def staff_update(cls, staff_uu_id: str, data, token_dict): return JSONResponse( content={"completed": True, "message": "Update Staff record", "data": {}}, status_code=status.HTTP_200_OK, ) class StaffPatchEventMethods(MethodToEvent): event_type = "PATCH" __event_keys__ = { "b1cd7c0a-1458-472b-894f-3adc857c8512": "staff_patch", } @classmethod def staff_patch(cls, staff_uu_id: str, data: PatchRecord, token_dict): return JSONResponse( content={ "completed": False, "message": "Patch Staff record failed", "data": {}, }, status_code=status.HTTP_200_OK, ) StaffListEventMethod = StaffListEventMethods( action=ActionsSchema(endpoint="/staff/list") ) StaffCreateEventMethod = StaffCreateEventMethods( action=ActionsSchema(endpoint="/staff/create") ) StaffGetByUUIDEventMethod = StaffGetByUUIDEventMethods( action=ActionsSchema(endpoint="/staff/get_by_duties_uu_id") ) StaffUpdateEventMethod = StaffUpdateEventMethods( action=ActionsSchema(endpoint="/staff/update") ) StaffPatchEventMethod = StaffPatchEventMethods( action=ActionsSchema(endpoint="/staff/patch") )