Beispiel #1
0
        // GET: User
        public async Task<ActionResult> Projects(string email)
        {
            var userRepository = new UserRepository();
            var user = await userRepository.FindByEmail(email);

            var projectRepository = new ProjectRepository();
            var projects = await projectRepository.FindProjectsForUser(user.Id);

            return JsonNet(projects);
        }
Beispiel #2
0
        public async Task<ActionResult> Leaves(string email, int year, int month)
        {
            var userRepository = new UserRepository();
            var user = await userRepository.FindByEmail(email);

            var scheduleRepository = new ScheduleRepository();
            var leaves = await scheduleRepository.FindLeavesByMonth(user.Id, year, month);

            return JsonNet(leaves);
        }
Beispiel #3
0
        public async Task<ActionResult> Apply(ApplyLeaveInputModel input)
        {
            if(input.Dates == null || !input.Dates.Any())
            {
                return Fail("No dates specified");
            }

            var dates = new List<DateTime>();
            string dateParseError = string.Empty;
            input.Dates.ForEach(d =>
            {
                DateTime parsedDate;
                if (!DateTime.TryParseExact(d, "dd/MM/yyyy", _cultureInfo, DateTimeStyles.AssumeUniversal, out parsedDate))
                {
                    dateParseError = "Invalid date format, expected date format is dd/MM/yyyy";
                }
                else
                {
                    dates.Add(parsedDate);
                }
            });

            if(!string.IsNullOrWhiteSpace(dateParseError))
            {
                return Fail(dateParseError);
            }

            if(dates.Any(d => d < DateTime.Now))
            {
                return Fail("One or more dates are in the past and are invalid");
            }

            if(string.IsNullOrWhiteSpace(input.Email))
            {
                return Fail("Email is required");
            }

            var userRepository = new UserRepository();
            var user = await userRepository.FindByEmail(input.Email);
            if(user == null)
            {
                return Fail("No user with that email found");
            }

            
            var scheduleRepository = new ScheduleRepository();
            string duplicateError = string.Empty;

            foreach (var d in dates)
            {
                var existingLeaves = await scheduleRepository.FindLeavesByMonth(user.Id, d.Year, d.Month);
                if (existingLeaves != null && existingLeaves.Any(l =>
                    l.Date.Value.Year == d.Year &&
                    l.Date.Value.Month == d.Month &&
                    l.Date.Value.Day == d.Day))
                {
                    duplicateError = "One or more dates are already applied before";
                    break;
                }
            }

            if(!string.IsNullOrWhiteSpace(duplicateError))
            {
                return Fail(duplicateError);
            }

            var leaveTypeRepository = new LeaveTypeRepository();
            var leaveType = await leaveTypeRepository.FindLeaveTypeByCodeName(input.LeaveCodeName);
            if(string.IsNullOrWhiteSpace(input.LeaveCodeName) || leaveType == null)
            {
                return Fail("Invalid leave code name");
            }

            if(string.IsNullOrWhiteSpace(input.Reason))
            {
                return Fail("Reason is required");
            }

            try
            {
                await scheduleRepository.ApplyLeave(user.Id, user.Profile.Name, dates, input.LeaveCodeName, input.Reason);
            }
            catch(Exception ex)
            {
                return Fail("Server error: " + ex.ToString());
            }

            return JsonNet(new {
                Success = true
            });
        }
Beispiel #4
0
        // GET: Overview
        public async Task<ActionResult> Index(string email)
        {
            var userRepository = new UserRepository();
            var user = await userRepository.FindByEmail(email);

            var scheduleRepository = new ScheduleRepository();
            var leaves = await scheduleRepository.FindAllLeaves(user.Id);

            var leaveTypeRepository = new LeaveTypeRepository();
            var allLeaveTypes = await leaveTypeRepository.FindAll();
            var leaveTypesLookUp = allLeaveTypes.ToDictionary(l => l.LeaveCodeName);

            var model = new OverviewModel
            {
                Leaves = leaves,
                User = user
            };

            model.LeaveEntitlement.Add(GetLeaveEntitlement(user, leaves, leaveTypesLookUp["annual_leave"], user.LeaveQuotas.AnnualLeave));
            model.LeaveEntitlement.Add(GetLeaveEntitlement(user, leaves, leaveTypesLookUp["sick_leave"], user.LeaveQuotas.SickLeave));
            model.LeaveEntitlement.Add(GetLeaveEntitlement(user, leaves, leaveTypesLookUp["training_leave"], user.LeaveQuotas.TrainingLeave));
            model.LeaveEntitlement.Add(GetLeaveEntitlement(user, leaves, leaveTypesLookUp["compassionate_leave"], user.LeaveQuotas.CompassionateLeave));
            model.LeaveEntitlement.Add(GetLeaveEntitlement(user, leaves, leaveTypesLookUp["maternity_leave"], user.LeaveQuotas.MaternityLeave));
            model.LeaveEntitlement.Add(GetLeaveEntitlement(user, leaves, leaveTypesLookUp["paternity_leave"], user.LeaveQuotas.PaternityLeave));
            model.LeaveEntitlement.Add(GetLeaveEntitlement(user, leaves, leaveTypesLookUp["child_care_leave"], user.LeaveQuotas.ChildCareLeave));
            model.LeaveEntitlement.Add(GetLeaveEntitlement(user, leaves, leaveTypesLookUp["extended_child_care_leave"], user.LeaveQuotas.ExtendedChildCareLeave));
            model.LeaveEntitlement.Add(GetLeaveEntitlement(user, leaves, leaveTypesLookUp["ns_leave"], user.LeaveQuotas.NSLeave));

            var upcomingLeaves = leaves.Where(l => l.Date > DateTime.Now).OrderBy(l => l.Date).ToList();
            //var upcomingLeaves = leaves.OrderBy(l => l.Date).ToList();

            var groupedUpcomingLeaves = upcomingLeaves.GroupBy(l => l.LeaveType);
            foreach (var group in groupedUpcomingLeaves)
            {
                var groupLeaves = group.ToList();
                var currentUpcoming = new UpcomingLeaves { Type = group.Key };
                DateTime currentDate = groupLeaves[0].Date.Value;
                currentUpcoming.From = FormatDate(currentDate);
                currentUpcoming.Reason = groupLeaves[0].Reason;

                if (groupLeaves.Count == 1)
                {
                    currentUpcoming.To = FormatDate(currentDate);
                    model.UpcomingLeaves.Add(currentUpcoming);
                }
                else
                {
                    int nextLeaveIndex = 1;
                    string reason = groupLeaves[nextLeaveIndex].Reason;
                    DateTime nextLeave = groupLeaves[nextLeaveIndex++].Date.Value;
                    
                    while (nextLeaveIndex <= groupLeaves.Count)
                    {
                        while (nextLeaveIndex <= groupLeaves.Count &&
                            (currentDate.AddDays(1) == nextLeave || 
                            currentDate.AddDays(1).DayOfWeek == DayOfWeek.Saturday || 
                            currentDate.AddDays(1).DayOfWeek == DayOfWeek.Sunday))
                        {
                            if (nextLeaveIndex < groupLeaves.Count && 
                                currentDate.AddDays(1) == nextLeave)
                            {
                                nextLeave = groupLeaves[nextLeaveIndex++].Date.Value;
                            }
                            currentDate = currentDate.AddDays(1);
                        }

                        if (currentDate != nextLeave)
                        {
                            currentUpcoming.To = FormatDate(currentDate);
                            model.UpcomingLeaves.Add(currentUpcoming);

                            currentDate = nextLeave;
                            currentUpcoming = new UpcomingLeaves
                            {
                                Type = group.Key,
                                From = FormatDate(currentDate),
                                Reason = reason
                            };
                        }

                        if (nextLeaveIndex < groupLeaves.Count)
                        {
                            reason = groupLeaves[nextLeaveIndex].Reason;
                            nextLeave = groupLeaves[nextLeaveIndex].Date.Value;
                            
                        }

                        nextLeaveIndex++;
                    }


                    if (currentUpcoming != null)
                    {
                        currentUpcoming.To = FormatDate(nextLeave);
                        model.UpcomingLeaves.Add(currentUpcoming);
                    }

                }
            }

            //upcomingLeaves.ForEach(l => model.UpcomingLeaves.Add(new UpcomingLeaves
            //    {
            //        Type = l.LeaveType,
            //        From = FormatDate(l.Date.Value),
            //        To = FormatDate(l.Date.Value)
            //    }));

            return JsonNet(model);
        }