示例#1
0
        public ActionResponse SendNotificationsForNewOrganizations(int newOrganizations, int organizationsWithoutType)
        {
            var            unitWork = new UnitOfWork(context);
            ActionResponse response = new ActionResponse();
            var            messages = unitWork.EmailMessagesRepository.GetAll();

            if (newOrganizations > 0)
            {
                string subject = "", message = "", footerMessage = "";
                var    emailMessage = (from m in messages
                                       where m.MessageType == EmailMessageType.NewIATIOrganization
                                       select m).FirstOrDefault();
                if (emailMessage != null)
                {
                    subject       = emailMessage.Subject;
                    message       = emailMessage.Message;
                    footerMessage = emailMessage.FooterMessage;
                }

                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;
                }

                IEmailHelper emailHelper = new EmailHelper(smtpSettings.AdminEmail, smtpSettings.SenderName, smtpSettingsModel);
                var          users       = unitWork.UserRepository.GetManyQueryable(u => u.UserType == UserTypes.Manager);
                var          emails      = (from u in users
                                            select u.Email);
                List <EmailAddress> emailAddresses = new List <EmailAddress>();
                foreach (var email in emails)
                {
                    emailAddresses.Add(new EmailAddress()
                    {
                        Email = email
                    });
                }

                IMessageHelper mHelper = new MessageHelper();
                message += mHelper.NewIATIOrganizationsMessage(newOrganizations, organizationsWithoutType);
                if (emailAddresses.Count > 0)
                {
                    emailHelper.SendEmailToUsers(emailAddresses, subject, "", message, footerMessage);
                }
            }
            return(response);
        }
示例#2
0
        public EmailService(AIMSDbContext cntxt)
        {
            context = cntxt;
            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;
                smtpSettings.SenderName      = smtpSettings.SenderName;
                emailHelper = new EmailHelper(smtpSettings.AdminEmail, smtpSettings.SenderName, smtpSettings);
            }
            else
            {
                emailHelper = new EmailHelper(null, null, null);
            }
        }
示例#3
0
        public ActionResponse SendPendingDeletionRequestsToManagement()
        {
            ActionResponse response = new ActionResponse();
            var            unitWork = new UnitOfWork(context);
            var            pendingDeletionRequests = unitWork.ProjectDeletionRepository.GetWithInclude(d => d.Status == ProjectDeletionStatus.Requested &&
                                                                                                       d.RequestedOn <= DateTime.Now.AddDays(-7) && !d.IsNotifiedToManager, new string[] { "Project", "RequestedBy" });
            var users       = unitWork.UserRepository.GetWithInclude(u => u.UserType == UserTypes.Manager, new string[] { "Organization" });
            var adminEmails = (from u in users
                               select u.Email);
            List <EmailAddress> usersEmailList = new List <EmailAddress>();

            foreach (var email in adminEmails)
            {
                usersEmailList.Add(new EmailAddress()
                {
                    Email = email
                });
            }

            if (usersEmailList.Count > 0)
            {
                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;
                }

                var projectIds = (from p in pendingDeletionRequests
                                  select p.ProjectId).ToList <int>();

                foreach (var request in pendingDeletionRequests)
                {
                    var receiversEmails = (from e in usersEmailList
                                           where e.Email != request.RequestedBy.Email
                                           select e).ToList();
                    string message = "", subject = "", footerMessage = "";
                    string projectTitle = "<h5>Project title: " + request.Project.Title + "</h5>";
                    string requestedBy  = "<h5>Requested by: " + request.RequestedBy.Email + "</h5>";
                    var    emailMessage = unitWork.EmailMessagesRepository.GetOne(m => m.MessageType == EmailMessageType.ProjectDeletionRequest);
                    if (emailMessage != null)
                    {
                        subject       = emailMessage.Subject;
                        message       = projectTitle + requestedBy + emailMessage.Message;
                        footerMessage = emailMessage.FooterMessage;
                    }
                    IEmailHelper emailHelper = new EmailHelper(smtpSettingsModel.AdminEmail, smtpSettings.SenderName, smtpSettingsModel);
                    emailHelper.SendEmailToUsers(receiversEmails, subject, "", message, footerMessage);
                    request.IsNotifiedToManager = true;
                    unitWork.ProjectDeletionRepository.Update(request);
                }
                if (pendingDeletionRequests.Any())
                {
                    unitWork.Save();
                }
            }
            return(response);
        }
示例#4
0
        public ActionResponse DeleteProject(int projectId, int userId)
        {
            using (var unitWork = new UnitOfWork(context))
            {
                IMessageHelper mHelper;
                ActionResponse response = new ActionResponse();
                var            user     = unitWork.UserRepository.GetOne(u => (u.UserType == UserTypes.Manager || u.UserType == UserTypes.SuperAdmin) && u.Id == userId);
                if (user == null)
                {
                    mHelper          = new MessageHelper();
                    response.Message = mHelper.GetInvalidAccountForProject();
                    response.Success = false;
                    return(response);
                }

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

                unitWork.ProjectRepository.Delete(project);
                unitWork.Save();

                int userOrganizationId = 0;
                var deletionRequest    = unitWork.ProjectDeletionRepository.GetWithInclude(p => p.ProjectId == projectId, new string[] { "RequestedBy" }).FirstOrDefault();
                if (deletionRequest != null)
                {
                    userOrganizationId = deletionRequest.RequestedBy.OrganizationId;
                }
                var projectFunderIds      = unitWork.ProjectFundersRepository.GetProjection(f => f.ProjectId == project.Id, f => f.FunderId);
                var projectImplementerIds = unitWork.ProjectImplementersRepository.GetProjection(i => i.ProjectId == project.Id, i => i.ImplementerId);
                var orgIds = projectFunderIds.Union(projectImplementerIds).ToList();
                if (!orgIds.Contains(userOrganizationId) && userOrganizationId != 0)
                {
                    orgIds.Add(userOrganizationId);
                }

                var userEmails  = unitWork.UserRepository.GetProjection(u => u.UserType == UserTypes.Standard && orgIds.Contains(u.OrganizationId), u => u.Email);
                var adminEmails = unitWork.UserRepository.GetProjection(u => u.UserType == UserTypes.Manager, u => u.Email);
                var allEmails   = userEmails.Union(adminEmails);

                List <EmailAddress> usersEmailList = new List <EmailAddress>();
                foreach (var email in allEmails)
                {
                    usersEmailList.Add(new EmailAddress()
                    {
                        Email = email
                    });
                }

                if (usersEmailList.Count > 0)
                {
                    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.ProjectDeleted);
                    if (emailMessage != null)
                    {
                        subject       = emailMessage.Subject;
                        message       = emailMessage.Message;
                        footerMessage = emailMessage.FooterMessage;
                    }
                    mHelper  = new MessageHelper();
                    message += mHelper.ProjectDeletionMessage(project.Title);
                    IEmailHelper emailHelper = new EmailHelper(smtpSettingsModel.AdminEmail, smtpSettings.SenderName, smtpSettingsModel);
                    emailHelper.SendEmailToUsers(usersEmailList, subject, "", message, footerMessage);
                }
                return(response);
            }
        }
示例#5
0
        public ActionResponse CancelRequest(int projectId, int userId)
        {
            using (var unitWork = new UnitOfWork(context))
            {
                IMessageHelper mHelper;
                ActionResponse response = new ActionResponse();
                var            project  = unitWork.ProjectRepository.GetByID(projectId);
                if (project == null)
                {
                    mHelper          = new MessageHelper();
                    response.Success = false;
                    response.Message = mHelper.GetNotFound("Project");
                    return(response);
                }

                var userOrganizationsList = unitWork.UserRepository.GetProjection(u => u.Id == userId, u => u.OrganizationId);
                var userOrganizationId    = (from orgId in userOrganizationsList
                                             select orgId).FirstOrDefault();

                int projectOrganizationId = 0;
                EFProjectDeletionRequests deletionRequest = null;
                var deletionRequestArr = unitWork.ProjectDeletionRepository.GetWithInclude(d => d.ProjectId == projectId, new string[] { "RequestedBy" });
                foreach (var delRequest in deletionRequestArr)
                {
                    projectOrganizationId = delRequest.RequestedBy.OrganizationId;
                    deletionRequest       = delRequest;
                }

                bool canEditProject = false;
                if (projectOrganizationId == userOrganizationId)
                {
                    canEditProject = true;
                }
                else
                {
                    var funderProjectsIds     = unitWork.ProjectFundersRepository.GetProjection(f => f.FunderId == userOrganizationId, f => f.ProjectId).ToList();
                    var implementerProjectIds = unitWork.ProjectImplementersRepository.GetProjection(i => i.ImplementerId == userOrganizationId, i => i.ProjectId).ToList();
                    var membershipProjectIds  = unitWork.ProjectMembershipRepository.GetProjection(m => (m.UserId == userId && m.IsApproved == true), m => m.ProjectId);
                    var combinedProjectIds    = funderProjectsIds.Union(implementerProjectIds);
                    combinedProjectIds = combinedProjectIds.Union(membershipProjectIds);
                    if (!combinedProjectIds.Contains(projectId))
                    {
                        mHelper          = new MessageHelper();
                        response.Success = false;
                        response.Message = mHelper.GetInvalidProjectEdit();
                        return(response);
                    }
                    canEditProject = true;
                }

                if (canEditProject)
                {
                    unitWork.ProjectDeletionRepository.Delete(deletionRequest);
                    unitWork.Save();

                    var projectFunderIds      = unitWork.ProjectFundersRepository.GetProjection(f => f.ProjectId == project.Id, f => f.FunderId);
                    var projectImplementerIds = unitWork.ProjectImplementersRepository.GetProjection(i => i.ProjectId == project.Id, i => i.ImplementerId);
                    var orgIds = projectFunderIds.Union(projectImplementerIds).ToList();
                    if (!orgIds.Contains(userOrganizationId))
                    {
                        orgIds.Add(userOrganizationId);
                    }

                    var userEmails  = unitWork.UserRepository.GetProjection(u => u.UserType == UserTypes.Standard && orgIds.Contains(u.OrganizationId), u => u.Email);
                    var adminEmails = unitWork.UserRepository.GetProjection(u => u.UserType == UserTypes.Manager, u => u.Email);
                    var allEmails   = userEmails.Union(adminEmails);

                    List <EmailAddress> usersEmailList = new List <EmailAddress>();
                    foreach (var email in allEmails)
                    {
                        usersEmailList.Add(new EmailAddress()
                        {
                            Email = email
                        });
                    }

                    if (usersEmailList.Count > 0)
                    {
                        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 = "";
                        string projectTitle = "<h5>Project title: " + project.Title + "</h5>";
                        var    emailMessage = unitWork.EmailMessagesRepository.GetOne(m => m.MessageType == EmailMessageType.ProjectDeletionCancelled);
                        if (emailMessage != null)
                        {
                            subject       = emailMessage.Subject;
                            message       = projectTitle + emailMessage.Message;
                            footerMessage = emailMessage.FooterMessage;
                        }
                        IEmailHelper emailHelper = new EmailHelper(smtpSettingsModel.AdminEmail, smtpSettings.SenderName, smtpSettingsModel);
                        emailHelper.SendEmailToUsers(usersEmailList, subject, "", message, footerMessage);
                    }
                }
                return(response);
            }
        }
示例#6
0
        public ActionResponse AddRequest(ProjectDeletionRequestModel model)
        {
            using (var unitWork = new UnitOfWork(context))
            {
                ActionResponse response = new ActionResponse();
                IMessageHelper mHelper;
                var            project = unitWork.ProjectRepository.GetByID(model.ProjectId);
                if (project == null)
                {
                    mHelper          = new MessageHelper();
                    response.Success = false;
                    response.Message = mHelper.GetNotFound("Project");
                    return(response);
                }

                var user = unitWork.UserRepository.GetWithInclude(u => u.Id == model.UserId, new string[] { "Organization" }).FirstOrDefault();
                if (user == null)
                {
                    mHelper          = new MessageHelper();
                    response.Success = false;
                    response.Message = mHelper.GetNotFound("User");
                    return(response);
                }

                var isRequestExists = unitWork.ProjectDeletionRepository.GetOne(p => p.ProjectId == project.Id);
                if (isRequestExists != null && isRequestExists.Status == ProjectDeletionStatus.Requested)
                {
                    mHelper          = new MessageHelper();
                    response.Success = false;
                    response.Message = mHelper.GetProjectDeletionExistsMessage();
                    return(response);
                }

                if (isRequestExists != null && isRequestExists.Status == ProjectDeletionStatus.Approved)
                {
                    mHelper          = new MessageHelper();
                    response.Success = false;
                    response.Message = mHelper.GetProjectDeletionApprovedMessage();
                    return(response);
                }

                if (isRequestExists != null && isRequestExists.Status == ProjectDeletionStatus.Cancelled)
                {
                    isRequestExists.Status          = ProjectDeletionStatus.Requested;
                    isRequestExists.RequestedOn     = DateTime.Now;
                    isRequestExists.StatusUpdatedOn = DateTime.Now;
                    isRequestExists.RequestedBy     = user;
                    unitWork.ProjectDeletionRepository.Update(isRequestExists);
                }
                else
                {
                    unitWork.ProjectDeletionRepository.Insert(new EFProjectDeletionRequests()
                    {
                        Project         = project,
                        RequestedBy     = user,
                        RequestedOn     = DateTime.Now,
                        StatusUpdatedOn = DateTime.Now,
                        Status          = ProjectDeletionStatus.Requested
                    });
                }
                unitWork.Save();

                var projectFunderIds      = unitWork.ProjectFundersRepository.GetProjection(f => f.ProjectId == project.Id, f => f.FunderId);
                var projectImplementerIds = unitWork.ProjectImplementersRepository.GetProjection(i => i.ProjectId == project.Id, i => i.ImplementerId);
                var orgIds = projectFunderIds.Union(projectImplementerIds).ToList();
                if (!orgIds.Contains(user.OrganizationId))
                {
                    orgIds.Add(user.OrganizationId);
                }

                var userEmails = unitWork.UserRepository.GetProjection(u => u.UserType == UserTypes.Standard && orgIds.Contains(u.OrganizationId) && u.Id != user.Id, u => u.Email);
                //var adminEmails = unitWork.UserRepository.GetProjection(u => u.UserType == UserTypes.Manager && u.Id != user.Id, u => u.Email);
                //var allEmails = userEmails.Union(adminEmails);

                List <EmailAddress> usersEmailList = new List <EmailAddress>();
                foreach (var email in userEmails)
                {
                    usersEmailList.Add(new EmailAddress()
                    {
                        Email = email
                    });
                }

                if (usersEmailList.Count > 0)
                {
                    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 = "";
                    string projectTitle = "<h5>Project title: " + project.Title + "</h5>";
                    string requestedBy  = "<h5>Requested by: " + user.Email + " (" + user.Organization.OrganizationName + ")</h5>";
                    var    emailMessage = unitWork.EmailMessagesRepository.GetOne(m => m.MessageType == EmailMessageType.ProjectDeletionRequest);
                    if (emailMessage != null)
                    {
                        subject       = emailMessage.Subject;
                        message       = projectTitle + requestedBy + emailMessage.Message;
                        footerMessage = emailMessage.FooterMessage;
                    }
                    IEmailHelper emailHelper = new EmailHelper(smtpSettingsModel.AdminEmail, smtpSettings.SenderName, smtpSettingsModel);
                    emailHelper.SendEmailToUsers(usersEmailList, subject, "", message, footerMessage);
                }
                return(response);
            }
        }
示例#7
0
        public ActionResponse UnApproveMembershipRequest(int userId, int projectId, int funderId, int ownerId, MembershipTypes membershipType)
        {
            using (var unitWork = new UnitOfWork(context))
            {
                ActionResponse response = new ActionResponse();
                IMessageHelper mHelper;
                UserTypes      userType = UserTypes.Standard;

                var actionUser = unitWork.UserRepository.GetOne(u => u.Id == ownerId);
                if (actionUser != null)
                {
                    userType = actionUser.UserType;
                }

                if (userType != UserTypes.Manager && userType != UserTypes.SuperAdmin)
                {
                    var isUserOwner = unitWork.ProjectRepository.GetOne(p => (p.Id == projectId && p.CreatedById == ownerId));
                    if (isUserOwner == null)
                    {
                        var isFunderOwner      = unitWork.ProjectFundersRepository.GetOne(f => f.FunderId == funderId && f.ProjectId == projectId);
                        var isImplementerOwner = unitWork.ProjectImplementersRepository.GetOne(i => i.ImplementerId == funderId && i.ProjectId == projectId);

                        if (isFunderOwner == null && isImplementerOwner == null)
                        {
                            mHelper          = new MessageHelper();
                            response.Message = mHelper.GetInvalidFunderApprovalMessage();
                            response.Success = false;
                            return(response);
                        }
                    }
                }

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

                var project = unitWork.ProjectRepository.GetOne(p => p.Id == projectId);
                if (project == null)
                {
                    mHelper          = new MessageHelper();
                    response.Message = mHelper.GetNotFound("Project");
                    response.Success = false;
                    return(response);
                }

                var isRequestExists = unitWork.ProjectMembershipRepository.GetOne(r => r.ProjectId == projectId && r.UserId == user.Id && r.MembershipType == membershipType);
                if (isRequestExists == null)
                {
                    mHelper          = new MessageHelper();
                    response.Message = mHelper.GetNotFound("Membership Request");
                    response.Success = false;
                    return(response);
                }
                unitWork.ProjectMembershipRepository.Delete(isRequestExists);
                unitWork.Save();

                //Send status email
                string requestedProject            = project.Title;
                List <EmailAddress> usersEmailList = new List <EmailAddress>();
                usersEmailList.Add(new EmailAddress()
                {
                    Email = user.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.ProjectPermissionDenied);
                    if (emailMessage != null)
                    {
                        subject       = emailMessage.Subject;
                        message       = emailMessage.Message;
                        footerMessage = emailMessage.FooterMessage;
                    }

                    mHelper  = new MessageHelper();
                    message += mHelper.ProjectPermissionDenied(requestedProject);
                    IEmailHelper emailHelper = new EmailHelper(smtpSettingsModel.AdminEmail, smtpSettings.SenderName, smtpSettingsModel);
                    emailHelper.SendEmailToUsers(usersEmailList, subject, "", message, footerMessage);
                }
                return(response);
            }
        }
示例#8
0
        public async Task <ActionResponse> ApproveMembershipRequestAsync(int userId, int projectId, int funderId, int ownerId, MembershipTypes membershipType)
        {
            using (var unitWork = new UnitOfWork(context))
            {
                ActionResponse response = new ActionResponse();
                IMessageHelper mHelper;
                UserTypes      userType = UserTypes.Standard;

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

                var actionUser = unitWork.UserRepository.GetOne(u => u.Id == ownerId);
                if (actionUser != null)
                {
                    userType = actionUser.UserType;
                }

                if (userType != UserTypes.Manager && userType != UserTypes.SuperAdmin)
                {
                    var isUserOwner = unitWork.ProjectRepository.GetOne(p => (p.Id == projectId && p.CreatedById == ownerId));
                    if (isUserOwner == null)
                    {
                        var isFunderOwner      = unitWork.ProjectFundersRepository.GetOne(f => f.FunderId == funderId && f.ProjectId == projectId);
                        var isImplementerOwner = unitWork.ProjectImplementersRepository.GetOne(i => i.ImplementerId == funderId && i.ProjectId == projectId);

                        if (isFunderOwner == null && isImplementerOwner == null)
                        {
                            mHelper          = new MessageHelper();
                            response.Message = mHelper.GetInvalidFunderApprovalMessage();
                            response.Success = false;
                            return(response);
                        }
                    }
                }

                var project = unitWork.ProjectRepository.GetOne(p => p.Id == projectId);
                if (user == null)
                {
                    mHelper          = new MessageHelper();
                    response.Message = mHelper.GetNotFound("Project");
                    response.Success = false;
                    return(response);
                }

                var userOrganization = unitWork.OrganizationRepository.GetOne(o => o.Id == user.OrganizationId);
                if (userOrganization == null)
                {
                    mHelper          = new MessageHelper();
                    response.Message = mHelper.GetNotFound("User Organization");
                    response.Success = false;
                    return(response);
                }

                IQueryable <EFProjectMembershipRequests> requests = null;
                if (userType == UserTypes.Manager || userType == UserTypes.SuperAdmin)
                {
                    requests = unitWork.ProjectMembershipRepository.GetManyQueryable(r => r.ProjectId == projectId && r.MembershipType == membershipType);
                }
                else
                {
                    requests = unitWork.ProjectMembershipRepository.GetManyQueryable(r => r.ProjectId == projectId && r.OrganizationId == user.OrganizationId && r.MembershipType == membershipType);
                    if (requests.Count() == 0)
                    {
                        mHelper          = new MessageHelper();
                        response.Message = mHelper.GetNotFound("Membership Request");
                        response.Success = false;
                        return(response);
                    }
                }

                var strategy = context.Database.CreateExecutionStrategy();
                await strategy.ExecuteAsync(async() =>
                {
                    using (var transaction = context.Database.BeginTransaction())
                    {
                        var approvalRequest = (from r in requests
                                               select r).FirstOrDefault();

                        foreach (var request in requests)
                        {
                            request.IsApproved = true;
                            unitWork.ProjectMembershipRepository.Update(request);
                        }
                        await unitWork.SaveAsync();

                        if (approvalRequest.MembershipType == MembershipTypes.Funder)
                        {
                            var projectFunder = unitWork.ProjectFundersRepository.GetOne(f => f.ProjectId == projectId && f.FunderId == user.OrganizationId);
                            if (projectFunder == null)
                            {
                                unitWork.ProjectFundersRepository.Insert(new EFProjectFunders()
                                {
                                    ProjectId = projectId,
                                    FunderId  = user.OrganizationId
                                });
                                await unitWork.SaveAsync();
                            }
                        }
                        else if (approvalRequest.MembershipType == MembershipTypes.Implementer)
                        {
                            var projectImplementer = unitWork.ProjectImplementersRepository.GetOne(i => i.ProjectId == projectId && i.ImplementerId == user.OrganizationId);
                            if (projectImplementer == null)
                            {
                                unitWork.ProjectImplementersRepository.Insert(new EFProjectImplementers()
                                {
                                    ProjectId     = projectId,
                                    ImplementerId = user.OrganizationId
                                });
                                await unitWork.SaveAsync();
                            }
                        }
                        transaction.Commit();
                    }
                });

                //Send status email
                string requestedProject            = project.Title;
                List <EmailAddress> usersEmailList = new List <EmailAddress>();
                usersEmailList.Add(new EmailAddress()
                {
                    Email = user.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.ProjectPermissionGranted);
                    if (emailMessage != null)
                    {
                        subject       = emailMessage.Subject;
                        message       = emailMessage.Message;
                        footerMessage = emailMessage.FooterMessage;
                    }

                    mHelper  = new MessageHelper();
                    message += mHelper.ProjectPermissionGranted(requestedProject);
                    IEmailHelper emailHelper = new EmailHelper(smtpSettingsModel.AdminEmail, smtpSettings.SenderName, smtpSettingsModel);
                    emailHelper.SendEmailToUsers(usersEmailList, subject, "", message, footerMessage);
                }
                return(response);
            }
        }
示例#9
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);
            }
        }
示例#10
0
        public async Task <ActionResponse> DeleteAsync(int id, int newId)
        {
            ActionResponse response = new ActionResponse();
            IMessageHelper mHelper;

            using (var unitWork = new UnitOfWork(context))
            {
                EFSector sector    = null;
                EFSector newSector = null;
                var      sectors   = await unitWork.SectorRepository.GetWithIncludeAsync(s => (s.Id == id || s.Id == newId), new string[] { "SectorType" });

                if (sectors.Count() < 2 && newId != 0)
                {
                    mHelper          = new MessageHelper();
                    response.Success = false;
                    response.Message = mHelper.GetNotFound("Sector");
                    return(await Task <ActionResponse> .Run(() => response).ConfigureAwait(false));
                }

                var sectorToDelete = (from s in sectors
                                      where s.Id == id
                                      select s).FirstOrDefault();

                if (sectorToDelete.IsUnAttributed)
                {
                    mHelper          = new MessageHelper();
                    response.Message = mHelper.UnattributedCannotBeDeleted("Sector");
                    response.Success = false;
                    return(response);
                }

                if (sectorToDelete != null)
                {
                    if (sectorToDelete.SectorType.IsSourceType)
                    {
                        mHelper          = new MessageHelper();
                        response.Message = mHelper.GetCannotBeDeleted("Sector imported from source");
                        response.Success = false;
                        return(response);
                    }
                }

                var projectSectors = await unitWork.ProjectSectorsRepository.GetManyQueryableAsync(s => (s.SectorId == id || s.SectorId == newId));

                var projectIds = (from s in projectSectors
                                  select s.ProjectId).Distinct().ToList <int>();
                if (projectIds.Count() > 0 && newId == 0)
                {
                    mHelper          = new MessageHelper();
                    response.Success = false;
                    response.Message = mHelper.GetDependentProjectsOnSectorMessage();
                    return(await Task <ActionResponse> .Run(() => response).ConfigureAwait(false));
                }

                var sectorsInDb = (from s in projectSectors
                                   select new SectorsKeyView()
                {
                    SectorId = s.SectorId,
                    ProjectId = s.ProjectId
                });
                projectSectors = (from s in projectSectors
                                  where s.SectorId == id
                                  select s);

                sector = (from s in sectors
                          where s.Id == id
                          select s).FirstOrDefault();

                newSector = (from s in sectors
                             where s.Id == newId
                             select s).FirstOrDefault();

                var           projects     = unitWork.ProjectRepository.GetWithInclude(p => projectIds.Contains(p.Id), new string[] { "CreatedBy" });
                List <string> projectNames = (from p in projects
                                              select p.Title).ToList <string>();
                var emails = (from p in projects
                              select p.CreatedBy.Email);
                try
                {
                    var strategy = context.Database.CreateExecutionStrategy();
                    await strategy.ExecuteAsync(async() =>
                    {
                        using (var transaction = context.Database.BeginTransaction())
                        {
                            List <EFProjectSectors> sectorsList = new List <EFProjectSectors>();
                            foreach (var projectSector in projectSectors)
                            {
                                var isExists = (from s in sectorsList
                                                where s.SectorId == newId && s.ProjectId == projectSector.ProjectId
                                                select s).FirstOrDefault();
                                var isExistsInDb = (from s in sectorsInDb
                                                    where s.SectorId == newId && s.ProjectId == projectSector.ProjectId
                                                    select s).FirstOrDefault();

                                if (isExists == null && isExistsInDb == null)
                                {
                                    sectorsList.Add(new EFProjectSectors()
                                    {
                                        SectorId        = newId,
                                        ProjectId       = projectSector.ProjectId,
                                        FundsPercentage = projectSector.FundsPercentage
                                    });
                                }
                                unitWork.ProjectSectorsRepository.Delete(projectSector);
                            }
                            await unitWork.SaveAsync();

                            unitWork.ProjectSectorsRepository.InsertMultiple(sectorsList);
                            await unitWork.SaveAsync();

                            unitWork.SectorRepository.Delete(sector);
                            await unitWork.SaveAsync();
                            transaction.Commit();

                            if (projectNames.Count > 0)
                            {
                                var users = unitWork.UserRepository.GetManyQueryable(u => u.UserType == UserTypes.Manager || u.UserType == UserTypes.SuperAdmin);
                                List <EmailAddress> emailAddresses = new List <EmailAddress>();
                                foreach (var user in users)
                                {
                                    emailAddresses.Add(new EmailAddress()
                                    {
                                        Email = user.Email
                                    });
                                }

                                foreach (var email in emails)
                                {
                                    var isEmailExists = (from e in emailAddresses
                                                         where e.Email.Equals(email, StringComparison.OrdinalIgnoreCase)
                                                         select e).FirstOrDefault();

                                    if (isEmailExists == null)
                                    {
                                        emailAddresses.Add(new EmailAddress()
                                        {
                                            Email = email
                                        });
                                    }
                                }

                                if (emailAddresses.Count > 0)
                                {
                                    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 subject   = "", message = "", footerMessage = "";
                                    var emailMessage = unitWork.EmailMessagesRepository.GetOne(m => m.MessageType == EmailMessageType.ChangedMappingEffectedProject);
                                    if (emailMessage != null)
                                    {
                                        subject       = emailMessage.Subject;
                                        message       = emailMessage.Message;
                                        footerMessage = emailMessage.FooterMessage;
                                    }

                                    mHelper = new MessageHelper();
                                    string oldSectorName = sector != null ? sector.SectorName : null;
                                    string newSectorName = newSector != null ? newSector.SectorName : null;
                                    message += mHelper.ChangedMappingAffectedProjectsMessage(projectNames, oldSectorName, newSectorName);
                                    IEmailHelper emailHelper = new EmailHelper(smtpSettingsModel.AdminEmail, smtpSettings.SenderName, smtpSettingsModel);
                                    emailHelper.SendEmailToUsers(emailAddresses, subject, "", message);
                                }
                            }
                        }
                    });
                }
                catch (Exception ex)
                {
                    response.Success = false;
                    response.Message = ex.Message;
                }
            }
            return(await Task <ActionResponse> .Run(() => response).ConfigureAwait(false));
        }