mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 11:53:12 +00:00
87 lines
3 KiB
C#
87 lines
3 KiB
C#
using Data.SeaHavenIndustries;
|
|
using SeaHaven.Services.DTOs;
|
|
|
|
namespace SeaHaven.Services.Helpers
|
|
{
|
|
public static class WorkOrderAuditProjection
|
|
{
|
|
private const string UnassignedLabel = "Unassigned";
|
|
|
|
public static WorkOrderAuditEntryDto ToDto(
|
|
WorkOrderAuditLog log,
|
|
IReadOnlyDictionary<string, string>? assignToDisplayNames = null)
|
|
{
|
|
var oldValue = log.OldValue ?? "";
|
|
var newValue = log.NewValue ?? "";
|
|
|
|
if (IsAssignToField(log.FieldName))
|
|
{
|
|
oldValue = ResolveAssignToDisplay(oldValue, assignToDisplayNames);
|
|
newValue = ResolveAssignToDisplay(newValue, assignToDisplayNames);
|
|
}
|
|
|
|
return new WorkOrderAuditEntryDto
|
|
{
|
|
Type = MapEventType(log.EventType),
|
|
DispatcherId = log.UserId,
|
|
Action = log.Action ?? "",
|
|
FieldName = log.FieldName,
|
|
OldValue = oldValue,
|
|
NewValue = newValue,
|
|
Time = log.CreatedAt.ToString("o"),
|
|
DispatchId = log.DispatchId
|
|
};
|
|
}
|
|
|
|
public static List<WorkOrderAuditEntryDto> ToDtos(
|
|
IEnumerable<WorkOrderAuditLog> logs,
|
|
IReadOnlyDictionary<string, string>? assignToDisplayNames = null)
|
|
=> logs.Select(log => ToDto(log, assignToDisplayNames)).ToList();
|
|
|
|
public static IEnumerable<string> CollectAssignToUserIds(IEnumerable<WorkOrderAuditLog> logs)
|
|
{
|
|
return logs
|
|
.Where(l => IsAssignToField(l.FieldName))
|
|
.SelectMany(l => new[] { l.OldValue, l.NewValue })
|
|
.Where(v => !string.IsNullOrWhiteSpace(v))
|
|
.Select(v => v!)
|
|
.Distinct(StringComparer.Ordinal);
|
|
}
|
|
|
|
internal static bool IsAssignToField(string? fieldName)
|
|
=> string.Equals(fieldName, "AssignTo", StringComparison.OrdinalIgnoreCase)
|
|
|| string.Equals(fieldName, "AssignedTo", StringComparison.OrdinalIgnoreCase);
|
|
|
|
private static string ResolveAssignToDisplay(
|
|
string value,
|
|
IReadOnlyDictionary<string, string>? assignToDisplayNames)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(value))
|
|
return UnassignedLabel;
|
|
|
|
if (assignToDisplayNames != null
|
|
&& assignToDisplayNames.TryGetValue(value, out var name)
|
|
&& !string.IsNullOrWhiteSpace(name))
|
|
{
|
|
return name;
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
private static string MapEventType(string? eventType)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(eventType))
|
|
return "manual";
|
|
|
|
return eventType.ToLowerInvariant() switch
|
|
{
|
|
"manual" => "manual",
|
|
"system" => "system",
|
|
"sync" => "sync",
|
|
"vendor" => "vendor",
|
|
_ => eventType.ToLowerInvariant()
|
|
};
|
|
}
|
|
}
|
|
}
|