Skip to content

Commit

Permalink
Merge pull request #221 from Promact/Issue-#215
Browse files Browse the repository at this point in the history
Updated module for new mail setting configuration
  • Loading branch information
Ankit Bhanvadia committed Mar 3, 2017
2 parents 45c8b49 + f81f3a7 commit 6c51c4f
Show file tree
Hide file tree
Showing 23 changed files with 678 additions and 100 deletions.
40 changes: 27 additions & 13 deletions Slack.Automation/Promact.Core.Repository/Client/Client.cs
Expand Up @@ -10,11 +10,12 @@
using Promact.Erp.Util.StringConstants;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Promact.Core.Repository.EmailServiceTemplateRepository;
using Promact.Erp.Util.HttpClient;
using Promact.Erp.DomainModel.DataRepository;
using System.Linq;
using Promact.Core.Repository.MailSettingDetailsByProjectAndModule;

namespace Promact.Core.Repository.Client
{
Expand All @@ -31,14 +32,15 @@ public class Client : IClient
private readonly IEmailServiceTemplateRepository _emailTemplateRepository;
private readonly IRepository<IncomingWebHook> _incomingWebHook;
private readonly ApplicationUserManager _userManager;
private readonly IMailSettingDetailsByProjectAndModuleRepository _mailSettingDetails;
#endregion

#region Constructor
public Client(IOauthCallsRepository oauthCallRepository, IStringConstantRepository stringConstant,
IEmailService emailService, IAttachmentRepository attachmentRepository, IHttpClientService httpClientService,
IEnvironmentVariableRepository envVariableRepository, ISlackUserRepository slackUserRepository,
IEmailServiceTemplateRepository emailTemplateRepository, IRepository<IncomingWebHook> incomingWebHook,
ApplicationUserManager userManager)
ApplicationUserManager userManager, IMailSettingDetailsByProjectAndModuleRepository mailSettingDetails)
{
_stringConstant = stringConstant;
_oauthCallRepository = oauthCallRepository;
Expand All @@ -50,6 +52,7 @@ public class Client : IClient
_emailTemplateRepository = emailTemplateRepository;
_incomingWebHook = incomingWebHook;
_userManager = userManager;
_mailSettingDetails = mailSettingDetails;
}
#endregion

Expand Down Expand Up @@ -124,11 +127,12 @@ public async Task SendSickLeaveMessageToUserIncomingWebhookAsync(LeaveRequest le
if (incomingWebHook != null)
await _httpClientService.PostAsync(incomingWebHook.IncomingWebHookUrl, slashIncomingWebhookJsonText, _stringConstant.JsonContentString);
EmailApplication email = new EmailApplication();
email.To = new List<string>();
// creating email templates corresponding to leave applied
email.Body = _emailTemplateRepository.EmailServiceTemplateSickLeave(leaveRequest);
email.From = managementEmail;
email.Subject = _stringConstant.EmailSubject;
email.To = user.Email;
email.To.Add(user.Email);
_emailService.Send(email);
}
#endregion
Expand All @@ -143,24 +147,35 @@ public async Task SendSickLeaveMessageToUserIncomingWebhookAsync(LeaveRequest le
/// <param name="attachment">Attachment to be send to team leader and management</param>
private async Task GetAttachmentAndSendToTLAndManagementAsync(string userId, LeaveRequest leaveRequest, string accessToken, List<SlashAttachment> attachment)
{
var teamLeaders = await _oauthCallRepository.GetTeamLeaderUserIdAsync(userId, accessToken);
var management = await _oauthCallRepository.GetManagementUserNameAsync(accessToken);
var userDetail = await _oauthCallRepository.GetUserByUserIdAsync(userId, accessToken);
foreach (var user in management)
EmailApplication email = new EmailApplication();
email.To = new List<string>();
email.CC = new List<string>();
var listOfprojectRelatedToUser = (await _oauthCallRepository.GetListOfProjectsEnrollmentOfUserByUserIdAsync(userId, accessToken)).Select(x => x.Id).ToList();
foreach (var projectId in listOfprojectRelatedToUser)
{
teamLeaders.Add(user);
var mailsetting = await _mailSettingDetails.GetMailSettingAsync(projectId, _stringConstant.LeaveModule, userId);
email.To.AddRange(mailsetting.To);
email.CC.AddRange(mailsetting.CC);
}
foreach (var teamLeader in teamLeaders)
email.To = email.To.Distinct().ToList();
email.CC = email.CC.Distinct().ToList();
var teamLeaderIds = (await _oauthCallRepository.GetTeamLeaderUserIdAsync(userId, accessToken)).Select(x=>x.Id).ToList();
var managementIds = (await _oauthCallRepository.GetManagementUserNameAsync(accessToken)).Select(x=>x.Id);
var userEmail = (await _userManager.FindByIdAsync(userId)).Email;
teamLeaderIds.AddRange(managementIds);
foreach (var teamLeaderId in teamLeaderIds)
{
var user = await _userManager.FindByIdAsync(teamLeader.Id);
var user = await _userManager.FindByIdAsync(teamLeaderId);
var slackUser = await _slackUserRepository.GetByIdAsync(user.SlackUserId);
var incomingWebHook = await _incomingWebHook.FirstOrDefaultAsync(x => x.UserId == user.SlackUserId);
//Creating an object of SlashIncomingWebhook as this format of value required while responsing to slack
var slashIncomingWebhookText = new SlashIncomingWebhook() { Channel = _stringConstant.AtTheRate + slackUser.Name, Username = _stringConstant.LeaveBot, Attachments = attachment };
var slashIncomingWebhookJsonText = JsonConvert.SerializeObject(slashIncomingWebhookText);
if (incomingWebHook != null)
await _httpClientService.PostAsync(incomingWebHook.IncomingWebHookUrl, slashIncomingWebhookJsonText, _stringConstant.JsonContentString);
EmailApplication email = new EmailApplication();
}
if (email.To.Any())
{
if (leaveRequest.EndDate != null)
{
// creating email templates corresponding to leave applied for casual leave
Expand All @@ -171,9 +186,8 @@ private async Task GetAttachmentAndSendToTLAndManagementAsync(string userId, Lea
// creating email templates corresponding to leave applied for casual leave
email.Body = _emailTemplateRepository.EmailServiceTemplateSickLeave(leaveRequest);
}
email.From = userDetail.Email;
email.From = userEmail;
email.Subject = _stringConstant.EmailSubject;
email.To = teamLeader.Email;
_emailService.Send(email);
}
}
Expand Down
@@ -0,0 +1,18 @@
using Promact.Erp.DomainModel.ApplicationClass;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Promact.Core.Repository.MailSettingDetailsByProjectAndModule
{
public interface IMailSettingDetailsByProjectAndModuleRepository
{
/// <summary>
/// Method used to get mail setting for a module by projectId
/// </summary>
/// <param name="projectId">project Id</param>
/// <param name="module">mail setting module</param>
/// <param name="userId">user's user Id</param>
/// <returns>mail setting details</returns>
Task<MailSettingAC> GetMailSettingAsync(int projectId, string module, string userId);
}
}
@@ -0,0 +1,124 @@
using Promact.Core.Repository.AttachmentRepository;
using Promact.Core.Repository.OauthCallsRepository;
using Promact.Erp.DomainModel.ApplicationClass;
using Promact.Erp.DomainModel.DataRepository;
using Promact.Erp.DomainModel.Models;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Promact.Core.Repository.MailSettingDetailsByProjectAndModule
{
public class MailSettingDetailsByProjectAndModuleRepository : IMailSettingDetailsByProjectAndModuleRepository
{
#region Private Variables
private readonly IRepository<MailSetting> _mailSettingDataRepository;
private readonly IRepository<MailSettingMapping> _mailSettingMappingDataRepository;
private readonly IRepository<GroupEmailMapping> _groupEmailMappingDataRepository;
private readonly IRepository<Group> _groupDataRepository;
private readonly IOauthCallsRepository _oauthCallRepository;
private readonly IAttachmentRepository _attachmentRepository;
private readonly ApplicationUserManager _userManager;
#endregion

#region Constructor
public MailSettingDetailsByProjectAndModuleRepository(IRepository<MailSetting> mailSettingDataRepository,
IRepository<MailSettingMapping> mailSettingMappingDataRepository, IRepository<GroupEmailMapping> groupEmailMappingDataRepository,
IRepository<Group> groupDataRepository, IOauthCallsRepository oauthCallRepository, IAttachmentRepository attachmentRepository,
ApplicationUserManager userManager)
{
_mailSettingDataRepository = mailSettingDataRepository;
_mailSettingMappingDataRepository = mailSettingMappingDataRepository;
_groupEmailMappingDataRepository = groupEmailMappingDataRepository;
_groupDataRepository = groupDataRepository;
_oauthCallRepository = oauthCallRepository;
_attachmentRepository = attachmentRepository;
_userManager = userManager;
}
#endregion

#region Public Method
/// <summary>
/// Method used to get mail setting for a module by projectId
/// </summary>
/// <param name="projectId">project Id</param>
/// <param name="module">mail setting module</param>
/// <param name="userId">user's user Id</param>
/// <returns>mail setting details</returns>
public async Task<MailSettingAC> GetMailSettingAsync(int projectId, string module, string userId)
{
MailSettingAC mailSetting = new MailSettingAC();
mailSetting.To = new List<string>();
mailSetting.CC = new List<string>();
// check mail setting for module and project is exist or not
if (_mailSettingDataRepository.Any(x => x.ProjectId == projectId && x.Module == module))
{
var mailSettingDetails = await _mailSettingDataRepository.FirstOrDefaultAsync(x => x.ProjectId == projectId && x.SendMail && x.Module == module);
if (mailSettingDetails != null)
{
mailSetting.Id = mailSettingDetails.Id;
mailSetting.Module = mailSettingDetails.Module;
mailSetting.ProjectId = mailSettingDetails.ProjectId;
mailSetting.SendMail = mailSettingDetails.SendMail;
mailSetting.To = await GetListOfEmailByMailSettingAsync(true, mailSettingDetails.Id, userId, projectId);
mailSetting.CC = await GetListOfEmailByMailSettingAsync(false, mailSettingDetails.Id, userId, projectId);
}
}
return mailSetting;
}
#endregion

#region Private Method
/// <summary>
/// Method to get list of emails for a mail setting
/// </summary>
/// <param name="isTo">is To or CC</param>
/// <param name="mailSettingId">mail setting Id</param>
/// <param name="userId">user's user Id</param>
/// <param name="projectId">project Id</param>
/// <returns></returns>
private async Task<List<string>> GetListOfEmailByMailSettingAsync(bool isTo, int mailSettingId, string userId, int projectId)
{
List<string> emails = new List<string>();
var mailSettingMappings = (await _mailSettingMappingDataRepository.FetchAsync(x => x.MailSettingId == mailSettingId && x.IsTo == isTo)).ToList();
if (mailSettingMappings.Any())
{
foreach (var mailSetingMapping in mailSettingMappings)
{
// if mail setting have email
if (!string.IsNullOrEmpty(mailSetingMapping.Email))
emails.Add(mailSetingMapping.Email);
// if mail setting have mapping of group
else
{
// to get name of group
var groupName = (await _groupDataRepository.FirstAsync(x => x.Id == mailSetingMapping.GroupId)).Name;
// to get access token of user
var accessToken = await _attachmentRepository.UserAccessTokenAsync((await _userManager.FindByIdAsync(userId)).UserName);
switch (groupName)
{
// Dynamic group - Team Leader
case "Team Leader":
emails.AddRange((await _oauthCallRepository.GetTeamLeaderUserIdAsync(userId, accessToken)).Select(x => x.Email));
break;
// Dynamic group - Management
case "Management":
emails.AddRange((await _groupEmailMappingDataRepository.FetchAsync(x => x.GroupId == mailSetingMapping.GroupId)).Select(x => x.Email));
break;
// Dynamic group - Team Members
case "Team Members":
emails.AddRange((await _oauthCallRepository.GetAllTeamMemberByProjectIdAsync(projectId, accessToken)).Select(x => x.Email));
break;
// Static group
default:
emails.AddRange((await _groupEmailMappingDataRepository.FetchAsync(x => x.GroupId == mailSetingMapping.GroupId)).Select(x => x.Email));
break;
}
}
}
}
return emails;
}
#endregion
}
}

0 comments on commit 6c51c4f

Please sign in to comment.