Ejemplo n.º 1
0
        public async Task <ActionResult <PersonViewModel> > ModAdminAsync(Guid oid, int modifier)
        {
            if (oid == Guid.Empty)
            {
                return(BadRequest("No valid id."));
            }
            try
            {
                //check if user exists
                TaskResult <User> result = await personService.GetUserAsync(oid);

                if (!result.Succeeded)
                {
                    return(UnprocessableEntity(new ErrorViewModel {
                        Type = Type.Error, Message = result.Message
                    }));
                }

                User user = new User
                {
                    AdditionalData = new Dictionary <string, object>(),
                    Id             = oid.ToString()
                };

                if (modifier == 4)
                {
                    //check if user is also a manager
                    TaskResult <List <Manager> > userManagesOtherProjects =
                        await personService.UserManagesOtherProjectsAsync(oid);

                    if (userManagesOtherProjects?.Data != null && userManagesOtherProjects.Data.Count > 0)
                    {
                        modifier = 2;
                    }
                }

                user.AdditionalData.Add(Extensions.GetInstance(b2CExtentionApplicationId).UserRoleExtension, modifier);
                result = await personService.UpdatePersonAsync(user);

                if (!result.Succeeded)
                {
                    return(UnprocessableEntity(new ErrorViewModel {
                        Type = Type.Error, Message = result.Message
                    }));
                }

                PersonViewModel personVm =
                    PersonViewModel.CreateVmFromUser(result.Data, Extensions.GetInstance(b2CExtentionApplicationId));
                return(Ok(personVm));
            }
            catch (Exception ex)
            {
                string message = GetType().Name + "Error in " + nameof(ModAdminAsync);
                logger.LogError(ex, message);
                return(UnprocessableEntity(new ErrorViewModel {
                    Type = Type.Error, Message = message
                }));
            }
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <List <ManagerViewModel> > > GetProjectManagersAsync(Guid projectId)
        {
            if (projectId == Guid.Empty)
            {
                return(BadRequest("No valid id received"));
            }
            try
            {
                TaskListResult <Manager> result = await personService.GetManagersAsync(projectId);

                //get users from b2c and add tot DTO
                foreach (Manager manager in result.Data)
                {
                    User temp = (await personService.GetUserAsync(manager.PersonId)).Data;
                    if (temp == null)
                    {
                        continue;
                    }
                    PersonViewModel vm =
                        PersonViewModel.CreateVmFromUser(temp, Extensions.GetInstance(b2CExtentionApplicationId));
                    if (vm == null)
                    {
                        continue;
                    }

                    Person person = PersonViewModel.CreatePerson(vm);
                    manager.Person = person;
                }

                if (!result.Succeeded)
                {
                    return(UnprocessableEntity(new ErrorViewModel {
                        Type = Type.Error, Message = result.Message
                    }));
                }
                if (result.Data == null || result.Data.Count == 0)
                {
                    return(Ok(new List <ManagerViewModel>()));
                }
                List <ManagerViewModel> managerVmList = result.Data.Select(ManagerViewModel.CreateVm).ToList();
                return(Ok(managerVmList));
            }
            catch (Exception ex)
            {
                string message = GetType().Name + "Error in " + nameof(GetProjectManagersAsync);
                logger.LogError(ex, message);
                return(UnprocessableEntity(new ErrorViewModel {
                    Type = Type.Error, Message = message
                }));
            }
        }
Ejemplo n.º 3
0
        public async Task <ActionResult <PersonViewModel> > GetPersonAsync(string email, string firstName, string lastName,
                                                                           string userRole,
                                                                           string city, int offset = 0, int pageSize = 20)
        {
            PersonFilter filter = new PersonFilter(offset, pageSize)
            {
                Email     = email,
                FirstName = firstName,
                LastName  = lastName,
                UserRole  = userRole,
                City      = city
            };

            List <PersonViewModel> personViewModels = new List <PersonViewModel>();

            try
            {
                TaskListResult <User> result = await personService.GetB2CMembersAsync(filter);

                if (!result.Succeeded)
                {
                    return(UnprocessableEntity(new ErrorViewModel {
                        Type = Type.Error, Message = result.Message
                    }));
                }
                if (result.Data == null)
                {
                    return(Ok(new List <PersonViewModel>()));
                }

                foreach (User user in result.Data)
                {
                    personViewModels.Add(PersonViewModel.CreateVmFromUser(user,
                                                                          Extensions.GetInstance(b2CExtentionApplicationId)));
                    if (personViewModels.Count == pageSize)
                    {
                        break;
                    }
                }

                return(Ok(new SearchResultViewModel <PersonViewModel>(filter.TotalItemCount, personViewModels)));
            }
            catch (Exception ex)
            {
                string message = GetType().Name + "Error in " + nameof(GetPersonAsync);
                logger.LogError(ex, message);
                return(UnprocessableEntity(new ErrorViewModel {
                    Type = Type.Error, Message = message
                }));
            }
        }
Ejemplo n.º 4
0
        public async Task <ActionResult <PersonViewModel> > UpdateUserAsync([FromBody] PersonViewModel personViewModel)
        {
            if (personViewModel == null || personViewModel.Id == Guid.Empty)
            {
                return(BadRequest("Invalid User"));
            }
            try
            {
                string oid = IdentityHelper.GetOid(HttpContext.User.Identity as ClaimsIdentity);
                if (oid == null)
                {
                    return(BadRequest("Invalid User"));
                }

                //only the owner of a profile or a boardmember or a committeemember can update user data
                if (personViewModel.Id.ToString() != oid &&
                    !UserHasRole(UserRole.Boardmember, (ClaimsIdentity)HttpContext.User.Identity) &&
                    !UserHasRole(UserRole.Committeemember, (ClaimsIdentity)HttpContext.User.Identity))
                {
                    return(BadRequest("Invalid User"));
                }

                User user = PersonViewModel.CreateUser(personViewModel,
                                                       Extensions.GetInstance(b2CExtentionApplicationId));
                TaskResult <User> result = await personService.UpdatePersonAsync(user);

                if (!result.Succeeded)
                {
                    return(Unauthorized());
                }
                return(Ok(PersonViewModel.CreateVmFromUser(result.Data,
                                                           Extensions.GetInstance(b2CExtentionApplicationId))));
            }
            catch (Exception ex)
            {
                string message = GetType().Name + "Error in " + nameof(UpdateUserAsync);
                logger.LogError(ex, message);
                return(UnprocessableEntity(new ErrorViewModel {
                    Type = Type.Error, Message = message
                }));
            }
        }
Ejemplo n.º 5
0
        public async Task <ActionResult <List <PersonViewModel> > > GetParticipants(Guid projectId)
        {
            if (projectId == Guid.Empty)
            {
                return(BadRequest("No valid id"));
            }
            try
            {
                TaskListResult <Participation> participationResult =
                    await participationService.GetParticipationsAsync(projectId);

                if (!participationResult.Succeeded)
                {
                    return(UnprocessableEntity(new ErrorViewModel
                    {
                        Type = Type.Error, Message = participationResult.Message
                    }));
                }

                List <PersonViewModel> persons = new List <PersonViewModel>();
                foreach (Participation participation in participationResult.Data)
                {
                    TaskResult <User> userResult = await personService.GetUserAsync(participation.PersonId);

                    if (userResult != null)
                    {
                        persons.Add(PersonViewModel.CreateVmFromUser(userResult.Data,
                                                                     Extensions.GetInstance(b2CExtentionApplicationId)));
                    }
                }

                return(Ok(persons));
            }
            catch (Exception ex)
            {
                string message = GetType().Name + "Error in " + nameof(GetParticipants);
                logger.LogError(ex, message);
                return(UnprocessableEntity(new ErrorViewModel {
                    Type = Type.Error, Message = message
                }));
            }
        }
Ejemplo n.º 6
0
        public async Task <ActionResult <PersonViewModel> > MakeManagerAsync(Guid projectId, Guid userId)
        {
            if (projectId == Guid.Empty || userId == Guid.Empty)
            {
                BadRequest("No valid Ids received.");
            }

            try
            {
                Project project = (await projectService.GetProjectDetailsAsync(projectId)).Data;
                if (project == null)
                {
                    return(BadRequest("Could not find project"));
                }
                User user = (await personService.GetUserAsync(userId)).Data;
                if (user == null)
                {
                    return(BadRequest("Could not find user"));
                }
                Person person = (await personService.GetPersonAsync(userId)).Data;
                if (person == null)
                {
                    return(BadRequest("Could not find person in DB"));
                }
                Manager manager = (await personService.GetManagerAsync(projectId, userId)).Data;
                if (manager != null)
                {
                    return(BadRequest("User already manages this project"));
                }

                PersonViewModel viewModel = PersonViewModel.CreateVmFromUserAndPerson(user, person,
                                                                                      Extensions.GetInstance(b2CExtentionApplicationId));
                if (viewModel == null)
                {
                    return(BadRequest("Unable to create manager"));
                }

                string oid = IdentityHelper.GetOid(HttpContext.User.Identity as ClaimsIdentity);
                manager = new Manager
                {
                    ProjectId  = project.Id,
                    Project    = project,
                    PersonId   = person.Id,
                    Person     = person,
                    LastEditBy = oid
                };

                TaskResult <Manager> result = await personService.MakeManagerAsync(manager);

                if (viewModel.UserRole != "Boardmember")
                {
                    await ModAdminAsync(userId, 2); //make user a manager in B2C
                }
                if (!result.Succeeded)
                {
                    return(UnprocessableEntity(new ErrorViewModel {
                        Type = Type.Error, Message = result.Message
                    }));
                }
                return(Ok(PersonViewModel.CreateVmFromUser(user, Extensions.GetInstance(b2CExtentionApplicationId))));
            }
            catch (Exception ex)
            {
                string message = GetType().Name + "Error in " + nameof(MakeManagerAsync);
                logger.LogError(ex, message);
                return(UnprocessableEntity(new ErrorViewModel {
                    Type = Type.Error, Message = message
                }));
            }
        }
Ejemplo n.º 7
0
        public async Task <ActionResult <List <ShiftViewModel> > > ExportDataAsync(Guid projectId)
        {
            if (projectId == Guid.Empty)
            {
                return(BadRequest("No valid id."));
            }
            try
            {
                TaskListResult <Shift> result = await shiftService.ExportDataAsync(projectId);

                if (!result.Succeeded)
                {
                    return(UnprocessableEntity(new ErrorViewModel {
                        Type = Type.Error, Message = result.Message
                    }));
                }
                if (result.Data == null || result.Data.Count == 0)
                {
                    return(Ok(new List <ShiftViewModel>()));
                }

                List <ShiftViewModel>  shiftVmList      = new List <ShiftViewModel>();
                List <PersonViewModel> personViewModels = new List <PersonViewModel>();
                foreach (Shift shift in result.Data)
                {
                    ShiftViewModel shiftVm = ShiftViewModel.CreateVm(shift);
                    shiftVm.Availabilities = new List <AvailabilityViewModel>();
                    foreach (Availability availability in shift.Availabilities)
                    {
                        PersonViewModel pvm;
                        Guid            id = availability.Participation.PersonId;
                        if (personViewModels.FirstOrDefault(pvms => pvms.Id == id) == null)
                        {
                            TaskResult <User> person = await personService.GetUserAsync(id);

                            pvm = PersonViewModel.CreateVmFromUser(person.Data,
                                                                   Extensions.GetInstance(b2CExtentionApplicationId));
                            personViewModels.Add(pvm);
                        }
                        else
                        {
                            pvm = personViewModels.FirstOrDefault(pvm => pvm.Id == id);
                        }

                        AvailabilityViewModel avm = AvailabilityViewModel.CreateVm(availability);
                        avm.Participation.Person = pvm;
                        shiftVm.Availabilities.Add(avm);
                    }

                    shiftVmList.Add(shiftVm);
                }

                return(Ok(shiftVmList));
            }
            catch (Exception ex)
            {
                string message = GetType()
                                 .Name + "Error in " + nameof(GetShiftAsync);
                logger.LogError(ex, message);
                return(UnprocessableEntity(new ErrorViewModel {
                    Type = Type.Error, Message = message
                }));
            }
        }
Ejemplo n.º 8
0
        public async Task <ActionResult <ScheduleDataViewModel> > GetScheduleAsync(Guid id)
        {
            if (id == Guid.Empty)
            {
                return(BadRequest());
            }
            try
            {
                TaskResult <Shift> shiftResult = await shiftService.GetShiftWithAvailabilitiesAsync(id);

                if (!shiftResult.Succeeded)
                {
                    return(UnprocessableEntity(new ErrorViewModel {
                        Type = Type.Error, Message = shiftResult.Message
                    }));
                }
                if (shiftResult.Data == null)
                {
                    return(NotFound());
                }

                List <ScheduleViewModel> schedules = new List <ScheduleViewModel>();

                //get a list with all days this week
                var culture = System.Threading.Thread.CurrentThread.CurrentCulture;
                int numberOfDaysBetweenNowAndStart =
                    shiftResult.Data.Date.DayOfWeek - culture.DateTimeFormat.FirstDayOfWeek;
                if (numberOfDaysBetweenNowAndStart < 0)
                {
                    numberOfDaysBetweenNowAndStart += 7;
                }
                DateTime firstDateThisWeek =
                    shiftResult.Data.Date.Subtract(TimeSpan.FromDays(numberOfDaysBetweenNowAndStart));
                List <DateTime> allDaysThisWeek = new List <DateTime>();
                for (int i = 0; i < 7; i++)
                {
                    allDaysThisWeek.Add(firstDateThisWeek.AddDays(i));
                }

                foreach (Availability availability in shiftResult.Data.Availabilities
                         .Where(a => a.Type == AvailibilityType.Ok || a.Type == AvailibilityType.Scheduled)
                         ) //filter for people that are registerd to be able to work
                {
                    //list all availabilities in this project of this person
                    TaskListResult <Availability> availabilities =
                        await availabilityService.FindAvailabilitiesAsync(
                            availability.Participation.ProjectId,
                            availability.Participation.PersonId);

                    //lookup person information in B2C
                    TaskResult <User> person = await personService.GetUserAsync(availability.Participation.PersonId);

                    //see if person is scheduled this day and this shift
                    if (availabilities.Data == null || availabilities.Data.Count <= 0)
                    {
                        continue;
                    }
                    int numberOfTimeScheduledThisDay = availabilities.Data
                                                       .Where(a => a.Shift.Date == shiftResult.Data.Date)
                                                       .Count(a => a.Type == AvailibilityType.Scheduled);

                    bool scheduledThisShift = availabilities.Data.FirstOrDefault(a =>
                                                                                 a.ShiftId == id && a.Type == AvailibilityType.Scheduled) != null;

                    //calculate the number of hours person is scheduled this week
                    double numberOfHoursScheduleThisWeek = availabilities.Data
                                                           .Where(a => a.Type == AvailibilityType.Scheduled && allDaysThisWeek.Contains(a.Shift.Date))
                                                           .Sum(availability1 =>
                                                                availability1.Shift.EndTime.Subtract(availability1.Shift.StartTime).TotalHours);

                    //add scheduleViewmodel to list
                    schedules.Add(new ScheduleViewModel
                    {
                        Person = PersonViewModel.CreateVmFromUser(person.Data,
                                                                  Extensions.GetInstance(b2CExtentionApplicationId)),
                        NumberOfTimesScheduledThisProject =
                            availabilities.Data.Count(a => a.Type == AvailibilityType.Scheduled),
                        ScheduledThisDay   = numberOfTimeScheduledThisDay > 0,
                        ScheduledThisShift = scheduledThisShift,
                        AvailabilityId     = availability.Id,
                        Preference         = availability.Preference,
                        Availabilities     = availabilities.Data
                                             .Where(a => a.Shift.Date == shiftResult.Data.Date)
                                             .Select(AvailabilityViewModel.CreateVm)
                                             .ToList(),
                        HoursScheduledThisWeek = numberOfHoursScheduleThisWeek,
                        Employability          = availability.Participation.MaxWorkingHoursPerWeek
                    });
                }

                ScheduleDataViewModel vm = new ScheduleDataViewModel
                {
                    Schedules = schedules,
                    Shift     = ShiftViewModel.CreateVm(shiftResult.Data)
                };

                return(Ok(vm));
            }
            catch (Exception ex)
            {
                string message = GetType()
                                 .Name + "Error in " + nameof(GetScheduleAsync);
                logger.LogError(ex, message);
                return(UnprocessableEntity(new ErrorViewModel {
                    Type = Type.Error, Message = message
                }));
            }
        }