예제 #1
0
        public async Task <Opportunity> CreateWorkflowAsync(Opportunity opportunity, string requestId = "")
        {
            try
            {
                _logger.LogError($"RequestId: {requestId} - Opportunityfactory_CreateWorkflowAsync CheckAccess CreateItemAsync");

                // Set initial opportunity state
                opportunity.Metadata.OpportunityState = OpportunityState.Creating;

                if (opportunity.Content.Template != null)
                {
                    if (opportunity.Content.Template.ProcessList != null)
                    {
                        if (opportunity.Content.Template.ProcessList.Count() > 1)
                        {
                            if (!opportunity.TemplateLoaded)
                            {
                                opportunity = await _teamChannelService.CreateWorkflowAsync(opportunity, requestId);

                                opportunity.Metadata.OpportunityState = OpportunityState.InProgress;
                            }
                            opportunity = await _memberService.CreateWorkflowAsync(opportunity, requestId);
                        }

                        bool checklistPass = false;
                        foreach (var item in opportunity.Content.Template.ProcessList)
                        {
                            if (item.ProcessType.ToLower() == "checklisttab" && checklistPass == false)
                            {
                                opportunity = await _checkListProcessService.CreateWorkflowAsync(opportunity, requestId);

                                checklistPass = true;
                            }
                            else if (item.ProcessType.ToLower() == "customerdecisiontab")
                            {
                                opportunity = await _customerDecisionProcessService.CreateWorkflowAsync(opportunity, requestId);
                            }
                            else if (item.ProcessType.ToLower() == "customerfeedbacktab")
                            {
                                opportunity = await _customerFeedbackProcessService.CreateWorkflowAsync(opportunity, requestId);
                            }
                            else if (item.ProcessType.ToLower() == "proposalstatustab")
                            {
                                opportunity = await _proposalStatusProcessService.CreateWorkflowAsync(opportunity, requestId);
                            }
                            else if (item.ProcessStep.ToLower() == "start process")
                            {
                                opportunity = await _startProcessService.CreateWorkflowAsync(opportunity, requestId);
                            }
                        }
                    }
                }
                else
                {
                    _logger.LogError($"RequestId: {requestId} - CreateWorkflowAsync Service Exception");
                    throw new AccessDeniedException($"RequestId: {requestId} - CreateWorkflowAsync Service Exception");
                }

                try
                {
                    opportunity = await _notesService.CreateWorkflowAsync(opportunity, requestId);

                    opportunity = await _dashboardService.CreateWorkflowAsync(opportunity, requestId);
                }
                catch
                {
                    _logger.LogError($"RequestId: {requestId} - CreateWorkflowAsync Service Exception");
                }

                return(opportunity);
            }
            catch (Exception ex)
            {
                _logger.LogError($"RequestId: {requestId} - CreateWorkflowAsync Service Exception: {ex}");
                throw new ResponseException($"RequestId: {requestId} - CreateWorkflowAsync Service Exception: {ex}");
            }
        }
        public async Task <Opportunity> CreateWorkflowAsync(Opportunity opportunity, string requestId = "")
        {
            try
            {
                // Set initial opportunity state
                opportunity.Metadata.OpportunityState = OpportunityState.Creating;

                // Remove empty sections from proposal document
                opportunity.Content.ProposalDocument.Content.ProposalSectionList = opportunity.Content.ProposalDocument.Content.ProposalSectionList.Where(x => !string.IsNullOrWhiteSpace(x.DisplayName)).ToList();

                // Delete empty ChecklistItems
                opportunity.Content.Checklists = opportunity.Content.Checklists.Where(x => x.ChecklistTaskList.Any(y => !string.IsNullOrWhiteSpace(y.Id) && !string.IsNullOrWhiteSpace(y.ChecklistItem))).ToList();

                //Granular Access : Start
                _logger.LogError($"RequestId: {requestId} - Opportunityfactory_UpdateItemAsync CheckAccess CreateItemAsync");

                // QUESTION:
                // When an opportunity is created the DealType.ProcessList is always null, then why do we have the IF below, this is done in the UpdateWorkflowAsync

                if (opportunity.Content.DealType.ProcessList != null)
                {
                    //create team and channels
                    if (await GroupIdCheckAsync(opportunity.DisplayName, requestId))
                    {
                        await CreateTeamAndChannelsAsync(opportunity, requestId);
                    }

                    if (StatusCodes.Status200OK == await _authorizationService.CheckAccessFactoryAsync(PermissionNeededTo.DealTypeWrite, requestId) ||
                        await _authorizationService.CheckAccessInOpportunityAsync(opportunity, PermissionNeededTo.Write, requestId))
                    {
                        bool checklistPass = false;
                        foreach (var item in opportunity.Content.DealType.ProcessList)
                        {
                            if (item.ProcessType.ToLower() == "checklisttab" && checklistPass == false)
                            {
                                //DashBoard Create call Start.
                                await UpdateDashBoardEntryAsync(opportunity, requestId);

                                //DashBoard Create call End.
                                opportunity = await _checkListProcessService.CreateWorkflowAsync(opportunity, requestId);

                                checklistPass = true;
                            }
                            else if (item.ProcessType.ToLower() == "customerdecisiontab")
                            {
                                opportunity = await _customerDecisionProcessService.CreateWorkflowAsync(opportunity, requestId);
                            }
                            else if (item.ProcessType.ToLower() == "proposalstatustab")
                            {
                                opportunity = await _proposalStatusProcessService.CreateWorkflowAsync(opportunity, requestId);
                            }
                            else if (item.ProcessStep.ToLower() == "start process")
                            {
                                opportunity = await _startProcessService.CreateWorkflowAsync(opportunity, requestId);
                            }
                            else if (item.ProcessStep.ToLower() == "new opportunity")
                            {
                                opportunity = await _newOpportunityProcessService.CreateWorkflowAsync(opportunity, requestId);
                            }
                        }
                    }
                    else
                    {
                        if (opportunity.Content.DealType != null)
                        {
                            _logger.LogError($"RequestId: {requestId} - CreateWorkflowAsync Service Exception");
                            throw new AccessDeniedException($"RequestId: {requestId} - CreateWorkflowAsync Service Exception");
                        }
                    }
                }

                // Update note created by (if one) and set it to relationship manager
                if (opportunity.Content.Notes?.Count > 0)
                {
                    var currentUser = (_userContext.User.Claims).ToList().Find(x => x.Type == "preferred_username")?.Value;
                    var callerUser  = await _userProfileRepository.GetItemByUpnAsync(currentUser, requestId);

                    if (callerUser != null)
                    {
                        opportunity.Content.Notes[0].CreatedBy       = callerUser;
                        opportunity.Content.Notes[0].CreatedDateTime = DateTimeOffset.Now;
                    }
                    else
                    {
                        _logger.LogWarning($"RequestId: {requestId} - CreateWorkflowAsync can't find {currentUser} to set note created by");
                    }
                }

                //Adding RelationShipManager and LoanOfficer into ProposalManager Team
                dynamic jsonDyn = null;
                foreach (var item in opportunity.Content.TeamMembers.Where(item => item.AssignedRole.DisplayName.Equals("LoanOfficer", StringComparison.OrdinalIgnoreCase) ||
                                                                           item.AssignedRole.DisplayName.Equals("RelationshipManager", StringComparison.OrdinalIgnoreCase)))
                {
                    try
                    {
                        if (jsonDyn == null)
                        {
                            var options = new List <QueryParam>()
                            {
                                new QueryParam("filter", $"startswith(displayName,'{_appOptions.GeneralProposalManagementTeam}')")
                            };
                            jsonDyn = await _graphUserAppService.GetGroupAsync(options, "", requestId);
                        }

                        if (!string.IsNullOrEmpty(jsonDyn.value[0].id.ToString()) && !string.IsNullOrEmpty(item.Fields.UserPrincipalName))
                        {
                            try
                            {
                                var groupID = jsonDyn.value[0].id.ToString();
                                Guard.Against.NullOrEmpty(item.Id, $"OpportunityFactorty_{item.AssignedRole.DisplayName} Id NullOrEmpty", requestId);
                                await _graphUserAppService.AddGroupMemberAsync(item.Id, groupID, requestId);
                            }
                            catch (Exception ex)
                            {
                                _logger.LogError($"RequestId: {requestId} - userId: {item.Id} - OpportunityFactorty_AddGroupMemberAsync_{item.AssignedRole.DisplayName} error in CreateWorkflowAsync: {ex}");
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError($"RequestId: {requestId} - userId: {item.Id} - OpportunityFactorty_AddGroupMemberAsync_{item.AssignedRole.DisplayName} error in CreateWorkflowAsync: {ex}");
                    }

                    // Send notification
                    // Define Sent To user profile
                    if (item.AssignedRole.DisplayName.Equals("LoanOfficer", StringComparison.OrdinalIgnoreCase))
                    {
                        try
                        {
                            _logger.LogInformation($"RequestId: {requestId} - CreateWorkflowAsync sendNotificationCardAsync new opportunity notification.");
                            var sendAccount = UserProfile.Empty;
                            sendAccount.Id          = item.Id;
                            sendAccount.DisplayName = item.DisplayName;
                            sendAccount.Fields.UserPrincipalName = item.Fields.UserPrincipalName;
                            await _cardNotificationService.sendNotificationCardAsync(opportunity, sendAccount, $"New opportunity {opportunity.DisplayName} has been assigned to ", requestId);
                        }
                        catch (Exception ex)
                        {
                            _logger.LogError($"RequestId: {requestId} - CreateWorkflowAsync sendNotificationCardAsync Action error: {ex}");
                        }
                    }
                }

                return(opportunity);
            }
            catch (Exception ex)
            {
                _logger.LogError($"RequestId: {requestId} - CreateWorkflowAsync Service Exception: {ex}");
                throw new ResponseException($"RequestId: {requestId} - CreateWorkflowAsync Service Exception: {ex}");
            }
        }