public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest request, ILogger log) { Guid organisationId = Guid.Parse(request.Query["organisationId"]); Guid commissionStatementId = Guid.Parse(request.Query["commissionStatementId"]); Guid commissionStatementTemplateId = Guid.Parse(request.Query["commissionStatementTemplateId"]); var scope = new ScopeOptions(organisationId, Guid.Empty, Guid.Empty, Scope.Organisation); var statement = await CommissionStatementService.GetCommissionStatement(scope, commissionStatementId); if (statement == null) { return(new NotFoundObjectResult(commissionStatementId)); } var path = new CommissionStatementPath(scope.OrganisationId, commissionStatementId); var files = await FileStorageService.GetFilesAsync(path); if (!files.Any()) { return(Utils.GetBadRequestObject("Reimport failed as there are no existing statement files.", commissionStatementId.ToString())); } var queryOptions = new CommissionStatementTemplateQueryOptions("", "", 0, 0); queryOptions.CompanyId.Add(statement.CompanyId.Value); queryOptions.Date = statement.Date; var templates = (await CommissionStatementTemplateService.GetTemplates(queryOptions)).Items; if (!templates.Any(t => t.Id == commissionStatementTemplateId)) { return(Utils.GetBadRequestObject("Reimport failed as the commissionStatementTemplateId is not valid.", commissionStatementTemplateId.ToString())); } var template = await CommissionStatementTemplateService.GetTemplate(commissionStatementTemplateId); await CommissionStatementService.DeleteCommissions(scope, commissionStatementId); var result = new ImportResult(); foreach (var fileInfo in files) { using (var stream = new MemoryStream()) { await FileStorageService.GetFile(fileInfo.Url, stream); var vatRate = await DirectoryLookupService.GetVATRate(statement.Date ?? DateTime.Now); var reader = new CommissionImportReader(template.Config, vatRate); var items = reader.Read(stream); result = await CommissionImportService.ImportCommissions(scope, commissionStatementId, items); } } return(new OkObjectResult(result)); }
public async Task GetTemplate() { var options = TestHelper.GetDbContext("GetTemplate"); //Given var temp1 = new CommissionStatementTemplateEntity { Id = Guid.NewGuid(), CompanyId = Guid.NewGuid(), Name = "Template 1", StartDate = DateTime.Now.AddDays(-1), EndDate = DateTime.Now.AddDays(1), BrokerSpecific = true, Config = new Config() }; var temp2 = new CommissionStatementTemplateEntity { Id = Guid.NewGuid() }; using (var context = new DataContext(options)) { context.CommissionStatementTemplate.Add(temp2); context.CommissionStatementTemplate.Add(temp1); context.SaveChanges(); } using (var context = new DataContext(options)) { var service = new CommissionStatementTemplateService(context, null, null); //When var actual = await service.GetTemplate(temp1.Id); //Then Assert.Equal(temp1.Id, actual.Id); Assert.Equal(temp1.Name, actual.Name); Assert.Equal(temp1.CompanyId, actual.CompanyId); Assert.Equal(temp1.Config, actual.Config); Assert.Equal(temp1.StartDate, actual.StartDate); Assert.Equal(temp1.EndDate, actual.EndDate); Assert.Equal(temp1.BrokerSpecific, actual.BrokerSpecific); } }
public async Task <IActionResult> Import(Guid commissionStatementId, [FromQuery] Guid commissionStatementTemplateId, [FromQuery] Guid?userId) { var scope = AuthenticationService.GetScope(User); var file = Request.Form.Files.FirstOrDefault(); if (file == null) { return(BadRequest()); } var statement = await CommissionStatementService.GetCommissionStatement(scope, commissionStatementId); var template = await CommissionStatementTemplateService.GetTemplate(commissionStatementTemplateId); string brokerFullName = null; if (template.BrokerSpecific) { if (!userId.HasValue) { return(this.BadRequestMessage("UserId required for broker specific templates.")); } var user = await UserService.GetUser(scope, userId.Value); if (user == null) { return(this.BadRequestMessage("Invalid UserId.")); } brokerFullName = $"{user.FirstName} {user.LastName}"; } var config = template.Config; var result = new ImportResult(); using (var stream = file.OpenReadStream()) { var vatRate = await DirectoryLookupService.GetVATRate(statement.Date ?? DateTime.Now); var reader = new CommissionImportReader(config, vatRate, brokerFullName); var items = reader.Read(stream); result = await CommissionImportService.ImportCommissions(scope, commissionStatementId, items); } if (result.UnknownCommissionTypeValues.Any()) { using (var stream = file.OpenReadStream()) { var attachment = GetEmailAttachment(file, stream); await SendUnkownCommissionTypesEmail(result, scope, commissionStatementId, template, attachment); } } if (!result.Results.Any(r => r.Success)) { using (var stream = file.OpenReadStream()) { var attachment = GetEmailAttachment(file, stream); await SendZeroEntriesEmail(scope, commissionStatementId, template, attachment); } } if (result.Results.Any()) { using (var stream = file.OpenReadStream()) { var path = new CommissionStatementFilePath(scope.OrganisationId, commissionStatementId, file.FileName); await FileStorageService.AddFileAsync(path, stream); } } return(Ok(result)); }
public async Task <IActionResult> Reimport(Guid commissionStatementId, [FromQuery] Guid commissionStatementTemplateId, [FromQuery] Guid?userId) { var scope = AuthenticationService.GetScope(User); var statement = await CommissionStatementService.GetCommissionStatement(scope, commissionStatementId); if (statement == null) { return(NotFound()); } var path = new CommissionStatementDirectoryPath(scope.OrganisationId, commissionStatementId); var fileInfoList = await FileStorageService.GetFileInfoListAsync(path); if (!fileInfoList.Any()) { return(this.BadRequestMessage("Reimport failed as there are no existing statement files.")); } var queryOptions = new CommissionStatementTemplateQueryOptions("", "", 0, 0); queryOptions.CompanyId.Add(statement.CompanyId.Value); queryOptions.Date = statement.Date; var templates = (await CommissionStatementTemplateService.GetTemplates(queryOptions)).Items; if (!templates.Any(t => t.Id == commissionStatementTemplateId)) { return(this.BadRequestMessage("Reimport failed as the commissionStatementTemplateId is not valid.")); } var template = await CommissionStatementTemplateService.GetTemplate(commissionStatementTemplateId); string brokerFullName = null; if (template.BrokerSpecific) { if (!userId.HasValue) { return(this.BadRequestMessage("UserId required for broker specific templates.")); } var user = await UserService.GetUser(scope, userId.Value); if (user == null) { return(this.BadRequestMessage("Invalid UserId.")); } brokerFullName = $"{user.FirstName} {user.LastName}"; } await CommissionStatementService.DeleteCommissions(scope, commissionStatementId); var result = new ImportResult(); foreach (var fileInfo in fileInfoList) { using (var stream = new MemoryStream()) { await FileStorageService.GetFile(fileInfo.Url, stream); var vatRate = await DirectoryLookupService.GetVATRate(statement.Date ?? DateTime.Now); var reader = new CommissionImportReader(template.Config, vatRate, brokerFullName); var items = reader.Read(stream); result = await CommissionImportService.ImportCommissions(scope, commissionStatementId, items); if (result.UnknownCommissionTypeValues.Any()) { stream.Position = 0; using (var copy = new MemoryStream()) { await stream.CopyToAsync(copy); copy.Position = 0; var attachment = GetEmailAttachmentFromCloud(fileInfo, copy); await SendUnkownCommissionTypesEmail(result, scope, commissionStatementId, template, attachment); } } if (!result.Results.Any(r => r.Success)) { stream.Position = 0; using (var copy = new MemoryStream()) { await stream.CopyToAsync(copy); copy.Position = 0; var attachment = GetEmailAttachmentFromCloud(fileInfo, copy); await SendZeroEntriesEmail(scope, commissionStatementId, template, attachment); } } } } return(Ok(result)); }