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 DeleteCommissions() { var options = TestHelper.GetDbContext("UpdateCommissionStatement"); var user1 = TestHelper.InsertUserDetailed(options); var statement1 = TestHelper.InsertCommissionStatement(options, user1.Organisation); var user2 = TestHelper.InsertUserDetailed(options); var statement2 = TestHelper.InsertCommissionStatement(options, user2.Organisation); using (var context = new DataContext(options)) { var bulkActions = new Mock <IBulkActions>(MockBehavior.Strict); var actualCommissionStatementId1 = Guid.Empty; bulkActions.Setup(c => c.BatchDeleteCommissionsAsync(It.IsAny <DataContext>(), It.IsAny <Guid>())) .Callback((DataContext c, Guid g1) => actualCommissionStatementId1 = g1) .Returns(Task.CompletedTask); var actualCommissionStatementId2 = Guid.Empty; bulkActions.Setup(c => c.BatchDeleteCommissionErrorsAsync(It.IsAny <DataContext>(), It.IsAny <Guid>())) .Callback((DataContext c, Guid g1) => actualCommissionStatementId2 = g1) .Returns(Task.CompletedTask); var auditService = new AuditServiceMock(); var service = new CommissionStatementService(context, bulkActions.Object, auditService); //When var scopeOptions = TestHelper.GetScopeOptions(user1); await service.DeleteCommissions(scopeOptions, statement1.Id); //Then Assert.Equal(statement1.Id, actualCommissionStatementId1); Assert.Equal(statement1.Id, actualCommissionStatementId2); //Reset actualCommissionStatementId1 = Guid.Empty; actualCommissionStatementId2 = Guid.Empty; // //Out of scope await service.DeleteCommissions(scopeOptions, statement2.Id); Assert.Equal(Guid.Empty, actualCommissionStatementId1); Assert.Equal(Guid.Empty, actualCommissionStatementId2); } }
public async Task <IActionResult> DeleteCommissions(Guid commissionStatementId) { var scope = AuthenticationService.GetScope(User); await CommissionStatementService.DeleteCommissions(scope, commissionStatementId); var path = new CommissionStatementDirectoryPath(scope.OrganisationId, commissionStatementId); var files = await FileStorageService.GetFileInfoListAsync(path); foreach (var file in files) { await FileStorageService.SoftDeleteFile(file.Url); } return(Ok(new Result(true))); }
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)); }