public async Task <IActionResult> AddEvent(Event newEvent) { string userID = User.FindFirst(ClaimTypes.NameIdentifier).Value; WorkingDay workingDay = await workingDayRepository.GetCurrentWorkingDay(userID); if (workingDay == null) { workingDay = new WorkingDay() { UserID = userID }; await workingDayRepository.Add(workingDay); } UserEvent lastEvent = workingDay.Events?.OrderByDescending(e => e.Time).FirstOrDefault(); if (lastEvent != null && DateTime.Now.Minute == lastEvent.Time.Minute && DateTime.Now.Subtract(lastEvent.Time).TotalSeconds < 60) { TempData["StatusMessage"] = "Multiple events can't be recorded in the same minute."; } else { await userEventRepository.AddIfValid(userID, new UserEvent { WorkingDayID = workingDay.ID, Event = newEvent }); await workingDayRepository.UpdateTimes(workingDay); } return(RedirectToAction("Index", "Home")); }
public async Task RecordDetectedOffense(ApplicationUser user, WorkingDay workingDay, OffenseDegree degree) { WorkingDay userWorkingDay; if (workingDay == null) // user was absent { // Create new empty working day object for the day that the user miss userWorkingDay = new WorkingDay(user.Id, DateTime.Today.AddDays(-1)); await workingDayRepository.Add(userWorkingDay); } else { userWorkingDay = workingDay; } Offense offense; if (await HasAllowance(user.Id, userWorkingDay, degree)) { offense = new Offense(userWorkingDay.ID, 0, degree) { HasAllowance = true }; } else { // Get how many times this offense occurred within // the last 90 days and increment it by one (for absence, count the unexcused ones only) int occurrence; if (degree == OffenseDegree.Absence) { occurrence = await offenseRepository.CountAbsenceInLast90Days(user.Id, userWorkingDay) + 1; } else { occurrence = await offenseRepository.CountOffensesInLast90Days(user.Id, userWorkingDay, degree) + 1; } // If the occurence is not within the Occurrence enum, assign it to the last enum (FifthOrMore) if (!Enum.IsDefined(typeof(Occurrence), occurrence)) { Array occurrencesArray = Enum.GetValues(typeof(Occurrence)); // Get the last object in the occurence enum & assign it to occurrence occurrence = (int)occurrencesArray.GetValue(occurrencesArray.Length - 1); } // Get penalty percent per day desired for this offense int penaltyPercent = ruleRepository.GetPenaltyPercent(degree, (Occurrence)occurrence); // Create new offence object & save it offense = new Offense(userWorkingDay.ID, penaltyPercent, degree); // Check if it needs approval if (degree == OffenseDegree.Absence || degree == OffenseDegree.LeaveEarly) { offense.NeedApproval = true; } } await offenseRepository.Add(offense); }