public virtual ActionResult Organization(int organizationId)
        {
            try
            {
                bool editable = false;
                if (this.userService.CurrentUserId != 0)
                {
                    if (organizationId != 0)
                    {
                        editable = this.organizationService.IsAdminOfOrganization(this.userService.CurrentUserId, organizationId);
                    }
                }
                var organizationVm = new OrganizationVm { Id = organizationId, Admin = new AdminVm() };

                if (organizationId != 0)
                {
                    if (!editable)
                    {
                        return View(WENEEDUHAVE.Organization.Views.OrganizationRead, organizationVm);
                    }
                }

                return EditOrganization(organizationVm);
            }
            catch (Exception e)
            {
                this.emailHelper.SendErrorEmail(e);
            }
            return View(WENEEDUHAVE.Error.Views.Index);
        }
 // POST api/organization
 public HttpResponseMessage Post(OrganizationVm organization)
 {
     var irsOrganization = registrationService.Get(organization.EIN);
     if (irsOrganization != null)
     {
         var start = DateTime.Now;
         Debug.WriteLine("####Entering Save Org: " + start);
         var response = SaveOrganization(organization, HttpStatusCode.Created);
         var end = DateTime.Now;
         Debug.WriteLine("#####Total Org save time: " + end.Subtract(start));
         return response;
     }
     return Request.CreateResponse(HttpStatusCode.BadRequest);
 }
 // PUT api/organization/5
 public HttpResponseMessage Put(OrganizationVm organization)
 {
     var userId = webSecurityService.CurrentUserId;
     bool isAdmin = organizationService.IsAdminOfOrganization(userId, organization.Id);
     if (!isAdmin)
     {
         return Request.CreateResponse(HttpStatusCode.Unauthorized);
     }
     var response = SaveOrganization(organization, HttpStatusCode.OK);
     return response;
 }
        private List<LocationVm> SaveLocationOnFirstSave(OrganizationVm organization)
        {
            ICollection<Location> locationsModel = new List<Location>();
            LocationVm vm = new LocationVm { Address = organization.ContactInformation };
            {
                locationsModel.Add(vm.ToModel());
            }
            try
            {
                this.organizationService.SaveLocations(organization.Id, locationsModel);

                List<LocationVm> responseModel = new List<LocationVm>();

                foreach (Location m in locationsModel)
                {
                    responseModel.Add(LocationVm.FromModel(m));
                }
                return responseModel;
            }
            catch (Exception e)
            {
                emailHelper.SendErrorEmail(e);
            }
            return null;
        }
        private OrganizationVm SaveModel(OrganizationVm organization, HttpResponseMessage response)
        {
            OrganizationVm newOrganization = null;
            WorkflowInstance workflowInstanceToSave = null;
            try
            {
                bool populateInitialLocation = false;
                var workflowInstance = organization.WorkflowInstance;

                if (organization.Id == 0)
                {
                    populateInitialLocation = true;
                }
                var start = DateTime.Now;
                Debug.WriteLine("####Entering Get States: " + start);
                var state = StaticCache.GetStates().First(s => s.Id == organization.ContactInformation.StateId);
                var end = DateTime.Now;
                Debug.WriteLine("#####Total Get State time: " + end.Subtract(start));
                start = DateTime.Now;
                Debug.WriteLine("####Entering Save Org: " + start);
                organization.ContactInformation.StateId = state.Id;
                var organizationToSave = organization.ToModel();
                organizationService.Save(organizationToSave);
                newOrganization = OrganizationVm.FromModel(organizationToSave);
                end = DateTime.Now;
                Debug.WriteLine("#####Total Save org time: " + end.Subtract(start));
                newOrganization.Saved = true;
                if (populateInitialLocation)
                {
                    newOrganization.Locations = SaveLocationOnFirstSave(newOrganization);
                }
                if (workflowInstance != null)
                {
                    workflowInstanceToSave = workflowInstance.ToModel();
                    workflowInstanceToSave.OrganizationId = organizationToSave.Id;
                }
            }
            catch (Exception e)
            {
                emailHelper.SendErrorEmail(e);
                newOrganization.Saved = false;
            }
            //as long as organization saves, don't worry about workflow state, just send an email
            try
            {
                this.workflowInstanceService.Save(workflowInstanceToSave);
                if (workflowInstanceToSave!=null)
                    newOrganization.WorkflowInstance = WorkflowInstanceVm.FromModel(workflowInstanceToSave);
            }
            catch
            {
                //Not emailing this because it's not a real error in this location. It's an expected exception when thhe org is saved outside the workflow
                //emailHelper.SendErrorEmail(e);
            }
            return newOrganization;
        }
 private HttpResponseMessage SaveOrganization(OrganizationVm organization, System.Net.HttpStatusCode statusCode)
 {
     HttpResponseMessage response = new HttpResponseMessage();
     try
     {
         if (ModelState.IsValid)
         {
             var organizationVm = SaveModel(organization, response);
             if (organizationVm.Saved)
                 return response = Request.CreateResponse(statusCode, organizationVm);
         }
     }
     catch (Exception e)
     {
         emailHelper.SendErrorEmail(e);
     }
     return Request.CreateResponse(HttpStatusCode.BadRequest);
 }
 //#if !DEBUG
 //[WeNeedUHave.Web.Infrastructure.Attributes.RequireHttps]
 //#endif
 public virtual ActionResult OrganizationEdit(int id)
 {
     var organizationVm = new OrganizationVm { Id = id, Admin = new AdminVm() };
     return PartialView(WENEEDUHAVE.Organization.Views._OrganizationEdit, organizationVm);
 }
 public virtual ActionResult EditOrganization(OrganizationVm organizationVm)
 {
     return View(WENEEDUHAVE.Organization.Views.OrganizationEdit, organizationVm);
 }
        public virtual ActionResult Logo()
        {
            var randomStringGenerator = new RandomStringGenerator();
            var objectKey = string.Format("uploads/{0}/${{filename}}", randomStringGenerator.GenerateString(16));
            var bucket = "wnuh";

            var maxFileSize = 512 * 1024 * 1024;
            var objectConfiguration = new AmazonS3ObjectConfiguration(objectKey, AmazonS3CannedAcl.Private, maxFileSize);

            var credentials = new AmazonCredentials(
                            ConfigurationManager.AppSettings.Get("amazon.access_key_id"),
                            ConfigurationManager.AppSettings.Get("amazon.secret_access_key"));

            var uploadConfiguration = new AmazonS3UploadConfiguration(credentials, bucket, objectConfiguration);

            var viewModel = new OrganizationVm { Configuration = uploadConfiguration };

            return PartialView(viewModel);
        }
        public virtual ActionResult SignUp(int? id)
        {
            try
            {
                OrganizationVm model = null;
                if (id == null)
                {
                    if (this.userService.CurrentUserId != -1)
                    {
                        var orgs = this.organizationService.GetByAdministrator().ToList();
                        //they've laready signedup before 
                        if (orgs.Count() > 1)
                        {
                            //select first incomplete org registration
                            foreach (var org in orgs)
                            {
                                //Check workflow if it is complete, if no get Org ID
                                model = OrganizationVm.FromModel(this.organizationService.Get(org.Id));

                                var instance = this.workflowInstanceService.Get(wi => wi.OrganizationId == model.Id).FirstOrDefault();
                                if (instance != null)
                                {
                                    model.WorkflowInstance = WorkflowInstanceVm.FromModel(instance);

                                    //This org isn't complete, so let it finish
                                    if (model.WorkflowInstance != null && !model.WorkflowInstance.IsComplete)
                                    {
                                        break;
                                    }
                                    else
                                    {

                                    }
                                }
                            }
                            //var incompleteOrgId = orgs.Select(

                        }
                        else if (orgs.Count() == 1)
                        {
                            //Check workflow if it is complete, if no get Org ID
                            model = OrganizationVm.FromModel(this.organizationService.Get(orgs.ElementAt(0).Id));

                            var instance = this.workflowInstanceService.Get(wi => wi.OrganizationId == model.Id).FirstOrDefault();
                            if (instance != null)
                            {
                                model.WorkflowInstance = WorkflowInstanceVm.FromModel(instance);

                                if (model.WorkflowInstance != null && model.WorkflowInstance.IsComplete)
                                {
                                    model = new OrganizationVm();
                                    model.Id = 0;
                                }
                                //This org isn't complete, so let it finish
                            }
                        }
                        else
                        {
                            model = new OrganizationVm();
                            model.Id = 0;
                        }
                    }
                    else
                    {
                        model = new OrganizationVm();
                        model.Id = 0;
                    }
                }
                else
                {
                    int orgId = 0;
                    Int32.TryParse(id.ToString(), out orgId); ;
                    model = OrganizationVm.FromModel(this.organizationService.Get(orgId));
                }
                return View(model);
            }
            catch (Exception e)
            {
                this.emailHelper.SendErrorEmail(e);
            }
            return View(WENEEDUHAVE.Error.Views.Index);
        }