45 lines
2.0 KiB
JavaScript
45 lines
2.0 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* Loeschfristen (Datenschutzerklaerung Abschnitt 7): Abo-Daten drei Jahre nach Ablauf des Jahres loeschen, in dem
|
|
* das letzte Abo eines Kunden endet. Einmal im Jahr im Januar ausfuehren, im Container als Datei:
|
|
* docker exec -w /app <container> node scripts/loeschfristen.mjs (Probelauf: Bericht, loescht nichts)
|
|
* docker exec -w /app -e LOESCHFRISTEN=mail <container> node scripts/loeschfristen.mjs (Bericht als T14 ans Team)
|
|
* docker exec -w /app -e LOESCHFRISTEN=apply <container> node scripts/loeschfristen.mjs (loescht, Bericht danach)
|
|
*/
|
|
import 'dotenv/config'
|
|
import { applyDeletions, findDueDeletions, formatReport } from '../server/services/retention.js'
|
|
import { sendAdminNotice } from '../server/services/mailer.js'
|
|
|
|
const MODES = new Set(['', 'mail', 'apply'])
|
|
|
|
/** mode: '' (Probelauf) | 'mail' | 'apply'. Liefert { plan, result, applied }. */
|
|
export async function run({ mode = '', now = new Date(), log = console.log } = {}) {
|
|
if (!MODES.has(mode)) throw new Error(`LOESCHFRISTEN="${mode}" ist unbekannt: leer (Probelauf), mail oder apply`)
|
|
const plan = await findDueDeletions(now)
|
|
if (mode === 'apply') {
|
|
const result = await applyDeletions(plan)
|
|
log('Löschfristen: gelöscht')
|
|
log(formatReport(plan, result))
|
|
return { plan, result, applied: true }
|
|
}
|
|
const report = formatReport(plan)
|
|
log('Löschfristen: Probelauf, es wird nichts gelöscht')
|
|
log(report)
|
|
if (mode === 'mail') {
|
|
const sent = await sendAdminNotice({
|
|
template: 'T14',
|
|
values: { modus: 'Probelauf', anzahl_kunden: plan.customers.length, bericht: report },
|
|
})
|
|
if (!sent.sent) log(`Bericht nicht verschickt: ${sent.reason}`)
|
|
else log('Bericht als T14 ans Team geschickt.')
|
|
}
|
|
return { plan, result: null, applied: false }
|
|
}
|
|
|
|
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
run({ mode: process.env.LOESCHFRISTEN || '' }).catch((err) => {
|
|
console.error('❌', err.message)
|
|
process.exit(1)
|
|
})
|
|
}
|