def update_total_days_by_id(id, body): # noqa: E501 """Updates a TotalDays in the system with form data. Role write:total_days must be granted # noqa: E501 :param id: ID of TotalDays to return :type id: int :param body: TotalDays object that needs to be updated in the system :type body: dict | bytes :rtype: TotalDays """ found = dao.get(id) if found is None: return ErrorApiResponse.TotalDaysNotFoundError(id=id), 404 if connexion.request.is_json: body = TotalDays.from_dict(connexion.request.get_json()) # noqa: E501 employee = employee_dao.get(body.employee_id) if employee is None: return ErrorApiResponse.EmployeeNotFoundError(body.employee_id), 404 # check already exists duplicate = dao.find_by_year(body.employee_id, body.year) if duplicate is not None and duplicate.id != id: return ErrorApiResponse.TotalDaysExistError(body.employee_id, body.year), 409 found.employee_id = body.employee_id found.year = body.year found.total_days = body.total_days try: dao.persist(found) return get_total_days_by_id(id) except Exception as ex: return ErrorApiResponse.InternalServerError(ex, type='total days'), 500
def add_total_days(body): # noqa: E501 """Add a employee total days to the system. Role write:total_days must be granted # noqa: E501 :param body: TotalDays object that needs to be added to the system :type body: dict | bytes :rtype: TotalDays """ if connexion.request.is_json: body = TotalDays.from_dict(connexion.request.get_json()) # noqa: E501 # check already exists found = dao.find_by_year(body.employee_id, body.year) if found is not None: return ErrorApiResponse.TotalDaysExistError(body.employee_id, body.year), 409 employee = employee_dao.get(body.employee_id) if employee is None: return ErrorApiResponse.EmployeeNotFoundError(body.employee_id), 404 orm = TotalDays_orm(employee_id=body.employee_id, total_days=body.total_days, year=body.year) try: dao.persist(orm) return find_total_days_by_year(body.employee_id, body.year) except Exception as ex: return ErrorApiResponse.InternalServerError(ex, type='total days'), 500
def get_employee_by_id(employeeId): # noqa: E501 """Find employee by ID Returns a single employee. # noqa: E501 :param employeeId: ID of empoyee to return :type employeeId: int :rtype: Employee """ found = dao.get(employeeId) if found is None: return ErrorApiResponse.EmployeeNotFoundError(id=employeeId), 404 return to_employee_dto(found)
def find_employee_by_email(email): # noqa: E501 """Finds Employee by given email # noqa: E501 :param email: Unique employee email :type email: str :rtype: Employee """ found = dao.find_by_email(email) if found is None: return ErrorApiResponse.EmployeeNotFoundError(email=email), 404 return to_employee_dto(found)
def update_leave_days_by_id(id, body): # noqa: E501 """Updates a LeaveDays in the system with form data. Role write:leave_days must be granted # noqa: E501 :param id: ID of LeaveDays to return :type id: int :param body: LeaveDays object that needs to be updated in the system :type body: dict | bytes :rtype: LeaveDays """ found = dao.get(id) if found is None: return ErrorApiResponse.LeaveDaysNotFoundError(id=id), 404 if connexion.request.is_json: body = LeaveDays.from_dict(connexion.request.get_json()) # noqa: E501 delta = body.end_date - body.start_date if delta.days < 0: return ErrorApiResponse.LeaveDaysPeriodError(body.start_date, body.end_date), 409 # check already exists duplicate = find_leave_days_by(body.employee_id, body.start_date, body.end_date) if len(duplicate) > 1 or (len(duplicate) == 1 and duplicate[0].id != id): return ErrorApiResponse.LeaveDaysExistError(body.employee_id, body.start_date, body.end_date), 409 # check employee exist employee = employee_dao.get(body.employee_id) if employee is None: return ErrorApiResponse.EmployeeNotFoundError(body.employee_id), 404 # check rules conflict = rules.check_rules(body.employee_id, body.start_date, body.end_date, id) if len(conflict) > 0: return ApiResponseConflict(code=5000, type='leave days', message='Restrictions are violated', details=conflict), 409 found.employee_id = body.employee_id found.start_date = body.start_date found.end_date = body.end_date found.leave_days = body.leave_days try: dao.persist(found) return get_leave_days_by_id(id) except Exception as ex: return ErrorApiResponse.InternalServerError(ex, type='leave days'), 500
def find_employee_total_days(employeeId): # noqa: E501 """Finds TotalDays by employee # noqa: E501 :param employeeId: Employee id to filter by :type employeeId: int :rtype: List[TotalDays] """ employee = employee_dao.get(employeeId) if employee is None: return ErrorApiResponse.EmployeeNotFoundError(employeeId), 404 try: return [to_dto(elem) for elem in dao.find_by(employeeId)] except Exception as ex: return ErrorApiResponse.InternalServerError(ex, type='total days'), 500
def delete_employee(employeeId): # noqa: E501 """Deletes an employee. Role write:employees must be granteds # noqa: E501 :param employeeId: Employee id to delete :type employeeId: int :rtype: ApiResponse """ found = dao.get(employeeId) if found is None: return ErrorApiResponse.EmployeeNotFoundError(id=employeeId), 404 try: dao.delete(found) return 'Successful operation', 204 except Exception as ex: return ErrorApiResponse.InternalServerError(ex, type='employee'), 500
def add_leave_days(body): # noqa: E501 """Add a employee LeaveDays to the system. Role write:leave_days must be granted # noqa: E501 :param body: LeaveDays object that needs to be added to the system :type body: dict | bytes :rtype: LeaveDays """ if connexion.request.is_json: body = LeaveDays.from_dict(connexion.request.get_json()) # noqa: E501 delta = body.end_date - body.start_date if delta.days < 0: return ErrorApiResponse.LeaveDaysPeriodError(body.start_date, body.end_date), 409 # check already exists found = dao.find_by(body.employee_id, body.start_date, body.end_date, None) if len(found) > 0: return ErrorApiResponse.LeaveDaysExistError(body.employee_id, body.start_date, body.end_date), 409 # check employee exist found = employee_dao.get(body.employee_id) if found is None: return ErrorApiResponse.EmployeeNotFoundError(body.employee_id), 404 # check rules FIXME conflict = rules.check_rules(body.employee_id, body.start_date, body.end_date) if len(conflict) > 0: return ApiResponseConflict(code=5000, type='leave days', message='Restrictions are violated', details=conflict), 409 orm = LeaveDays_orm(employee_id=body.employee_id, leave_days=body.leave_days, start_date=body.start_date, end_date=body.end_date) try: dao.persist(orm) return find_leave_days_by_date(body.employee_id, body.start_date) except Exception as ex: return ErrorApiResponse.InternalServerError(ex, type='leave days'), 500
def update_employee_by_id(employeeId, body): # noqa: E501 """Updates an employee in the system with form data. Role write:employees must be granted # noqa: E501 :param employeeId: ID of empoyee to return :type employeeId: int :param body: Employee object that needs to be added to the system :type body: dict | bytes :rtype: Employee """ found = dao.get(employeeId) if found is None: return ErrorApiResponse.EmployeeNotFoundError(id=employeeId), 404 if connexion.request.is_json: body = Employee.from_dict(connexion.request.get_json()) # noqa: E501 # check email already exists duplicate = dao.find_by_email(body.email) if duplicate is not None and duplicate.employee_id != body._employee_id: return ErrorApiResponse.EmployeeEmailExistError(body.email), 409 # check for team number team = team_dao.get(body.team_id) if team is None: return ErrorApiResponse.TeamNotFoundError(id=body.team_id), 404 found.full_name = body.full_name found.position = body.position found.specialization = body.specialization if body.specialization else '' found.team_id = body.team_id found.expert = body.expert found.email = body.email try: dao.persist(found) return get_employee_by_id(employeeId) except Exception as ex: return ErrorApiResponse.InternalServerError(ex, type='employee'), 500