public async Task <StatusCodes> DeleteItemAsync(string id, string requestId = "") { _logger.LogInformation($"RequestId: {requestId} - OpportunityRepository_DeleteItemAsync called."); try { Guard.Against.Null(id, nameof(id), requestId); var opportunitySiteList = new SiteList { SiteId = _appOptions.ProposalManagementRootSiteId, ListId = _appOptions.OpportunitiesListId }; var opportunity = await _graphSharePointAppService.GetListItemByIdAsync(opportunitySiteList, id, "all", requestId); Guard.Against.Null(opportunity, $"OpportunityRepository_y_DeleteItemsAsync getItemsById: {id}", requestId); var opportunityJson = opportunity["fields"]["OpportunityObject"].ToString(); var oppArtifact = JsonConvert.DeserializeObject <Opportunity>(opportunityJson.ToString(), new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Ignore, NullValueHandling = NullValueHandling.Ignore }); // Check access var roles = new List <Role>(); roles.Add(new Role { DisplayName = "RelationshipManager" }); var checkAccess = await _opportunityFactory.CheckAccessAsync(oppArtifact, roles, requestId); if (!checkAccess) { _logger.LogError($"RequestId: {requestId} - CheckAccess DeleteItemAsync"); } if (oppArtifact.Metadata.OpportunityState == OpportunityState.Creating) { var result = await _graphSharePointAppService.DeleteFileOrFolderAsync(_appOptions.ProposalManagementRootSiteId, $"TempFolder/{oppArtifact.DisplayName}", requestId); // TODO: Check response } var json = await _graphSharePointAppService.DeleteListItemAsync(opportunitySiteList, id, requestId); Guard.Against.Null(json, nameof(json), requestId); return(StatusCodes.Status204NoContent); } catch (Exception ex) { _logger.LogError($"RequestId: {requestId} - OpportunityRepository_DeleteItemAsync Service Exception: {ex}"); throw new ResponseException($"RequestId: {requestId} - OpportunityRepository_DeleteItemAsync Service Exception: {ex}"); } }
private async Task <JObject> DeleteFileOrFolderAsync(string siteId, string itemPath, string requestId = "") { _logger.LogInformation($"RequestId: {requestId} - DeleteFileOrFolderAsync called."); try { Guard.Against.NullOrEmpty(siteId, nameof(siteId), requestId); Guard.Against.NullOrEmpty(itemPath, nameof(itemPath), requestId); return(await _graphSharePointAppService.DeleteFileOrFolderAsync(siteId, itemPath, requestId)); } catch (Exception ex) { _logger.LogError($"RequestId: {requestId} - DeleteFileOrFolderAsync Service Exception: {ex}"); throw new ResponseException($"RequestId: {requestId} - DeleteFileOrFolderAsync Service Exception: {ex}"); } }
// Workflow Actions public async Task <Opportunity> MoveTempFileToTeamAsync(Opportunity opportunity, string requestId = "") { try { // Find entries that need to be moved var moveFiles = false; foreach (var itm in opportunity.DocumentAttachments) { if (itm.DocumentUri == "TempFolder") { moveFiles = true; } } if (moveFiles) { var fromSiteId = _appOptions.ProposalManagementRootSiteId; var toSiteId = String.Empty; var fromItemPath = String.Empty; var toItemPath = String.Empty; string pattern = @"[ `~!@#$%^&*()_|+\-=?;:'" + '"' + @",.<>\{\}\[\]\\\/]"; string replacement = ""; Regex regEx = new Regex(pattern); var path = regEx.Replace(opportunity.DisplayName, replacement); //var path = WebUtility.UrlEncode(opportunity.DisplayName); //var path = opportunity.DisplayName.Replace(" ", ""); var siteIdResponse = await _graphSharePointAppService.GetSiteIdAsync(_appOptions.SharePointHostName, path, requestId); dynamic responseDyn = siteIdResponse; toSiteId = responseDyn.id.ToString(); if (!String.IsNullOrEmpty(toSiteId)) { var updatedDocumentAttachments = new List <DocumentAttachment>(); foreach (var itm in opportunity.DocumentAttachments) { var updDoc = DocumentAttachment.Empty; if (itm.DocumentUri == "TempFolder") { fromItemPath = $"TempFolder/{opportunity.DisplayName}/{itm.FileName}"; toItemPath = $"General/{itm.FileName}"; var resp = new JObject(); try { resp = await _graphSharePointAppService.MoveFileAsync(fromSiteId, fromItemPath, toSiteId, toItemPath, requestId); updDoc.Id = new Guid().ToString(); updDoc.DocumentUri = String.Empty; //doc.Id = resp.id; } catch (Exception ex) { _logger.LogWarning($"RequestId: {requestId} - MoveTempFileToTeam: from: {fromItemPath} to: {toItemPath} Service Exception: {ex}"); } } updDoc.FileName = itm.FileName; updDoc.Note = itm.Note ?? String.Empty; updDoc.Tags = itm.Tags ?? String.Empty; updDoc.Category = Category.Empty; updDoc.Category.Id = itm.Category.Id; updDoc.Category.Name = itm.Category.Name; updatedDocumentAttachments.Add(updDoc); } opportunity.DocumentAttachments = updatedDocumentAttachments; // Delete temp files var result = await _graphSharePointAppService.DeleteFileOrFolderAsync(_appOptions.ProposalManagementRootSiteId, $"TempFolder/{opportunity.DisplayName}", requestId); } } return(opportunity); } catch (Exception ex) { _logger.LogError($"RequestId: {requestId} - MoveTempFileToTeam Service Exception: {ex}"); throw new ResponseException($"RequestId: {requestId} - MoveTempFileToTeam Service Exception: {ex}"); } }
public async Task <StatusCodes> DeleteItemAsync(string id, string requestId = "") { try { Guard.Against.Null(id, nameof(id), requestId); //Granular Access : Start var access = await CheckAccessAsync(PermissionNeededTo.WritePartial, PermissionNeededTo.Write, PermissionNeededTo.WriteAll, requestId); var currentUser = (_userContext.User.Claims).ToList().Find(x => x.Type == "preferred_username")?.Value; if (!access.haveAccess && !access.haveSuperAcess) { // 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 }; var opportunity = await _graphSharePointAppService.GetListItemByIdAsync(opportunitySiteList, id, "all", requestId); Guard.Against.Null(opportunity, $"OpportunityRepository_y_DeleteItemsAsync getItemsById: {id}", requestId); var opportunityJson = opportunity["fields"]["OpportunityObject"].ToString(); var oppArtifact = JsonConvert.DeserializeObject <Opportunity>(opportunityJson.ToString(), new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Ignore, NullValueHandling = NullValueHandling.Ignore }); var roles = new List <Role>(); roles.Add(new Role { DisplayName = "RelationshipManager" }); //Granular Access : Start if (!access.haveSuperAcess) { if (!(oppArtifact.Content.TeamMembers).ToList().Any (teamMember => teamMember.Fields.UserPrincipalName == 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 if (oppArtifact.Metadata.OpportunityState == OpportunityState.Creating) { var result = await _graphSharePointAppService.DeleteFileOrFolderAsync(_appOptions.ProposalManagementRootSiteId, $"TempFolder/{oppArtifact.DisplayName}", requestId); // TODO: Check response } var json = await _graphSharePointAppService.DeleteListItemAsync(opportunitySiteList, id, requestId); Guard.Against.Null(json, nameof(json), requestId); //For DashBorad--delete opportunity await DeleteOpportunityFrmDashboardAsync(id, requestId); return(StatusCodes.Status204NoContent); } catch (Exception ex) { _logger.LogError($"RequestId: {requestId} - OpportunityRepository_DeleteItemAsync Service Exception: {ex}"); throw new ResponseException($"RequestId: {requestId} - OpportunityRepository_DeleteItemAsync Service Exception: {ex}"); } }