new features
This commit is contained in:
131
backend/src/services/cleanup.service.ts
Normal file
131
backend/src/services/cleanup.service.ts
Normal file
@@ -0,0 +1,131 @@
|
||||
import pool from '../config/database';
|
||||
import logger from '../utils/logger';
|
||||
|
||||
export interface CleanupResult {
|
||||
count: number;
|
||||
deleted: boolean;
|
||||
}
|
||||
|
||||
class CleanupService {
|
||||
|
||||
async cleanupNotifications(olderThanDays: number, confirm: boolean): Promise<CleanupResult> {
|
||||
const cutoff = `${olderThanDays} days`;
|
||||
if (!confirm) {
|
||||
const { rows } = await pool.query(
|
||||
`SELECT COUNT(*)::int AS count FROM benachrichtigungen WHERE erstellt_am < NOW() - $1::interval`,
|
||||
[cutoff]
|
||||
);
|
||||
return { count: rows[0].count, deleted: false };
|
||||
}
|
||||
const { rowCount } = await pool.query(
|
||||
`DELETE FROM benachrichtigungen WHERE erstellt_am < NOW() - $1::interval`,
|
||||
[cutoff]
|
||||
);
|
||||
logger.info(`Cleanup: deleted ${rowCount} notifications older than ${olderThanDays} days`);
|
||||
return { count: rowCount ?? 0, deleted: true };
|
||||
}
|
||||
|
||||
async cleanupAuditLog(olderThanDays: number, confirm: boolean): Promise<CleanupResult> {
|
||||
const cutoff = `${olderThanDays} days`;
|
||||
if (!confirm) {
|
||||
const { rows } = await pool.query(
|
||||
`SELECT COUNT(*)::int AS count FROM audit_log WHERE created_at < NOW() - $1::interval`,
|
||||
[cutoff]
|
||||
);
|
||||
return { count: rows[0].count, deleted: false };
|
||||
}
|
||||
const { rowCount } = await pool.query(
|
||||
`DELETE FROM audit_log WHERE created_at < NOW() - $1::interval`,
|
||||
[cutoff]
|
||||
);
|
||||
logger.info(`Cleanup: deleted ${rowCount} audit_log entries older than ${olderThanDays} days`);
|
||||
return { count: rowCount ?? 0, deleted: true };
|
||||
}
|
||||
|
||||
async cleanupEvents(olderThanDays: number, confirm: boolean): Promise<CleanupResult> {
|
||||
const cutoff = `${olderThanDays} days`;
|
||||
if (!confirm) {
|
||||
const { rows } = await pool.query(
|
||||
`SELECT COUNT(*)::int AS count FROM events WHERE end_date < NOW() - $1::interval`,
|
||||
[cutoff]
|
||||
);
|
||||
return { count: rows[0].count, deleted: false };
|
||||
}
|
||||
const { rowCount } = await pool.query(
|
||||
`DELETE FROM events WHERE end_date < NOW() - $1::interval`,
|
||||
[cutoff]
|
||||
);
|
||||
logger.info(`Cleanup: deleted ${rowCount} events older than ${olderThanDays} days`);
|
||||
return { count: rowCount ?? 0, deleted: true };
|
||||
}
|
||||
|
||||
async cleanupBookings(olderThanDays: number, confirm: boolean): Promise<CleanupResult> {
|
||||
const cutoff = `${olderThanDays} days`;
|
||||
if (!confirm) {
|
||||
const { rows } = await pool.query(
|
||||
`SELECT COUNT(*)::int AS count FROM fahrzeug_buchungen WHERE end_date < NOW() - $1::interval AND status IN ('completed', 'cancelled')`,
|
||||
[cutoff]
|
||||
);
|
||||
return { count: rows[0].count, deleted: false };
|
||||
}
|
||||
const { rowCount } = await pool.query(
|
||||
`DELETE FROM fahrzeug_buchungen WHERE end_date < NOW() - $1::interval AND status IN ('completed', 'cancelled')`,
|
||||
[cutoff]
|
||||
);
|
||||
logger.info(`Cleanup: deleted ${rowCount} bookings older than ${olderThanDays} days`);
|
||||
return { count: rowCount ?? 0, deleted: true };
|
||||
}
|
||||
|
||||
async cleanupOrders(olderThanDays: number, confirm: boolean): Promise<CleanupResult> {
|
||||
const cutoff = `${olderThanDays} days`;
|
||||
if (!confirm) {
|
||||
const { rows } = await pool.query(
|
||||
`SELECT COUNT(*)::int AS count FROM bestellungen WHERE updated_at < NOW() - $1::interval AND status = 'abgeschlossen'`,
|
||||
[cutoff]
|
||||
);
|
||||
return { count: rows[0].count, deleted: false };
|
||||
}
|
||||
const { rowCount } = await pool.query(
|
||||
`DELETE FROM bestellungen WHERE updated_at < NOW() - $1::interval AND status = 'abgeschlossen'`,
|
||||
[cutoff]
|
||||
);
|
||||
logger.info(`Cleanup: deleted ${rowCount} orders older than ${olderThanDays} days`);
|
||||
return { count: rowCount ?? 0, deleted: true };
|
||||
}
|
||||
|
||||
async cleanupVehicleHistory(olderThanDays: number, confirm: boolean): Promise<CleanupResult> {
|
||||
const cutoff = `${olderThanDays} days`;
|
||||
if (!confirm) {
|
||||
const { rows } = await pool.query(
|
||||
`SELECT COUNT(*)::int AS count FROM fahrzeug_wartungslog WHERE datum < NOW() - $1::interval`,
|
||||
[cutoff]
|
||||
);
|
||||
return { count: rows[0].count, deleted: false };
|
||||
}
|
||||
const { rowCount } = await pool.query(
|
||||
`DELETE FROM fahrzeug_wartungslog WHERE datum < NOW() - $1::interval`,
|
||||
[cutoff]
|
||||
);
|
||||
logger.info(`Cleanup: deleted ${rowCount} vehicle history entries older than ${olderThanDays} days`);
|
||||
return { count: rowCount ?? 0, deleted: true };
|
||||
}
|
||||
|
||||
async cleanupEquipmentHistory(olderThanDays: number, confirm: boolean): Promise<CleanupResult> {
|
||||
const cutoff = `${olderThanDays} days`;
|
||||
if (!confirm) {
|
||||
const { rows } = await pool.query(
|
||||
`SELECT COUNT(*)::int AS count FROM ausruestung_wartungslog WHERE datum < NOW() - $1::interval`,
|
||||
[cutoff]
|
||||
);
|
||||
return { count: rows[0].count, deleted: false };
|
||||
}
|
||||
const { rowCount } = await pool.query(
|
||||
`DELETE FROM ausruestung_wartungslog WHERE datum < NOW() - $1::interval`,
|
||||
[cutoff]
|
||||
);
|
||||
logger.info(`Cleanup: deleted ${rowCount} equipment history entries older than ${olderThanDays} days`);
|
||||
return { count: rowCount ?? 0, deleted: true };
|
||||
}
|
||||
}
|
||||
|
||||
export default new CleanupService();
|
||||
Reference in New Issue
Block a user