Skip to content

Commit

Permalink
Latest key API endpoind extended
Browse files Browse the repository at this point in the history
  • Loading branch information
leechwort committed Dec 1, 2023
1 parent 3375a57 commit 886db34
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions app/models/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,26 @@ def get_latest_key(cls):
Get last triggered key, to add new users by clicking on any reader
"""
connection = sqlite3.connect(app.config["DATABASE_URI"])
connection.row_factory = sqlite3.Row
# This query returns extended info about latest key event (user_key, operation_time, device_name, user_name)
query = """
SELECT el.user_key, el.operation_time, d.name AS device_name, u.name AS user_name
FROM event_logs el
INNER JOIN devices d ON el.device_id = d.id
INNER JOIN users u ON el.user_key = u.key
WHERE el.user_key IS NOT NULL AND el.operation_type = 'deny_access'
ORDER BY el.operation_time DESC
LIMIT 1
"""
rows = (
connection.cursor()
.execute(
"SELECT user_key "
"FROM event_logs "
"WHERE user_key IS NOT NULL AND operation_type = 'deny_access' "
"ORDER BY operation_time DESC LIMIT 1"
)
.execute(query)
.fetchone()
)
connection.close()

if rows is None:
return None
result_dict = dict(rows)

return rows[0]
return result_dict

0 comments on commit 886db34

Please sign in to comment.