コード例 #1
0
        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);
        }