Skip to content

Commit

Permalink
Merge pull request #359 from Promact/removeUnncessarySlackEvent
Browse files Browse the repository at this point in the history
Removed unnecessary slack events.
  • Loading branch information
Ankit Bhanvadia committed Jun 1, 2017
2 parents 2be8235 + a9e8d3b commit 0789b0e
Show file tree
Hide file tree
Showing 17 changed files with 129 additions and 410 deletions.
@@ -1,5 +1,4 @@
using Promact.Erp.DomainModel.ApplicationClass;
using Promact.Erp.DomainModel.ApplicationClass.SlackRequestAndResponse;
using Promact.Erp.DomainModel.Models;
using System.Threading.Tasks;

Expand All @@ -16,31 +15,22 @@ public interface IOAuthLoginRepository
/// <returns>user information</returns>
Task<ApplicationUser> AddNewUserFromExternalLoginAsync(string email, string refreshToken, string userId);


/// <summary>
/// Method to get OAuth Server's app information
/// </summary>
/// <param name="refreshToken"></param>
/// <returns>Oauth</returns>
OAuthApplication ExternalLoginInformation(string refreshToken);


/// <summary>
/// Method to add Slack Users,channels and groups information
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
Task AddSlackUserInformationAsync(string code);

/// <summary>
/// Method to update slack user table when there is any changes in slack
/// </summary>
/// <param name="slackEvent"></param>
Task SlackEventUpdateAsync(SlackEventApiAC slackEvent);

/// <summary>
/// Method to update slack channel table when a channel is added or updated in team.
/// </summary>
/// <param name="slackEvent"></param>
Task SlackChannelAddAsync(SlackEventApiAC slackEvent);

/// <summary>
/// Method check user slackid is exists or ot
Expand Down
Expand Up @@ -15,7 +15,6 @@
using System.Threading.Tasks;



namespace Promact.Core.Repository.ExternalLoginRepository
{
public class OAuthLoginRepository : IOAuthLoginRepository
Expand Down Expand Up @@ -56,6 +55,7 @@ public class OAuthLoginRepository : IOAuthLoginRepository
#endregion

#region Public Methods

/// <summary>
/// Method to add a new user in Application user table and store user's external login information in UserLogin table
/// </summary>
Expand All @@ -80,6 +80,7 @@ public async Task<ApplicationUser> AddNewUserFromExternalLoginAsync(string email
return userInfo;
}


/// <summary>
/// Method to get OAuth Server's app information
/// </summary>
Expand All @@ -97,6 +98,7 @@ public OAuthApplication ExternalLoginInformation(string refreshToken)
return oAuth;
}


/// <summary>
/// Method to add/update Slack User,channels and groups information
/// </summary>
Expand Down Expand Up @@ -187,7 +189,6 @@ public async Task AddSlackUserInformationAsync(string code)
}
else
throw new SlackAuthorizeException(_stringConstant.SlackAuthError + slackUsers.ErrorMessage);

}


Expand All @@ -212,50 +213,6 @@ private async Task AddChannelGroupAsync(SlackChannelDetails slackChannelDetails)
slackChannel.Name = slackChannelDetails.Name;
await _slackChannelRepository.UpdateSlackChannelAsync(slackChannel);
}

}


/// <summary>
/// Method to update slack user table when there is any changes in slack
/// </summary>
/// <param name="slackEvent"></param>
public async Task SlackEventUpdateAsync(SlackEventApiAC slackEvent)
{
SlackUserDetails user = await _slackUserDetailsRepository.FirstOrDefaultAsync(x => x.UserId == slackEvent.Event.User.UserId);
_logger.Debug("User details : " + user);
if (user == null)
{
_logger.Debug("SlackUserRepository - AddSlackUserAsync");
await _slackUserRepository.AddSlackUserAsync(slackEvent.Event.User);
}
}


/// <summary>
/// Method to update slack channel table when a channel is added or updated in team.
/// </summary>
/// <param name="slackEvent"></param>
public async Task SlackChannelAddAsync(SlackEventApiAC slackEvent)
{
SlackChannelDetails channel = await _slackChannelDetails.FirstOrDefaultAsync(x => x.ChannelId == slackEvent.Event.Channel.ChannelId);
_logger.Debug("Channel : " + channel);
if (channel == null)
{
_loggerSlackEvent.Debug("New channel adding");
slackEvent.Event.Channel.CreatedOn = DateTime.UtcNow;
_slackChannelDetails.Insert(slackEvent.Event.Channel);
await _slackChannelDetails.SaveChangesAsync();
_loggerSlackEvent.Debug("Channel added successfully");
}
else
{
_loggerSlackEvent.Debug("Updating channel");
channel.Name = slackEvent.Event.Channel.Name;
_slackChannelDetails.Update(channel);
await _slackChannelDetails.SaveChangesAsync();
_loggerSlackEvent.Debug("Channel updated successfully");
}
}


Expand Down
Expand Up @@ -13,11 +13,11 @@ public interface ILeaveManagementBotRepository
Task<string> ProcessLeaveAsync(string slackUserId, string answer);

/// <summary>
/// method to convert slack user id to slack user's name - SS
/// method to convert slack user regex id to slack id - SS
/// </summary>
/// <param name="message">message from slack</param>
/// <param name="userFound">if user is not found</param>
/// <returns>message after conversation</returns>
string ProcessToConvertSlackIdToSlackUserName(string message, out bool userFound);
string ProcessToConvertSlackUserRegexIdToSlackId(string message, out bool userFound);
}
}
Expand Up @@ -6,6 +6,7 @@
using Promact.Core.Repository.OauthCallsRepository;
using Promact.Core.Repository.SlackUserRepository;
using Promact.Erp.DomainModel.ApplicationClass;
using Promact.Erp.DomainModel.ApplicationClass.SlackRequestAndResponse;
using Promact.Erp.DomainModel.DataRepository;
using Promact.Erp.DomainModel.Models;
using Promact.Erp.Util.StringLiteral;
Expand Down Expand Up @@ -91,7 +92,13 @@ public async Task<string> ProcessLeaveAsync(string slackUserId, string answer)
{
// if leave list for other user
if (slackText.Count > 2)
replyText = await GetLeaveListAsync(user.Id, slackText[2]);
{
var employee = await _slackUserRepository.GetByIdAsync(slackText[2]);
if (employee != null)
replyText = await GetLeaveListAsync(user.Id, employee);
else
replyText = _stringConstant.PointUserUsingAtTheRate;
}
// if leave list for user itself
else
replyText = await GetLeaveListAsync(user.Id, null);
Expand Down Expand Up @@ -156,7 +163,7 @@ public async Task<string> ProcessLeaveAsync(string slackUserId, string answer)
/// <param name="message">message from slack</param>
/// <param name="userNotFound">if user is not found</param>
/// <returns>message after conversation</returns>
public string ProcessToConvertSlackIdToSlackUserName(string message, out bool userNotFound)
public string ProcessToConvertSlackUserRegexIdToSlackId(string message, out bool userNotFound)
{
// regex pattern for slack message of user id
Regex pattern = new Regex(@_stringConstant.UserIdPattern);
Expand All @@ -171,7 +178,7 @@ public string ProcessToConvertSlackIdToSlackUserName(string message, out bool us
if (applicant != null)
{
userNotFound = false;
message = message.Replace(match.Value, applicant.Name);
message = message.Replace(match.Value, applicant.UserId);
}
// if user slack detail is not found then message will be send for user not found
else
Expand Down Expand Up @@ -557,44 +564,36 @@ private async Task<string> AddLeaveSendMailDetailsAsync(string answer, Temporary
/// <param name="userId">user's Id</param>
/// <param name="empployeeName">employee name who's leave will displayed</param>
/// <returns>reply to be send</returns>
private async Task<string> GetLeaveListAsync(string userId, string employeeName)
private async Task<string> GetLeaveListAsync(string userId, SlackUserDetailAc employeeSlackDetails)
{
string replyText = string.Empty;
List<LeaveRequest> leaves = new List<LeaveRequest>();
// if null then user want to get own leave list
if (employeeName != null)
if (employeeSlackDetails != null)
{
// user's username
var username = (await _userManager.FindByIdAsync(userId)).UserName;
// check if user is admin or not
if (await _oauthCallRepository.UserIsAdminAsync(userId, (await _attachmentRepository.UserAccessTokenAsync(username))))
{
// check if user exist or not
// employee slack details whose leave detail user wan to get
var employeeSlackDetails = await _slackUserRepository.GetBySlackNameAsync(employeeName);
var employee = await _userManager.Users.FirstOrDefaultAsync(x => x.SlackUserId == employeeSlackDetails.UserId);
// check if user exist or not
if (employeeSlackDetails != null)
if (employee != null)
{
// employee slack details whose leave detail user wan to get
var employee = await _userManager.Users.FirstOrDefaultAsync(x => x.SlackUserId == employeeSlackDetails.UserId);
// check if user exist or not
if (employee != null)
{
// leave list of employee
leaves = _leaveRequestRepository.LeaveListByUserId(employee.Id).ToList();
// if exist then format the message
if (leaves.Any())
replyText = GetLeaveListMessageByLeaveList(leaves);
// else message will be no record found for employee
else
replyText = string.Format(_stringConstant.LeaveListForOtherErrorMessage, employeeName);
}
// message to ask employee to do login with promact oauth server
// leave list of employee
leaves = _leaveRequestRepository.LeaveListByUserId(employee.Id).ToList();
// if exist then format the message
if (leaves.Any())
replyText = GetLeaveListMessageByLeaveList(leaves);
// else message will be no record found for employee
else
replyText = _stringConstant.MessageToRequestToAddToSlackOtherUser;
replyText = string.Format(_stringConstant.LeaveListForOtherErrorMessage, employeeSlackDetails.Name);
}
// message to ask employee to do add to slack
// message to ask employee to do login with promact oauth server
else
replyText = string.Format(_stringConstant.UserNotFoundRequestToAddToSlackOtherUser, employeeName);
replyText = _stringConstant.MessageToRequestToAddToSlackOtherUser;
}
// if user is not admin then unauthorize message will be send
else
Expand Down

0 comments on commit 0789b0e

Please sign in to comment.