def retrieve_team_leader(self, tl_pk: int): """ Retrieving a team leader for a given team primary key :param tl_pk: :return: """ tl_entity = self._team_leader_repo.retrieve_team_leader(tl_pk) if tl_entity is None: raise domain_validators.ObjectEntityDoesNotExist( "Team leader does not exist") return tl_entity
def remove_team_employee(self, te_pk): team_employee = self._team_employee_repo.team_employee_exists(te_pk) if team_employee: if self._team_employee_repo.employee_has_more_teams( employee_pk=team_employee.employee.id): # delete return self._team_employee_repo.delete_team_employee(te_pk) else: raise domain_validators.EmployeeHasOneTeam() else: raise domain_validators.ObjectEntityDoesNotExist( "Team Employee doesnt exists")
def change_team_leader( self, request_data: request_data_models.UpdateTeamLeaderRequestData): """ Change a leader of a team to a new leader :param request_data: :return: """ # check if team exists in repository old_team_leader = self._team_leader_repo.retrieve_team_leader( request_data.id) if old_team_leader is None: raise domain_validators.ObjectEntityDoesNotExist( "Team leader does not exist") team = self._team_repo.team_exists(team_pk=request_data.team_id) if team is None: raise domain_validators.TeamDoesNotExist() if team.id != old_team_leader.id: raise domain_validators.UpdateOfTeamLeaderOfWrongTeam() print(F"Old team: {team.id}") # team is in repository # check is employee exist in repository before assigning him/her as a leader employee = self._employee_repo.employee_exists( employee_pk=request_data.employee_id) if employee is None: raise domain_validators.EmployeeDoesNotExist() updated_tl_entity = TeamLeaderEntity( id=old_team_leader.id, leader=employee, team=team, created_at=old_team_leader.created_at, updated_at=datetime.datetime.now()) tl_entity = self._team_leader_repo.save_team_leader(updated_tl_entity) return tl_entity
def retrieve_team_employee(self, te_pk: int): team_employee = self._team_employee_repo.retrieve_team_employees(te_pk) if team_employee is None: raise domain_validators.ObjectEntityDoesNotExist( "Team employee does not exist") return team_employee
def add_work_arrangement( self, request_data: request_data_models.CreateWorkArrangementData): """ Add new work arrangement of an employee :param request_data: :return: """ employee = self._employee_repo.employee_exists( request_data.employee_id) if employee is None: raise domain_validators.ObjectEntityDoesNotExist( "Employee does not exist") # check if employee is part time: can not add multiple work arrangement for full time employees if not employee.is_part_time(): raise domain_validators.MultipleWorksForFullTimeEmployee() team = self._team_repo.team_exists(request_data.team_id) # check if team exist in repository if team is None: raise domain_validators.TeamDoesNotExist() total_percent = self._work_arrangement_repo.get_employee_work_arrangements_percent( employee_pk=employee.id) if total_percent + request_data.percent > 100: raise domain_validators.Max40HoursExceeded() # check if an employee is a team employee if self._team_employee_repo.is_a_member(team_pk=team.id, employee_pk=employee.id): if self._work_arrangement_repo.has_work_arrangement_with_team( employee_pk=employee.id, team_pk=team.id): # employee can not have more than 1 work arrangement in on team raise domain_validators.MultipleWorkArrangementInOneTeam() else: # add team employee if an employee is not a team employee new_te_entity = TeamEmployeeEntity( employee=employee, team=team, created_at=datetime.datetime.now(), updated_at=datetime.datetime.now()) self._team_employee_repo.save_team_employee(new_te_entity) # check if team has a leader otherwise make an employee a team leader if team.has_a_leader: # team already have a leader pass else: # No leader, make new employee the leader by default new_tl_entity = TeamLeaderEntity( leader=employee, team=team, created_at=datetime.datetime.now(), updated_at=datetime.datetime.now()) self._team_leader_repo.save_team_leader(new_tl_entity) # create work arrangement entity work_arrangement = WorkArrangementEntity(percent=request_data.percent, remarks=request_data.remarks, employee=employee, team=team) # save new work arrangement new_wa_entity = self._work_arrangement_repo.save(work_arrangement) # save respective work time work_hours = WorkArrangementEntity.calculate_work_time_hours( request_data.percent) work_time = WorkTimeEntity(hours=work_hours, employee=employee, work_arrangement=new_wa_entity) self._work_time_repo.save_work_time(work_time) # get employee with update work times and pay employee_with_updated_pay = self._employee_repo.retrieve_by_id( new_wa_entity.employee.id) new_wa_entity.employee = employee_with_updated_pay return new_wa_entity
def retrieve_work_arrangement(self, wa_pk): work_arrangement = self._work_arrangement_repo.retrieve_by_pk(wa_pk) if work_arrangement is None: raise domain_validators.ObjectEntityDoesNotExist( "Work arrangement does not exist") return work_arrangement
def update_work_arrangement( self, request_data: request_data_models.UpdateWorkArrangementData): """ Update existing work arrangement :param request_data: :return: """ work_arrangement_entity = self._work_arrangement_repo.work_arrangement_exists( wa_pk=request_data.id) if work_arrangement_entity is None: raise domain_validators.ObjectEntityDoesNotExist( "Work arrangement does not exist in repository") employee = self._employee_repo.employee_exists( request_data.employee_id) if employee is None: raise domain_validators.ObjectEntityDoesNotExist( "Employee doesn't exist") # check if employee is part time: can not add multiple work arrangement for full time employees if not employee.is_part_time(): raise domain_validators.MultipleWorksForFullTimeEmployee() team = self._team_repo.team_exists(request_data.team_id) # check if team exist in repository if team is None: raise domain_validators.TeamDoesNotExist() total_percent = self._work_arrangement_repo.get_employee_work_arrangements_percent( employee_pk=employee.id) if (total_percent - work_arrangement_entity.percent) + request_data.percent > 100: raise domain_validators.Max40HoursExceeded() # check if an employee is a team employee if self._team_employee_repo.is_a_member(team_pk=team.id, employee_pk=employee.id): if self._work_arrangement_repo.has_work_arrangement_with_team(employee_pk=request_data.id, team_pk=team.id) and \ request_data.id != employee.id: # employee can not have more than 1 work arrangement in on team if work arrangement team is updated raise domain_validators.MultipleWorkArrangementInOneTeam() else: # add team employee if an employee is not a team employee new_te_entity = TeamEmployeeEntity( employee=employee, team=team, created_at=datetime.datetime.now(), updated_at=datetime.datetime.now()) self._team_employee_repo.save_team_employee(new_te_entity) # check if team has a leader otherwise make an employee a team leader if team.has_a_leader: pass else: new_tl_entity = TeamLeaderEntity( leader=employee, team=team, created_at=datetime.datetime.now(), updated_at=datetime.datetime.now()) self._team_leader_repo.save_team_leader(new_tl_entity) # create updated work arrangement entity work_arrangement = WorkArrangementEntity(id=request_data.id, percent=request_data.percent, remarks=request_data.remarks, employee=employee, team=team) # save new work arrangement new_wa_entity = self._work_arrangement_repo.save(work_arrangement) # get and update respective work time work_time = self._work_time_repo.retrieve_by_work_arrangement_pk( work_arrangement_pk=new_wa_entity.id) work_hours = WorkArrangementEntity.calculate_work_time_hours( request_data.percent) print(F"work time {work_time}") if work_time: # work time in repository: update it work_time.hours = work_hours else: # work time not in database: create new one work_time = WorkTimeEntity(hours=work_hours, employee=employee, work_arrangement=new_wa_entity) self._work_time_repo.save_work_time(work_time) # get employee with update work times and pay employee_with_updated_pay = self._employee_repo.retrieve_by_id( new_wa_entity.employee.id) new_wa_entity.employee = employee_with_updated_pay return new_wa_entity