private async Task <string> CreateTeamAndChannelsAsync(Opportunity opportunity, string requestId = "") { _logger.LogError($"RequestId: {requestId} - createTeamAndChannels Started"); try { //create team var responce = await _graphTeamsAppService.CreateTeamAsync(opportunity.DisplayName, requestId); var groupID = responce["id"].ToString(); //Get general channel id dynamic generalChannelObj = await _graphTeamsAppService.ListChannelAsync(groupID); string generalChannelId = generalChannelObj.value[0].id.ToString(); foreach (var process in opportunity.Content.DealType.ProcessList.Where(x => !x.Channel.Equals("none", StringComparison.OrdinalIgnoreCase))) { await _graphTeamsAppService.CreateChannelAsync(groupID, process.Channel, process.Channel + " Channel"); } try { //Vault is no longer using so we will comment out this in the near future Guard.Against.NullOrEmpty(await _azureKeyVaultService.GetValueFromVaultAsync(VaultKeys.Upn), "CreateWorkflowAsync_Admin_Ups Null or empty", requestId); var responseJson = await _graphUserAppService.AddGroupOwnerAsync(await _azureKeyVaultService.GetValueFromVaultAsync(VaultKeys.Upn), groupID); var response = await _graphUserAppService.AddGroupMemberAsync(await _azureKeyVaultService.GetValueFromVaultAsync(VaultKeys.Upn), groupID); } catch (Exception ex) { _logger.LogError($"RequestId: {requestId} - CreateTeamAndChannels_AddAppToTeamAsync Service Exception: {ex}"); } // Invoke Dynamics webhook setup // TODO: this condition is temporarly added to the OpportunityFactory in order to avoid invoking the DynamicsController unless the Dynamics configuration // if available, otherwise it blows up in the ctor. if (!string.IsNullOrWhiteSpace(opportunity.Reference)) { try { // Call to AddIn helper once team has been created await _addInHelper.CallAddInWebhookAsync(opportunity, requestId); } catch (Exception ex) { _logger.LogError($"RequestId: {requestId} -_addInHelper.CallAddInWebhookAsync(opportunity, requestId): {ex}"); } } // Activate Document Id Service await _addInHelper.ActivateDocumentId(opportunity); return(generalChannelId); } catch (Exception ex) { _logger.LogError($"RequestId: {requestId} - CreateTeamAndChannels Service Exception: {ex}"); throw new ResponseException($"RequestId: {requestId} - CreateTeamAndChannels Service Exception: {ex}"); } }
public async Task <Opportunity> UpdateWorkflowAsync(Opportunity opportunity, string requestId = "") { try { var initialState = opportunity.Metadata.OpportunityState; if (opportunity.Metadata.OpportunityState != OpportunityState.Creating) { if (opportunity.Content.CustomerDecision.Approved) { opportunity.Metadata.OpportunityState = OpportunityState.Accepted; } try { opportunity = await MoveTempFileToTeamAsync(opportunity, requestId); } catch (Exception ex) { _logger.LogError($"RequestId: {requestId} - UpdateWorkflowAsync_MoveTempFileToTeam Service Exception: {ex}"); } // Add / update members // Get Group id //var opportunityName = opportunity.DisplayName.Replace(" ", ""); var opportunityName = WebUtility.UrlEncode(opportunity.DisplayName); var options = new List <QueryParam>(); options.Add(new QueryParam("filter", $"startswith(displayName,'{opportunityName}')")); var groupIdJson = await _graphUserAppService.GetGroupAsync(options, "", requestId); dynamic jsonDyn = groupIdJson; var group = String.Empty; if (groupIdJson.HasValues) { group = jsonDyn.value[0].id.ToString(); } // add to team group var teamMembersComplete = 0; var isLoanOfficerSelected = false; foreach (var item in opportunity.Content.TeamMembers) { var groupID = group; var userId = item.Id; var oItem = item; if (item.AssignedRole.DisplayName == "RelationshipManager") { // In case an admin or background workflow will trigger this update after team/channels are created, relationship manager should also be added as owner try { Guard.Against.NullOrEmpty(item.Id, $"UpdateWorkflowAsync_{item.AssignedRole.DisplayName} Id NullOrEmpty", requestId); var responseJson = await _graphUserAppService.AddGroupOwnerAsync(userId, groupID, requestId); } catch (Exception ex) { _logger.LogError($"RequestId: {requestId} - userId: {userId} - UpdateWorkflowAsync_AddGroupOwnerAsync_{item.AssignedRole.DisplayName} error in CreateWorkflowAsync: {ex}"); } } else if (item.AssignedRole.DisplayName == "LoanOfficer") { if (!String.IsNullOrEmpty(item.Id)) { isLoanOfficerSelected = true; //Reltionship manager should be set to complete if loan officer is selected } try { Guard.Against.NullOrEmpty(item.Id, $"UpdateWorkflowAsync_{item.AssignedRole.DisplayName} Id NullOrEmpty", requestId); var responseJson = await _graphUserAppService.AddGroupOwnerAsync(userId, groupID, requestId); } catch (Exception ex) { _logger.LogError($"RequestId: {requestId} - userId: {userId} - UpdateWorkflowAsync_AddGroupOwnerAsync_{item.AssignedRole.DisplayName} error in CreateWorkflowAsync: {ex}"); } } else { if (!String.IsNullOrEmpty(item.Fields.UserPrincipalName)) { teamMembersComplete = teamMembersComplete + 1; try { Guard.Against.NullOrEmpty(item.Id, $"UpdateWorkflowAsync_{item.AssignedRole.DisplayName} Id NullOrEmpty", requestId); var responseJson = await _graphUserAppService.AddGroupMemberAsync(userId, groupID, requestId); } catch (Exception ex) { _logger.LogError($"RequestId: {requestId} - userId: {userId} - UpdateWorkflowAsync_AddGroupMemberAsync_{item.AssignedRole.DisplayName} error in CreateWorkflowAsync: {ex}"); } } } } //Update status of team members var oppCheckLists = opportunity.Content.Checklists.ToList(); var roleMappings = (await _roleMappingRepository.GetAllAsync(requestId)).ToList(); //TODO: LinQ var updatedTeamlist = new List <TeamMember>(); foreach (var item in opportunity.Content.TeamMembers) { var oItem = item; oItem.Status = ActionStatus.NotStarted; if (opportunity.Content.CustomerDecision.Approved) { oItem.Status = ActionStatus.Completed; } else { var roleMap = roleMappings.Find(x => x.RoleName == item.AssignedRole.DisplayName); if (item.AssignedRole.DisplayName != "LoanOfficer" && item.AssignedRole.DisplayName != "RelationshipManager") { if (roleMap != null) { _logger.LogInformation($"RequestId: {requestId} - UpdateOpportunityAsync teamMember status sync with checklist status RoleName: {roleMap.RoleName}"); var checklistItm = oppCheckLists.Find(x => x.ChecklistChannel == roleMap.Channel); if (checklistItm != null) { _logger.LogInformation($"RequestId: {requestId} - UpdateOpportunityAsync teamMember status sync with checklist status: {checklistItm.ChecklistStatus.Name}"); oItem.Status = checklistItm.ChecklistStatus; } } } else if (item.AssignedRole.DisplayName == "RelationshipManager") { var exisitngLoanOfficers = ((opportunity.Content.TeamMembers).ToList()).Find(x => x.AssignedRole.DisplayName == "LoanOfficer"); if (exisitngLoanOfficers == null) { oItem.Status = ActionStatus.InProgress; } else { oItem.Status = ActionStatus.Completed; } } else if (item.AssignedRole.DisplayName == "LoanOfficer") { var teamList = ((opportunity.Content.TeamMembers).ToList()).FindAll(x => x.AssignedRole.DisplayName != "LoanOfficer" && x.AssignedRole.DisplayName != "RelationshipManager"); if (teamList != null) { var expectedTeam = roleMappings.FindAll(x => x.RoleName != "LoanOfficer" && x.RoleName != "RelationshipManager" && x.RoleName != "Administrator"); if (expectedTeam != null) { if (teamList.Count != 0) { oItem.Status = ActionStatus.InProgress; } if (teamList.Count >= expectedTeam.Count) { oItem.Status = ActionStatus.Completed; } } } } } updatedTeamlist.Add(oItem); } opportunity.Content.TeamMembers = updatedTeamlist; } // Send notification _logger.LogInformation($"RequestId: {requestId} - UpdateWorkflowAsync initialState: {initialState.Name} - {opportunity.Metadata.OpportunityState.Name}"); if (initialState.Value != opportunity.Metadata.OpportunityState.Value) { try { _logger.LogInformation($"RequestId: {requestId} - CreateWorkflowAsync sendNotificationCardAsync opportunity state change notification."); var sendTo = UserProfile.Empty; var sendNotificationCard = await _cardNotificationService.sendNotificationCardAsync(opportunity, sendTo, $"Opportunity state for {opportunity.DisplayName} has been changed to {opportunity.Metadata.OpportunityState.Name}", requestId); } catch (Exception ex) { _logger.LogError($"RequestId: {requestId} - CreateWorkflowAsync sendNotificationCardAsync OpportunityState error: {ex}"); } } // Delete empty ChecklistItems //opportunity.Content.Checklists = await RemoveEmptyFromChecklist(opportunity.Content.Checklists, requestId); return(opportunity); } catch (Exception ex) { _logger.LogError($"RequestId: {requestId} - UpdateWorkflowAsync Service Exception: {ex}"); throw new ResponseException($"RequestId: {requestId} - UpdateWorkflowAsync Service Exception: {ex}"); } }
private async Task <string> CreateTeamAndChannelsAsync(Opportunity opportunity, string requestId = "") { _logger.LogError($"RequestId: {requestId} - createTeamAndChannels Started"); try { //create team var responce = await _graphTeamsAppService.CreateTeamAsync(opportunity.DisplayName, requestId); //get Group ID bool check = true; dynamic jsonDyn = null; var opportunityName = WebUtility.UrlEncode(opportunity.DisplayName); var options = new List <QueryParam>(); options.Add(new QueryParam("filter", $"startswith(displayName,'{opportunityName}')")); while (check) { var groupIdJson = await _graphUserAppService.GetGroupAsync(options, "", requestId); jsonDyn = groupIdJson; JArray jsonArray = JArray.Parse(jsonDyn["value"].ToString()); if (jsonArray.Count() > 0) { if (!String.IsNullOrEmpty(jsonDyn.value[0].id.ToString())) { check = false; } } } var groupID = String.Empty; groupID = jsonDyn.value[0].id.ToString(); //Get general channel id var generalChannel = await _graphTeamsAppService.ListChannelAsync(groupID); dynamic generalChannelObj = generalChannel; string generalChannelId = generalChannelObj.value[0].id.ToString(); foreach (var process in opportunity.Content.DealType.ProcessList) { if (process.Channel.ToLower() != "none") { await _graphTeamsAppService.CreateChannelAsync(groupID, process.Channel, process.Channel + " Channel"); } } //adding app is currently not supported this code is there so that we can add the app to the team once //graph api supports usercontext for this functionality try { //Vault is no longer using so we will comment out this in the near future Guard.Against.NullOrEmpty(await _azureKeyVaultService.GetValueFromVaultAsync(VaultKeys.Upn), "CreateWorkflowAsync_Admin_Ups Null or empty", requestId); var responseJson = await _graphUserAppService.AddGroupOwnerAsync(await _azureKeyVaultService.GetValueFromVaultAsync(VaultKeys.Upn), groupID); var response = await _graphUserAppService.AddGroupMemberAsync(await _azureKeyVaultService.GetValueFromVaultAsync(VaultKeys.Upn), groupID); } catch (Exception ex) { _logger.LogError($"RequestId: {requestId} - CreateTeamAndChannels_AddAppToTeamAsync Service Exception: {ex}"); } //adding addin to the app try { await _graphTeamsOnBehalfService.AddAppToTeamAsync(groupID); } catch (Exception ex) { _logger.LogError($"RequestId: {requestId} - CreateTeamAndChannels_AddAppToTeamAsync Service Exception: {ex}"); } // Invoke Dynamics webhook setup // TODO: this condition is temporarly added to the OpportunityFactory in order to avoid invoking the DynamicsController unless the Dynamics configuration // if available, otherwise it blows up in the ctor. if (!string.IsNullOrWhiteSpace(opportunity.Reference)) { try { // Call to AddIn helper once team has been created await _addInHelper.CallAddInWebhookAsync(opportunity, requestId); } catch (Exception ex) { _logger.LogError($"RequestId: {requestId} -_addInHelper.CallAddInWebhookAsync(opportunity, requestId): {ex}"); } } // Activate Document Id Service await _addInHelper.ActivateDocumentId(opportunity); return(generalChannelId); } catch (Exception ex) { _logger.LogError($"RequestId: {requestId} - CreateTeamAndChannels Service Exception: {ex}"); throw new ResponseException($"RequestId: {requestId} - CreateTeamAndChannels Service Exception: {ex}"); } }