public async Task HandleAsync(MemberJoinedCapabilityDomainEvent domainEvent) { var capabilities = await _capabilityRepository.GetById(domainEvent.Payload.CapabilityId); foreach (var capability in capabilities) { if (capability == null) { _logger.LogError( $"Couldn't get capability with ID {domainEvent.Payload.CapabilityId}. Can't add member {domainEvent.Payload.MemberEmail} to Slack."); return; } try { await _slackFacade.InviteToChannel( email : domainEvent.Payload.MemberEmail, channelIdentifier : capability.SlackChannelId.ToString()); } catch (SlackFacadeException ex) { _logger.LogError($"Issue with Slack API during InviteToChannel: {ex} : {ex.Message}"); } try { //TODO: Should we remove this? if (string.IsNullOrEmpty(capability.SlackUserGroupId)) { var userGroup = await _slackService.EnsureUserGroupExists(capability.Name); capability.SetUserGroupID(userGroup.Id); } await capability.AddMember(domainEvent.Payload.MemberEmail); await _capabilityRepository.Update(capability); // Add user to Slack user group: await _slackFacade.AddUserGroupUser(email : domainEvent.Payload.MemberEmail, userGroupId : capability.SlackUserGroupId); } catch (SlackFacadeException ex) { _logger.LogError($"Issue with Slack API during AddUserGroupUser: {ex} : {ex.Message}"); } } }
public async Task HandleAsync(MemberLeftCapabilityDomainEvent domainEvent) { var capabilitiesJoinedByMember = await _capabilityRepository.GetByFilter(o => o.Members.Any(m => m.Email == domainEvent.Payload.MemberEmail)); var capabilitiesBeingLeft = capabilitiesJoinedByMember.Where(o => o.Id.Equals(domainEvent.Payload.CapabilityId)).ToList(); foreach (var capability in capabilitiesBeingLeft) { await capability.RemoveMember(domainEvent.Payload.MemberEmail); await _capabilityRepository.Update(capability); if (!capabilitiesJoinedByMember.Where(c => c.Id != capability.Id).Any(o => o.SlackChannelId == capability.SlackChannelId)) { try { // Remove user from Slack channel: await _slackFacade.RemoveFromChannel( email : domainEvent.Payload.MemberEmail, channelIdentifier : capability.SlackChannelId.ToString()); } catch (SlackFacadeException ex) { _logger.LogError($"Issue with Slack API during RemoveFromChannel: {ex} : {ex.Message}"); } try { // Remove user from Slack user group: await _slackFacade.RemoveUserGroupUser( email : domainEvent.Payload.MemberEmail, userGroupId : capability.SlackUserGroupId); } catch (SlackFacadeException ex) { _logger.LogError($"Issue with Slack API during RemoveUserGroupUser: {ex} : {ex.Message}"); } // Notify user that it has been removed. await _slackFacade.SendNotificationToUser( email : domainEvent.Payload.MemberEmail, message : $"Thank you for your contributions to capability {capability.Name}.\nYou have been removed from corresponding Slack channel and user group."); } } }