Exemplo n.º 1
0
            public async Task <ActionstepDocument> Handle(ActionstepSavePDFCommand message, CancellationToken token)
            {
                if (message is null)
                {
                    throw new ArgumentNullException(nameof(message));
                }

                ValidationResult result = _validator.Validate(message);

                if (!result.IsValid)
                {
                    throw new ValidationException("Invalid input.", result.Errors);
                }

                TokenSetQuery tokenSetQuery = new TokenSetQuery(message.AuthenticatedUser?.Id, message.OrgKey);

                var actionResponse = await _actionstepService.Handle <GetActionResponse>(new GetActionRequest(tokenSetQuery, message.MatterId));

                string fileName = $"{message.MatterId}_Settlement Statement_{DateTime.UtcNow.ToString("_yyyy-MM-dd hh-mm", CultureInfo.InvariantCulture)}.pdf";

                UploadFileResponse file = await _actionstepService.UploadFile(tokenSetQuery, fileName, message.FilePath);

                #region Check Documents Folder
                ActionFolder             actionFolder   = new ActionFolder(actionResponse.Action.Id);
                GetActionFolderRequest   folderRequest  = new GetActionFolderRequest(tokenSetQuery, actionFolder);
                ListActionFolderResponse folderResponse = await _actionstepService.Handle <ListActionFolderResponse>(folderRequest);

                var parentFolderId = folderResponse.ActionFolders.Where(af => af.Name == "Documents").Select(af => af.Id).FirstOrDefault();
                #endregion

                ActionDocument            document    = new ActionDocument(actionResponse.Action.Id, fileName, file, parentFolderId);
                SaveActionDocumentRequest saveRequest = new SaveActionDocumentRequest(tokenSetQuery, document);

                var saveResponse = await _actionstepService.Handle <SaveActionDocumentResponse>(saveRequest);

                var fileUrl = new Uri(saveResponse.ActionDocument.SharepointUrl);

                string documentUrl = $"https://{fileUrl.Host}/mym/asfw/workflow/documents/views/action_id/{actionResponse.Action.Id}#mode=browse&view=list&folder={parentFolderId}&drive=DL";

                return(new ActionstepDocument(fileUrl, fileName, new Uri(documentUrl)));
            }
Exemplo n.º 2
0
        public async Task <FTAttachment> Handle(SavePolicyPDFToActionstepCommand request, CancellationToken cancellationToken)
        {
            if (request is null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            // Get Actionstep matter info
            var tokenSetQuery = new TokenSetQuery(request.AuthenticatedUser?.Id, request.ActionstepOrg);

            var actionResponse = await _actionstepService.Handle <GetActionResponse>(new GetActionRequest(tokenSetQuery, request.MatterId));

            UploadFileResponse file = await _actionstepService.UploadFile(tokenSetQuery, request.FileName, request.FilePath);

            #region Check Documents Folder
            ActionFolder             actionFolder   = new ActionFolder(actionResponse.Action.Id);
            GetActionFolderRequest   folderRequest  = new GetActionFolderRequest(tokenSetQuery, actionFolder);
            ListActionFolderResponse folderResponse = await _actionstepService.Handle <ListActionFolderResponse>(folderRequest);

            var parentFolderId = folderResponse.ActionFolders.Where(af => af.Name == "Documents").Select(af => af.Id).FirstOrDefault();
            #endregion

            ActionDocument            document    = new ActionDocument(actionResponse.Action.Id, request.FileName, file, parentFolderId);
            SaveActionDocumentRequest saveRequest = new SaveActionDocumentRequest(tokenSetQuery, document);

            var saveResponse = await _actionstepService.Handle <SaveActionDocumentResponse>(saveRequest);

            var fileUrl = new Uri(saveResponse.ActionDocument.SharepointUrl);

            string documentUrl = $"https://{fileUrl.Host}/mym/asfw/workflow/documents/views/action_id/{actionResponse.Action.Id}#mode=browse&view=list&folder={parentFolderId}&drive=DL";

            return(new FTAttachment()
            {
                FileName = request.FileName,
                FileUrl = documentUrl
            });
        }
Exemplo n.º 3
0
            protected override async Task Handle(SaveResourcesCommand message, CancellationToken token)
            {
                if (message is null)
                {
                    throw new ArgumentNullException(nameof(message));
                }

                ValidationResult result = _validator.Validate(message);

                if (!result.IsValid)
                {
                    throw new ValidationException("Unable to save Property Resources, the command message was invalid.", result.Errors);
                }

                using (var request = new HttpRequestMessage(HttpMethod.Get, message.ResourceURL))
                {
                    var byteArray = Encoding.ASCII.GetBytes($"{message.InfoTrackCredentials.Username}:{message.InfoTrackCredentials.Password}");
                    request.Headers.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));

                    using (var infoTrackFileDownloadResponse = await _infoTrackService.Client.SendAsync(request))
                    {
                        infoTrackFileDownloadResponse.EnsureSuccessStatusCode();

                        using (var infoTrackFileStream = await infoTrackFileDownloadResponse.Content.ReadAsStreamAsync())
                        {
                            if (infoTrackFileStream.Length <= 0)
                            {
                                throw new HttpRequestException($"Unable to download the resource from InfoTrack.");
                            }
                            else
                            {
                                var tokenSetQuery = new TokenSetQuery(message.AuthenticatedUser?.Id, message.ActionstepOrgKey);

                                var fileNameWithExtension = DetermineBestFileNameWithExtension(message, infoTrackFileDownloadResponse);

                                var fileUploadResponse = await _actionstepService.UploadFile(tokenSetQuery, fileNameWithExtension, infoTrackFileStream);

                                if (fileUploadResponse == null)
                                {
                                    throw new Exception("Unknown error uploading document to Actionstep.");
                                }

                                // Get all folders for matter and check to see if the specified folder exists. If it does, we need its ID.
                                var actionFolder     = new ActionFolder(message.MatterId);
                                var getFolderRequest = new GetActionFolderRequest(tokenSetQuery, actionFolder);
                                var folderResponse   = await _actionstepService.Handle <ListActionFolderResponse>(getFolderRequest);

                                /// Will be null if <see cref="SaveResourcesCommand.FolderName"/> wasn't found. In which case the document will be saved at the root of the matter.
                                var parentFolderId = folderResponse.ActionFolders.FirstOrDefault(af => af.Name == message.FolderName)?.Id;

                                /// <see cref="ActionstepDocument"/> represents the object in "Matter Documents", as opposed to the file content from above (which is just in a big bucket).
                                var document = new ActionDocument(message.MatterId, fileNameWithExtension, fileUploadResponse, parentFolderId);

                                var saveRequest = new SaveActionDocumentRequest(tokenSetQuery, document);

                                await _actionstepService.Handle <SaveActionDocumentResponse>(saveRequest);
                            }
                        }
                    }
                }
            }
            public async Task <DocumentRelationship> Handle(CopyDocumentVersionToActionstepCommand request, CancellationToken cancellationToken)
            {
                if (request is null)
                {
                    throw new System.ArgumentNullException(nameof(request));
                }
                _validator.ValidateAndThrow(request);

                var tempFilePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
                DocumentFileInfo globalXDocumentFileInfo;

                var tokenSetQuery = new TokenSetQuery(request.ActionstepUserId, request.ActionstepOrgKey);

                // Check to make sure this matter exists first. This will throw if it's not found.
                var matterInfo = await _actionstepService.Handle <GetActionResponse>(new GetActionRequest(tokenSetQuery, request.ActionstepMatterId));

                if (matterInfo is null)
                {
                    throw new InvalidActionstepMatterException($"Matter '{request.ActionstepMatterId}' was not found in Actionstep.");
                }

                try
                {
                    try
                    {
                        globalXDocumentFileInfo = await _globalXService.DownloadDocument(
                            request.DocumentVersion.DocumentId.Value,
                            request.DocumentVersion.DocumentVersionId.Value,
                            tempFilePath,
                            request.GlobalXUserId);
                    }
                    catch (Exception ex)
                    {
                        throw new FailedToDownloadGlobalXDocumentException(ex);
                    }

                    try
                    {
                        var fileName = !string.IsNullOrEmpty(globalXDocumentFileInfo?.FileName)
                            ? globalXDocumentFileInfo.FileName
                            : request.DocumentVersion.DocumentName;

                        var fileUploadResponse = await _actionstepService.UploadFile(tokenSetQuery, fileName, tempFilePath);

                        if (fileUploadResponse is null)
                        {
                            throw new Exception("Unknown error uploading document to Actionstep.");
                        }

                        // Get all folders for matter and check to see if the specified folder exists. If it does, we need its ID.
                        var actionFolder     = new ActionFolder(request.ActionstepMatterId);
                        var getFolderRequest = new GetActionFolderRequest(tokenSetQuery, actionFolder);
                        var folderResponse   = await _actionstepService.Handle <ListActionFolderResponse>(getFolderRequest);

                        /// Will be null if the folder name wasn't found. In which case the document will be saved at the root of the matter.
                        var parentFolderId = folderResponse?.ActionFolders?.FirstOrDefault(af => af.Name == ActionstepFolderFirstPreference)?.Id ??
                                             folderResponse?.ActionFolders?.FirstOrDefault(af => af.Name == ActionstepFolderSecondPreference)?.Id;

                        /// <see cref="ActionstepDocument"/> represents the object in "Matter Documents", as opposed to the file content from above (which is just in a big bucket).
                        var document = new ActionDocument(request.ActionstepMatterId, fileName, fileUploadResponse, parentFolderId);
                        var saveActionDocumentRequest = new SaveActionDocumentRequest(tokenSetQuery, document);

                        var saveActionDocumentResponse = await _actionstepService.Handle <SaveActionDocumentResponse>(saveActionDocumentRequest);

                        return(new DocumentRelationship(
                                   request.DocumentVersion.DocumentId.Value,
                                   request.DocumentVersion.DocumentVersionId.Value,
                                   fileName,
                                   request.DocumentVersion.MimeType,
                                   request.ActionstepOrgKey,
                                   request.ActionstepMatterId,
                                   saveActionDocumentResponse.ActionDocument.Id,
                                   new Uri(saveActionDocumentResponse.ActionDocument.SharepointUrl)));
                    }
                    catch (InvalidTokenSetException ex)
                    {
                        throw new FailedToUploadGlobalXDocumentToActionstepException(
                                  $"Invalid Actionstep Token, unable to upload document. Token revoked at '{ex.TokenSet?.RevokedAt}', Token ID '{ex.TokenSet?.Id}'.",
                                  ex);
                    }
                    catch (Exception ex)
                    {
                        throw new FailedToUploadGlobalXDocumentToActionstepException(ex.Message, ex);
                    }
                }
                finally
                {
                    if (File.Exists(tempFilePath))
                    {
                        File.Delete(tempFilePath);
                    }
                }
            }