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
2025-07-02 21:30:53 +03:00

54 lines
1.4 KiB
Python

# This file is for database functions
from db.db import get_db
conn = get_db()
def get_users(search):
# Get search param
search_param = f"%{search}%"
print(search_param)
# Generate cursor
cursor = conn.cursor()
# Search if needed
if search:
cursor.execute("SELECT * FROM Employees WHERE username LIKE ?", (search_param,))
else:
cursor.execute("SELECT * FROM Employees")
# Fetch users from database
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
}