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

50 lines
1.4 KiB
Python
Raw Normal View History

2025-07-02 15:32:59 +03:00
# This file is for database functions
from db.db import get_db
2025-07-02 19:52:42 +03:00
conn = get_db()
2025-07-02 21:30:53 +03:00
def get_users(search):
# Get search param
search_param = f"%{search}%"
# Generate cursor
2025-07-02 15:32:59 +03:00
cursor = conn.cursor()
2025-07-02 21:30:53 +03:00
# 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
2025-07-02 15:32:59 +03:00
users = cursor.fetchall()
return users
2025-07-02 19:52:42 +03:00
def get_attendance(id):
# Set up query for worked hours, group by and ordered by date
cursor = conn.cursor()
cursor.execute("""
2025-07-02 22:25:03 +03:00
SELECT date, sum(hours_worked) as hours_worked
FROM Attendance
WHERE employee_id = ?
2025-07-02 19:52:42 +03:00
GROUP BY date
ORDER BY date DESC
""", (id,))
2025-07-02 22:25:03 +03:00
attendance = cursor.fetchall()
2025-07-02 19:52:42 +03:00
2025-07-02 22:25:03 +03:00
# Fetch username from database
# I am not using table JOIN because when theres no attendance, the query returns no rows (and shows user doesnt exist)
cursor.execute("SELECT username FROM Employees WHERE id = ?", (id,))
username = cursor.fetchone()
2025-07-02 19:52:42 +03:00
2025-07-02 22:25:03 +03:00
# Check if user exists
if not username:
return {"username": None, "attendance": []}
2025-07-02 19:52:42 +03:00
# Return clean api response
return {
2025-07-02 22:25:03 +03:00
"username": username[0],
2025-07-02 19:52:42 +03:00
"attendance": attendance
}