|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Resgrid.Model; |
| 6 | +using Resgrid.Model.Services; |
| 7 | + |
| 8 | +namespace Resgrid.Services |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Aggregates the offline shift-start REFERENCE data set (department configuration + a SAFE personnel roster) into |
| 12 | + /// one payload. Read-only. Personnel is projected to <see cref="ReferencePersonnel"/> so no credentials, security |
| 13 | + /// fields, or contact-verification secrets are exposed. The live per-incident state is delivered separately by the |
| 14 | + /// incident-command bundle (/Sync/Bundle) and delta (/Sync/Changes) endpoints. |
| 15 | + /// </summary> |
| 16 | + public class SyncService : ISyncService |
| 17 | + { |
| 18 | + private readonly ICallsService _callsService; |
| 19 | + private readonly ICommandsService _commandsService; |
| 20 | + private readonly IUnitsService _unitsService; |
| 21 | + private readonly IDepartmentGroupsService _departmentGroupsService; |
| 22 | + private readonly IMappingService _mappingService; |
| 23 | + private readonly IProtocolsService _protocolsService; |
| 24 | + private readonly ICheckInTimerService _checkInTimerService; |
| 25 | + private readonly ICustomStateService _customStateService; |
| 26 | + private readonly IUserProfileService _userProfileService; |
| 27 | + private readonly IUserStateService _userStateService; |
| 28 | + private readonly IFeatureToggleService _featureToggleService; |
| 29 | + |
| 30 | + public SyncService( |
| 31 | + ICallsService callsService, |
| 32 | + ICommandsService commandsService, |
| 33 | + IUnitsService unitsService, |
| 34 | + IDepartmentGroupsService departmentGroupsService, |
| 35 | + IMappingService mappingService, |
| 36 | + IProtocolsService protocolsService, |
| 37 | + ICheckInTimerService checkInTimerService, |
| 38 | + ICustomStateService customStateService, |
| 39 | + IUserProfileService userProfileService, |
| 40 | + IUserStateService userStateService, |
| 41 | + IFeatureToggleService featureToggleService) |
| 42 | + { |
| 43 | + _callsService = callsService; |
| 44 | + _commandsService = commandsService; |
| 45 | + _unitsService = unitsService; |
| 46 | + _departmentGroupsService = departmentGroupsService; |
| 47 | + _mappingService = mappingService; |
| 48 | + _protocolsService = protocolsService; |
| 49 | + _checkInTimerService = checkInTimerService; |
| 50 | + _customStateService = customStateService; |
| 51 | + _userProfileService = userProfileService; |
| 52 | + _userStateService = userStateService; |
| 53 | + _featureToggleService = featureToggleService; |
| 54 | + } |
| 55 | + |
| 56 | + public async Task<SyncReferenceData> GetReferenceDataAsync(int departmentId) |
| 57 | + { |
| 58 | + var data = new SyncReferenceData { ServerTimestampMs = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }; |
| 59 | + |
| 60 | + // Configuration / reference entities returned as-is (audited: no secret scalar fields, no IdentityUser navs). |
| 61 | + data.CallTypes = await _callsService.GetCallTypesForDepartmentAsync(departmentId) ?? new List<CallType>(); |
| 62 | + data.CallPriorities = await _callsService.GetCallPrioritiesForDepartmentAsync(departmentId) ?? new List<DepartmentCallPriority>(); |
| 63 | + data.CommandTemplates = await _commandsService.GetAllCommandsForDepartmentAsync(departmentId) ?? new List<CommandDefinition>(); |
| 64 | + data.Units = await _unitsService.GetUnitsForDepartmentAsync(departmentId) ?? new List<Unit>(); |
| 65 | + data.UnitTypes = await _unitsService.GetUnitTypesForDepartmentAsync(departmentId) ?? new List<UnitType>(); |
| 66 | + data.Pois = await _mappingService.GetPOIsForDepartmentAsync(departmentId) ?? new List<Poi>(); |
| 67 | + data.PoiTypes = await _mappingService.GetPOITypesForDepartmentAsync(departmentId) ?? new List<PoiType>(); |
| 68 | + data.Protocols = await _protocolsService.GetAllProtocolsForDepartmentAsync(departmentId) ?? new List<DispatchProtocol>(); |
| 69 | + data.CheckInTimerConfigs = await _checkInTimerService.GetTimerConfigsForDepartmentAsync(departmentId) ?? new List<CheckInTimerConfig>(); |
| 70 | + data.PersonnelStates = await _customStateService.GetAllActiveCustomStatesForDepartmentAsync(departmentId) ?? new List<CustomState>(); |
| 71 | + data.UnitStates = await _customStateService.GetAllActiveUnitStatesForDepartmentAsync(departmentId) ?? new List<CustomState>(); |
| 72 | + data.Features = await _featureToggleService.EvaluateAllForDepartmentAsync(departmentId) ?? new List<FeatureFlagEvaluation>(); |
| 73 | + |
| 74 | + // Groups: project to a safe shape. The raw DepartmentGroup.Members carry IdentityUser navs we must not leak, |
| 75 | + // and mutating the (possibly cached) entities to strip them would be unsafe. |
| 76 | + var groups = await _departmentGroupsService.GetAllGroupsForDepartmentAsync(departmentId) ?? new List<DepartmentGroup>(); |
| 77 | + data.Groups = groups.Select(g => new ReferenceGroup |
| 78 | + { |
| 79 | + GroupId = g.DepartmentGroupId, |
| 80 | + Name = g.Name, |
| 81 | + Type = g.Type, |
| 82 | + ParentGroupId = g.ParentDepartmentGroupId |
| 83 | + }).ToList(); |
| 84 | + |
| 85 | + data.Personnel = await BuildPersonnelAsync(departmentId, groups); |
| 86 | + |
| 87 | + return data; |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Builds the SAFE personnel roster (name + mobile, primary group, current state) projected from UserProfile + |
| 92 | + /// UserState — never exposing the IdentityUser nav, password/security fields, or the UserProfile contact- |
| 93 | + /// verification codes / CalendarSyncToken. Mirrors the field exposure of the existing v4 PersonnelInfoResultData. |
| 94 | + /// </summary> |
| 95 | + private async Task<List<ReferencePersonnel>> BuildPersonnelAsync(int departmentId, List<DepartmentGroup> groups) |
| 96 | + { |
| 97 | + var personnel = new List<ReferencePersonnel>(); |
| 98 | + |
| 99 | + var profiles = await _userProfileService.GetAllProfilesForDepartmentAsync(departmentId); |
| 100 | + if (profiles == null || profiles.Count == 0) |
| 101 | + return personnel; |
| 102 | + |
| 103 | + // First group membership wins as the member's "primary" group. |
| 104 | + var userGroup = new Dictionary<string, ReferenceGroup>(); |
| 105 | + foreach (var g in groups) |
| 106 | + { |
| 107 | + if (g.Members == null) |
| 108 | + continue; |
| 109 | + |
| 110 | + foreach (var m in g.Members) |
| 111 | + { |
| 112 | + if (!string.IsNullOrWhiteSpace(m.UserId) && !userGroup.ContainsKey(m.UserId)) |
| 113 | + userGroup[m.UserId] = new ReferenceGroup { GroupId = g.DepartmentGroupId, Name = g.Name }; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + var states = await _userStateService.GetStatesForDepartmentAsync(departmentId) ?? new List<UserState>(); |
| 118 | + var stateByUser = states |
| 119 | + .Where(s => !string.IsNullOrWhiteSpace(s.UserId)) |
| 120 | + .GroupBy(s => s.UserId) |
| 121 | + .ToDictionary(grp => grp.Key, grp => grp.OrderByDescending(s => s.Timestamp).First()); |
| 122 | + |
| 123 | + foreach (var profile in profiles.Values) |
| 124 | + { |
| 125 | + if (profile == null || string.IsNullOrWhiteSpace(profile.UserId)) |
| 126 | + continue; |
| 127 | + |
| 128 | + var person = new ReferencePersonnel |
| 129 | + { |
| 130 | + UserId = profile.UserId, |
| 131 | + FirstName = profile.FirstName, |
| 132 | + LastName = profile.LastName, |
| 133 | + MobilePhone = profile.MobileNumber |
| 134 | + }; |
| 135 | + |
| 136 | + if (userGroup.TryGetValue(profile.UserId, out var grp)) |
| 137 | + { |
| 138 | + person.GroupId = grp.GroupId; |
| 139 | + person.GroupName = grp.Name; |
| 140 | + } |
| 141 | + |
| 142 | + if (stateByUser.TryGetValue(profile.UserId, out var state)) |
| 143 | + { |
| 144 | + person.StateId = state.State; |
| 145 | + person.StateTimestamp = state.Timestamp; |
| 146 | + } |
| 147 | + |
| 148 | + personnel.Add(person); |
| 149 | + } |
| 150 | + |
| 151 | + return personnel; |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments