refactoring unused functions
This commit is contained in:
@@ -1,23 +1,80 @@
|
||||
from datetime import datetime
|
||||
|
||||
from scheduler.core.enums import DayOfWeek, WorkTime
|
||||
from scheduler.database import storage
|
||||
from scheduler.database.schedule_repository import ScheduleRepository
|
||||
from scheduler.manager.validator import validate_employee_can_join_shift
|
||||
|
||||
|
||||
def define_schedule_fields():
|
||||
# include user_id to identify the employee
|
||||
# include company_id if schedules need to be filtered by company
|
||||
# include day_of_week for the assigned day
|
||||
# include shift value for morning, afternoon, or evening
|
||||
# include enter_time and out_time for clock-in and clock-out
|
||||
pass
|
||||
return {
|
||||
"user_id": int,
|
||||
"company_id": int,
|
||||
"day_of_week": DayOfWeek,
|
||||
"shift": WorkTime,
|
||||
"enter_time": datetime,
|
||||
"out_time": datetime | None,
|
||||
}
|
||||
|
||||
|
||||
def register_user_enter_and_out(connection, user_id, enter_time, day_of_week, out_time):
|
||||
# Add manager validation before saving once scheduling rules are complete.
|
||||
return storage.add_schedule(connection, user_id, enter_time, day_of_week, out_time)
|
||||
def _value(value):
|
||||
return value.value if hasattr(value, "value") else value
|
||||
|
||||
|
||||
def output_weekly_schedule():
|
||||
# group saved schedules by day of week
|
||||
# inside each day, group schedules by shift
|
||||
# list employee names for each shift
|
||||
# show missing coverage when a shift has fewer than two employees
|
||||
pass
|
||||
def register_user_enter_and_out(
|
||||
connection, user_id, enter_time, day_of_week, out_time, shift
|
||||
):
|
||||
if not validate_employee_can_join_shift(
|
||||
connection, user_id, _value(day_of_week), _value(shift)
|
||||
):
|
||||
return None
|
||||
return storage.add_schedule(
|
||||
connection,
|
||||
user_id,
|
||||
enter_time,
|
||||
_value(day_of_week),
|
||||
out_time,
|
||||
_value(shift),
|
||||
)
|
||||
|
||||
|
||||
def output_weekly_schedule(connection=None, company_id=None):
|
||||
should_close = False
|
||||
if connection is None:
|
||||
connection = storage.get_connection()
|
||||
should_close = True
|
||||
|
||||
try:
|
||||
repository = ScheduleRepository(connection)
|
||||
schedules = repository.list_weekly(company_id)
|
||||
|
||||
grouped: dict[str, dict[str, list[str]]] = {}
|
||||
for schedule in schedules:
|
||||
day = _value(schedule["day_of_week"])
|
||||
shift = _value(schedule["shift"])
|
||||
name = f"{schedule['user']['name']} {schedule['user']['last_name']}"
|
||||
grouped.setdefault(day, {}).setdefault(shift, []).append(name)
|
||||
|
||||
days_order = [d.value for d in DayOfWeek]
|
||||
shifts_order = [s.value for s in WorkTime]
|
||||
|
||||
for day in days_order:
|
||||
shifts = grouped.get(day, {})
|
||||
if not shifts:
|
||||
continue
|
||||
print(f"{day}:")
|
||||
for shift in shifts_order:
|
||||
names = shifts.get(shift, [])
|
||||
if not names:
|
||||
continue
|
||||
print(f" {shift}: {', '.join(names)}")
|
||||
if len(names) < 2:
|
||||
print(
|
||||
f" warning: {shift} on {day} "
|
||||
"has fewer than two employees"
|
||||
)
|
||||
|
||||
return grouped
|
||||
finally:
|
||||
if should_close:
|
||||
connection.close()
|
||||
|
||||
Reference in New Issue
Block a user