Exemplo n.º 1
0
        public async Task <JObject> GetTeamGroupDriveAsync(string teamGroupName)
        {
            _logger.LogInformation("GetTeamGroupDriveAsync called.");

            try
            {
                Guard.Against.NullOrEmpty(teamGroupName, nameof(teamGroupName));
                string result = string.Concat(teamGroupName.Where(c => !char.IsWhiteSpace(c)));

                // TODO: Implement,, the below code is part of boilerplate
                var siteIdResponse = await _graphSharePointAppService.GetSiteIdAsync(_appOptions.SharePointHostName, result);

                dynamic responseDyn = siteIdResponse;
                var     siteId      = responseDyn.id.ToString();

                var driveResponse = await _graphSharePointAppService.GetSiteDriveAsync(siteId);

                return(driveResponse);
            }
            catch (Exception ex)
            {
                _logger.LogError("GetTeamGroupDriveAsync error: " + ex);
                throw;
            }
        }
Exemplo n.º 2
0
        // 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 <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}");
            }
        }