Jun 26 - Update - Assigned new registered user with default daily shift

This commit is contained in:
2026-06-26 14:25:42 -04:00
parent 6da29529a3
commit 2106fd60a4
+81
View File
@@ -290,12 +290,93 @@ def register_user(username: str, password: str, full_name: str = "", email: str
cur.close() cur.close()
log_action(None, "REGISTER", "users", new_id, log_action(None, "REGISTER", "users", new_id,
f"New user self-registered: '{username}'.") f"New user self-registered: '{username}'.")
try:
assign_default_shift(new_id)
except Exception as e:
logger.warning(f"register_user: could not assign default shift to user_id={new_id}: {e}")
return new_id return new_id
finally: finally:
if conn: if conn:
conn.close() conn.close()
# Name that identifies the auto-created default shift for new registrations.
# Admins can rename it from the Shifts page without breaking anything —
# once the shift exists it is looked up by id (stored nowhere persistent here),
# but a rename just means the next registration creates a second default shift.
_DEFAULT_SHIFT_NAME = "Default (MonFri)"
_DEFAULT_SHIFT_DAYS = "23456" # MySQL DAYOFWEEK: Mon=2 Tue=3 Wed=4 Thu=5 Fri=6
_DEFAULT_SHIFT_START = "09:00:00"
_DEFAULT_SHIFT_END = "17:30:00"
def assign_default_shift(user_id: int) -> None:
"""
Find or create the Default shift and enrol a newly registered user in it.
Also ensures every currently-active daily website is present in the shift.
Safe to call multiple times — INSERT IGNORE throughout, nothing is overwritten.
"""
conn = None
try:
conn = get_connection()
cur = conn.cursor(dictionary=True)
# Find the default shift (by name)
cur.execute(
"SELECT id FROM shifts WHERE name=%s AND is_active=1 LIMIT 1",
(_DEFAULT_SHIFT_NAME,),
)
row = cur.fetchone()
if row:
shift_id = row["id"]
else:
cur = conn.cursor()
cur.execute(
"INSERT INTO shifts "
"(name, days_of_week, start_time, end_time, note, is_active, created_by) "
"VALUES (%s, %s, %s, %s, %s, 1, NULL)",
(
_DEFAULT_SHIFT_NAME, _DEFAULT_SHIFT_DAYS,
_DEFAULT_SHIFT_START, _DEFAULT_SHIFT_END,
"Auto-created default shift for self-registered users.",
),
)
conn.commit()
shift_id = cur.lastrowid
log_action(None, "CREATE_SHIFT", "shifts", shift_id,
f"Auto-created default shift '{_DEFAULT_SHIFT_NAME}'.")
# Sync all active daily websites into the shift (INSERT IGNORE = no duplicates)
cur = conn.cursor(dictionary=True)
cur.execute(
"SELECT id FROM websites WHERE check_type='daily' AND is_active=1 ORDER BY name"
)
daily_sites = cur.fetchall()
cur = conn.cursor()
for idx, w in enumerate(daily_sites):
cur.execute(
"INSERT IGNORE INTO shift_websites (shift_id, website_id, sort_order) "
"VALUES (%s, %s, %s)",
(shift_id, w["id"], idx),
)
# Enrol the user (INSERT IGNORE — idempotent)
cur.execute(
"INSERT IGNORE INTO shift_users (shift_id, user_id) VALUES (%s, %s)",
(shift_id, user_id),
)
conn.commit()
cur.close()
log_action(None, "ASSIGN_DEFAULT_SHIFT", "shifts", shift_id,
f"User id={user_id} enrolled in '{_DEFAULT_SHIFT_NAME}' "
f"with {len(daily_sites)} daily website(s).")
finally:
if conn:
conn.close()
def create_user(admin_id, username, password, role, full_name, email=None): def create_user(admin_id, username, password, role, full_name, email=None):
conn = None conn = None
try: try: