def create_bot(cls, bot_data): admin = Authenticator.authenticate_team(bot_data.authentication, UserRoles.is_admin) try: new_client = UserDatabaseClient.add_client() new_bot = Bot( bot_id=new_client.id, name=bot_data.name, callback=bot_data.callback, token=Authenticator.generate(new_client.id) ) BotDatabaseClient.add_bot(new_bot) team_bot = TeamUser( user_id=new_client.id, team_id=admin.team_id, role=TeamRoles.BOT.value ) TeamDatabaseClient.add_team_user(team_bot) DatabaseClient.commit() cls.logger().info(f"Bot #{new_bot.id} created in team {admin.team_id} with callback url {new_bot.callback} " f"by admin {admin.id}.") return SuccessfulUserMessageResponse("Bot created.", UserResponseStatus.OK.value) except IntegrityError as exc: DatabaseClient.rollback() if BotDatabaseClient.get_bot_by_name(bot_data.name) is not None: cls.logger().info(f"Failing to create bot {bot_data.name}. Name already in use.", exc) return BadRequestUserMessageResponse("Name already in use for other bot.", UserResponseStatus.ALREADY_REGISTERED.value) else: cls.logger().info(f"Failing to create bot {bot_data.name}.") return UnsuccessfulClientResponse("Couldn't create bot.")
def accept_invite(cls, invitation_data): user = Authenticator.authenticate(invitation_data) invite = TeamDatabaseClient.get_team_invite_by_token( invitation_data.invite_token, user.email) if invite is None: return BadRequestTeamMessageResponse( "You weren't invited to this team.", UserResponseStatus.WRONG_CREDENTIALS.value) new_user_team = TeamUser(user_id=user.id, team_id=invite.team_id) try: TeamDatabaseClient.add_team_user(new_user_team) TeamDatabaseClient.delete_invite(invite) DatabaseClient.commit() BotService.tito_welcome(new_user_team.user_id, new_user_team.team_id) cls.logger().info( f"User #{user.id} joined team #{invite.team_id}.") except IntegrityError: DatabaseClient.rollback() cls.logger().error( f"User #{user.id} failed at joining team #{invite.team_id}.") return UnsuccessfulTeamMessageResponse("Couldn't join team.") else: return SuccessfulTeamMessageResponse( "Team joined!", TeamResponseStatus.ADDED.value)
def add_user(cls, add_data): admin = Authenticator.authenticate(add_data.authentication, UserRoles.is_admin) user = UserDatabaseClient.get_user_by_id(add_data.add_user_id) if user is None: cls.logger().info(f"User {add_data.add_user_id} not found.") raise UserNotFoundError("User not found.", UserResponseStatus.USER_NOT_FOUND.value) if user.role == UserRoles.ADMIN.value: cls.logger().warning( f"Admin #{admin.id} trying to add other admin to a team.") return BadRequestTeamMessageResponse( "You cannot add an admin to a team!", TeamResponseStatus.ROLE_UNAVAILABLE.value) if TeamDatabaseClient.get_user_in_team_by_ids( user.id, add_data.authentication.team_id) is not None: cls.logger().info( f"User {add_data.add_user_id} already part of team #{add_data.authentication.team_id}." ) return BadRequestTeamMessageResponse( "This user already belongs to the team.", TeamResponseStatus.ALREADY_REGISTERED.value) previous_invitation = TeamDatabaseClient.get_team_invite( add_data.authentication.team_id, user.email) if previous_invitation is not None: cls.logger().info( f"Deleting old invitation for user {add_data.add_user_id} to team " f"#{add_data.authentication.team_id}.") TeamDatabaseClient.delete_invite(previous_invitation) DatabaseClient.commit() added_user = TeamUser(user_id=add_data.add_user_id, team_id=add_data.authentication.team_id) try: TeamDatabaseClient.add_team_user(added_user) DatabaseClient.commit() BotService.tito_welcome(added_user.user_id, added_user.team_id) cls.logger().info( f"Added user #{added_user.user_id} to team #{added_user.team_id} by admin #{admin.id}." ) return SuccessfulTeamMessageResponse( "User added.", TeamResponseStatus.ADDED.value) except IntegrityError: DatabaseClient.rollback() cls.logger().error( f"Couldn't add user #{added_user.user_id} to team #{added_user.team_id}." ) return UnsuccessfulTeamMessageResponse( "Couldn't invite user to team.")
def register_tito_in_team(cls, team_id): try: team_tito = TeamUser( user_id=cls.TITO_ID, team_id=team_id, role=TeamRoles.BOT.value ) TeamDatabaseClient.add_team_user(team_tito) DatabaseClient.commit() cls.logger().info(f"Tito added to team #{team_id}.") except SQLAlchemyError as exc: DatabaseClient.rollback() cls.logger().error(f"Failing to register Tito into team #{team_id}.", exc) raise
def create_team(cls, new_team_data): user = Authenticator.authenticate(new_team_data) new_team = Team(name=new_team_data.team_name, picture=new_team_data.picture, location=new_team_data.location, description=new_team_data.description, welcome_message=new_team_data.welcome_message) try: team = TeamDatabaseClient.add_team(new_team) new_team.id = team.id new_user_by_team = TeamUser(user_id=user.id, team_id=team.id, role=TeamRoles.CREATOR.value) TeamDatabaseClient.add_team_user(new_user_by_team) BotService.register_tito_in_team(team.id) DatabaseClient.commit() cls.logger().info(f"Team #{team.id} created.") cls.logger().info( f"User #{user.id} assigned as team #{team.id} {new_user_by_team.role}." ) except IntegrityError: DatabaseClient.rollback() if TeamDatabaseClient.get_team_by_name( new_team_data.team_name) is not None: cls.logger().info( f"Failing to create team {new_team_data.team_name}. Name already in use." ) return BadRequestTeamMessageResponse( f"Name {new_team_data.team_name} already in use for other team.", TeamResponseStatus.ALREADY_REGISTERED.value) else: cls.logger().error( f"Failing to create team {new_team_data.team_name}.") return UnsuccessfulTeamMessageResponse("Couldn't create team.") else: return SuccessfulTeamResponse(new_team, TeamResponseStatus.CREATED.value)