Auto-close forgotten checkouts on the next check-in
Browse files客户确认的漏卡规则:「以下一次打卡记」——下次打卡时用当下的时间把上一班补上。
- services/checkin.py 新增 close_stale_shift():找出该用户最后一条
「有上班卡、无下班卡、日期早于今天」的记录,用当前时间补上下班卡
- clock_in() 在建新记录前先调用它,并把被补的日期放进返回值
- 按钮回调在提示里说明「顺手补上了 X 号的下班卡」,让用户知道发生了什么
- 补出的时长超过 MAX_SHIFT_MINUTES 就封顶并标异常:忘了打卡的人往往隔天
才回来,不封顶的话一个「24 小时的班」会把周累计冲得没法看
- status_of() 为 auto_closed 的记录返回「自动补卡」,卡片用 🔸 区分
- config 里 MISSING_CHECKOUT_POLICY 默认值改为 next_checkin 并注明落点
已用独立测试库验证:造一条昨天的漏卡记录,今日打卡后被补成 16 小时(封顶)
并标记 auto_closed。
Co-Authored-By: Claude Code <noreply@anthropic.com>
- app.py +3 -0
- core/config.py +5 -2
- services/checkin.py +42 -5
- utils/card_builder.py +1 -1
app.py
CHANGED
|
@@ -305,6 +305,9 @@ async def on_btn_click(_bot: Bot, ev: Event):
|
|
| 305 |
res = await checkin.clock_in(user_id, name)
|
| 306 |
if res["ok"]:
|
| 307 |
note = f"✅ **{name}** 已记录上班时间"
|
|
|
|
|
|
|
|
|
|
| 308 |
else:
|
| 309 |
note = f"ℹ️ 今天已经打过上班卡了"
|
| 310 |
else:
|
|
|
|
| 305 |
res = await checkin.clock_in(user_id, name)
|
| 306 |
if res["ok"]:
|
| 307 |
note = f"✅ **{name}** 已记录上班时间"
|
| 308 |
+
if res.get("closed_date"):
|
| 309 |
+
note += (f"\n🔸 顺手补上了 `{res['closed_date']}` 那天的下班卡"
|
| 310 |
+
"(客户规则:漏卡以下一次打卡记)")
|
| 311 |
else:
|
| 312 |
note = f"ℹ️ 今天已经打过上班卡了"
|
| 313 |
else:
|
core/config.py
CHANGED
|
@@ -47,8 +47,11 @@ WEEK_START = os.environ.get("WEEK_START", "monday").lower()
|
|
| 47 |
# 跨天夜班归属:True = 记在「上班」那天;False = 记在「下班」那天
|
| 48 |
OVERNIGHT_TO_START_DAY = True
|
| 49 |
|
| 50 |
-
# 漏点「下班打卡」的处理
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
# 单次打卡时长上限(分钟)。防止误点:早上点了上班、晚上又点上班,或忘记下班
|
| 54 |
# 导致隔天点下班时算出一个 30 小时的「工时」。
|
|
|
|
| 47 |
# 跨天夜班归属:True = 记在「上班」那天;False = 记在「下班」那天
|
| 48 |
OVERNIGHT_TO_START_DAY = True
|
| 49 |
|
| 50 |
+
# 漏点「下班打卡」的处理。
|
| 51 |
+
# 客户已确认:「以下一次打卡记」——下次打卡时用当下的时间把上一班补上。
|
| 52 |
+
# 实现在 services/checkin.py 的 close_stale_shift(),这里保留开关是为了
|
| 53 |
+
# 万一以后要改成 other 策略时有地方落。
|
| 54 |
+
MISSING_CHECKOUT_POLICY = os.environ.get("MISSING_CHECKOUT_POLICY", "next_checkin").lower()
|
| 55 |
|
| 56 |
# 单次打卡时长上限(分钟)。防止误点:早上点了上班、晚上又点上班,或忘记下班
|
| 57 |
# 导致隔天点下班时算出一个 30 小时的「工时」。
|
services/checkin.py
CHANGED
|
@@ -16,6 +16,39 @@ def _doc_id(user_id: str, date_str: str) -> str:
|
|
| 16 |
return f"{user_id}:{date_str}"
|
| 17 |
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
async def clock_in(user_id: str, name: str) -> dict:
|
| 20 |
now_dt = timeutil.now()
|
| 21 |
date_str = timeutil.today_str()
|
|
@@ -24,6 +57,9 @@ async def clock_in(user_id: str, name: str) -> dict:
|
|
| 24 |
if doc and doc.get("in_ts"):
|
| 25 |
return {"ok": False, "code": "already_in", "in_ts": doc["in_ts"]}
|
| 26 |
|
|
|
|
|
|
|
|
|
|
| 27 |
await db.checkins.update_one(
|
| 28 |
{"_id": _doc_id(user_id, date_str)},
|
| 29 |
{"$set": {"user_id": user_id, "name": name,
|
|
@@ -31,8 +67,9 @@ async def clock_in(user_id: str, name: str) -> dict:
|
|
| 31 |
"$setOnInsert": {"out_ts": None, "worked_min": None, "abnormal": False}},
|
| 32 |
upsert=True,
|
| 33 |
)
|
| 34 |
-
logger.info("[Checkin] 上班 %s %s", name, now_dt.isoformat()
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
|
| 38 |
async def clock_out(user_id: str, name: str) -> dict:
|
|
@@ -124,7 +161,7 @@ def status_of(doc: dict) -> str:
|
|
| 124 |
if not doc.get("in_ts"):
|
| 125 |
return "缺上班卡"
|
| 126 |
if doc.get("out_ts"):
|
|
|
|
|
|
|
| 127 |
return "异常" if doc.get("abnormal") else "正常"
|
| 128 |
-
|
| 129 |
-
return "待补下班卡"
|
| 130 |
-
return "缺下班卡"
|
|
|
|
| 16 |
return f"{user_id}:{date_str}"
|
| 17 |
|
| 18 |
|
| 19 |
+
async def close_stale_shift(user_id: str, name: str, close_at):
|
| 20 |
+
"""把上一条「只打了上班、没打下班」的记录用 close_at 收尾。
|
| 21 |
+
|
| 22 |
+
客户规则是「漏点下班卡以下一次打卡记」——所以下次打卡时,顺手用当下的
|
| 23 |
+
时间把上一班补上。返回被补的那天日期,没有可补的则返回 None。
|
| 24 |
+
|
| 25 |
+
补出来的时长如果超过 MAX_SHIFT_MINUTES 就**封顶**并标记异常:忘了打卡
|
| 26 |
+
的人往往是隔天甚至几天后才回来,不封顶的话一个「24 小时的班」会把本周
|
| 27 |
+
累计和周累计冲得没法看。
|
| 28 |
+
"""
|
| 29 |
+
stale = await db.checkins.find_one(
|
| 30 |
+
{"user_id": user_id, "in_ts": {"$ne": None}, "out_ts": None,
|
| 31 |
+
"date": {"$lt": timeutil.today_str()}},
|
| 32 |
+
sort=[("date", -1)],
|
| 33 |
+
)
|
| 34 |
+
if not stale:
|
| 35 |
+
return None
|
| 36 |
+
|
| 37 |
+
worked = timeutil.minutes_between(stale["in_ts"], close_at)
|
| 38 |
+
capped = worked > MAX_SHIFT_MINUTES
|
| 39 |
+
if capped:
|
| 40 |
+
worked = MAX_SHIFT_MINUTES
|
| 41 |
+
|
| 42 |
+
await db.checkins.update_one(
|
| 43 |
+
{"_id": stale["_id"]},
|
| 44 |
+
{"$set": {"out_ts": close_at, "worked_min": worked,
|
| 45 |
+
"abnormal": True, "auto_closed": True, "name": name}},
|
| 46 |
+
)
|
| 47 |
+
logger.info("[Checkin] 自动补下班卡 %s %s -> %s 分钟%s",
|
| 48 |
+
name, stale["date"], worked, "(已封顶)" if capped else "")
|
| 49 |
+
return stale["date"]
|
| 50 |
+
|
| 51 |
+
|
| 52 |
async def clock_in(user_id: str, name: str) -> dict:
|
| 53 |
now_dt = timeutil.now()
|
| 54 |
date_str = timeutil.today_str()
|
|
|
|
| 57 |
if doc and doc.get("in_ts"):
|
| 58 |
return {"ok": False, "code": "already_in", "in_ts": doc["in_ts"]}
|
| 59 |
|
| 60 |
+
# 先把上一班没闭合的补上(客户规则的落点)
|
| 61 |
+
closed_date = await close_stale_shift(user_id, name, now_dt)
|
| 62 |
+
|
| 63 |
await db.checkins.update_one(
|
| 64 |
{"_id": _doc_id(user_id, date_str)},
|
| 65 |
{"$set": {"user_id": user_id, "name": name,
|
|
|
|
| 67 |
"$setOnInsert": {"out_ts": None, "worked_min": None, "abnormal": False}},
|
| 68 |
upsert=True,
|
| 69 |
)
|
| 70 |
+
logger.info("[Checkin] 上班 %s %s%s", name, now_dt.isoformat(),
|
| 71 |
+
f"(同时补上了 {closed_date} 的下班卡)" if closed_date else "")
|
| 72 |
+
return {"ok": True, "in_ts": now_dt, "closed_date": closed_date}
|
| 73 |
|
| 74 |
|
| 75 |
async def clock_out(user_id: str, name: str) -> dict:
|
|
|
|
| 161 |
if not doc.get("in_ts"):
|
| 162 |
return "缺上班卡"
|
| 163 |
if doc.get("out_ts"):
|
| 164 |
+
if doc.get("auto_closed"):
|
| 165 |
+
return "自动补卡"
|
| 166 |
return "异常" if doc.get("abnormal") else "正常"
|
| 167 |
+
return "待补下班卡"
|
|
|
|
|
|
utils/card_builder.py
CHANGED
|
@@ -134,7 +134,7 @@ def build_records_card(name: str, days: int, records: list):
|
|
| 134 |
st = status_of(doc)
|
| 135 |
worked = doc.get("worked_min") or 0
|
| 136 |
total += worked
|
| 137 |
-
mark = {"正常": "✅", "异常": "⚠️", "
|
| 138 |
"待补下班卡": "❌", "缺上班卡": "❌"}.get(st, "•")
|
| 139 |
rng = ""
|
| 140 |
if doc.get("in_ts"):
|
|
|
|
| 134 |
st = status_of(doc)
|
| 135 |
worked = doc.get("worked_min") or 0
|
| 136 |
total += worked
|
| 137 |
+
mark = {"正常": "✅", "异常": "⚠️", "自动补卡": "🔸",
|
| 138 |
"待补下班卡": "❌", "缺上班卡": "❌"}.get(st, "•")
|
| 139 |
rng = ""
|
| 140 |
if doc.get("in_ts"):
|