public async Task <IActionResult> MergeClients([FromBody] MergeClients merge) { var scope = AuthenticationService.GetScope(User); var result = await ClientService.MergeClients(scope, merge); if (!result.Success) { return(BadRequest(result.ValidationFailures)); } return(Ok(result)); }
public async Task MergeClients_ScopeCheck() { var options = TestHelper.GetDbContext("MergeClients_ScopeCheck"); //Given var user = TestHelper.InsertUserDetailed(options); var clientSource1 = TestHelper.InsertClient(options, user.Organisation, "8210035032082"); var clientSource2 = TestHelper.InsertClient(options, user.Organisation); var user2 = TestHelper.InsertUserDetailed(options); var target = new ClientEdit { ClientTypeId = ClientType.CLIENT_TYPE_INDIVIDUAL, IdNumber = "8210035032082" }; using (var context = new DataContext(options)) { context.SaveChanges(); } using (var context = new DataContext(options)) { var auditService = new AuditServiceMock(); var service = new ClientService(context, auditService); var merge = new MergeClients() { TargetClient = target, SourceClientIds = new List <Guid>() { clientSource1.Client.Id, clientSource2.Client.Id } }; //When var scope = TestHelper.GetScopeOptions(user2.Organisation.Id); var result = await service.MergeClients(scope, merge); //Then Assert.False(result.Success); Assert.Equal("SourceClientIds", result.ValidationFailures[0].PropertyName); Assert.Equal("Invalid Source Client Ids", result.ValidationFailures[0].ErrorMessage); } }
public async Task MergeClients() { var client = new ClientEdit() { Id = Guid.NewGuid(), }; var merge = new MergeClients() { TargetClient = client, SourceClientIds = new List <Guid>() { Guid.NewGuid(), Guid.NewGuid() } }; var service = new Mock <IClientService>(); var authService = TestHelper.MockAuthenticationService(Scope.Branch); var result = new Result() { Success = true }; ScopeOptions options = null; MergeClients merged = null; service.Setup(c => c.MergeClients(It.IsAny <ScopeOptions>(), It.Is <MergeClients>(m => m == merge))) .Callback((ScopeOptions o, MergeClients m) => { merged = m; options = o; }) .ReturnsAsync(result); var controller = new MergeController(service.Object, null, authService.Object); var actual = await controller.MergeClients(merge); Assert.Same(merge, merged); Assert.Equal(Scope.Branch, options.Scope); var okResult = Assert.IsType <OkObjectResult>(actual); var returnValue = Assert.IsType <Result>(okResult.Value); Assert.Same(result, returnValue); }
public async Task <Result> MergeClients(ScopeOptions scope, MergeClients merge) { var clientValidator = new ClientValidator(_context, scope, true); var result = clientValidator.Validate(merge.TargetClient, ruleSet: "default").GetResult(); if (!result.Success) { return(result); } var mergeValidator = new MergeClientsValidator(_context, scope); result = mergeValidator.Validate(merge).GetResult(); if (!result.Success) { return(result); } var strategy = _context.Database.CreateExecutionStrategy(); result = await strategy.ExecuteAsync <Result>(async() => { using (var transaction = _context.Database.BeginTransaction()) { try { //Insert the 'new' client var entity = MapModelToEntity(merge.TargetClient); entity.OrganisationId = scope.OrganisationId; await _context.Client.AddAsync(entity); await _context.SaveChangesAsync(); merge.TargetClient.Id = entity.Id; //Move dependancies to the new client ----------------------------------------------------- //1. Policies var policies = await _context.Policy.Where(p => merge.SourceClientIds.Contains(p.ClientId)).ToListAsync(); foreach (var policy in policies) { policy.ClientId = merge.TargetClient.Id.Value; } //2. Contacts var contacts = await _context.Contact.Where(c => merge.SourceClientIds.Contains(c.ClientId)).ToListAsync(); foreach (var contact in contacts) { contact.ClientId = merge.TargetClient.Id.Value; } //3. Commission Errors var commissionErrors = await _context.CommissionError.Where(c => merge.SourceClientIds.Contains(c.ClientId.Value)).ToListAsync(); foreach (var commissionError in commissionErrors) { commissionError.ClientId = merge.TargetClient.Id.Value; } //4. To Commission Allocations var toCommissionAllocations = await _context.CommissionAllocation.Where(c => merge.SourceClientIds.Contains(c.ToClientId)).ToListAsync(); foreach (var toCommissionAllocation in toCommissionAllocations) { toCommissionAllocation.ToClientId = merge.TargetClient.Id.Value; } //5. From Commission Allocations var fromCommissionAllocations = await _context.CommissionAllocation.Where(c => merge.SourceClientIds.Contains(c.FromClientId)).ToListAsync(); foreach (var fromCommissionAllocation in fromCommissionAllocations) { fromCommissionAllocation.FromClientId = merge.TargetClient.Id.Value; } await _context.SaveChangesAsync(); //---------------------------------------------------------------------------------------- //Delete 'old' clients var clientToDelete = await _context.Client.Where(m => merge.SourceClientIds.Contains(m.Id)).ToListAsync(); foreach (var client in clientToDelete) { client.IsDeleted = true; } await _context.SaveChangesAsync(); //Commit await transaction.CommitAsync(); return(new Result(true)); } catch { transaction.Rollback(); return(new Result(false)); } } }); result.Tag = merge.TargetClient; await _auditService.InsertAuditLog(scope, "Merge", "Client", merge.TargetClient.Id, merge); return(result); }
public async Task MergeClients() { var options = TestHelper.GetDbContext("MergeClients"); //Given var user = TestHelper.InsertUserDetailed(options); var clientSource1 = TestHelper.InsertClient(options, user.Organisation, "8210035032082"); var clientSource2 = TestHelper.InsertClient(options, user.Organisation); var client3 = TestHelper.InsertClient(options, user.Organisation); var user2 = TestHelper.InsertUserDetailed(options); var client4 = TestHelper.InsertClient(options, user2.Organisation, "8210035032082"); //Same Id but different organisation var target = new ClientEdit { ClientTypeId = ClientType.CLIENT_TYPE_INDIVIDUAL, FirstName = "FN 1", LastName = "LN 1", MaidenName = "MN 1", Initials = "INI 1", PreferredName = "PN 1", IdNumber = "8210035032082", DateOfBirth = new DateTime(1982, 10, 3), TaxNumber = "889977", MarritalStatusId = Guid.NewGuid(), MarriageDate = new DateTime(2009, 11, 13), }; using (var context = new DataContext(options)) { context.SaveChanges(); } using (var context = new DataContext(options)) { var auditService = new AuditServiceMock(); var service = new ClientService(context, auditService); var merge = new MergeClients() { TargetClient = target, SourceClientIds = new List <Guid>() { clientSource1.Client.Id, clientSource2.Client.Id } }; //When var scope = TestHelper.GetScopeOptions(user.Organisation.Id); var result = await service.MergeClients(scope, merge); //Then Assert.True(result.Success); //Check new client added var clientId = ((ClientEdit)result.Tag).Id; var actual = context.Client.Find(clientId); Assert.Equal(target.FirstName, actual.FirstName); Assert.Equal(target.LastName, actual.LastName); Assert.Equal(target.MaidenName, actual.MaidenName); Assert.Equal(target.Initials, actual.Initials); Assert.Equal(target.PreferredName, actual.PreferredName); Assert.Equal(target.IdNumber, actual.IdNumber); Assert.Equal(target.DateOfBirth, actual.DateOfBirth); Assert.Equal(target.TaxNumber, actual.TaxNumber); Assert.Equal(target.MarritalStatusId, actual.MarritalStatusId); Assert.Equal(target.MarriageDate, actual.MarriageDate); Assert.False(actual.IsDeleted); //Check old clients deleted actual = context.Client.Find(clientSource1.Client.Id); Assert.True(actual.IsDeleted); actual = context.Client.Find(clientSource2.Client.Id); Assert.True(actual.IsDeleted); //Dummy un-effected actual = context.Client.Find(client3.Client.Id); Assert.False(actual.IsDeleted); //Different Organisation un-effected actual = context.Client.Find(client4.Client.Id); Assert.False(actual.IsDeleted); } }