public void Given_a_draft_checklist_when_assigning_then_status_is_Assigned()
        {
            //GIVN
            var checklist = new Checklist() {Status = "Draft"};
            var qaAdvisor = new QaAdvisor();

            _checklistRepository
               .Setup(x => x.GetById(It.IsAny<Guid>()))
               .Returns(() => checklist);

            _qaAdvisorRepository
               .Setup(x => x.GetById(It.IsAny<Guid>()))
               .Returns(() => qaAdvisor);

            var target = GetTarget();
            var viewModel = new QaAdvisorViewModel();
            viewModel.Id = Guid.NewGuid();

            //WHEN
            target.AssignChecklistToQaAdvisor(Guid.NewGuid(), viewModel);

            Assert.That(checklist.Status,Is.EqualTo("Assigned"));
        }
        public void Given_a_checklist_when_assigning_then_updated_required_is_added()
        {
            //GIVN
            var checklist = new Checklist() {Id = Guid.NewGuid()};
            var qaAdvisor = new QaAdvisor();

            _checklistRepository
               .Setup(x => x.GetById(It.IsAny<Guid>()))
               .Returns(() => checklist);

            _qaAdvisorRepository
               .Setup(x => x.GetById(It.IsAny<Guid>()))
               .Returns(() => qaAdvisor);

            var target = GetTarget();
            var viewModel = new QaAdvisorViewModel();
            viewModel.Id = Guid.NewGuid();

            //WHEN
            target.SendUpdateRequiredEmailNotification(checklist.Id);

            Assert.That(checklist.UpdatesRequiredLog.Count, Is.EqualTo(1));
        }
        public void Given_QA_Advisors_exists_when_Post_then_qaAdvisor_is_updated()
        {
            QaAdvisor savedQaAdvisor = null;

            // Given
            var qaAdvisor = new QaAdvisor() { Id = Guid.NewGuid() };
            var model = new QaAdvisorViewModel(){Id = qaAdvisor.Id, Forename= "Stansa", Surname="Stark", InRotation = true};
            var target = new QaAdvisorController(_dependencyFactory.Object);

            _qaAdvisorRepository.Setup(x => x.GetById(qaAdvisor.Id))
                .Returns(() => qaAdvisor);

            _qaAdvisorRepository.Setup(x => x.SaveOrUpdate(It.IsAny<QaAdvisor>()))
                .Callback<QaAdvisor>(x => savedQaAdvisor = x);

            // When
            target.Post(model);

            // Then
            Assert.That(savedQaAdvisor.Id,Is.EqualTo(model.Id));
            Assert.That(savedQaAdvisor.InRotation, Is.EqualTo(model.InRotation));
            Assert.That(savedQaAdvisor.Forename, Is.EqualTo(model.Forename));
            Assert.That(savedQaAdvisor.Surname, Is.EqualTo(model.Surname));
        }
        public HttpResponseMessage AssignChecklistToQaAdvisor(Guid id, QaAdvisorViewModel model)
        {
            try
            {
                var checklist = _checklistRepository.GetById(id);

                if (checklist == null)
                {
                    throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound,
                        "Checklist: " + id + "Not Found."));
                }

                var qaAdvisor = _qaAdvisorRepository.GetById(model.Id);

                checklist.QaAdvisor = qaAdvisor;
                checklist.Status = "Assigned";

                //Save.
                _checklistRepository.SaveOrUpdate(checklist);
                _checklistRepository.Flush();


                var sendChecklistAssignedEmail = new SendChecklistAssignedEmail
                {
                    ChecklistId = id,
                    AssignedToId = model.Id
                };

                if (checklist.ClientId.HasValue)
                {
                    var clientDetail = _clientDetailsService.Get(checklist.ClientId.Value);

                    var site = checklist.SiteId.HasValue
                        ? _clientDetailsService.GetSite(checklist.ClientId.Value, checklist.SiteId.Value)
                        : null;

                    var postcode = site != null ? site.Postcode : "";

                    sendChecklistAssignedEmail.Can = clientDetail.CAN;
                    sendChecklistAssignedEmail.SiteName = site != null ? site.SiteName : "";
                    sendChecklistAssignedEmail.Address1 = site != null ? site.Address1 : "";
                    sendChecklistAssignedEmail.Address2 = site != null ? site.Address2 : "";
                    sendChecklistAssignedEmail.Address3 = site != null ? site.Address3 : "";
                    sendChecklistAssignedEmail.Address4 = site != null ? site.Address4 : "";
                    sendChecklistAssignedEmail.Address5 = site != null ? site.Address5 : "";
                    sendChecklistAssignedEmail.Postcode = postcode;
                }
                else
                {
                    sendChecklistAssignedEmail.Can = "Not specified";
                    sendChecklistAssignedEmail.Postcode = "Not specified";
                    ;
                }

                _bus.Send(sendChecklistAssignedEmail);
            }

            catch (Exception ex)
            {
                LogManager.GetLogger(typeof (ChecklistController)).Error(ex);
                throw;
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }
        public void Post(QaAdvisorViewModel model)
        {
            try
            {
                var advisor = _qaAdvisorRepository.GetById(model.Id);
                if (advisor == null)
                {
                    QaAdvisor.Create(model.Forename, model.Surname, model.Email);
                }
                else
                {
                    advisor.Update(model.Forename, model.Surname, model.Email, model.InRotation);
                }

                advisor.InRotation = model.InRotation;

                _qaAdvisorRepository.SaveOrUpdate(advisor);

            }
            catch (Exception ex)
            {
                LogManager.GetLogger(typeof(QaAdvisorController)).Error(ex);
                throw;
            }
        }
        public HttpResponseMessage AssignChecklistToQaAdvisor(Guid id, QaAdvisorViewModel model)
        {
            try
            {
                var checklist = _checklistRepository.GetById(id);

                if (checklist == null)
                {
                    throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound,
                        "Checklist: " + id + "Not Found."));
                }

                var qaAdvisor = _qaAdvisorRepository.GetById(model.Id);

                _qualityControlService.AssignChecklistToQaAdvisor(checklist, qaAdvisor);

                _checklistRepository.SaveOrUpdate(checklist);
                _checklistRepository.Flush();
            }

            catch (Exception ex)
            {
                LogManager.GetLogger(typeof (ChecklistController)).Error(ex);
                throw;
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }