public void TestIsProjectValidToClose_ReturnFalseWhenProjectContainsActiveTasks()
        {
            var mockRepository = new Mock <IProjectRepository>();
            var manageProject  = new ManageProject(mockRepository.Object, fixture.ManageProjectLogger);

            var taskDetail = new TaskDetail()
            {
                Id = 1, Name = "Task 1", Priority = 20, ActiveStatus = true
            };

            var taskDetailsList = new List <TaskDetail>()
            {
                taskDetail,
                new TaskDetail()
                {
                    Id = 2, Name = "Task 2 ", Priority = 20
                },
            };

            var project = new Project()
            {
                ProjectId = 1, ProjectName = "Project 1", Priority = 20, TaskDetails = taskDetailsList
            };

            var result = manageProject.IsProjectValidToClose(project);

            Assert.False(result);
        }
        public async Task TestDeleteProjectAsync_VerifyGetAsyncCalledOnce()
        {
            var mockRepository = new Mock <IProjectRepository>();
            var manageProject  = new ManageProject(mockRepository.Object, fixture.ManageProjectLogger);
            var project        = new Project();
            var result         = await manageProject.DeleteProjectAsync(project);

            mockRepository.Verify(r => r.DeleteAsync(project), Times.Once);
        }
        public async Task TestGetAllProjectsAsync_VerifyGetAllAsyncCalledOnce()
        {
            var mockRepository = new Mock <IProjectRepository>();
            var manageProject  = new ManageProject(mockRepository.Object, fixture.ManageProjectLogger);

            var result = await manageProject.GetAllProjectsAsync();

            mockRepository.Verify(r => r.GetAllAsync(), Times.Once);
        }
        public void TestIsProjectValidToClose_ReturnTrueWhenProjectNotContainsAnyTasks()
        {
            var mockRepository = new Mock <IProjectRepository>();
            var manageProject  = new ManageProject(mockRepository.Object, fixture.ManageProjectLogger);

            var project = new Project()
            {
                ProjectId = 1, ProjectName = "Project 1", Priority = 20, TaskDetails = new List <TaskDetail>()
            };

            var result = manageProject.IsProjectValidToClose(project);

            Assert.True(result);
        }
Пример #5
0
        public async Task <IActionResult> Edit(int id, ManageProject model)
        {
            if (id != model.project.ProjectId)
            {
                return(NotFound());
            }

            var uid    = (await _usermgr.GetUserAsync(User)).Id;
            var assist = _context.ProjectEmployees
                         .Where(pe => pe.ProjectId == id && pe.Role == ProjectEmployee.PROJECT_ASSISTANT)
                         .FirstOrDefault();

            bool isAssist = assist != null && assist.EmployeeId.Equals(uid);

            if (isAssist)//the assistants model will have two nulls because they cannot change PM or PMA
            {
                model.projectManager = _context.ProjectEmployees
                                       .Where(pe => pe.ProjectId == id && pe.Role == ProjectEmployee.PROJECT_MANAGER)
                                       .FirstOrDefault()
                                       .EmployeeId;
                model.managersAssistant = uid;
            }
            else
            {
                if (model.projectManager == model.managersAssistant)
                {
                    ViewBag.MgrIsAssist = "Manager and Assistant cannot be the same person!";
                    return(await Edit(id));
                }
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(model.project);

                    foreach (var req in model.requests)
                    {
                        var updateReq = _context.ProjectRequests.FirstOrDefault(r =>
                                                                                r.ProjectId == req.ProjectId && r.PayGradeId == req.PayGradeId);

                        updateReq.AmountRequested = req.AmountRequested;
                    }

                    ProjectEmployee managerPE = _context.ProjectEmployees
                                                .Where(e => e.ProjectId == id && e.Role == ProjectEmployee.PROJECT_MANAGER)
                                                .FirstOrDefault();

                    if (model.projectManager != managerPE.EmployeeId) //we are setting a new PM
                    {
                        ProjectEmployee managerRE = _context.ProjectEmployees
                                                    .Where(e => e.ProjectId == id &&
                                                           e.Role == ProjectEmployee.RESPONSIBLE_ENGINEER)
                                                    .Join(_context.WorkPackages,
                                                          wp => wp.WorkPackageId,
                                                          pe => pe.WorkPackageId,
                                                          (pe, wp) => new { wp, pe })
                                                    .Where(join => join.wp.WorkPackageCode == "00000")
                                                    .Select(join => join.pe)
                                                    .FirstOrDefault();

                        Employee oldmgr = _context.Employees.Find(managerPE.EmployeeId);
                        await _usermgr.RemoveFromRoleAsync(oldmgr, ApplicationRole.PM);

                        await _usermgr.RemoveFromRoleAsync(oldmgr, ApplicationRole.RE);

                        Employee newmgr = _context.Employees.Find(model.projectManager);
                        await _usermgr.AddToRoleAsync(newmgr, ApplicationRole.PM);

                        await _usermgr.AddToRoleAsync(newmgr, ApplicationRole.RE);

                        ProjectEmployee unassigned = _context.ProjectEmployees
                                                     .Where(pe => pe.ProjectId == id &&
                                                            pe.EmployeeId == newmgr.Id &&
                                                            pe.Role == ProjectEmployee.NOT_ASSIGNED)
                                                     .FirstOrDefault();

                        if (unassigned != null)
                        {
                            unassigned.Role   = ProjectEmployee.RESPONSIBLE_ENGINEER;
                            unassigned.Status = ProjectEmployee.CURRENTLY_WORKING;
                            _context.Update(unassigned);
                            _context.Remove(managerRE);
                        }
                        else
                        {
                            managerRE.EmployeeId = model.projectManager;
                            _context.Update(managerRE);
                        }

                        managerPE.EmployeeId = model.projectManager;
                        _context.Update(managerPE);
                    }

                    var assistantPE = _context.ProjectEmployees
                                      .Where(e => e.ProjectId == id && e.Role == ProjectEmployee.PROJECT_ASSISTANT)
                                      .FirstOrDefault();

                    if (!String.IsNullOrEmpty(model.managersAssistant)) //if we are setting an assistant
                    {
                        if (assistantPE == null)                        //if the project doesnt have an assitant already
                        {
                            assistantPE = new ProjectEmployee           //add them to the project as the PA
                            {
                                EmployeeId    = model.managersAssistant,
                                ProjectId     = id,
                                Role          = ProjectEmployee.PROJECT_ASSISTANT,
                                Status        = ProjectEmployee.CURRENTLY_WORKING,
                                WorkPackageId = null
                            };
                            _context.Add(assistantPE);
                        }
                        else if (model.managersAssistant != assistantPE.EmployeeId) //if the project already had a PA and this one is NEW
                        {
                            _context.Remove(assistantPE);
                            _context.Update(new ProjectEmployee
                            {
                                EmployeeId    = model.managersAssistant,
                                ProjectId     = id,
                                Role          = ProjectEmployee.PROJECT_ASSISTANT,
                                Status        = ProjectEmployee.CURRENTLY_WORKING,
                                WorkPackageId = null
                            });
                            Employee oldAssist = _context.Employees.Find(assistantPE.EmployeeId);
                            await _usermgr.RemoveFromRoleAsync(oldAssist, ApplicationRole.PA);

                            //The following makes sure that removing an assistant, doesnt remove them from the
                            //project if that was their only assignment in the project
                            var check = _context.ProjectEmployees
                                        .Where(w => w.ProjectId == id && w.EmployeeId == assistantPE.EmployeeId)
                                        .FirstOrDefault();
                            if (check == null)
                            {
                                _context.Add(new ProjectEmployee
                                {
                                    EmployeeId = assistantPE.EmployeeId,
                                    ProjectId  = id,
                                    Status     = ProjectEmployee.CURRENTLY_WORKING,
                                    Role       = ProjectEmployee.NOT_ASSIGNED
                                });
                            }
                        }

                        Employee assistant = _context.Employees.Find(assistantPE.EmployeeId);
                        await _usermgr.AddToRoleAsync(assistant, ApplicationRole.PA);
                    }
                    else
                    {                                   // if we are clearing the PA
                        if (assistantPE != null)
                        {
                            Employee oldAssist = _context.Employees.Find(assistantPE.EmployeeId);
                            await _usermgr.RemoveFromRoleAsync(oldAssist, ApplicationRole.PA);

                            _context.Remove(assistantPE);
                        }
                    }

                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ProjectExists(model.project.ProjectId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(model.project));
        }
Пример #6
0
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var project = _context.Projects
                          .Include(w => w.WorkPackages)
                          .First(f => f.ProjectId == id);

            if (project == null)
            {
                return(NotFound());
            }

            var manager = _context.ProjectEmployees
                          .Where(e => e.ProjectId == id && e.Role == ProjectEmployee.PROJECT_MANAGER)
                          .FirstOrDefault();

            var assistant = _context.ProjectEmployees
                            .Include(a => a.Employee)
                            .Where(e => e.ProjectId == id && e.Role == ProjectEmployee.PROJECT_ASSISTANT)
                            .FirstOrDefault();

            //Check authorization to edit
            var uid = (await _usermgr.GetUserAsync(User)).Id;

            if (!User.IsInRole("AD") && manager.EmployeeId != uid && assistant != null && assistant.EmployeeId != uid)
            {
                return(RedirectToAction(nameof(Index)));
            }

            //We get the ProjectEmployees separately so we can Include the Employee of each
            var mgmtAndUnasigned = _context.ProjectEmployees
                                   .Include(e => e.Employee)
                                   .Where(e => e.ProjectId == id &&
                                          e.Status == ProjectEmployee.CURRENTLY_WORKING)
                                   .GroupBy(pe => new { pe.EmployeeId, pe.Role })
                                   .Select(pe => pe.FirstOrDefault())
                                   .OrderBy(pe => pe.Role)
                                   .Distinct()
                                   .ToList();

            var reqs = _context.ProjectRequests
                       .Include(r => r.PayGrade)
                       .Where(r => r.ProjectId == id)
                       .ToList();

            //This is for when new pay grades have been added that the project does not yet have
            #region Not Enough Pay Grades
            var grds = _context.PayGrades.ToList();
            if (reqs.Count < grds.Count)
            {
                bool exists = false;
                foreach (var grade in grds)
                {
                    foreach (var req in reqs)
                    {
                        if (grade.PayGradeId == req.PayGradeId)
                        {
                            exists = true;
                            break;
                        }
                    }

                    if (!exists)
                    {
                        ProjectRequest request = new ProjectRequest
                        {
                            PayGradeId      = grade.PayGradeId,
                            AmountRequested = 0,
                            ProjectId       = id,
                            Status          = ProjectRequest.VALID
                        };
                        reqs.Add(request);
                        _context.Add(request);
                    }
                    else
                    {
                        exists = false;
                    }
                }
                await _context.SaveChangesAsync();
            }
            #endregion

            ManageProject model = new ManageProject();
            model.project = project;
            model.project.ProjectEmployees = mgmtAndUnasigned;
            model.requests       = reqs;
            model.projectManager = manager.EmployeeId;
            if (assistant != null)
            {
                model.managersAssistant = assistant.EmployeeId;
            }

            List <Employee> mgrList = new List <Employee>();
            List <Employee> assList = new List <Employee>();

            ViewBag.Assistant = false;

            if (User.IsInRole("AD"))    //Admin
            {
                mgrList = _context.Employees
                          .ToList();

                assList = mgrList;
            }
            else if (manager.EmployeeId == uid) // Project Manager
            {
                mgrList = _context.ProjectEmployees
                          .Where(pe => pe.ProjectId == id)
                          .Join(_context.Employees,
                                pe => pe.EmployeeId,
                                em => em.Id,
                                (pe, em) => em)
                          .Distinct()
                          .ToList();

                assList = mgrList;
            }
            else // Assistant
            {
                mgrList.Add(_context.Employees.Find(manager.EmployeeId));
                assList.Add(_context.Employees.Find(assistant.EmployeeId));
                ViewBag.Assistant = true;
            }

            List <SelectListItem> mgrItems = new List <SelectListItem>();
            mgrItems.AddRange(new SelectList(mgrList, "Id", "Email"));


            List <SelectListItem> assItems = new List <SelectListItem>();

            if (ViewBag.Assistant)
            {
                assItems.Insert(0, new SelectListItem {
                    Text = assistant.Employee.Email, Value = assistant.EmployeeId
                });
            }
            else
            {
                assItems.AddRange(new SelectList(mgrList, "Id", "Email"));
                assItems.Insert(0, new SelectListItem {
                    Text = "None", Value = ""
                });
            }


            ViewBag.EmployeesM = mgrItems;
            ViewBag.EmployeesA = assItems;

            ViewBag.Status = new SelectList(Project.Statuses.ToList(), "Key", "Value", project.Status);
            ViewBag.WPs    = new SelectList(_context.WorkPackages.Where(w => w.ProjectId == project.ProjectId).ToList()
                                            , "Name", "Name", project.WorkPackages.First());

            return(View(model));
        }