From c39cec5e3c7aad065694bdbbb2e44d2e1b8e09f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Rua?= <140734849+joao4all@users.noreply.github.com> Date: Thu, 14 Mar 2024 14:43:05 +0000 Subject: [PATCH] Feat #438 [User Management] User profiles are now modifyable (#532) * now user profiles are editable --- .../UserManagementTableTestFixture.cs | 81 ++++++++++++++++++- .../UserManagement/UserManagementTable.razor | 12 +-- .../UserManagementTable.razor.cs | 38 +++++++-- .../IUserManagementTableViewModel.cs | 6 ++ .../UserManagementTableViewModel.cs | 23 +++++- 5 files changed, 147 insertions(+), 13 deletions(-) diff --git a/COMETwebapp.Tests/Components/UserManagement/UserManagementTableTestFixture.cs b/COMETwebapp.Tests/Components/UserManagement/UserManagementTableTestFixture.cs index 766f9b33..5052a495 100644 --- a/COMETwebapp.Tests/Components/UserManagement/UserManagementTableTestFixture.cs +++ b/COMETwebapp.Tests/Components/UserManagement/UserManagementTableTestFixture.cs @@ -257,6 +257,46 @@ public void Teardown() this.messageBus.ClearSubscriptions(); } + [Test] + public async Task VerifyComponent() + { + var renderer = this.context.RenderComponent(); + + Assert.Multiple(() => + { + Assert.That(this.viewModel.DataSource.Count, Is.EqualTo(2)); + Assert.That(renderer.Markup, Does.Contain(this.person.Name)); + Assert.That(renderer.Markup, Does.Contain(this.person1.Name)); + }); + + var grid = renderer.FindComponent(); + var buttons = grid.FindComponents(); + var addNewPersonButton = buttons[0]; + var editPersonButton = buttons[1]; + + await grid.InvokeAsync(addNewPersonButton.Instance.Click.InvokeAsync); + + Assert.Multiple(() => + { + Assert.That(this.viewModel.Person.Name?.Trim(), Is.Empty); + Assert.That(renderer.Instance.ShouldCreatePerson, Is.EqualTo(true)); + }); + + await renderer.InvokeAsync(grid.Instance.EditModelSaving.InvokeAsync); + this.sessionService.Verify(x => x.CreateThings(It.IsAny(), It.Is>(c => c.Contains(this.viewModel.Person))), Times.Once); + + await grid.InvokeAsync(editPersonButton.Instance.Click.InvokeAsync); + + Assert.Multiple(() => + { + Assert.That(this.viewModel.Person.Name, Is.EqualTo(this.viewModel.Rows.Items.First().PersonName)); + Assert.That(renderer.Instance.ShouldCreatePerson, Is.EqualTo(false)); + }); + + await renderer.InvokeAsync(grid.Instance.EditModelSaving.InvokeAsync); + this.sessionService.Verify(x => x.UpdateThing(It.IsAny(), It.Is(c => c == this.viewModel.Person)), Times.Once); + } + [Test] public async Task VerifyActivatingPerson() { @@ -288,6 +328,8 @@ public async Task VerifyActivatingPerson() public async Task VerifyAddingPerson() { var renderer = this.context.RenderComponent(); + this.viewModel.IsDefaultEmail = true; + this.viewModel.IsDefaultTelephoneNumber = true; Assert.Multiple(() => { @@ -302,7 +344,9 @@ public async Task VerifyAddingPerson() Surname = "Test", ShortName = "TT", IsActive = true, - IsDeprecated = false + IsDeprecated = false, + EmailAddress = { new EmailAddress() }, + TelephoneNumber = { new TelephoneNumber() } }; await this.viewModel.AddingPerson(); @@ -311,6 +355,41 @@ public async Task VerifyAddingPerson() Assert.Multiple(() => { Assert.That(this.viewModel.Rows.Count, Is.EqualTo(2)); }); } + [Test] + public async Task VerifyEditingPerson() + { + var renderer = this.context.RenderComponent(); + this.viewModel.IsDefaultEmail = true; + this.viewModel.IsDefaultTelephoneNumber = true; + + Assert.Multiple(() => + { + Assert.That(this.viewModel.DataSource.Count, Is.EqualTo(2)); + Assert.That(renderer.Markup, Does.Contain(this.person.Name)); + Assert.That(renderer.Markup, Does.Contain(this.person1.Name)); + }); + + this.viewModel.Person = new Person + { + GivenName = "Test", + Surname = "Test", + ShortName = "TT", + IsActive = true, + IsDeprecated = false, + EmailAddress = { new EmailAddress() }, + TelephoneNumber = { new TelephoneNumber() } + }; + + await this.viewModel.EditingPerson(); + this.messageBus.SendMessage(new ObjectChangedEvent(this.viewModel.Person, EventKind.Updated)); + + Assert.Multiple(() => + { + this.sessionService.Verify(x => x.UpdateThing(It.IsAny(), this.viewModel.Person), Times.Once); + Assert.Multiple(() => { Assert.That(this.viewModel.Rows.Count, Is.EqualTo(2)); }); + }); + } + [Test] public async Task VerifyDeprecatingPerson() { diff --git a/COMETwebapp/Components/UserManagement/UserManagementTable.razor b/COMETwebapp/Components/UserManagement/UserManagementTable.razor index a20da008..b52246ea 100644 --- a/COMETwebapp/Components/UserManagement/UserManagementTable.razor +++ b/COMETwebapp/Components/UserManagement/UserManagementTable.razor @@ -21,11 +21,10 @@ -------------------------------------------------------------------------------> @using COMETwebapp.ViewModels.Components.UserManagement.Rows @using CDP4Common.SiteDirectoryData -@using COMETwebapp.ViewModels.Components.UserManagement; -@inherits COMET.Web.Common.Components.Applications.ApplicationBase; +@inherits COMET.Web.Common.Components.Applications.ApplicationBase - @@ -60,11 +59,14 @@ - + @{ var row = (PersonRowViewModel)context.DataItem; + + + if (row.IsDeprecated) { diff --git a/COMETwebapp/Components/UserManagement/UserManagementTable.razor.cs b/COMETwebapp/Components/UserManagement/UserManagementTable.razor.cs index 25839fec..95b9da30 100644 --- a/COMETwebapp/Components/UserManagement/UserManagementTable.razor.cs +++ b/COMETwebapp/Components/UserManagement/UserManagementTable.razor.cs @@ -26,7 +26,8 @@ namespace COMETwebapp.Components.UserManagement { using CDP4Common.SiteDirectoryData; - using COMETwebapp.ViewModels.Components.UserManagement; + using COMET.Web.Common.Extensions; + using COMETwebapp.ViewModels.Components.UserManagement.Rows; using DevExpress.Blazor; @@ -47,6 +48,11 @@ public partial class UserManagementTable /// private IGrid Grid { get; set; } + /// + /// Gets the condition to check if a person should be created + /// + public bool ShouldCreatePerson { get; private set; } = true; + /// /// Method invoked when a custom summary calculation is required, allowing you to /// perform custom calculations based on the data displayed in the grid. @@ -73,7 +79,7 @@ public static void CustomSummary(GridCustomSummaryEventArgs e) } /// - /// Method invoked to "Show/Hide Deprecated Items" + /// Method invoked to "Show/Hide Deprecated Items" /// public void HideOrShowDeprecatedItems() { @@ -97,8 +103,9 @@ protected override void OnInitialized() this.ViewModel.OnInitialized(); - this.Disposables.Add(this.ViewModel.Rows.CountChanged.Subscribe(_ => this.InvokeAsync(this.StateHasChanged))); - this.Disposables.Add(this.ViewModel.Rows.Connect().AutoRefresh().Subscribe(_ => this.InvokeAsync(this.StateHasChanged))); + this.Disposables.Add(this.ViewModel.Rows.CountChanged.SubscribeAsync(_ => this.InvokeAsync(this.StateHasChanged))); + this.Disposables.Add(this.ViewModel.WhenAnyValue(x => x.Person).SubscribeAsync(_ => this.InvokeAsync(this.StateHasChanged))); + this.Disposables.Add(this.ViewModel.Rows.Connect().AutoRefresh().SubscribeAsync(_ => this.InvokeAsync(this.StateHasChanged))); } /// @@ -107,14 +114,33 @@ protected override void OnInitialized() /// A private void CustomizeEditPerson(GridCustomizeEditModelEventArgs e) { - var dataItem = (Person)e.DataItem; + var dataItem = (PersonRowViewModel)e.DataItem; + this.ShouldCreatePerson = e.IsNew; if (dataItem == null) { e.EditModel = new Person(); + this.ViewModel.Person = new Person(); + return; + } + + e.EditModel = dataItem; + this.ViewModel.Person = dataItem.Person.Clone(true); + } + + /// + /// Method that is invoked when the edit/add person model form is being saved + /// + /// A + private async Task OnEditModelSaving() + { + if (!this.ShouldCreatePerson) + { + await this.ViewModel.EditingPerson(); + return; } - this.ViewModel.Person = new Person(); + await this.ViewModel.AddingPerson(); } /// diff --git a/COMETwebapp/ViewModels/Components/UserManagement/IUserManagementTableViewModel.cs b/COMETwebapp/ViewModels/Components/UserManagement/IUserManagementTableViewModel.cs index 909e6bca..9870b70b 100644 --- a/COMETwebapp/ViewModels/Components/UserManagement/IUserManagementTableViewModel.cs +++ b/COMETwebapp/ViewModels/Components/UserManagement/IUserManagementTableViewModel.cs @@ -151,5 +151,11 @@ public interface IUserManagementTableViewModel : IApplicationBaseViewModel, IHav /// want the component to refresh when that operation is completed. /// void OnInitialized(); + + /// + /// Tries to edit an existing + /// + /// A + Task EditingPerson(); } } diff --git a/COMETwebapp/ViewModels/Components/UserManagement/UserManagementTableViewModel.cs b/COMETwebapp/ViewModels/Components/UserManagement/UserManagementTableViewModel.cs index 39cb330f..9875c254 100644 --- a/COMETwebapp/ViewModels/Components/UserManagement/UserManagementTableViewModel.cs +++ b/COMETwebapp/ViewModels/Components/UserManagement/UserManagementTableViewModel.cs @@ -27,7 +27,6 @@ namespace COMETwebapp.ViewModels.Components.UserManagement using AntDesign; using CDP4Common.CommonData; - using CDP4Common.EngineeringModelData; using CDP4Common.SiteDirectoryData; using CDP4Dal; @@ -187,6 +186,28 @@ public void OnCancelButtonClick() this.IsOnDeprecationMode = false; } + /// + /// Tries to edit an existing + /// + /// A + public async Task EditingPerson() + { + if (this.IsDefaultEmail) + { + this.Person.DefaultEmailAddress = this.EmailAddress; + } + + if (this.IsDefaultTelephoneNumber) + { + this.Person.DefaultTelephoneNumber = this.TelephoneNumber; + } + + this.Person.EmailAddress.Add(this.EmailAddress); + this.Person.TelephoneNumber.Add(this.TelephoneNumber); + + await this.sessionService.UpdateThing(this.sessionService.GetSiteDirectory(), this.Person); + } + /// /// Tries to create a new ///