public async Task <OpportunityViewModel> GetItemByNameAsync(string name, bool isCheckName, string requestId = "") { _logger.LogInformation($"RequestId: {requestId} - GetItemByNameAsync called."); Guard.Against.Null(name, nameof(name), requestId); try { var thisOpportunity = await _opportunityRepository.GetItemByNameAsync(name, isCheckName, requestId); var opportunityViewModel = new OpportunityViewModel(); if (thisOpportunity.DisplayName != name.Replace("'", "")) { _logger.LogWarning($"RequestId: {requestId} - GetItemByNameAsync no items found"); throw new NoItemsFound($"RequestId: {requestId} - Method name: GetItemByNameAsync - No Items Found"); } else { if (isCheckName) { opportunityViewModel = new OpportunityViewModel(); opportunityViewModel.DisplayName = name.Replace("'", ""); } else { opportunityViewModel = await _opportunityHelpers.ToOpportunityViewModelAsync(thisOpportunity, requestId); } } // TODO: foreach service to filter out data you don't have access return(opportunityViewModel); } catch (Exception ex) { _logger.LogError($"RequestId: {requestId} - GetItemByNameAsync Service Exception: {ex}"); throw new ResponseException($"RequestId: {requestId} - GetItemByNameAsync Service Exception: {ex}"); } }
public async Task <JObject> UploadDocumentTeamAsync(string opportunityName, string docType, IFormFile file, string requestId = "") { _logger.LogInformation($"RequestId: {requestId} - UploadDocumentTeamAsync called."); try { Guard.Against.NullOrEmpty(opportunityName, nameof(opportunityName), requestId); Guard.Against.NullOrEmpty(docType, nameof(docType), requestId); Guard.Against.Null(file, nameof(file), requestId); var sections = new List <DocumentSection>(); var folder = String.Empty; var docTypeParts = docType.Split(new char[] { ',', '=' }); //0 = ChecklistDocument, 1 = channle name, 2 = Checklist item Id if (docType == DocumentContext.ProposalTemplate.Name) { // If docType is proposal document template, try to extract sections before upload so if fails, upload is skipped sections = (ExtractSections(file.OpenReadStream(), file.FileName, requestId)).ToList(); Guard.Against.Null(sections, "UploadDocumentTeamAsync_sections", requestId); folder = "Formal Proposal"; } else if (docType == DocumentContext.Attachment.Name) { folder = "TempFolder"; } else { folder = docTypeParts[1].Replace(" ", ""); } // Get opportunity to update the associated docUri //var opportunity = Opportunity.Empty; var opportunity = await _opportunityRepository.GetItemByNameAsync($"'{opportunityName}'", false, requestId); Guard.Against.Null(opportunity, "UploadDocumentTeamAsync_GetItemByNameAsync", requestId); // Start a simple retry var retryGetOpTimes = 1; while ((String.IsNullOrEmpty(opportunity.Id)) && retryGetOpTimes < 7) { _logger.LogInformation($"RequestId: {requestId} - UploadDocumentTeamAsync get opportunity delay started: {retryGetOpTimes} at {DateTime.Now}."); await Task.Delay(4000 + (retryGetOpTimes * 1000)); opportunity = await _opportunityRepository.GetItemByNameAsync($"'{opportunityName}'", false, requestId); retryGetOpTimes = retryGetOpTimes + 1; } Guard.Against.NullOrEmpty(opportunity.Id, "UploadDocumentTeamAsync_opportunity_GetItemByNameAsync", requestId); if (opportunity.DisplayName != opportunityName) { throw new ResponseException($"RequestId: {requestId} - UploadDocumentTeamAsync GetItemByNameAsync mistmatch for opportunity: {opportunityName}"); } var siteName = opportunityName.Replace(" ", ""); var siteIdResponse = new JObject(); var siteId = String.Empty; if (folder == "TempFolder") { // Initial attachment is uploaded to private site for proposal mnagement siteName = "ProposlManagement"; siteId = _appOptions.ProposalManagementRootSiteId; } else { try { siteIdResponse = await _graphSharePointAppService.GetSiteIdAsync(_appOptions.SharePointHostName, siteName, requestId); dynamic responseDyn = siteIdResponse; siteId = responseDyn.id.ToString(); } catch (Exception ex) { _logger.LogInformation($"RequestId: {requestId} - UploadDocumentTeamAsync get site id error: {ex}"); } } var retryGetSiteTimes = 1; while ((String.IsNullOrEmpty(siteId)) && retryGetSiteTimes < 4) { _logger.LogInformation($"RequestId: {requestId} - UploadDocumentTeamAsync get site id delay started: {retryGetOpTimes} at {DateTime.Now}."); await Task.Delay(4000 + (retryGetSiteTimes * 1000)); siteIdResponse = await _graphSharePointAppService.GetSiteIdAsync(_appOptions.SharePointHostName, siteName, requestId); dynamic responseDyn = siteIdResponse; siteId = responseDyn.id.ToString(); retryGetSiteTimes = retryGetSiteTimes + 1; } Guard.Against.NullOrEmpty(siteId, "UploadDocumentTeamAsync_GetSiteIdAsync", requestId); if (docType == DocumentContext.Attachment.Name) { // Create folder with opportunity name in internal sharepoint under root\TempFolder TODO: Get name form app settings P2 try { var respFolder = await CreateFolderAsync(siteId, opportunity.DisplayName, folder, requestId); } catch (Exception ex) { _logger.LogWarning($"RequestId: {requestId} - UploadDocumentTeamAsync CreateFolderAsync Exception: {ex}"); } folder = folder + $"/{opportunity.DisplayName}"; } var respUpload = await UploadDocumentAsync(siteId, folder, file, requestId); dynamic respUploadDyn = respUpload; string webUrl = respUploadDyn.webUrl.ToString(); string docId = respUploadDyn.id.ToString(); //Todo: Granular Premission if (docType == DocumentContext.ProposalTemplate.Name) { // If docType is proposal document template, update sections & documentUri opportunity.Content.ProposalDocument.Content.ProposalSectionList = sections; opportunity.Content.ProposalDocument.Id = docId; opportunity.Content.ProposalDocument.Metadata.DocumentUri = webUrl; } else if (docType == DocumentContext.Attachment.Name) { if (opportunity.DocumentAttachments == null) { opportunity.DocumentAttachments = new List <DocumentAttachment>(); } var updDocumentAttachments = new List <DocumentAttachment>(); foreach (var itm in opportunity.DocumentAttachments) { var doc = itm; if (itm.FileName == file.FileName) { doc.Id = docId; doc.FileName = file.FileName; doc.Note = itm.Note ?? String.Empty; doc.Tags = itm.Tags ?? String.Empty; doc.DocumentUri = "TempFolder"; doc.Category = Category.Empty; doc.Category.Id = itm.Category.Id; doc.Category.Name = itm.Category.Name; } updDocumentAttachments.Add(doc); } opportunity.DocumentAttachments = updDocumentAttachments; } else if (docType.StartsWith($"{DocumentContext.ChecklistDocument.Name}=")) { var checklistTaskId = docTypeParts[2]; var channel = docTypeParts[1]; var newChecklists = new List <Checklist>(); foreach (var item in opportunity.Content.Checklists.ToList()) { var newChecklist = new Checklist(); newChecklist.ChecklistTaskList = new List <ChecklistTask>(); newChecklist.ChecklistChannel = item.ChecklistChannel; newChecklist.ChecklistStatus = item.ChecklistStatus; newChecklist.Id = item.Id; if (channel != item.ChecklistChannel) { newChecklist.ChecklistTaskList = item.ChecklistTaskList; } else { var newChecklistTask = new ChecklistTask(); foreach (var sItem in item.ChecklistTaskList) { if (sItem.Id == checklistTaskId) { newChecklistTask.Id = sItem.Id; newChecklistTask.ChecklistItem = sItem.ChecklistItem; newChecklistTask.Completed = sItem.Completed; newChecklistTask.FileUri = webUrl; newChecklist.ChecklistTaskList.Add(newChecklistTask); } else { newChecklist.ChecklistTaskList.Add(sItem); } } } newChecklists.Add(newChecklist); } opportunity.Content.Checklists = newChecklists; } // Update the opportunity var respUpdate = await _opportunityRepository.UpdateItemAsync(opportunity, requestId); Guard.Against.NotStatus200OK(respUpdate, "UploadDocumentTeamAsync_UpdateItemAsync", requestId); return(respUpload); } catch (Exception ex) { _logger.LogError($"RequestId: {requestId} - UploadDocumentTeamAsync Service Exception: {ex}"); throw new ResponseException($"RequestId: {requestId} - UploadDocumentTeamAsync Service Exception: {ex}"); } }