public virtual async Task <bool> AcceptChanges(string commonidentifier, string actionKey, string actionInstanceId, int registerEnvironment, [FromBody] string content) { if (string.IsNullOrEmpty(content)) { throw new ArgumentException("Content was null"); } var surveillanceAction = Di.GetInstance <ISurveillanceAction>(actionKey); if (!surveillanceAction.ValidJson(content)) { throw new ArgumentException("Content was not in the right format given actionkey: " + actionKey); } var latestResultDb = Di.GetInstance <IJsonStorage <LatestSurveillanceResult> >(); var latestResult = await LatestSurveillanceResult.GetLatestSurveillanceResult(actionKey, actionInstanceId, GetTeam(), latestResultDb); if (latestResult != null) { await latestResultDb.Delete(latestResult); } var claimDb = Di.GetInstance <ITableStorageDb <SurveilledItem> >(); var surveillance = await SurveilledItem.Get(actionKey, actionInstanceId, GetTeam(), claimDb); surveillance.ContentAsJson = content; surveillance.ClaimedWhen = DateTime.Now; await claimDb.InsertAsync(surveillance, true, true); return(true); }
public virtual async Task <bool> Off(string commonidentifier, string actionKey, string actionInstanceId, int registerEnvironment) { var claimDb = Di.GetInstance <ITableStorageDb <SurveilledItem> >(); var element = await SurveilledItem.Get(actionKey, actionInstanceId, GetTeam(), claimDb); if (element == null) { return(false); } claimDb.DeleteMany(new [] { element }); var latestResultDb = Di.GetInstance <IJsonStorage <LatestSurveillanceResult> >(); var latestResult = await LatestSurveillanceResult.GetLatestSurveillanceResult(actionKey, actionInstanceId, GetTeam(), latestResultDb); if (latestResult != null) { await latestResultDb.Delete(latestResult); } var team = GetTeam().Id; var anyOtherSurveillanceItems = TriggerAllSurveillanceItems <RegisterPersonModel> .GetAllSurveillanceItems(Di) .Any(item => !string.IsNullOrEmpty(item.CommonIdentifier) && item.CommonIdentifier == commonidentifier && item.TeamProjectInt == team); if (!anyOtherSurveillanceItems) { await _personIndex.RemoveFromPropertyList(commonidentifier, "Teams", team.ToString()); } return(true); }
private static async Task <Surveillance> GetSurveillance(Hit hit, ISurveillanceAction isurveillance, Team teamForLoggedInUser, ITableStorageDb <SurveilledItem> surveiItemDb, IJsonStorage <LatestSurveillanceResult> surveilResultDb) { var s = new Surveillance { ActionFriendlyName = isurveillance.GetFriendlyName(), ActionKey = isurveillance.GetKey(), UrlToToggle = GetRoute(hit.CommonIdentifier, isurveillance.GetKey(), hit.GetActionInstanceIdentifier(), hit.RegisterEnvironmentInt) }; s.TeamsThatSurveillThisInstance = (await SurveilledItem.GetAllForActionIdentifier(isurveillance.GetKey(), hit.GetActionInstanceIdentifier(), surveiItemDb)).ToList(); if (s.TeamsThatSurveillThisInstance.Any(team => team.TeamProjectInt == teamForLoggedInUser.Id)) { s.OriginalContentAsJson = s.TeamsThatSurveillThisInstance.First(team => team.TeamProjectInt == teamForLoggedInUser.Id).ContentAsJson; s.LatestSurveillanceResultForMyTeam = await LatestSurveillanceResult.GetLatestSurveillanceResult(s.ActionKey, hit.GetActionInstanceIdentifier(), teamForLoggedInUser, surveilResultDb); s.IsChecked = true; } else { s.IsChecked = false; } return(s); }
public static async Task <bool> InsertIntoWorkQ(IQueue q, SurveilledItem surveillanceItem) { return(await q.SendAsync(new SimpleQueueItem(QueueItemType.HodorTriggerOneSurveillanceItem, surveillanceItem.Id()) { Content = surveillanceItem.GetPartionkeyAndRowkey() })); }
private ISurveillanceAction TryGetSurveillanceAction(SurveilledItem s, ILog logger) { try { return(_di.GetInstance <ISurveillanceAction>(s.ActionKey)); } catch (Exception e) { logger.Error("Fail to resolve ISurveillanceAction for item with actionKey " + s.ActionKey, e); return(null); } }
public virtual async Task <IEnumerable <LatestSurveillanceResult> > GetLatestResults() { var latestResults = (await LatestSurveillanceResult.GetLatestSurveillanceResult(GetTeam(), Di.GetInstance <IJsonStorage <LatestSurveillanceResult> >())).ToList(); var claimDb = Di.GetInstance <ITableStorageDb <SurveilledItem> >(); foreach (var possibleSurveillanceAction in Di.GetAllInstancesOf <ISurveillanceAction>()) { foreach (var surveilanceItem in await SurveilledItem.GetAllForteam(possibleSurveillanceAction.GetKey(), GetTeam(), claimDb)) { var existing = latestResults.FirstOrDefault(result => result.ActionInstanceIdentifier == surveilanceItem.ActionInstanceIdentifier && result.ActionKey == surveilanceItem.ActionKey); if (existing != null) { if (string.IsNullOrEmpty(existing.CommonIdentifier)) { existing.CommonIdentifier = surveilanceItem.CommonIdentifier; } existing.Taken = true; continue; } var lsr = new LatestSurveillanceResult { ActionKey = surveilanceItem.ActionKey, ActionInstanceIdentifier = surveilanceItem.ActionInstanceIdentifier, CommonIdentifier = surveilanceItem.CommonIdentifier ?? surveilanceItem.ActionInstanceIdentifier, RegisterEnvironmentInt = surveilanceItem.RegisterEnvironmentInt, RegisteredBy = surveilanceItem.RegisteredByFriendlyName, TeamProjectInt = surveilanceItem.TeamProjectInt, Success = true, Taken = true }; latestResults.Add(lsr); } } return(latestResults.Where(x => x.Taken)); }
public async Task <bool> DoAsync(QueueItem msg, ILog logger) { var simpleQMsg = JsonConvert.DeserializeObject <Tuple <string, string> >(msg.GetContentAsJson()); string partitionKey, rowKey; SurveilledItem.ParsePartitionkeyAndRowkey(simpleQMsg, out partitionKey, out rowKey); var suveillanceItem = await _di.GetInstance <ITableStorageDb <SurveilledItem> >().GetAsync(partitionKey, rowKey); if (suveillanceItem == null) { logger.Error($"Tried to find surveillanceitem with partitionkey={partitionKey} and rowkey={rowKey}, but couldnt find it"); return(true); } var surveillanceAction = TryGetSurveillanceAction(suveillanceItem, logger); if (surveillanceAction == null) { return(true); } Impersonate(suveillanceItem.RegisteredByUsername, logger); var surveillanceResult = await surveillanceAction.Check(suveillanceItem, _di); if (surveillanceResult == null) { return(true); } //fixing commonidentifier on old objects. can remove this code in the future if (string.IsNullOrEmpty(suveillanceItem.CommonIdentifier) && !string.IsNullOrEmpty(surveillanceResult.CommonIdentifier)) { try { suveillanceItem.CommonIdentifier = surveillanceResult.CommonIdentifier; await _di.GetInstance <ITableStorageDb <SurveilledItem> >().InsertAsync(suveillanceItem, true, true); } catch (Exception e) { logger.Warn("Fail when linking surveillance to suveillanceItem. suveillanceItem: " + JsonConvert.SerializeObject(surveillanceResult), e); } } var msgResult = surveillanceResult.Success ? "Surveillanceresult was successfull. Found no deviation" : "Surveillanceresult failed. Text: " + surveillanceResult.ErrorText + Environment.NewLine + "Compared object: " + suveillanceItem.ContentAsJson + " with " + surveillanceResult.ContentAsJson; logger.Info(msgResult); var latestResult = new LatestSurveillanceResult() { ActionKey = surveillanceResult.ActionKey, ActionInstanceIdentifier = surveillanceResult.ActionInstanceIdentifier, Id = surveillanceResult.Id, ContentAsJson = surveillanceResult.ContentAsJson, TeamProjectInt = surveillanceResult.TeamProjectInt, CheckedAt = surveillanceResult.CheckedAt, ErrorText = surveillanceResult.ErrorText, RegisterEnvironmentInt = surveillanceResult.RegisterEnvironmentInt, RegisteredBy = surveillanceResult.RegisteredBy, Success = surveillanceResult.Success }; if (PostResults) { await _di.GetInstance <IJsonStorage <SurveillanceResult> >().Post(surveillanceResult); return(await _di.GetInstance <IJsonStorage <LatestSurveillanceResult> >().Put(latestResult)); } return(true); }