meraproject/services/user-reader/app/labor_cache.py

52 lines
1.5 KiB
Python
Raw Permalink Normal View History

"""Инвалидация tMerakomisTimeCache — порт Cache::removeRows."""
from __future__ import annotations
from datetime import date
from app.labor import _column_lookup, _prefixed_col, _resolve_table
from app.main import _quote_ident, _table_columns
from app.merakomis_schema import TIME_CACHE_TABLE
def invalidate_time_cache(
cur,
db: str,
day: date,
emp_id: int | None = None,
project_id: int | None = None,
) -> int:
table = _resolve_table(cur, db, TIME_CACHE_TABLE)
cols = _table_columns(cur, db, table)
lut = _column_lookup(cols)
begin_col = _prefixed_col(lut, TIME_CACHE_TABLE, "begin")
end_col = _prefixed_col(lut, TIME_CACHE_TABLE, "end")
emp_col = _prefixed_col(lut, TIME_CACHE_TABLE, "emp")
proj_col = _prefixed_col(lut, TIME_CACHE_TABLE, "project")
if not begin_col or not end_col:
return 0
ds = day.isoformat()
conds = [
f"{_quote_ident(begin_col)} <= %s",
f"{_quote_ident(end_col)} >= %s",
]
params: list = [ds, ds]
if emp_id is not None and emp_col:
conds.append(f"{_quote_ident(emp_col)} = %s")
params.append(emp_id)
if project_id is not None and proj_col:
conds.append(
f"({_quote_ident(proj_col)} = %s OR {_quote_ident(proj_col)} = 0)"
)
params.append(project_id)
tq = _quote_ident(table)
cur.execute(
f"DELETE FROM {tq} WHERE {' AND '.join(conds)}",
tuple(params),
)
return int(cur.rowcount)