/// <summary> /// HMRCs the vat upload. /// </summary> private void HmrcVatUpload() { Post["UploadHmrcPdf", "api/v1/marketplace/hmrc/upload/vat/{customerId}", runAsync : true] = async(o, ct) => { string customerId = o.customerId; FilesUploadModel model; //Bind try { model = this.Bind <FilesUploadModel>(); model.Files = this.Request.Files; } catch (ModelBindingException ex) { Log.Warn("binding error on hmrc registration request: " + customerId, ex); return(CreateErrorResponse(b => b .WithCustomerId(customerId) .WithModelBindingException(ex))); } //Validate InfoAccumulator info = Validate(model, PdfValidator); 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 HmrcProcessUploadedFilesCommand { CustomerId = model.CustomerId, Files = paths }; var cts = new CancellationTokenSource(Config.SendReceiveTaskTimeoutMilis); var response = await ProcessUploadedFilesSendReceive.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))); }; }
/// <summary> /// Validates the specified command. /// </summary> /// <param name="command">The command.</param> /// <returns></returns> private InfoAccumulator Validate(HmrcProcessUploadedFilesCommand command) { InfoAccumulator info = new InfoAccumulator(); if (CollectionUtils.IsEmpty(command.Files)) { info.AddError("no files to parse"); } return(info); }
/// <summary> /// Handles the specified command. /// </summary> /// <param name="command">The command.</param> public void Handle(HmrcProcessUploadedFilesCommand command) { InfoAccumulator info = Validate(command); if (info.HasErrors) { SendReply(info, command); return; } var vatReturns = ParseVatReturns(command.Files, info); if (info.HasErrors) { SendReply(info, command); return; } int customerId = int.Parse(EncryptionUtils.SafeDecrypt(command.CustomerId)); Customer customer = CustomerQueries.GetCustomerById(customerId); info = MarketPlaceQueries.ValidateCustomerMarketPlace(HmrcInternalId, customer.Name); if (info.HasErrors) { SendReply(info, command); return; } AccountModel hmrcAccountModel = new AccountModel { }; //TODO check it out what to put here byte[] securityData = SerializationUtils.SerializeToBinaryXml(hmrcAccountModel); securityData = EncryptionUtils.Encrypt(securityData); int marketplaceId = (int)MarketPlaceQueries.CreateNewMarketPlace(customerId, customer.Name, securityData, HmrcInternalId); if (marketplaceId < 1) { string msg = string.Format("could not create marketplace for customer {0}", command.CustomerId); //writes encrypted customer id Log.Error(msg); throw new Exception(msg); } var updateHistory = new CustomerMarketPlaceUpdateHistory() { CustomerMarketPlaceId = marketplaceId, UpdatingStart = DateTime.UtcNow }; int marketPlaceHistoryId = (int)MarketPlaceQueries.UpsertMarketPlaceUpdatingHistory(updateHistory); if (marketPlaceHistoryId < 1) { string message = string.Format("could not upsert marketplace history for customer: {0}", command.CustomerId); Log.Error(message); throw new Exception(message); } bool res = HmrcQueries.SaveVatReturns(vatReturns, null, marketplaceId, marketPlaceHistoryId); if (!res) { throw new Exception("could not save vat returns"); } updateHistory = new CustomerMarketPlaceUpdateHistory() { CustomerMarketPlaceId = marketplaceId, UpdatingEnd = DateTime.UtcNow }; marketPlaceHistoryId = (int)MarketPlaceQueries.UpsertMarketPlaceUpdatingHistory(updateHistory); if (marketPlaceHistoryId < 1) { throw new Exception("could not save marketplace history"); } SendReply(info, command); }