Files
dbx-main/app/static/erp-overdue.js
T

107 lines
5.5 KiB
JavaScript

// 마감 지난 내 업무 알림 — ERP 모든 화면 위쪽 가운데(2026-10-13).
// · GET /project/api/my-overdue — 내가 담당인 미완료 업무 중 마감일이 오늘(한국시간) 이전인 것(지금 볼 수 있는 것만).
// · [닫기] 하면 1시간 동안 다시 띄우지 않는다(이 브라우저의 모든 탭 — localStorage). 1시간이 지나면 다시 나타난다.
// 단, 닫을 때 없던 업무가 새로 마감을 넘기면 1시간을 기다리지 않고 바로 띄운다.
// · 5분마다·창을 다시 볼 때 확인. 다 완료하면 알림은 사라진다.
// · Project 권한이 없거나(403) 로그아웃(401)·모듈 미설정(503)이면 조용히 멈춘다. iframe(수신함 업무 창 등) 안에서는 돌지 않는다.
(function () {
"use strict";
if (window.top !== window) return;
const POLL_MS = 5 * 60 * 1000;
const SNOOZE_MS = 60 * 60 * 1000;
const KEY = "erp_overdue_snooze"; // {until: ms, ids: [업무 id]}
const MAX_ROWS = 5;
let box = null, timer = 0, busy = false, stopped = false, last = [], memSnooze = null;
function esc(s) {
return String(s == null ? "" : s).replace(/[&<>"']/g, function (c) {
return { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" }[c];
});
}
function readSnooze() {
try { return JSON.parse(localStorage.getItem(KEY) || "null") || memSnooze; } catch (_) { return memSnooze; }
}
function writeSnooze(v) {
memSnooze = v;
try { localStorage.setItem(KEY, JSON.stringify(v)); } catch (_) {}
}
// 닫은 지 1시간이 안 됐고, 지금 목록이 닫을 때 본 업무뿐이면 쉰다.
function snoozed(tasks) {
const s = readSnooze();
if (!s || !(Date.now() < s.until)) return false;
const seen = s.ids || [];
return tasks.every(function (t) { return seen.indexOf(t.id) >= 0; });
}
function daysLate(due, today) {
const a = Date.parse(String(due).slice(0, 10) + "T00:00:00Z"), b = Date.parse(String(today).slice(0, 10) + "T00:00:00Z");
return isNaN(a) || isNaN(b) ? 0 : Math.round((b - a) / 86400000);
}
function md(due) {
const m = /^(\d{4})-(\d{2})-(\d{2})/.exec(due || "");
return m ? parseInt(m[2], 10) + "/" + parseInt(m[3], 10) : "";
}
function schedule() { clearTimeout(timer); if (!stopped) timer = setTimeout(poll, POLL_MS); }
async function poll() {
if (busy || stopped) return;
if (document.hidden) { schedule(); return; }
busy = true;
try {
const res = await fetch("/project/api/my-overdue", { headers: { Accept: "application/json" }, credentials: "same-origin" });
if (res.status === 401 || res.status === 403 || res.status === 404 || res.status === 503) { stopped = true; hide(); return; }
if (!res.ok) return;
const data = await res.json();
last = Array.isArray(data.tasks) ? data.tasks : [];
if (!last.length || snoozed(last)) hide();
else show(last, data.today || "");
} catch (_) {
// 네트워크 오류는 다음 주기에 다시
} finally {
busy = false;
schedule();
}
}
function show(tasks, today) {
if (!box) {
box = document.createElement("div");
box.className = "erp-overdue";
box.setAttribute("role", "alert");
document.body.appendChild(box); // 화면 배율(.erp-app zoom) 밖 — 늘 화면 위쪽 가운데
box.addEventListener("click", function (e) {
if (e.target.closest("[data-overdue-close]")) dismiss();
});
}
const rows = tasks.slice(0, MAX_ROWS).map(function (t) {
const late = daysLate(t.due_date, today);
const where = [t.project_name, t.parent_title ? "상위: " + t.parent_title : ""].filter(Boolean).join(" · ");
return '<a class="erp-overdue-item" href="/project/t/' + encodeURIComponent(t.id) + '">' +
'<span class="erp-overdue-dot" style="background:' + esc(t.project_color || "#e5484d") + '"></span>' +
'<span class="erp-overdue-text"><b>' + esc(t.title || "(제목 없음)") + "</b><small>" + esc(where) + "</small></span>" +
'<span class="erp-overdue-late">마감 ' + esc(md(t.due_date)) + (late > 0 ? " · " + late + "일 지남" : "") + "</span></a>";
}).join("");
const more = tasks.length > MAX_ROWS
? '<a class="erp-overdue-more" href="/project/">그 밖에 ' + (tasks.length - MAX_ROWS) + "건 — 내 업무에서 보기</a>" : "";
box.innerHTML =
'<div class="erp-overdue-head"><span class="erp-overdue-ic" aria-hidden="true">⏰</span>' +
'<b>마감이 지난 내 업무 ' + tasks.length + "건</b>" +
'<button type="button" class="erp-overdue-close" data-overdue-close>닫기</button></div>' +
'<div class="erp-overdue-list">' + rows + more + "</div>";
box.hidden = false;
}
function hide() { if (box) box.hidden = true; }
function dismiss() {
writeSnooze({ until: Date.now() + SNOOZE_MS, ids: last.map(function (t) { return t.id; }) });
hide();
// 1시간 뒤 바로 다시 확인(5분 주기를 기다리지 않게)
setTimeout(poll, SNOOZE_MS + 1000);
}
// 다른 탭에서 닫으면 이 탭도 닫는다.
window.addEventListener("storage", function (e) { if (e.key === KEY && last.length && snoozed(last)) hide(); });
document.addEventListener("visibilitychange", function () { if (!document.hidden) poll(); });
if (window.__ERP_OVERDUE_TEST__) window.__erpOverdue = { poll: poll, state: function () { return { last: last, stopped: stopped, box: box }; } };
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", poll);
else poll();
})();