This repository has been archived on 2025-07-06. You can view files and clone it, but cannot push or open issues or pull requests.
Files
bolderjara-serviss/backend/db/users.py
Leons Aleksandrovs ebc0665468 Finished backend
2025-07-02 19:52:42 +03:00

42 lines
1.2 KiB
Python

# This file is for database functions
from db.db import get_db
conn = get_db()
def get_users():
cursor = conn.cursor()
cursor.execute("SELECT * FROM Employees")
users = cursor.fetchall()
return users
def get_attendance(id):
# Set up query for worked hours, group by and ordered by date
cursor = conn.cursor()
cursor.execute("""
SELECT date, sum(hours_worked) as hours_worked, e.username
FROM Attendance a
JOIN Employees e ON a.employee_id = e.id
WHERE a.employee_id = ?
GROUP BY date
ORDER BY date DESC
""", (id,))
data = cursor.fetchall()
# Check if there's any attendance
if not data:
return {"username": None, "attendance": []}
# Remove repetetive username, and return clean api response
username = data[0]["username"] # get username from first row
attendance = [
{"date": row["date"], "hours_worked": row["hours_worked"]}
for row in data
]
# Return clean api response
return {
"username": username,
"attendance": attendance
}