class TrackingAuditLogger {
constructor() {
this.deletionLog = [];
}
logDeletion(trackingId, result, deletedBy = "api") {
const logEntry = {
trackingId,
deletedAt: new Date().toISOString(),
deletedBy,
success: result.status === "deleted",
finalStatistics: result.final_statistics,
error: result.error || null,
};
this.deletionLog.push(logEntry);
// Store in persistent storage
this.storeAuditLog(logEntry);
console.log(`Audit log: Tracking ${trackingId} deletion logged`);
}
async storeAuditLog(logEntry) {
// Implement your audit storage logic
// Could be database, logging service, etc.
console.log("Storing audit log entry:", logEntry);
}
getDeletionHistory(trackingId = null) {
if (trackingId) {
return this.deletionLog.filter((entry) => entry.trackingId === trackingId);
}
return this.deletionLog;
}
}
// Usage
const auditLogger = new TrackingAuditLogger();
async function deleteTrackingWithAudit(apiKey, trackingId) {
try {
const result = await deleteTracking(apiKey, trackingId);
auditLogger.logDeletion(trackingId, result);
return result;
} catch (error) {
auditLogger.logDeletion(trackingId, { error: error.message, status: "failed" });
throw error;
}
}