public async Task <Opportunity> GetItemByNameAsync(string name, bool isCheckName, string requestId = "") { _logger.LogInformation($"RequestId: {requestId} - OpportunityRepository_GetItemByNameAsync called."); try { Guard.Against.NullOrEmpty(name, nameof(name), requestId); //Granular Access : Start var access = await CheckAccessAsync(PermissionNeededTo.ReadPartial, PermissionNeededTo.Read, PermissionNeededTo.ReadAll, requestId); var currentUser = (_userContext.User.Claims).ToList().Find(x => x.Type == "preferred_username")?.Value; if (!access.haveSuperAcess && !access.haveAccess && !access.havePartial) { // This user is not having any write permissions, so he won't be able to update _logger.LogError($"RequestId: {requestId} - OpportunityRepository_GetItemByIdAsync current user: {currentUser} AccessDeniedException"); throw new AccessDeniedException($"RequestId: {requestId} - OpportunityRepository_GetItemByIdAsync current user: {currentUser} AccessDeniedException"); } //Granular Access : End var opportunitySiteList = new SiteList { SiteId = _appOptions.ProposalManagementRootSiteId, ListId = _appOptions.OpportunitiesListId }; name = name.Replace("'", ""); var nameEncoded = WebUtility.UrlEncode(name); var options = new List <QueryParam>(); options.Add(new QueryParam("filter", $"startswith(fields/Name,'{nameEncoded}')")); var json = await _graphSharePointAppService.GetListItemAsync(opportunitySiteList, options, "all", requestId); Guard.Against.Null(json, "OpportunityRepository_GetItemByNameAsync GetListItemAsync Null", requestId); dynamic jsonDyn = json; if (jsonDyn.value.HasValues) { foreach (var item in jsonDyn.value) { if (item.fields.Name == name) { if (isCheckName) { // If just checking for name, rtunr empty opportunity and skip access check var emptyOpportunity = Opportunity.Empty; emptyOpportunity.DisplayName = name; return(emptyOpportunity); } var opportunityJson = item.fields.OpportunityObject.ToString(); var oppArtifact = JsonConvert.DeserializeObject <Opportunity>(opportunityJson, new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Ignore, NullValueHandling = NullValueHandling.Ignore }); oppArtifact.Id = jsonDyn.value[0].fields.id.ToString(); //Granular Access : Start if (!access.haveSuperAcess) { if (!CheckTeamMember(oppArtifact, currentUser)) { // This user is not having any write permissions, so he won't be able to update _logger.LogError($"RequestId: {requestId} - OpportunityRepository_GetItemByIdAsync current user: {currentUser} AccessDeniedException"); throw new AccessDeniedException($"RequestId: {requestId} - OpportunityRepository_GetItemByIdAsync current user: {currentUser} AccessDeniedException"); } } //Granular Access : End return(oppArtifact); } } } // Not found _logger.LogError($"RequestId: {requestId} - OpportunityRepository_GetItemByNameAsync opportunity: {name} - Not found."); return(Opportunity.Empty); } catch (Exception ex) { _logger.LogError($"RequestId: {requestId} - OpportunityRepository_GetItemByNameAsync Service Exception: {ex}"); throw new ResponseException($"RequestId: {requestId} - OpportunityRepository_GetItemByNameAsync Service Exception: {ex}"); } }
public async Task <StatusCodes> UpdateItemAsync(Opportunity opportunity, string requestId = "") { _logger.LogInformation($"RequestId: {requestId} - OpportunityRepository_UpdateItemAsync called."); Guard.Against.Null(opportunity, nameof(opportunity), requestId); Guard.Against.NullOrEmpty(opportunity.Id, nameof(opportunity.Id), requestId); try { // TODO: This section will be replaced with a workflow _logger.LogInformation($"RequestId: {requestId} - OpportunityRepository_UpdateItemAsync SharePoint List for opportunity."); // Check access var checkAccess = await _opportunityFactory.CheckAccessAnyAsync(opportunity, requestId); if (!checkAccess) { _logger.LogError($"RequestId: {requestId} - OpportunityRepository_UpdateItemAsync CheckAccessAny"); } // Workflow processor opportunity = await _opportunityFactory.UpdateWorkflowAsync(opportunity, requestId); //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; } } var opportunityJObject = JObject.FromObject(opportunity); // Create Json object for SharePoint create list item dynamic opportunityJson = new JObject(); opportunityJson.OpportunityId = opportunity.Id; //opportunityJson.Name = opportunity.DisplayName; TODO: In wave 1 nme can't be changed opportunityJson.OpportunityState = opportunity.Metadata.OpportunityState.Name; opportunityJson.OpportunityObject = JsonConvert.SerializeObject(opportunity, Formatting.Indented); //opportunityJson.OpportunityObject = opportunityJObject.ToString(); opportunityJson.LoanOfficer = loanOfficerId; opportunityJson.RelationshipManager = relationshipManagerId; var opportunitySiteList = new SiteList { SiteId = _appOptions.ProposalManagementRootSiteId, ListId = _appOptions.OpportunitiesListId }; var result = await _graphSharePointAppService.UpdateListItemAsync(opportunitySiteList, opportunity.Id, opportunityJson.ToString(), requestId); _logger.LogInformation($"RequestId: {requestId} - OpportunityRepository_UpdateItemAsync finished SharePoint List for opportunity."); // Update 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; var options = new List <QueryParam>(); options.Add(new QueryParam("filter", $"startswith(fields/Title,'{opportunity.DisplayName}')")); try { var json = await _graphSharePointAppService.GetListItemAsync(opportunitySubSiteList, options, "All", requestId); Guard.Against.Null(json, "OpportunityRepository_UpdateItemAsync GetListItemAsync Null", requestId); dynamic jsonDyn = json; string opportunityId = jsonDyn.value[0].fields.id.ToString(); if (!String.IsNullOrEmpty(opportunityId)) { var resultPub = await _graphSharePointAppService.UpdateListItemAsync(opportunitySubSiteList, opportunityId, pubOpportunityFieldsJson.ToString(), requestId); } } catch (Exception ex) { // Dont brak the opportunity creation of entry can't be updated to subsite (public) _logger.LogError($"RequestId: {requestId} - OpportunityRepository_UpdateItemAsync SubsiteOpportunity Service Exception: {ex}"); } return(StatusCodes.Status200OK); } catch (Exception ex) { _logger.LogError($"RequestId: {requestId} - OpportunityRepository_UpdateItemAsync Service Exception: {ex}"); throw new ResponseException($"RequestId: {requestId} - OpportunityRepository_UpdateItemAsync Service Exception: {ex}"); } }