/// <summary>
        /// Handles the specified command.
        /// </summary>
        /// <param name="command">The command.</param>
        public void Handle(DocsUploadCommand command)
        {
            InfoAccumulator info = new InfoAccumulator();
            int             customerId;

            try {
                customerId = CustomerIdEncryptor.DecryptCustomerId(command.CustomerId, command.CommandOriginator);
            } catch (Exception ex) {
                Log.Error(ex.Message);
                info.AddError("Invalid customer id.");
                SendReply(info, command, resp => resp.CustomerId = command.CustomerId);
                return;
            }

            var  fileMetaData = command.Files.Select(path => ConvertToFileMetadata(path, command.IsBankDocuments, customerId));
            bool res          = DocsUploadQueries.SaveCompanyUploadedFiles(fileMetaData);

            if (!res)
            {
                string error = string.Format("could not save some or all uploaded files for customer: {0}", command.CustomerId);
                info.AddError(error);
                RegisterError(info, command);
                throw new Exception("Failed to save some files");//we want to retry
            }

            SendReply(info, command, resp => resp.CustomerId = command.CustomerId);
        }
示例#2
0
        /// <summary>
        /// Handles the upload.
        /// </summary>
        /// <param name="o">The context.</param>
        /// <param name="ct">The cancellation token.</param>
        /// <param name="validator">The validator.</param>
        /// <param name="isBankDocuments">Designates whether the specified document is bank document or not</param>
        /// <returns></returns>
        private async Task <Response> HandleUpload(dynamic o, CancellationToken ct, AbstractValidator <FilesUploadModel> validator, bool isBankDocuments)
        {
            string customerId = o.customerId;

            FilesUploadModel model;

            //Bind
            try {
                model       = this.Bind <FilesUploadModel>();
                model.Files = this.Request.Files;
            } catch (ModelBindingException ex) {
                Log.Warn("binding documents upload request: " + customerId, ex);
                return(CreateErrorResponse(b => b
                                           .WithCustomerId(customerId)
                                           .WithModelBindingException(ex)));
            }

            //Validate
            InfoAccumulator info = Validate(model, validator);

            if (info.HasErrors)
            {
                return(CreateErrorResponse(b => b
                                           .WithCustomerId(customerId)
                                           .WithErrorMessages(info.GetErrors())));
            }

            Dictionary <string, Task <string> > fileToTask = this.Request.Files.ToDictionary(f => f.Name, ProcessFile);

            try {
                var paths = await Task.WhenAll(fileToTask.Values);

                var command = new DocsUploadCommand {
                    CustomerId      = model.CustomerId,
                    Files           = paths,
                    IsBankDocuments = isBankDocuments
                };
                var cts      = new CancellationTokenSource(Config.SendReceiveTaskTimeoutMilis);
                var response = await UploadCommandSendReceive.SendAsync(Config.ServiceAddress, command, cts);

                if (response.HasErrors)
                {
                    return(CreateErrorResponse(b => b
                                               .WithCustomerId(customerId)
                                               .WithErrorMessages(response.Errors)));
                }
            } catch (AggregateException ex) {
                var failedFileNames = fileToTask
                                      .Where(p => p.Value.Exception != null)
                                      .Select(f => f.Key);
                return(CreateErrorResponse(b => b
                                           .WithCustomerId(customerId)
                                           .WithErrorMessages(failedFileNames), HttpStatusCode.InternalServerError));
            }

            return(CreateOkResponse(b => b.WithCustomerId(customerId)));
        }