Skip to content
This repository has been archived by the owner on Nov 7, 2019. It is now read-only.

Commit

Permalink
Merge pull request #2 from josecoelho96/dev/extra-commands
Browse files Browse the repository at this point in the history
Dev/extra commands
  • Loading branch information
josecoelho96 authored Sep 22, 2018
2 parents 621f6e7 + a9df257 commit 03f56f0
Show file tree
Hide file tree
Showing 7 changed files with 616 additions and 14 deletions.
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,36 @@ Allows to buy something from another user. It performs a transfer, between the c
### List last transactions
`/movimentos <qty>`
List transactions. If the user has a team, list the last `qty` transactions of his team. If the current user doesn't have a team, an error message appears stating how to join a team.

### List all teams
`/ver-equipas`
List all teams. Provides the team name and team id of each team participating.
### List all teams registered
`/ver-equipas-registo`
List all registered teams. Provides the team name and team id and entry code of each team registered.
### View team details
`/detalhes-equipa <team_id>`
Used to list all details of a team. The `team-id` must be provided.
### View user details
`/detalhes-participante <@user|user-id>`
Used to list details of a participant. The `@user` or `user-id` must be provided.

## Current features:
- Request origin verification/validation

## To be added
```./ver-equipas``` - List all teams. \
Can only be performed by admins. Used to list all teams.


```./detalhes-equipa <team>``` \
Can only be performed by admins. Used to list all details of a team. The `team-id` must be provided.


```./bug <money-change> <description>``` \
Can only be performed by admins. Used to change all teams balances.


```./tornar-admin <@user>``` \
Can only be performed by admins. Used to make `@user` an admin.


## Features to add
- Auto add users to channels
- Report logs to channel
- Report money receival on buy operation
- Permissions system
- Error codes
- Single user transactions listing

## Problems found
- How to create first admin.
Expand Down
180 changes: 179 additions & 1 deletion src/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ def get_last_transactions(slack_user_id, max_quantity):
try:
db_connection = connect()
except exceptions.DatabaseConnectionError as ex:
log.critical("Couldn't get user slack details: {}".format(ex))
log.critical("Couldn't get last transactions: {}".format(ex))
raise exceptions.QueryDatabaseError("Could not connect to database: {}".format(ex))
else:
cursor = db_connection.cursor()
Expand Down Expand Up @@ -657,4 +657,182 @@ def get_last_transactions(slack_user_id, max_quantity):
result = [r for r in cursor.fetchall()]
cursor.close()
db_connection.close()
return result

def get_teams():
""" Gets the teams list."""
try:
db_connection = connect()
except exceptions.DatabaseConnectionError as ex:
log.critical("Couldn't get teams: {}".format(ex))
raise exceptions.QueryDatabaseError("Could not connect to database: {}".format(ex))
else:
cursor = db_connection.cursor()

sql_string = """
SELECT team_id, team_name
FROM teams
"""
try:
cursor.execute(sql_string)
except Exception as ex:
log.error("Couldn't get teams list: {}".format(ex))
cursor.close()
db_connection.close()
raise exceptions.QueryDatabaseError("Could not perform database select query: {}".format(ex))
else:
result = [r for r in cursor.fetchall()]
cursor.close()
db_connection.close()
return result

def get_teams_registration():
""" Gets the registration teams list."""
try:
db_connection = connect()
except exceptions.DatabaseConnectionError as ex:
log.critical("Couldn't get registration teams: {}".format(ex))
raise exceptions.QueryDatabaseError("Could not connect to database: {}".format(ex))
else:
cursor = db_connection.cursor()

sql_string = """
SELECT team_id, team_name, entry_code
FROM team_registration
"""
try:
cursor.execute(sql_string)
except Exception as ex:
log.error("Couldn't get teams list: {}".format(ex))
cursor.close()
db_connection.close()
raise exceptions.QueryDatabaseError("Could not perform database select query: {}".format(ex))
else:
result = [r for r in cursor.fetchall()]
cursor.close()
db_connection.close()
return result

def get_team_details(team_id):
""" Gets a team details."""
try:
db_connection = connect()
except exceptions.DatabaseConnectionError as ex:
log.critical("Couldn't get team details: {}".format(ex))
raise exceptions.QueryDatabaseError("Could not connect to database: {}".format(ex))
else:
cursor = db_connection.cursor()

sql_string = """
SELECT team_id, team_name, balance
FROM teams
WHERE team_id = %s
"""
data = (
team_id,
)
try:
cursor.execute(sql_string, data)
except Exception as ex:
log.error("Couldn't get team details: {}".format(ex))
cursor.close()
db_connection.close()
raise exceptions.QueryDatabaseError("Could not perform database select query: {}".format(ex))
else:
result = cursor.fetchone()
cursor.close()
db_connection.close()
return result

def get_team_users(team_id):
""" Gets a team users."""
try:
db_connection = connect()
except exceptions.DatabaseConnectionError as ex:
log.critical("Couldn't get team users: {}".format(ex))
raise exceptions.QueryDatabaseError("Could not connect to database: {}".format(ex))
else:
cursor = db_connection.cursor()

sql_string = """
SELECT slack_id, slack_name, user_id
FROM users
WHERE team = %s
"""
data = (
team_id,
)
try:
cursor.execute(sql_string, data)
except Exception as ex:
log.error("Couldn't get team users: {}".format(ex))
cursor.close()
db_connection.close()
raise exceptions.QueryDatabaseError("Could not perform database select query: {}".format(ex))
else:
result = [r for r in cursor.fetchall()]
cursor.close()
db_connection.close()
return result

def get_user_details_from_slack_id(slack_id):
""" Gets an user details from its slack id."""
try:
db_connection = connect()
except exceptions.DatabaseConnectionError as ex:
log.critical("Couldn't get user details: {}".format(ex))
raise exceptions.QueryDatabaseError("Could not connect to database: {}".format(ex))
else:
cursor = db_connection.cursor()

sql_string = """
SELECT slack_id, slack_name, user_id, team
FROM users
WHERE slack_id = %s
"""
data = (
slack_id,
)
try:
cursor.execute(sql_string, data)
except Exception as ex:
log.error("Couldn't get user details: {}".format(ex))
cursor.close()
db_connection.close()
raise exceptions.QueryDatabaseError("Could not perform database select query: {}".format(ex))
else:
result = cursor.fetchone()
cursor.close()
db_connection.close()
return result

def get_user_details_from_user_id(user_id):
""" Gets an user details from its user id."""
try:
db_connection = connect()
except exceptions.DatabaseConnectionError as ex:
log.critical("Couldn't get user details: {}".format(ex))
raise exceptions.QueryDatabaseError("Could not connect to database: {}".format(ex))
else:
cursor = db_connection.cursor()

sql_string = """
SELECT slack_id, slack_name, user_id, team
FROM users
WHERE user_id = %s
"""
data = (
user_id,
)
try:
cursor.execute(sql_string, data)
except Exception as ex:
log.error("Couldn't get user details: {}".format(ex))
cursor.close()
db_connection.close()
raise exceptions.QueryDatabaseError("Could not perform database select query: {}".format(ex))
else:
result = cursor.fetchone()
cursor.close()
db_connection.close()
return result
6 changes: 5 additions & 1 deletion src/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@
"CHECK_BALANCE": "/saldo",
"BUY": "/compra",
"LIST_TRANSACTIONS": "/movimentos",
"LIST_TEAMS": "/ver-equipas",
"LIST_TEAMS_REGISTRATION": "/ver-equipas-registo",
"TEAM_DETAILS": "/detalhes-equipa",
"USER_DETAILS": "/detalhes",
}

INITIAL_TEAM_BALANCE = 200

SLACK_REQUEST_TIMESTAMP_MAX_GAP_MINUTES = 1.0
SLACK_REQUEST_TIMESTAMP_MAX_GAP_MINUTES = 60.0
Loading

0 comments on commit 03f56f0

Please sign in to comment.