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 #5 from josecoelho96/dev/new-features
Browse files Browse the repository at this point in the history
Dev/new features
  • Loading branch information
josecoelho96 authored Sep 25, 2018
2 parents 844678c + 7f3213a commit 55fd324
Show file tree
Hide file tree
Showing 7 changed files with 667 additions and 103 deletions.
1 change: 1 addition & 0 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ APP_PORT_EXTERNAL=
# Slack related
SLACK_SUPPORT_CHANNEL_ID=
SLACK_SIGNING_SECRET=
SLACK_USER_TOKEN=
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,21 @@ Lists the last `qty` transactions made/received in the entire application.
## Current features:
- Request origin verification/validation
- Roles/Permissions
- Auto add users to channels
- Report money receival on buy operation

## Commands to add
- TBD

## Features to add
- Auto add users to channels
- Report logs to channel
- Report money receival on buy operation
- Error codes

## Problems found
- How to create first admin.
- Implementation: Log levels aren't well defined.
- IDs are not being verified as unique.
- Users are only added to users table on join team commands, should be done always, but on every request will make this slow.
Possible solution: Create new comand?
- FIXED: ~~Entry codes are not being verified as unique. Not using UUID.~~
- FIXED: ~~Users are only added to users table on join team commands, should be done always, but on every request will make this slow.~~

## Bug list
- ...
Expand Down
32 changes: 15 additions & 17 deletions db/init/create.sql
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

CREATE TABLE IF NOT EXISTS requests (
id SERIAL PRIMARY KEY,
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
created_at TIMESTAMP DEFAULT NOW(),
token TEXT,
team_id TEXT,
team_domain TEXT,
channel_id TEXT,
channel_name TEXT,
user_id TEXT,
user_name TEXT,
slack_user_id TEXT,
slack_user_name TEXT,
command TEXT,
command_text TEXT,
response_url TEXT,
Expand All @@ -16,34 +18,30 @@ CREATE TABLE IF NOT EXISTS requests (
);

CREATE TABLE IF NOT EXISTS team_registration (
id SERIAL PRIMARY KEY,
team_id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
created_at TIMESTAMP DEFAULT NOW(),
team_id UUID,
team_name TEXT,
entry_code TEXT
);

CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
user_id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
created_at TIMESTAMP DEFAULT NOW(),
slack_id TEXT,
slack_name TEXT,
user_id UUID,
team UUID,
name TEXT,
email TEXT
team UUID
);

CREATE TABLE IF NOT EXISTS teams (
id SERIAL PRIMARY KEY,
team_id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
created_at TIMESTAMP DEFAULT NOW(),
team_id UUID,
team_name TEXT,
balance NUMERIC(10, 4)
balance NUMERIC(10, 4),
slack_channel_id TEXT
);

CREATE TABLE IF NOT EXISTS transactions (
id SERIAL PRIMARY KEY,
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
created_at TIMESTAMP DEFAULT NOW(),
origin_user_id UUID,
destination_user_id UUID,
Expand All @@ -52,14 +50,14 @@ CREATE TABLE IF NOT EXISTS transactions (
);

CREATE TABLE IF NOT EXISTS permissions (
id SERIAL PRIMARY KEY,
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
created_at TIMESTAMP DEFAULT NOW(),
user_id UUID,
staff_function TEXT
)
);

CREATE TABLE IF NOT EXISTS rewards (
id SERIAL PRIMARY KEY,
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
created_at TIMESTAMP DEFAULT NOW(),
given_by UUID,
amount NUMERIC(10, 4),
Expand Down
154 changes: 139 additions & 15 deletions src/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def save_request_log(request, success, description):
team_domain,
channel_id,
channel_name,
user_id,
user_name,
slack_user_id,
slack_user_name,
command,
command_text,
response_url,
Expand Down Expand Up @@ -111,7 +111,7 @@ def team_name_available(team_name):
db_connection.close()
return result

def save_team_registration(team_id, team_name, entry_code):
def save_team_registration(team_name, entry_code):
"""Saves a team registration entry - Name, ID and Entry code."""
try:
db_connection = connect()
Expand All @@ -123,13 +123,12 @@ def save_team_registration(team_id, team_name, entry_code):

sql_string = """
INSERT INTO team_registration (
team_id,
team_name,
entry_code
) VALUES (%s, %s, %s)
) VALUES (%s, %s)
RETURNING team_id
"""
data = (
team_id,
team_name,
entry_code
)
Expand All @@ -142,8 +141,10 @@ def save_team_registration(team_id, team_name, entry_code):
db_connection.close()
raise exceptions.QueryDatabaseError("Could not perform database insertion query: {}".format(ex))
else:
result = cursor.fetchone()[0]
cursor.close()
db_connection.close()
return result

def user_exists(slack_id):
"""Checks if a user exists in the database."""
Expand Down Expand Up @@ -179,7 +180,7 @@ def user_exists(slack_id):
db_connection.close()
return result

def save_user(slack_id, slack_name, user_id):
def save_user(slack_id, slack_name):
"""Saves a user exists in the database."""
try:
db_connection = connect()
Expand All @@ -192,14 +193,12 @@ def save_user(slack_id, slack_name, user_id):
sql_string = """
INSERT INTO users (
slack_id,
slack_name,
user_id
) VALUES (%s, %s, %s)
slack_name
) VALUES (%s, %s)
"""
data = (
slack_id,
slack_name,
user_id
)

try:
Expand Down Expand Up @@ -323,7 +322,7 @@ def is_team_created(team_id):
db_connection.close()
return result

def create_team(team_id, team_name):
def create_team(team_id, team_name, channel_id):
"""Creates a new team."""
try:
db_connection = connect()
Expand All @@ -337,13 +336,15 @@ def create_team(team_id, team_name):
INSERT INTO teams (
team_id,
team_name,
balance
) VALUES (%s, %s, %s)
balance,
slack_channel_id
) VALUES (%s, %s, %s, %s)
"""
data = (
team_id,
team_name,
INITIAL_TEAM_BALANCE
INITIAL_TEAM_BALANCE,
channel_id
)

try:
Expand Down Expand Up @@ -1396,3 +1397,126 @@ def get_last_all_transactions(max_quantity):
cursor.close()
db_connection.close()
return result

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

# NOTE: Multiple operations, change to having more info on the table?
sql_string = """
SELECT entry_code
FROM team_registration
"""
try:
cursor.execute(sql_string)
except Exception as ex:
log.error("Couldn't get entry codes transactions: {}".format(ex))
cursor.close()
db_connection.close()
raise exceptions.QueryDatabaseError("Could not perform database select query: {}".format(ex))
else:
result = [r[0] for r in cursor.fetchall()]
cursor.close()
db_connection.close()
return result

def get_team_slack_group_id(team_id):
""" Gets the slack group id of a team."""

try:
db_connection = connect()
except exceptions.DatabaseConnectionError as ex:
log.critical("Couldn't get team slack group id: {}".format(ex))
raise exceptions.QueryDatabaseError("Could not connect to database: {}".format(ex))
else:
cursor = db_connection.cursor()
sql_string = """
SELECT slack_channel_id
FROM teams
WHERE team_id = %s
"""
data = (
team_id,
)
try:
cursor.execute(sql_string, data)
except Exception as ex:
log.error("Couldn't get entry codes transactions: {}".format(ex))
cursor.close()
db_connection.close()
raise exceptions.QueryDatabaseError("Could not perform database select query: {}".format(ex))
else:
results = cursor.fetchone()[0]
cursor.close()
db_connection.close()
return results

def get_team_slack_group_id_from_slack_user_id(slack_user_id):
""" Gets the slack group id of a team."""
try:
db_connection = connect()
except exceptions.DatabaseConnectionError as ex:
log.critical("Couldn't get team slack group id: {}".format(ex))
raise exceptions.QueryDatabaseError("Could not connect to database: {}".format(ex))
else:
cursor = db_connection.cursor()
sql_string = """
SELECT slack_channel_id
FROM teams
WHERE team_id IN (
SELECT team
FROM users
WHERE slack_id = %s
)
"""
data = (
slack_user_id,
)
try:
cursor.execute(sql_string, data)
except Exception as ex:
log.error("Couldn't get team channel id: {}".format(ex))
cursor.close()
db_connection.close()
raise exceptions.QueryDatabaseError("Could not perform database select query: {}".format(ex))
else:
if cursor.rowcount == 1:
results = cursor.fetchone()[0]
else:
results = None

cursor.close()
db_connection.close()
return results

def get_all_teams_slack_group_id():
""" Gets the slack group id of all teams."""
try:
db_connection = connect()
except exceptions.DatabaseConnectionError as ex:
log.critical("Couldn't get teams slack group id: {}".format(ex))
raise exceptions.QueryDatabaseError("Could not connect to database: {}".format(ex))
else:
cursor = db_connection.cursor()
sql_string = """
SELECT slack_channel_id
FROM teams
"""
try:
cursor.execute(sql_string)
except Exception as ex:
log.error("Couldn't get teams channel id: {}".format(ex))
cursor.close()
db_connection.close()
raise exceptions.QueryDatabaseError("Could not perform database select query: {}".format(ex))
else:
result = [r[0] for r in cursor.fetchall()]
cursor.close()
db_connection.close()
return result
Loading

0 comments on commit 55fd324

Please sign in to comment.