Ejemplo n.º 1
0
        // GET: LeaveTypes
        public async Task<ActionResult> Index()
        {
            var leaveTypeRepository = new LeaveTypeRepository();
            var allLeaveTypes = await leaveTypeRepository.FindAll();

            return JsonNet(allLeaveTypes);
        }
Ejemplo n.º 2
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
            });
        }
Ejemplo n.º 3
0
        public async Task<ActionResult> LeaveDetails(string id, int year, int month, int day)
        {
            var projectRepository = new ProjectRepository();
            var memberIds = await projectRepository.FindMembersInProject(id);

            var scheduleRepository = new ScheduleRepository();
            var leaves = await scheduleRepository.FindLeavesByMonthForProjectMembers(memberIds, year, month);

            var leaveTypeRepository = new LeaveTypeRepository();
            var leaveTypes = await leaveTypeRepository.FindAll();

            var model = new LeaveDetailsModel();
            model.Date = new DateTime(year, month, day);
            model.Leaves = leaves.Where(l => l.Date.Value.Day == day)
                                 .Select(l => new LeaveModel
                                    {
                                        CreatedById = l.CreatedById,
                                        Date = l.Date.Value.ToString("dd MMMM yyyy"),
                                        Hours = l.Hours,
                                        Id = l.Id,
                                        IsApproved = l.IsApproved,
                                        IsHalfDay = l.IsHalfDay,
                                        IsOnMorning = l.IsOnMorning,
                                        LeaveType = l.LeaveType,
                                        Reason = l.Reason,
                                        Type = LookUpLeaveName(l.LeaveType, leaveTypes)
                                    })
                                    .ToList();

            var userRepository = new UserRepository();
            var users = await userRepository.FindByIds(memberIds);
            var userLookUp = users.ToDictionary(u => u.Id);
            model.Leaves.ForEach(l => {
                if(userLookUp.ContainsKey(l.CreatedById))
                {
                    var user = userLookUp[l.CreatedById];
                    l.CreatedByName = user.Profile.Name;
                    l.CreatedByProfileImage = user.GoogleProfile.ProfileImageUrl;
                }
            });

            var countryCodes = await userRepository.FindDistinctCountryCodeForMembers(memberIds);
            var holidayRepository = new HolidayRepository();
            var holidays = await holidayRepository.FindHolidaysForCountries(year, countryCodes);

            model.Holidays = holidays.Where(h => h.Date.Month == month && h.Date.Day == day)
                .Select(h => new HolidayModel
                {
                    Country = h.Country,
                    Name = h.Name,
                    Date = h.Date.ToString("dd MMMM yyyy")
                }).ToList();
            var usersByCountry = users.GroupBy(u => u.Profile.CountryCode);
            foreach (var byCountry in usersByCountry)
            {
                if(byCountry.Count() > 1)
                {
                    var holiday = model.Holidays.FirstOrDefault(h => h.Country == byCountry.Key);
                    if(holiday != null)
                    {
                        model.Holidays.AddRange(Enumerable.Repeat(holiday, byCountry.Count() - 1));
                    }
                }
            }

            return JsonNet(model);
        }
Ejemplo n.º 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);
        }