public async Task <StatusCodes> CreateItemAsync(Permission entity, string requestId = "")
        {
            _logger.LogInformation($"RequestId: {requestId} - PermissionRepo_CreateItemAsync called.");

            try
            {
                var siteList = new SiteList
                {
                    SiteId = _appOptions.ProposalManagementRootSiteId,
                    ListId = _appOptions.Permissions
                };

                // Create Json object for SharePoint create list item
                dynamic itemFieldsJson = new JObject();
                itemFieldsJson.Name  = entity.Name;
                itemFieldsJson.Title = entity.Id;

                dynamic itemJson = new JObject();
                itemJson.fields = itemFieldsJson;

                await _graphSharePointAppService.CreateListItemAsync(siteList, itemJson.ToString(), requestId);

                _logger.LogInformation($"RequestId: {requestId} - PermissionRepo_CreateItemAsync finished creating SharePoint list item.");

                await SetCacheAsync(requestId);

                return(StatusCodes.Status201Created);
            }
            catch (Exception ex)
            {
                _logger.LogError($"RequestId: {requestId} - PermissionRepo_CreateItemAsync error: {ex}");
                throw;
            }
        }
        public async Task <StatusCodes> CreateItemAsync(Groups entity, string requestId = "")
        {
            _logger.LogInformation($"RequestId: {requestId} - GroupsRepo_CreateItemAsync called.");

            try
            {
                var siteList = new SiteList
                {
                    SiteId = _appOptions.ProposalManagementRootSiteId,
                    ListId = _appOptions.GroupsListId
                };

                // Create Json object for SharePoint create list item
                dynamic itemFieldsJson = new JObject();
                itemFieldsJson.GroupName = entity.GroupName;
                itemFieldsJson.Title     = entity.Id;
                itemFieldsJson.Process   = JsonConvert.SerializeObject(entity.Processes, Formatting.Indented);

                dynamic itemJson = new JObject();
                itemJson.fields = itemFieldsJson;

                await _graphSharePointAppService.CreateListItemAsync(siteList, itemJson.ToString(), requestId);

                _logger.LogInformation($"RequestId: {requestId} - GroupsRepo_CreateItemAsync finished creating SharePoint list item.");

                return(StatusCodes.Status201Created);
            }
            catch (Exception ex)
            {
                _logger.LogError($"RequestId: {requestId} - GroupsRepo_CreateItemAsync error: {ex}");
                throw;
            }
        }
        public async Task <StatusCodes> CreateItemAsync(MetaData entity, string requestId = "")
        {
            _logger.LogInformation($"RequestId: {requestId} - MetaDataRepo_CreateItemAsync called.");

            try
            {
                // Create Json object for SharePoint create list item
                dynamic itemFieldsJson = new JObject();
                itemFieldsJson.FieldName = entity.DisplayName;
                itemFieldsJson.FieldType = entity.FieldType.Name.ToString();
                if (entity.FieldType.Name == FieldType.DropDown.Name)
                {
                    itemFieldsJson.FieldValue = JsonConvert.SerializeObject(entity.Values, Formatting.Indented);
                }
                else
                {
                    itemFieldsJson.FieldValue = entity.Values;
                }
                itemFieldsJson.FieldScreen = entity.Screen;

                dynamic itemJson = new JObject();
                itemJson.fields = itemFieldsJson;

                await _graphSharePointAppService.CreateListItemAsync(siteList, itemJson.ToString(), requestId);

                _logger.LogInformation($"RequestId: {requestId} - MetaDataRepo_CreateItemAsync finished creating SharePoint list item.");

                return(StatusCodes.Status201Created);
            }
            catch (Exception ex)
            {
                _logger.LogError($"RequestId: {requestId} - MetaDataRepo_CreateItemAsync error: {ex}");
                throw;
            }
        }
Beispiel #4
0
        public async Task <JObject> CreateItemAsync(Tasks entity, string requestId = "")
        {
            _logger.LogInformation($"RequestId: {requestId} - TasksRepo_CreateItemAsync called.");

            try
            {
                var siteList = new SiteList
                {
                    SiteId = _appOptions.ProposalManagementRootSiteId,
                    ListId = _appOptions.TasksListId
                };

                // Create Json object for SharePoint create list item
                dynamic itemFieldsJson = new JObject();
                itemFieldsJson.Title = entity.Id;
                itemFieldsJson.Name  = entity.Name;

                dynamic itemJson = new JObject();
                itemJson.fields = itemFieldsJson;

                var result = await _graphSharePointAppService.CreateListItemAsync(siteList, itemJson.ToString(), requestId);

                _logger.LogInformation($"RequestId: {requestId} - TasksRepo_CreateItemAsync finished creating SharePoint list item.");

                return(result);
            }
            catch (Exception ex)
            {
                _logger.LogError($"RequestId: {requestId} - TasksRepo_CreateItemAsync error: {ex}");
                throw;
            }
        }
Beispiel #5
0
        public async Task CreateSiteProcessesAsync(string requestId = "")
        {
            _logger.LogInformation($"RequestId: {requestId} - SetupService_CreateSiteProcessesAsync called.");
            try
            {
                var siteList = new SiteList
                {
                    SiteId = _appOptions.ProposalManagementRootSiteId,
                    ListId = _appOptions.ProcessListId
                };
                var processes = getProcesses();
                foreach (var process in processes)
                {
                    try
                    {
                        // Create Json object for SharePoint create list item
                        dynamic itemFieldsJson = new JObject();
                        itemFieldsJson.ProcessType = process.ProcessType;
                        itemFieldsJson.Channel     = process.Channel;
                        itemFieldsJson.ProcessStep = process.ProcessStep;
                        itemFieldsJson.RoleName    = process.RoleName;
                        itemFieldsJson.RoleId      = process.RoleId;
                        dynamic itemJson = new JObject();
                        itemJson.fields = itemFieldsJson;


                        _logger.LogDebug($"RequestId: {requestId} - SetupService_CreateSiteProcessesAsync debug result: {itemJson}");

                        var result = await _graphSharePointAppService.CreateListItemAsync(siteList, itemJson.ToString());

                        _logger.LogDebug($"RequestId: {requestId} - SetupService_CreateSiteProcessesAsync debug result: {result}");
                    }
                    catch (Exception ex)
                    {
                        _logger.LogWarning($"RequestId: {requestId} - SetupService_CreateSiteProcessesAsync warning: {ex}");
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"RequestId: {requestId} - SetupService_CreateSiteProcessesAsync Service Exception: {ex}");
                throw;
            }
        }
Beispiel #6
0
        public async Task <StatusCodes> CreateOpportunityAsync(Dashboard entity, string requestId = "")
        {
            _logger.LogInformation($"RequestId: {requestId} - DashboradRepository_CreateItemAsync called.");

            try
            {
                var siteList = new SiteList
                {
                    SiteId = _appOptions.ProposalManagementRootSiteId,
                    ListId = _appOptions.DashboardListId
                };

                // Create Json object for SharePoint create list item
                dynamic itemFieldsJson = new JObject();
                dynamic itemJson       = new JObject();
                itemFieldsJson.Title                = entity.Id;
                itemFieldsJson.CustomerName         = entity.CustomerName;
                itemFieldsJson.Status               = entity.Status;
                itemFieldsJson.OpportunityID        = entity.OpportunityId;
                itemFieldsJson.StartDate            = entity.StartDate;
                itemFieldsJson.StatusChangedDate    = entity.StatusChangedDate;
                itemFieldsJson.TargetCompletionDate = entity.TargetCompletionDate;

                itemFieldsJson.OpportunityName     = entity.OpportunityName;
                itemFieldsJson.LoanOfficer         = entity.LoanOfficer;
                itemFieldsJson.RelationshipManager = entity.RelationshipManager;
                //itemFieldsJson.OpportunityStartDate = entity.StartDate;

                //Giving days fields a 0 value @ inception
                itemFieldsJson.TotalNoOfDays            = entity.TotalNoOfDays;
                itemFieldsJson.CreditCheckNoOfDays      = entity.CreditCheckNoOfDays;
                itemFieldsJson.ComplianceReviewNoOfDays = entity.ComplianceReviewNoOfDays;
                itemFieldsJson.FormalProposalNoOfDays   = entity.FormalProposalNoOfDays;
                itemFieldsJson.RiskAssessmentNoOfDays   = entity.RiskAssessmentNoOfDays;

                itemJson.fields = itemFieldsJson;

                var result = await _graphSharePointAppService.CreateListItemAsync(siteList, itemJson.ToString(), requestId);

                _logger.LogInformation($"RequestId: {requestId} - DashboradRepository_CreateItemAsync finished creating SharePoint list item.");

                return(StatusCodes.Status200OK);
            }
            catch (Exception ex)
            {
                _logger.LogError($"RequestId: {requestId} - DashboradRepository_CreateItemAsync error: {ex}");
                throw new ResponseException($"RequestId: {requestId} - DashboradRepository_CreateItemAsync Service Exception: {ex}");
            }
        }
        public async Task <StatusCodes> CreateItemAsync(Role entity, string requestId = "")
        {
            _logger.LogInformation($"RequestId: {requestId} - RolesRepo_CreateItemAsync called.");

            try
            {
                if (!(await CheckRoleAdGroupNameExist(entity.AdGroupName.Trim(), requestId)))
                {
                    try
                    {
                        await _graphTeamsAppService.CreateGroupAsync(entity.AdGroupName.Trim(), entity.AdGroupName.Trim() + " Group");
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError($"RequestId: {requestId} - SetupService_CreateAdminGroupAsync error: {ex}");
                    }
                }

                var siteList = new SiteList
                {
                    SiteId = _appOptions.ProposalManagementRootSiteId,
                    ListId = _appOptions.RoleListId
                };

                // Create Json object for SharePoint create list item
                dynamic itemFieldsJson = new JObject();
                dynamic itemJson       = new JObject();
                itemFieldsJson.AdGroupName     = entity.AdGroupName.Trim();
                itemFieldsJson.Role            = entity.DisplayName.Trim();
                itemFieldsJson.TeamsMembership = entity.TeamsMembership.Name.ToString();
                itemFieldsJson.Permissions     = JsonConvert.SerializeObject(entity.Permissions, Formatting.Indented);
                itemJson.fields = itemFieldsJson;

                await _graphSharePointAppService.CreateListItemAsync(siteList, itemJson.ToString(), requestId);

                _logger.LogInformation($"RequestId: {requestId} - RolesRepo_CreateItemAsync finished creating SharePoint list item.");

                await SetCacheAsync(requestId);

                return(StatusCodes.Status201Created);
            }
            catch (Exception ex)
            {
                _logger.LogError($"RequestId: {requestId} - RolesRepo_CreateItemAsync error: {ex}");
                throw;
            }
        }
Beispiel #8
0
        public async Task <StatusCodes> CreateItemAsync(Template template, string requestId = "")
        {
            _logger.LogInformation($"RequestId: {requestId} - TemplateRepository_CreateItemAsync called.");

            try
            {
                Guard.Against.Null(template, nameof(template), requestId);
                Guard.Against.NullOrEmpty(template.TemplateName, nameof(template.TemplateName), requestId);

                // TODO Check access

                // Ensure id is blank since it will be set by SharePoint
                template.Id = String.Empty;

                _logger.LogInformation($"RequestId: {requestId} - TemplateRepository_CreateItemAsync creating SharePoint List for template.");
                // Create Json object for SharePoint create list item
                dynamic templateFieldsJson = new JObject();
                templateFieldsJson.TemplateName = template.TemplateName;
                templateFieldsJson.Description  = template.Description;
                templateFieldsJson.CreatedBy    = JsonConvert.SerializeObject(template.CreatedBy, Formatting.Indented);
                //set todays date as the last used date
                templateFieldsJson.LastUsed        = DateTimeOffset.Now.Date;
                templateFieldsJson.ProcessList     = JsonConvert.SerializeObject(template.ProcessList, Formatting.Indented);
                templateFieldsJson.DefaultTemplate = template.DefaultTemplate.ToString();

                dynamic templateJson = new JObject();
                templateJson.fields = templateFieldsJson;

                var templateSiteList = new SiteList
                {
                    SiteId = _appOptions.ProposalManagementRootSiteId,
                    ListId = _appOptions.TemplateListId
                };

                await _graphSharePointAppService.CreateListItemAsync(templateSiteList, templateJson.ToString(), requestId);

                _logger.LogInformation($"RequestId: {requestId} - TemplateRepository_CreateItemAsync finished creating SharePoint List for template.");
                // END TODO

                return(StatusCodes.Status201Created);
            }
            catch (Exception ex)
            {
                _logger.LogError($"RequestId: {requestId} - TemplateRepository_CreateItemAsync Service Exception: {ex}");
                throw new ResponseException($"RequestId: {requestId} - TemplateRepository_CreateItemAsync Service Exception: {ex}");
            }
        }
Beispiel #9
0
        public async Task <StatusCodes> CreateOpportunityAsync(Dashboard entity, string requestId = "")
        {
            _logger.LogInformation($"RequestId: {requestId} - DashboradRepository_CreateItemAsync called.");

            try
            {
                var siteList = new SiteList
                {
                    SiteId = _appOptions.ProposalManagementRootSiteId,
                    ListId = _appOptions.DashboardListId
                };

                // Create Json object for SharePoint create list item
                dynamic itemFieldsJson = new JObject();
                dynamic itemJson       = new JObject();

                itemFieldsJson.Title           = entity.Id;
                itemFieldsJson.CustomerName    = entity.CustomerName;
                itemFieldsJson.Status          = entity.Status;
                itemFieldsJson.StartDate       = entity.StartDate;
                itemFieldsJson.OpportunityName = entity.OpportunityName;
                itemFieldsJson.OpportunityID   = entity.OpportunityId;

                itemFieldsJson.TotalNoOfDays = entity.TotalNoOfDays;

                itemFieldsJson.ProcessNoOfDays = JsonConvert.SerializeObject(entity.ProcessList, Formatting.Indented);

                itemFieldsJson.ProcessEndDates = JsonConvert.SerializeObject(entity.ProcessEndDateList, Formatting.Indented);

                itemFieldsJson.ProcessLoanOfficers = JsonConvert.SerializeObject(entity.ProcessLoanOfficerNames, Formatting.Indented);

                itemJson.fields = itemFieldsJson;

                var result = await _graphSharePointAppService.CreateListItemAsync(siteList, itemJson.ToString(), requestId);

                _logger.LogInformation($"RequestId: {requestId} - DashboradRepository_CreateItemAsync finished creating SharePoint list item.");

                return(StatusCodes.Status200OK);
            }
            catch (Exception ex)
            {
                _logger.LogError($"RequestId: {requestId} - DashboradRepository_CreateItemAsync error: {ex}");
                throw new ResponseException($"RequestId: {requestId} - DashboradRepository_CreateItemAsync Service Exception: {ex}");
            }
        }
Beispiel #10
0
        public async Task <JObject> CreateItemAsync(ProcessesType process, string requestId = "")
        {
            _logger.LogInformation($"RequestId: {requestId} - ProcessRepository_CreateItemAsync called.");

            try
            {
                Guard.Against.Null(process, nameof(process), requestId);
                Guard.Against.NullOrEmpty(process.ProcessStep, nameof(process.ProcessStep), requestId);

                // Ensure id is blank since it will be set by SharePoint
                process.Id = String.Empty;

                _logger.LogInformation($"RequestId: {requestId} - processRepository_CreateItemAsync creating SharePoint List for process.");

                // Create Json object for SharePoint create list item
                dynamic processFieldsJson = new JObject();
                processFieldsJson.ProcessType = process.ProcessType;
                processFieldsJson.ProcessStep = process.ProcessStep;
                processFieldsJson.Channel     = process.Channel;
                processFieldsJson.RoleId      = process.RoleId;
                processFieldsJson.RoleName    = process.RoleName;

                dynamic processJson = new JObject();
                processJson.fields = processFieldsJson;

                var processSiteList = new SiteList
                {
                    SiteId = _appOptions.ProposalManagementRootSiteId,
                    ListId = _appOptions.ProcessListId
                };

                var result = await _graphSharePointAppService.CreateListItemAsync(processSiteList, processJson.ToString(), requestId);

                _logger.LogInformation($"RequestId: {requestId} - processRepository_CreateItemAsync finished creating SharePoint List for process.");

                return(result);
            }
            catch (Exception ex)
            {
                _logger.LogError($"RequestId: {requestId} - processRepository_CreateItemAsync Service Exception: {ex}");
                throw new ResponseException($"RequestId: {requestId} - processRepository_CreateItemAsync Service Exception: {ex}");
            }
        }
Beispiel #11
0
        public async Task <StatusCodes> CreateItemAsync(RoleMapping entity, string requestId = "")
        {
            _logger.LogInformation($"RequestId: {requestId} - RoleMappingRepo_CreateItemAsync called.");

            try
            {
                if (!(await checkADGroupNameinAADAsync(entity.AdGroupName.Trim())))
                {
                    return(StatusCodes.Status400BadRequest);
                }

                var siteList = new SiteList
                {
                    SiteId = _appOptions.ProposalManagementRootSiteId,
                    ListId = _appOptions.RoleMappingsListId
                };

                // Create Json object for SharePoint create list item
                dynamic itemFieldsJson = new JObject();
                dynamic itemJson       = new JObject();
                itemFieldsJson.Title       = entity.Id;
                itemFieldsJson.ADGroupName = entity.AdGroupName.Trim();
                itemFieldsJson.Role        = entity.Role.DisplayName;
                itemFieldsJson.Permissions = JsonConvert.SerializeObject(entity.Permissions, Formatting.Indented);
                itemJson.fields            = itemFieldsJson;

                var result = await _graphSharePointAppService.CreateListItemAsync(siteList, itemJson.ToString(), requestId);

                await SetUpdatedRoleMappingListInCacheAsync(requestId);

                _logger.LogInformation($"RequestId: {requestId} - RoleMappingRepo_CreateItemAsync finished creating SharePoint list item.");

                return(StatusCodes.Status201Created);
            }
            catch (Exception ex)
            {
                _logger.LogError($"RequestId: {requestId} - RoleMappingRepo_CreateItemAsync error: {ex}");
                throw;
            }
        }
        public async Task <StatusCodes> CreateItemAsync(RoleMapping entity, string requestId = "")
        {
            _logger.LogInformation($"RequestId: {requestId} - RoleMappingRepo_CreateItemAsync called.");

            try
            {
                var siteList = new SiteList
                {
                    SiteId = _appOptions.ProposalManagementRootSiteId,
                    ListId = _appOptions.RolesListId
                };

                // Create Json object for SharePoint create list item
                dynamic itemFieldsJson = new JObject();
                itemFieldsJson.Title       = entity.Id;
                itemFieldsJson.AdGroupName = entity.AdGroupName;
                itemFieldsJson.RoleName    = entity.RoleName;
                itemFieldsJson.AdGroupId   = entity.AdGroupId;
                itemFieldsJson.ProcessStep = entity.ProcessStep;
                itemFieldsJson.Channel     = entity.Channel;
                itemFieldsJson.ProcessType = entity.ProcessType;


                dynamic itemJson = new JObject();
                itemJson.fields = itemFieldsJson;

                var result = await _graphSharePointAppService.CreateListItemAsync(siteList, itemJson.ToString(), requestId);

                _logger.LogInformation($"RequestId: {requestId} - RoleMappingRepo_CreateItemAsync finished creating SharePoint list item.");

                return(StatusCodes.Status201Created);
            }
            catch (Exception ex)
            {
                _logger.LogError($"RequestId: {requestId} - RoleMappingRepo_CreateItemAsync error: {ex}");
                throw;
            }
        }
        public async Task <JObject> CreateItemAsync(MetaData entity, string requestId = "")
        {
            _logger.LogInformation($"RequestId: {requestId} - MetaDataRepo_CreateItemAsync called.");

            try
            {
                // Create Json object for SharePoint create list item
                dynamic itemFieldsJson = new JObject();
                itemFieldsJson.FieldName     = entity.DisplayName;
                itemFieldsJson.FieldUniqueId = string.IsNullOrWhiteSpace(entity.UniqueId) ? entity.DisplayName.ToLowerInvariant().Replace(" ", "") : entity.UniqueId;
                itemFieldsJson.FieldType     = entity.FieldType.Name.ToString();
                if (entity.FieldType.Name == FieldType.DropDown.Name)
                {
                    itemFieldsJson.FieldValue = JsonConvert.SerializeObject(entity.Values, Formatting.Indented);
                }
                else
                {
                    itemFieldsJson.FieldValue = entity.Values;
                }
                itemFieldsJson.FieldScreen   = entity.Screen;
                itemFieldsJson.FieldRequired = entity.Required.ToString();

                dynamic itemJson = new JObject();
                itemJson.fields = itemFieldsJson;

                var result = await _graphSharePointAppService.CreateListItemAsync(siteList, itemJson.ToString(), requestId);

                _logger.LogInformation($"RequestId: {requestId} - MetaDataRepo_CreateItemAsync finished creating SharePoint list item.");

                return(result);
            }
            catch (Exception ex)
            {
                _logger.LogError($"RequestId: {requestId} - MetaDataRepo_CreateItemAsync error: {ex}");
                throw;
            }
        }
Beispiel #14
0
        public async Task <StatusCodes> CreateItemAsync(Opportunity opportunity, string requestId = "")
        {
            _logger.LogInformation($"RequestId: {requestId} - OpportunityRepository_CreateItemAsync called.");

            try
            {
                Guard.Against.Null(opportunity, nameof(opportunity), requestId);
                Guard.Against.NullOrEmpty(opportunity.DisplayName, nameof(opportunity.DisplayName), requestId);

                var roles = new List <Role>();
                roles.Add(new Role {
                    DisplayName = "RelationshipManager"
                });

                //Granular Access : Start
                if (StatusCodes.Status401Unauthorized == await _authorizationService.CheckAccessFactoryAsync(PermissionNeededTo.Create, requestId))
                {
                    return(StatusCodes.Status401Unauthorized);
                }
                //Granular Access : End
                // Ensure id is blank since it will be set by SharePoint
                opportunity.Id = String.Empty;

                // TODO: This section will be replaced with a workflow
                opportunity = await _opportunityFactory.CreateWorkflowAsync(opportunity, requestId);


                _logger.LogInformation($"RequestId: {requestId} - OpportunityRepository_CreateItemAsync creating SharePoint List for opportunity.");

                //Get loan officer & relationship manager values
                var loanOfficerId          = String.Empty;
                var relationshipManagerId  = String.Empty;
                var loanOfficerUpn         = String.Empty;
                var relationshipManagerUpn = String.Empty;
                foreach (var item in opportunity.Content.TeamMembers)
                {
                    if (item.AssignedRole.DisplayName == "LoanOfficer" && !String.IsNullOrEmpty(item.Id))
                    {
                        loanOfficerId  = item.Id;
                        loanOfficerUpn = item.Fields.UserPrincipalName;
                    }
                    if (item.AssignedRole.DisplayName == "RelationshipManager" && !String.IsNullOrEmpty(item.Id))
                    {
                        relationshipManagerId  = item.Id;
                        relationshipManagerUpn = item.Fields.UserPrincipalName;
                    }
                }


                // Create Json object for SharePoint create list item
                dynamic opportunityFieldsJson = new JObject();
                opportunityFieldsJson.Name                = opportunity.DisplayName;
                opportunityFieldsJson.OpportunityState    = opportunity.Metadata.OpportunityState.Name;
                opportunityFieldsJson.OpportunityObject   = JsonConvert.SerializeObject(opportunity, Formatting.Indented);
                opportunityFieldsJson.LoanOfficer         = loanOfficerId;
                opportunityFieldsJson.RelationshipManager = relationshipManagerId;
                opportunityFieldsJson.Reference           = opportunity.Reference ?? String.Empty;

                dynamic opportunityJson = new JObject();
                opportunityJson.fields = opportunityFieldsJson;

                var opportunitySiteList = new SiteList
                {
                    SiteId = _appOptions.ProposalManagementRootSiteId,
                    ListId = _appOptions.OpportunitiesListId
                };

                var result = await _graphSharePointAppService.CreateListItemAsync(opportunitySiteList, opportunityJson.ToString(), requestId);

                //DashBoard Create call Start.
                try
                {
                    var id = JObject.Parse(result.ToString()).SelectToken("id").ToString();
                    await CreateDashBoardEntryAsync(requestId, id, opportunity);
                }
                catch (Exception ex)
                {
                    _logger.LogError($"RequestId: {requestId} - OpportunityRepository_CreateItemAsync create dashboard entry Exception: {ex}");
                    //await CreateDashBoardEntryAsync(requestId, "1", opportunity);
                }
                //DashBoard Create call End.
                _logger.LogInformation($"RequestId: {requestId} - OpportunityRepository_CreateItemAsync finished creating SharePoint List for opportunity.");

                return(StatusCodes.Status201Created);
            }
            catch (Exception ex)
            {
                _logger.LogError($"RequestId: {requestId} - OpportunityRepository_CreateItemAsync Service Exception: {ex}");
                throw new ResponseException($"RequestId: {requestId} - OpportunityRepository_CreateItemAsync Service Exception: {ex}");
            }
        }
        public async Task <StatusCodes> CreateItemAsync(Opportunity opportunity, string requestId = "")
        {
            _logger.LogInformation($"RequestId: {requestId} - OpportunityRepository_CreateItemAsync called.");

            try
            {
                Guard.Against.Null(opportunity, nameof(opportunity), requestId);
                Guard.Against.NullOrEmpty(opportunity.DisplayName, nameof(opportunity.DisplayName), requestId);

                // Check access
                var roles = new List <Role>();
                roles.Add(new Role {
                    DisplayName = "RelationshipManager"
                });
                var checkAccess = await _opportunityFactory.CheckAccessAsync(opportunity, roles, requestId);

                if (!checkAccess)
                {
                    _logger.LogError($"RequestId: {requestId} - OpportunityRepository_CreateItemAsync CheckAccess CreateItemAsync");
                }

                // Ensure id is blank since it will be set by SharePoint
                opportunity.Id = String.Empty;

                // TODO: This section will be replaced with a workflow
                opportunity = await _opportunityFactory.CreateWorkflowAsync(opportunity, requestId);


                _logger.LogInformation($"RequestId: {requestId} - OpportunityRepository_CreateItemAsync creating SharePoint List for opportunity.");

                //Get loan officer & relationship manager values
                var loanOfficerId          = String.Empty;
                var relationshipManagerId  = String.Empty;
                var loanOfficerUpn         = String.Empty;
                var relationshipManagerUpn = String.Empty;
                foreach (var item in opportunity.Content.TeamMembers)
                {
                    if (item.AssignedRole.DisplayName == "LoanOfficer" && !String.IsNullOrEmpty(item.Id))
                    {
                        loanOfficerId  = item.Id;
                        loanOfficerUpn = item.Fields.UserPrincipalName;
                    }
                    if (item.AssignedRole.DisplayName == "RelationshipManager" && !String.IsNullOrEmpty(item.Id))
                    {
                        relationshipManagerId  = item.Id;
                        relationshipManagerUpn = item.Fields.UserPrincipalName;
                    }
                }


                // Create Json object for SharePoint create list item
                dynamic opportunityFieldsJson = new JObject();
                //opportunityFieldsJson.OpportunityId = opportunity.Id;
                opportunityFieldsJson.Name                = opportunity.DisplayName;
                opportunityFieldsJson.OpportunityState    = opportunity.Metadata.OpportunityState.Name;
                opportunityFieldsJson.OpportunityObject   = JsonConvert.SerializeObject(opportunity, Formatting.Indented);
                opportunityFieldsJson.LoanOfficer         = loanOfficerId;
                opportunityFieldsJson.RelationshipManager = relationshipManagerId;

                dynamic opportunityJson = new JObject();
                opportunityJson.fields = opportunityFieldsJson;

                var opportunitySiteList = new SiteList
                {
                    SiteId = _appOptions.ProposalManagementRootSiteId,
                    ListId = _appOptions.OpportunitiesListId
                };

                var result = await _graphSharePointAppService.CreateListItemAsync(opportunitySiteList, opportunityJson.ToString(), requestId);

                _logger.LogInformation($"RequestId: {requestId} - OpportunityRepository_CreateItemAsync finished creating SharePoint List for opportunity.");
                // END TODO

                // Add entry in Opportunities public sub site
                var opportunitySubSiteList = new SiteList
                {
                    SiteId = _appOptions.OpportunitiesSubSiteId,
                    ListId = _appOptions.PublicOpportunitiesListId
                };


                // Create Json object for SharePoint create list item
                dynamic pubOpportunityFieldsJson = new JObject();
                pubOpportunityFieldsJson.Title               = opportunity.DisplayName;
                pubOpportunityFieldsJson.LoanOfficer         = loanOfficerUpn;
                pubOpportunityFieldsJson.RelationshipManager = relationshipManagerUpn;
                pubOpportunityFieldsJson.State               = opportunity.Metadata.OpportunityState.Name;

                dynamic pubOpportunityJson = new JObject();
                pubOpportunityJson.fields = pubOpportunityFieldsJson;

                try
                {
                    var resultPub = await _graphSharePointAppService.CreateListItemAsync(opportunitySubSiteList, pubOpportunityJson.ToString(), requestId);
                }
                catch (Exception ex)
                {
                    // Dont brak the opportunity creation of entry can't be added to subsite (public)
                    _logger.LogError($"RequestId: {requestId} - OpportunityRepository_CreateItemAsync SubsiteOpportunity Service Exception: {ex}");
                }


                return(StatusCodes.Status201Created);
            }
            catch (Exception ex)
            {
                _logger.LogError($"RequestId: {requestId} - OpportunityRepository_CreateItemAsync Service Exception: {ex}");
                throw new ResponseException($"RequestId: {requestId} - OpportunityRepository_CreateItemAsync Service Exception: {ex}");
            }
        }
        public async Task <StatusCodes> CreateItemAsync(Opportunity opportunity, string requestId = "")
        {
            _logger.LogInformation($"RequestId: {requestId} - OpportunityRepository_CreateItemAsync called.");

            try
            {
                Guard.Against.Null(opportunity, nameof(opportunity), requestId);
                Guard.Against.NullOrEmpty(opportunity.DisplayName, nameof(opportunity.DisplayName), requestId);

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

                //add default business process, since this is coming from API (outside app)
                if (opportunity.Content.Template == null)
                {
                    opportunity.Content.Template = (await _templateRepository.GetAllAsync(requestId)).ToList().Find(x => x.DefaultTemplate);
                }

                //Granular Access : Start
                if (StatusCodes.Status401Unauthorized == await _authorizationService.CheckAccessFactoryAsync(PermissionNeededTo.Create, requestId))
                {
                    return(StatusCodes.Status401Unauthorized);
                }
                //Granular Access : End
                // Ensure id is blank since it will be set by SharePoint
                opportunity.Id = String.Empty;

                opportunity = await _opportunityFactory.CreateWorkflowAsync(opportunity, requestId);

                _logger.LogInformation($"RequestId: {requestId} - OpportunityRepository_CreateItemAsync creating SharePoint List for opportunity.");

                // Create Json object for SharePoint create list item
                dynamic opportunityFieldsJson = new JObject();
                opportunityFieldsJson.Name             = opportunity.DisplayName;
                opportunityFieldsJson.OpportunityState = opportunity.Metadata.OpportunityState.Name;
                try
                {
                    opportunityFieldsJson.OpportunityObject = JsonConvert.SerializeObject(opportunity, Formatting.Indented);
                    //TODO
                    opportunityFieldsJson.TemplateLoaded = opportunity.TemplateLoaded.ToString();
                }
                catch (Exception ex)
                {
                    _logger.LogError($"RequestId: {requestId} - OpportunityRepository_CreateItemAsync create dashboard entry Exception: {ex}");
                }
                opportunityFieldsJson.Reference = opportunity.Reference ?? String.Empty;

                dynamic opportunityJson = new JObject();
                opportunityJson.fields = opportunityFieldsJson;

                var opportunitySiteList = new SiteList
                {
                    SiteId = _appOptions.ProposalManagementRootSiteId,
                    ListId = _appOptions.OpportunitiesListId
                };

                await _graphSharePointAppService.CreateListItemAsync(opportunitySiteList, opportunityJson.ToString(), requestId);

                //DashBoard Create call End.
                _logger.LogInformation($"RequestId: {requestId} - OpportunityRepository_CreateItemAsync finished creating SharePoint List for opportunity.");

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