Reapply "added txt file export as extra export."

This reverts commit d51c16399c.
This commit is contained in:
Edison
2026-03-19 16:56:30 +08:00
parent d51c16399c
commit c98b16dbd7
2 changed files with 75 additions and 0 deletions
+34
View File
@@ -392,6 +392,7 @@ export default function () {
}
const wantXlsx = String(req.query.format || 'csv').toLowerCase() === 'xlsx'
const wantTxt = String(req.query.format || '').toLowerCase() === 'txt'
let workerIdClause = ''
let departmentClause = ''
@@ -522,6 +523,39 @@ export default function () {
byWorkerForXlsx.set(`${w.username}||${w.full_name}||${w.department}`, perWorkerRows)
}
// ===== TXT branch =====
if (wantTxt) {
const lines = []
for (const wId in workByDay) {
const w = workByDay[wId]
const seen = new Set()
for (const day of Object.keys(w.days).sort()) {
const events = w.days[day].slice().sort((a, b) => a.time - b.time)
for (const e of events) {
const code = e.type === 'clock_in' ? '1' : '0'
const date = ymdInTZ(e.time, TZ)
const timeStr = new Intl.DateTimeFormat('en-GB', {
timeZone: TZ,
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false,
}).format(e.time)
const line = `${w.username},"${code}","${date} ${timeStr}";"${w.full_name}"`
if (!seen.has(line)) {
seen.add(line)
lines.push(line)
}
}
}
}
res.set('X-Export-TZ', TZ)
res.header('Content-Type', 'text/plain')
.attachment(`attendance_${startDate}_to_${endDate}.txt`)
.send(lines.join('\n'))
return
}
// ===== XLSX branch: grouped header + per-day summary sheet =====
if (wantXlsx) {
const wb = new ExcelJS.Workbook()