public IActionResult Post(ProjectMembershipModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            string email = User.FindFirst(ClaimTypes.Email)?.Value;

            if (string.IsNullOrEmpty(email))
            {
                return(BadRequest("Invalid attempt"));
            }

            ProjectMembershipRequestModel requestModel = new ProjectMembershipRequestModel()
            {
                ProjectId      = model.ProjectId,
                UserEmail      = email,
                MembershipType = model.MembershipType
            };
            var response = service.AddMembershipRequest(requestModel);

            if (!response.Success)
            {
                return(BadRequest(response.Message));
            }
            return(Ok(true));
        }
Ejemplo n.º 2
0
        public ActionResponse AddMembershipRequest(ProjectMembershipRequestModel model)
        {
            using (var unitWork = new UnitOfWork(context))
            {
                ActionResponse response = new ActionResponse();
                IMessageHelper mHelper;

                try
                {
                    var project = unitWork.ProjectRepository.GetByID(model.ProjectId);
                    if (project == null)
                    {
                        mHelper          = new MessageHelper();
                        response.Message = mHelper.GetNotFound("Project");
                        response.Success = false;
                        return(response);
                    }

                    var user = unitWork.UserRepository.GetOne(u => u.Email == model.UserEmail);
                    if (user == null)
                    {
                        mHelper          = new MessageHelper();
                        response.Message = mHelper.GetNotFound("User");
                        response.Success = false;
                        return(response);
                    }

                    int membershipConstant = (int)model.MembershipType;
                    var requestExists      = unitWork.ProjectMembershipRepository.GetOne(r => (r.ProjectId == model.ProjectId && r.UserId == user.Id && r.MembershipType == (MembershipTypes)model.MembershipType));
                    if (requestExists != null)
                    {
                        mHelper             = new MessageHelper();
                        requestExists.Dated = DateTime.Now;
                        unitWork.ProjectMembershipRepository.Update(requestExists);
                        response.Message = mHelper.AlreadyExists("Membership request");
                        return(response);
                    }
                    else
                    {
                        requestExists = unitWork.ProjectMembershipRepository.Insert(new EFProjectMembershipRequests()
                        {
                            Project        = project,
                            User           = user,
                            OrganizationId = user.OrganizationId,
                            Dated          = DateTime.Now,
                            MembershipType = model.MembershipType,
                            IsApproved     = false
                        });
                    }
                    unitWork.Save();

                    //Send email to all members of the funder
                    var    projectFunderIds       = unitWork.ProjectFundersRepository.GetProjection(p => p.ProjectId == model.ProjectId, p => p.FunderId);
                    var    users                  = unitWork.UserRepository.GetManyQueryable(u => projectFunderIds.Contains(u.OrganizationId));
                    var    requestingUser         = unitWork.UserRepository.GetWithInclude(u => u.Id == user.Id, new string[] { "Organization" }).FirstOrDefault();
                    string requestingOrganization = requestingUser != null ? requestingUser.Organization.OrganizationName : null;

                    List <EmailAddress> usersEmailList = new List <EmailAddress>();
                    foreach (var u in users)
                    {
                        usersEmailList.Add(new EmailAddress()
                        {
                            Email = u.Email,
                        });
                    }
                    if (usersEmailList.Count == 0)
                    {
                        var projectOwner = unitWork.UserRepository.GetOne(o => o.Id == project.CreatedById);
                        if (projectOwner != null)
                        {
                            usersEmailList.Add(new EmailAddress()
                            {
                                Email = projectOwner.Email
                            });
                        }
                    }

                    if (usersEmailList.Count > 0)
                    {
                        //Send emails
                        ISMTPSettingsService smtpService = new SMTPSettingsService(context);
                        var smtpSettings = smtpService.GetPrivate();
                        SMTPSettingsModel smtpSettingsModel = new SMTPSettingsModel();
                        if (smtpSettings != null)
                        {
                            smtpSettingsModel.Host       = smtpSettings.Host;
                            smtpSettingsModel.Port       = smtpSettings.Port;
                            smtpSettingsModel.Username   = smtpSettings.Username;
                            smtpSettingsModel.Password   = smtpSettings.Password;
                            smtpSettingsModel.AdminEmail = smtpSettings.AdminEmail;
                            smtpSettingsModel.SenderName = smtpSettings.SenderName;
                        }

                        string message = "", subject = "", footerMessage = "";
                        var    emailMessage = unitWork.EmailMessagesRepository.GetOne(m => m.MessageType == EmailMessageType.NewOrgToProject);
                        if (emailMessage != null)
                        {
                            subject       = emailMessage.Subject;
                            message       = emailMessage.Message;
                            footerMessage = emailMessage.FooterMessage;
                        }

                        mHelper = new MessageHelper();
                        string role = model.MembershipType.ToString();
                        message += mHelper.NewOrganizationForProject(requestingOrganization, project.Title, role);
                        IEmailHelper emailHelper = new EmailHelper(smtpSettingsModel.AdminEmail, smtpSettings.SenderName, smtpSettingsModel);
                        emailHelper.SendEmailToUsers(usersEmailList, subject, "", message, footerMessage);
                    }
                }
                catch (Exception ex)
                {
                    response.Message = ex.Message;
                    response.Success = false;
                }
                return(response);
            }
        }