public IActionResult EditUnit(int unitId) { var model = new NewUnitView(); model.Unit = _unitsService.GetUnitById(unitId); if (!_authorizationService.CanUserModifyUnit(UserId, unitId)) { Unauthorized(); } model.Types = _unitsService.GetUnitTypesForDepartment(DepartmentId); var groups = new List <DepartmentGroup>(); groups.Add(new DepartmentGroup { Name = "No Station" }); groups.AddRange(_departmentGroupsService.GetAllStationGroupsForDepartment(DepartmentId)); model.Stations = groups; model.UnitRoles = _unitsService.GetRolesForUnit(unitId); return(View(model)); }
public bool CanUserModifyUnit(string userId, int unitId) { var department = _departmentsService.GetDepartmentByUserId(userId); var unit = _unitsService.GetUnitById(unitId); if (department == null || unit == null) { return(false); } if (unit.DepartmentId != department.DepartmentId) { return(false); } if (department.IsUserAnAdmin(userId)) { return(true); } return(false); }
private HttpResponseMessage ProcessSetUnitState(UnitStateInput stateInput) { var unit = _unitsService.GetUnitById(stateInput.Uid); if (unit == null) { throw HttpStatusCode.NotFound.AsException(); } if (unit.DepartmentId != DepartmentId) { throw HttpStatusCode.Unauthorized.AsException(); } if (this.ModelState.IsValid) { try { var state = new UnitState(); state.UnitId = stateInput.Uid; state.LocalTimestamp = stateInput.Lts; if (!String.IsNullOrWhiteSpace(stateInput.Lat)) { state.Latitude = decimal.Parse(stateInput.Lat); } if (!String.IsNullOrWhiteSpace(stateInput.Lon)) { state.Longitude = decimal.Parse(stateInput.Lon); } if (!String.IsNullOrWhiteSpace(stateInput.Acc)) { state.Accuracy = decimal.Parse(stateInput.Acc); } if (!String.IsNullOrWhiteSpace(stateInput.Alt)) { state.Altitude = decimal.Parse(stateInput.Alt); } if (!String.IsNullOrWhiteSpace(stateInput.Alc)) { state.AltitudeAccuracy = decimal.Parse(stateInput.Alc); } if (!String.IsNullOrWhiteSpace(stateInput.Spd)) { state.Speed = decimal.Parse(stateInput.Spd); } if (!String.IsNullOrWhiteSpace(stateInput.Hdn)) { state.Heading = decimal.Parse(stateInput.Hdn); } state.State = (int)stateInput.Typ; state.Timestamp = stateInput.Tms ?? DateTime.UtcNow; state.Note = stateInput.Not; if (state.Latitude.HasValue && state.Longitude.HasValue) { state.GeoLocationData = string.Format("{0},{1}", state.Latitude.Value, state.Longitude.Value); } if (stateInput.Rto > 0) { state.DestinationId = stateInput.Rto; } var savedState = _unitsService.SetUnitState(state, DepartmentId); if (stateInput.Roles != null && stateInput.Roles.Count > 0) { var unitRoles = _unitsService.GetRolesForUnit(savedState.UnitId); var roles = new List <UnitStateRole>(); foreach (var role in stateInput.Roles) { if (!string.IsNullOrWhiteSpace(role.Uid)) { var unitRole = new UnitStateRole(); unitRole.UnitStateId = savedState.UnitStateId; unitRole.UserId = role.Uid;; unitRole.UnitStateRoleId = role.Rid; if (String.IsNullOrWhiteSpace(role.Nme)) { var savedRole = unitRoles.FirstOrDefault(x => x.UnitRoleId == unitRole.UnitStateRoleId); if (savedRole != null) { unitRole.Role = savedRole.Name; } } else { unitRole.Role = role.Nme; } unitRole.Id = 0; unitRole.UnitStateRoleId = 0; roles.Add(unitRole); //_unitsService.AddUnitStateRoleForEvent(savedState.UnitStateId, role.Uid, role.Rid, savedState.Unit.Name, savedState.Timestamp); } } _unitsService.AddAllUnitStateRoles(roles); } OutboundEventProvider.UnitStatusTopicHandler handler = new OutboundEventProvider.UnitStatusTopicHandler(); handler.Handle(new UnitStatusEvent() { DepartmentId = DepartmentId, Status = savedState }); if (savedState.UnitStateId > 0) { return(Request.CreateResponse(HttpStatusCode.Created)); } } catch (Exception ex) { Logging.LogException(ex); throw HttpStatusCode.InternalServerError.AsException(); } } throw HttpStatusCode.BadRequest.AsException(); }
public HttpResponseMessage SetUnitLocation(UnitLocationInput locationInput) { var unit = _unitsService.GetUnitById(locationInput.Uid); if (unit == null) { throw HttpStatusCode.NotFound.AsException(); } if (unit.DepartmentId != DepartmentId) { throw HttpStatusCode.Unauthorized.AsException(); } if (this.ModelState.IsValid) { try { CqrsEvent locationEvent = new CqrsEvent(); UnitLocation location = new UnitLocation(); location.UnitId = locationInput.Uid; if (locationInput.Tms.HasValue) { location.Timestamp = locationInput.Tms.Value; } else { location.Timestamp = DateTime.UtcNow; } if (!String.IsNullOrWhiteSpace(locationInput.Lat) && locationInput.Lat != "NaN" && !String.IsNullOrWhiteSpace(locationInput.Lon) && locationInput.Lon != "NaN") { location.Latitude = decimal.Parse(locationInput.Lat); location.Longitude = decimal.Parse(locationInput.Lon); if (!String.IsNullOrWhiteSpace(locationInput.Acc) && locationInput.Acc != "NaN") { location.Accuracy = decimal.Parse(locationInput.Acc); } if (!String.IsNullOrWhiteSpace(locationInput.Alt) && locationInput.Alt != "NaN") { location.Altitude = decimal.Parse(locationInput.Alt); } if (!String.IsNullOrWhiteSpace(locationInput.Alc) && locationInput.Alc != "NaN") { location.AltitudeAccuracy = decimal.Parse(locationInput.Alc); } if (!String.IsNullOrWhiteSpace(locationInput.Spd) && locationInput.Spd != "NaN") { location.Speed = decimal.Parse(locationInput.Spd); } if (!String.IsNullOrWhiteSpace(locationInput.Hdn) && locationInput.Hdn != "NaN") { location.Heading = decimal.Parse(locationInput.Hdn); } locationEvent.Type = (int)CqrsEventTypes.UnitLocation; locationEvent.Data = ObjectSerialization.Serialize(location); _cqrsProvider.EnqueueCqrsEvent(locationEvent); return(Request.CreateResponse(HttpStatusCode.Created)); } else { return(Request.CreateResponse(HttpStatusCode.NotModified)); } } catch (Exception ex) { Logging.LogException(ex); throw HttpStatusCode.InternalServerError.AsException(); } } throw HttpStatusCode.BadRequest.AsException(); }
public int GetDepartmentIdForType(NotificationItem ni) { switch ((EventTypes)ni.Type) { case EventTypes.PersonnelStaffingChanged: var state = _userStateService.GetUserStateById(ni.StateId); if (state != null) { var department = _departmentsService.GetDepartmentByUserId(state.UserId, true); if (department != null) { return(department.DepartmentId); } else { return(0); } } else { return(0); } case EventTypes.PersonnelStatusChanged: var status = _actionLogsService.GetActionlogById(ni.StateId); if (status != null) { var department = _departmentsService.GetDepartmentByUserId(status.UserId, true); if (department != null) { return(department.DepartmentId); } else { return(0); } } else { return(0); } case EventTypes.CalendarEventAdded: var cal = _calendarService.GetCalendarItemById(ni.ItemId); if (cal != null) { return(cal.DepartmentId); } else { return(0); } case EventTypes.CalendarEventUpcoming: var calUp = _calendarService.GetCalendarItemById(ni.ItemId); if (calUp != null) { return(calUp.DepartmentId); } else { return(0); } case EventTypes.CalendarEventUpdated: var calUpdate = _calendarService.GetCalendarItemById(ni.ItemId); if (calUpdate != null) { return(calUpdate.DepartmentId); } else { return(0); } case EventTypes.DocumentAdded: var docAdded = _documentsService.GetDocumentById(ni.ItemId); if (docAdded != null) { return(docAdded.DepartmentId); } else { return(0); } case EventTypes.LogAdded: var logAdded = _workLogsService.GetWorkLogById(ni.ItemId); if (logAdded != null) { return(logAdded.DepartmentId); } else { return(0); } case EventTypes.NoteAdded: var noteAdded = _notesService.GetNoteById(ni.ItemId); if (noteAdded != null) { return(noteAdded.DepartmentId); } else { return(0); } case EventTypes.ShiftCreated: var shiftCreated = _shiftsService.GetShiftById(ni.ItemId); if (shiftCreated != null) { return(shiftCreated.DepartmentId); } else { return(0); } case EventTypes.ShiftDaysAdded: var shiftDaysAdded = _shiftsService.GetShiftById(ni.ItemId); if (shiftDaysAdded != null) { return(shiftDaysAdded.DepartmentId); } else { return(0); } case EventTypes.ShiftUpdated: var shiftUpdated = _shiftsService.GetShiftById(ni.ItemId); if (shiftUpdated != null) { return(shiftUpdated.DepartmentId); } else { return(0); } case EventTypes.UnitAdded: var unitAdded = _unitsService.GetUnitById(ni.UnitId); if (unitAdded != null) { return(unitAdded.DepartmentId); } else { return(0); } case EventTypes.UnitStatusChanged: var unitStatusChanged = _unitsService.GetUnitStateById(ni.StateId); if (unitStatusChanged != null) { var unit = _unitsService.GetUnitById(unitStatusChanged.UnitId); if (unit != null) { return(unit.DepartmentId); } else { return(0); } } else { return(0); } } return(0); }
public static void ProcessCallQueueItem(CallQueueItem cqi) { try { if (cqi != null && cqi.Call != null && cqi.Call.HasAnyDispatches()) { if (_communicationService == null) { _communicationService = Bootstrapper.GetKernel().Resolve <ICommunicationService>(); } if (_callsService == null) { _callsService = Bootstrapper.GetKernel().Resolve <ICallsService>(); } List <int> groupIds = new List <int>(); /* Trying to see if I can eek out a little perf here now that profiles are in Redis. Previously the * the parallel operation would cause EF errors. This shouldn't be the case now because profiles are * cached and GetProfileForUser operations will hit that first. */ if (cqi.Profiles == null || !cqi.Profiles.Any()) { if (_userProfilesService == null) { _userProfilesService = Bootstrapper.GetKernel().Resolve <IUserProfileService>(); } cqi.Profiles = _userProfilesService.GetAllProfilesForDepartment(cqi.Call.DepartmentId).Select(x => x.Value).ToList(); } if (cqi.CallDispatchAttachmentId > 0) { //var callsService = Bootstrapper.GetKernel().Resolve<ICallsService>(); cqi.Call.ShortenedAudioUrl = _callsService.GetShortenedAudioUrl(cqi.Call.CallId, cqi.CallDispatchAttachmentId); } cqi.Call.ShortenedCallUrl = _callsService.GetShortenedCallLinkUrl(cqi.Call.CallId); try { cqi.Call.CallPriority = _callsService.GetCallPrioritesById(cqi.Call.DepartmentId, cqi.Call.Priority, false); } catch { /* Doesn't matter */ } var dispatchedUsers = new HashSet <string>(); // Dispatch Personnel if (cqi.Call.Dispatches != null && cqi.Call.Dispatches.Any()) { Parallel.ForEach(cqi.Call.Dispatches, d => { dispatchedUsers.Add(d.UserId); try { var profile = cqi.Profiles.FirstOrDefault(x => x.UserId == d.UserId); if (profile != null) { _communicationService.SendCall(cqi.Call, d, cqi.DepartmentTextNumber, cqi.Call.DepartmentId, profile, cqi.Address); } } catch (SocketException sex) { } }); } if (_departmentGroupsService == null) { _departmentGroupsService = Bootstrapper.GetKernel().Resolve <IDepartmentGroupsService>(); } // Dispatch Groups if (cqi.Call.GroupDispatches != null && cqi.Call.GroupDispatches.Any()) { foreach (var d in cqi.Call.GroupDispatches) { if (!groupIds.Contains(d.DepartmentGroupId)) { groupIds.Add(d.DepartmentGroupId); } var members = _departmentGroupsService.GetAllMembersForGroup(d.DepartmentGroupId); foreach (var member in members) { if (!dispatchedUsers.Contains(member.UserId)) { dispatchedUsers.Add(member.UserId); try { var profile = cqi.Profiles.FirstOrDefault(x => x.UserId == member.UserId); _communicationService.SendCall(cqi.Call, new CallDispatch() { UserId = member.UserId }, cqi.DepartmentTextNumber, cqi.Call.DepartmentId, profile, cqi.Address); } catch (SocketException sex) { } catch (Exception ex) { Logging.LogException(ex); } } } } } // Dispatch Units if (cqi.Call.UnitDispatches != null && cqi.Call.UnitDispatches.Any()) { if (_unitsService == null) { _unitsService = Bootstrapper.GetKernel().Resolve <IUnitsService>(); } foreach (var d in cqi.Call.UnitDispatches) { var unit = _unitsService.GetUnitById(d.UnitId); if (unit != null && unit.StationGroupId.HasValue) { if (!groupIds.Contains(unit.StationGroupId.Value)) { groupIds.Add(unit.StationGroupId.Value); } } _communicationService.SendUnitCall(cqi.Call, d, cqi.DepartmentTextNumber, cqi.Address); var unitAssignedMembers = _unitsService.GetCurrentRolesForUnit(d.UnitId); if (unitAssignedMembers != null && unitAssignedMembers.Count() > 0) { foreach (var member in unitAssignedMembers) { if (!dispatchedUsers.Contains(member.UserId)) { dispatchedUsers.Add(member.UserId); try { var profile = cqi.Profiles.FirstOrDefault(x => x.UserId == member.UserId); _communicationService.SendCall(cqi.Call, new CallDispatch() { UserId = member.UserId }, cqi.DepartmentTextNumber, cqi.Call.DepartmentId, profile, cqi.Address); } catch (SocketException sex) { } catch (Exception ex) { Logging.LogException(ex); } } } } else { if (unit.StationGroupId.HasValue) { var members = _departmentGroupsService.GetAllMembersForGroup(unit.StationGroupId.Value); foreach (var member in members) { if (!dispatchedUsers.Contains(member.UserId)) { dispatchedUsers.Add(member.UserId); try { var profile = cqi.Profiles.FirstOrDefault(x => x.UserId == member.UserId); _communicationService.SendCall(cqi.Call, new CallDispatch() { UserId = member.UserId }, cqi.DepartmentTextNumber, cqi.Call.DepartmentId, profile, cqi.Address); } catch (SocketException sex) { } catch (Exception ex) { Logging.LogException(ex); } } } } } } } // Dispatch Roles if (cqi.Call.RoleDispatches != null && cqi.Call.RoleDispatches.Any()) { if (_rolesService == null) { _rolesService = Bootstrapper.GetKernel().Resolve <IPersonnelRolesService>(); } foreach (var d in cqi.Call.RoleDispatches) { var members = _rolesService.GetAllMembersOfRole(d.RoleId); foreach (var member in members) { if (!dispatchedUsers.Contains(member.UserId)) { dispatchedUsers.Add(member.UserId); try { var profile = cqi.Profiles.FirstOrDefault(x => x.UserId == member.UserId); _communicationService.SendCall(cqi.Call, new CallDispatch() { UserId = member.UserId }, cqi.DepartmentTextNumber, cqi.Call.DepartmentId, profile, cqi.Address); } catch (SocketException sex) { } catch (Exception ex) { Logging.LogException(ex); } } } } } // Send Call Print to Printer if (_printerProvider == null) { _printerProvider = Bootstrapper.GetKernel().Resolve <IPrinterProvider>(); } Dictionary <int, DepartmentGroup> fetchedGroups = new Dictionary <int, DepartmentGroup>(); if (cqi.Call.Dispatches != null && cqi.Call.Dispatches.Any()) { foreach (var d in cqi.Call.Dispatches) { var group = _departmentGroupsService.GetGroupForUser(d.UserId, cqi.Call.DepartmentId); if (group != null) { if (!groupIds.Contains(group.DepartmentGroupId)) { groupIds.Add(group.DepartmentGroupId); } if (!fetchedGroups.ContainsKey(group.DepartmentGroupId)) { fetchedGroups.Add(group.DepartmentGroupId, group); } } } } foreach (var groupId in groupIds) { try { DepartmentGroup group = null; if (fetchedGroups.ContainsKey(groupId)) { group = fetchedGroups[groupId]; } else { group = _departmentGroupsService.GetGroupById(groupId); } if (!String.IsNullOrWhiteSpace(group.PrinterData) && group.DispatchToPrinter) { var printerData = JsonConvert.DeserializeObject <DepartmentGroupPrinter>(group.PrinterData); var apiKey = SymmetricEncryption.Decrypt(printerData.ApiKey, Config.SystemBehaviorConfig.ExternalLinkUrlParamPassphrase); var callUrl = _callsService.GetShortenedCallPdfUrl(cqi.Call.CallId, true, groupId); var printJob = _printerProvider.SubmitPrintJob(apiKey, printerData.PrinterId, "CallPrint", callUrl); } } catch (Exception ex) { Logging.LogException(ex); } } } } finally { _communicationService = null; } }
/// <summary> /// Повертає екземпляр одиниці виміру за ідентифікатором /// </summary> /// <param name="id">Ідентифікатор одиниці виміру</param> /// <returns>Екземпляр категорії</returns> public UnitsDtoModel GetUnitById(int id) => unitsService.GetUnitById(id);