File size: 2,366 Bytes
fc06b79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System.Text.Json;
using ContactManagementAPI.Models;

namespace ContactManagementAPI.Services
{
    public class AdminHistoryService
    {
        private readonly string _historyFilePath;
        private static readonly JsonSerializerOptions JsonOptions = new()
        {
            WriteIndented = false
        };

        public AdminHistoryService(IWebHostEnvironment environment)
        {
            var historyDirectory = Path.Combine(environment.ContentRootPath, "App_Data");
            Directory.CreateDirectory(historyDirectory);
            _historyFilePath = Path.Combine(historyDirectory, "admin-history.jsonl");
        }

        public void Log(string actionType, string entityType, int? entityId, string performedBy, string details)
        {
            var entry = new AdminHistoryEntry
            {
                ActionType = actionType,
                EntityType = entityType,
                EntityId = entityId,
                PerformedBy = string.IsNullOrWhiteSpace(performedBy) ? "Unknown" : performedBy,
                Details = details,
                PerformedAt = DateTime.Now
            };

            var line = JsonSerializer.Serialize(entry, JsonOptions);
            File.AppendAllText(_historyFilePath, line + Environment.NewLine);
        }

        public IReadOnlyList<AdminHistoryEntry> GetLatest(int take = 200)
        {
            if (!File.Exists(_historyFilePath))
            {
                return Array.Empty<AdminHistoryEntry>();
            }

            var entries = new List<AdminHistoryEntry>();
            var lines = File.ReadLines(_historyFilePath);
            foreach (var line in lines)
            {
                if (string.IsNullOrWhiteSpace(line))
                {
                    continue;
                }

                try
                {
                    var entry = JsonSerializer.Deserialize<AdminHistoryEntry>(line);
                    if (entry != null)
                    {
                        entries.Add(entry);
                    }
                }
                catch
                {
                    // Ignore malformed history lines and continue
                }
            }

            return entries
                .OrderByDescending(e => e.PerformedAt)
                .Take(take)
                .ToList();
        }
    }
}