public new IActionResult Post([FromBody] Substitute substitute) { if (CurrentUser.IsAdmin || CurrentUser.Id.Equals(substitute.LeaderId)) { substitute.StartDateTimestamp = _sub.GetStartOfDayTimestamp(substitute.StartDateTimestamp); if (substitute.EndDateTimestamp != UnlimitedPeriod) { substitute.EndDateTimestamp = _sub.GetEndOfDayTimestamp(substitute.EndDateTimestamp); } // Return BadRequest if a sub or personal approver already exists in the time period. Otherwise create the sub or approver. var isAllowed = _sub.CheckIfNewSubIsAllowed(substitute); var result = isAllowed ? base.Post(substitute) : BadRequest(); if (isAllowed) { _sub.UpdateReportsAffectedBySubstitute(substitute); } return(result); } return(StatusCode(StatusCodes.Status403Forbidden)); }
/// <summary> /// Updates ResponsibleLeader on all reports that had a substitute which expired yesterday or became active today. /// </summary> public void UpdateLeadersOnExpiredOrActivatedSubstitutes() { var yesterdayTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1).AddDays(1))).TotalSeconds; var currentTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; var endOfDayStamp = _subService.GetEndOfDayTimestamp(yesterdayTimestamp); var startOfDayStamp = _subService.GetStartOfDayTimestamp(currentTimestamp); var affectedSubstitutes = _subRepo.AsQueryable().Where(s => (s.EndDateTimestamp == endOfDayStamp) || (s.StartDateTimestamp == startOfDayStamp)).ToList(); Console.WriteLine(affectedSubstitutes.Count() + " substitutes have expired or become active. Updating affected reports."); foreach (var sub in affectedSubstitutes) { _subService.UpdateReportsAffectedBySubstitute(sub); } _logger.Debug($"{this.GetType().Name}, UpdateLeadersOnExpiredOrActivatedSubstitutes() finished"); }
/// <summary> /// Updates ResponsibleLeader on all reports that had a substitute which expired yesterday or became active today. /// </summary> public void UpdateLeadersOnExpiredOrActivatedSubstitutes() { // TODO Find something more generic for updating drive and vacation reports. var yesterdayTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1).AddDays(1))).TotalSeconds; var currentTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; var endOfDayStamp = _subService.GetEndOfDayTimestamp(yesterdayTimestamp); var startOfDayStamp = _subService.GetStartOfDayTimestamp(currentTimestamp); var affectedSubstitutes = _subRepo.AsNoTracking().Where(s => (s.EndDateTimestamp == endOfDayStamp) || (s.StartDateTimestamp == startOfDayStamp)).ToList(); Console.WriteLine(affectedSubstitutes.Count() + " substitutes have expired or become active. Updating affected reports."); foreach (var sub in affectedSubstitutes) { _subService.UpdateReportsAffectedBySubstitute(sub); } }