diff --git a/changelog.d/+fahrzeug-status-follows-organisation.changed.md b/changelog.d/+fahrzeug-status-follows-organisation.changed.md new file mode 100644 index 00000000..dd9f2c54 --- /dev/null +++ b/changelog.d/+fahrzeug-status-follows-organisation.changed.md @@ -0,0 +1 @@ +Placing a Fahrzeug in a Bereitstellungsraum sets its Kräfte status to Bereitstellungsraum, and placing it in an Abschnitt sets it to Im Einsatz, whether it is placed in Organisation or from the Kräfte list (#827). diff --git a/src/LageBuch.AppLogic/Services/UnitPlacement.cs b/src/LageBuch.AppLogic/Services/UnitPlacement.cs new file mode 100644 index 00000000..b59fb2a8 --- /dev/null +++ b/src/LageBuch.AppLogic/Services/UnitPlacement.cs @@ -0,0 +1,72 @@ +using LageBuch.Domain.Leadership; +using LageBuch.Sync; + +namespace LageBuch.AppLogic.Services; + +/// +/// Places a unit in a node and brings its status along (#827). A Fahrzeug placed in a +/// Bereitstellungsraum is in the Bereitstellungsraum, and one placed in an Abschnitt is im Einsatz, +/// so the Kräfte tab must not go on showing "Auf Anfahrt" after Organisation has put it to work. +/// +/// This lives here rather than in Incident.MoveForceUnit for two reasons. The status takes +/// the Stammdaten's own spelling, and only AppLogic sees the Stammdaten. And it is two existing +/// session calls, so a joined client needs no new command and the sync protocol is unchanged. The +/// ETB gets two lines for it, the move and the status change, which is what happened. +/// +/// +public static class UnitPlacement +{ + /// The status a unit placed in a Bereitstellungsraum gets. + public const string BereitstellungsraumStatus = "Bereitstellungsraum"; + + /// The status a unit placed in an Abschnitt or Unterabschnitt gets. + public const string ImEinsatzStatus = "Im Einsatz"; + + /// + /// The status placement in a node of implies, spelt the way + /// spells it, or null when the kind implies none. The fixed name is + /// used as it is when the catalogue does not list it, so a fresh install without Stammdaten + /// still gets one. + /// + public static string? ImpliedStatus(CommandNodeKind kind, IReadOnlyList catalogue) + { + ArgumentNullException.ThrowIfNull(catalogue); + + return kind switch + { + CommandNodeKind.Bereitstellung => StammdatenCatalogue.Normalize(BereitstellungsraumStatus, catalogue), + CommandNodeKind.Abschnitt or CommandNodeKind.Unterabschnitt => + StammdatenCatalogue.Normalize(ImEinsatzStatus, catalogue), + _ => null, + }; + } + + /// + /// Moves to (null = takes it out of its + /// node), then sets the status the new node implies. Taking a unit out leaves its status alone: + /// nothing says what it is doing now. The session's refusals propagate as they are, before any + /// status is written. + /// + public static void Move(IIncidentSession session, IReadOnlyList statusCatalogue, Guid unitId, Guid? nodeId) + { + ArgumentNullException.ThrowIfNull(session); + ArgumentNullException.ThrowIfNull(statusCatalogue); + + // Read before the move: a joined client's Incident may not show the move yet afterwards. + var unit = session.Incident.Forces.FirstOrDefault(f => f.Id == unitId); + var target = nodeId is { } id ? session.Incident.CommandNodes.FirstOrDefault(n => n.Id == id) : null; + + session.MoveForceUnit(unitId, nodeId); + + if (unit is null || target is null || unit.NodeId == nodeId) + { + return; + } + + var status = ImpliedStatus(target.Kind, statusCatalogue); + if (status is not null && !string.Equals(status, unit.Status, StringComparison.Ordinal)) + { + session.UpdateForceUnit(unitId, status, unit.Notes); + } + } +} diff --git a/src/LageBuch.AppLogic/ViewModels/ForcesViewModel.cs b/src/LageBuch.AppLogic/ViewModels/ForcesViewModel.cs index 314cd9d2..80682b90 100644 --- a/src/LageBuch.AppLogic/ViewModels/ForcesViewModel.cs +++ b/src/LageBuch.AppLogic/ViewModels/ForcesViewModel.cs @@ -834,7 +834,7 @@ private ForceRow ToRow(Domain.ForceUnit f) => ActionError = null; try { - _session.MoveForceUnit(f.Id, nodeId); + UnitPlacement.Move(_session, StatusOptions, f.Id, nodeId); } catch (Exception ex) when (ex is InvalidOperationException or ArgumentException or KeyNotFoundException) { diff --git a/src/LageBuch.AppLogic/ViewModels/LeadershipViewModel.cs b/src/LageBuch.AppLogic/ViewModels/LeadershipViewModel.cs index b1f4dafb..a5f01286 100644 --- a/src/LageBuch.AppLogic/ViewModels/LeadershipViewModel.cs +++ b/src/LageBuch.AppLogic/ViewModels/LeadershipViewModel.cs @@ -308,7 +308,7 @@ public bool MoveUnit(Guid unitId, Guid? nodeId) return false; } - return Run(() => _session.MoveForceUnit(unitId, nodeId)); + return Run(() => UnitPlacement.Move(_session, _masterData.UnitStatus, unitId, nodeId)); } /// A drag of began; ignored on a read-only Einsatz. diff --git a/tests/LageBuch.AppLogic.Tests/ForcesViewModelTests.cs b/tests/LageBuch.AppLogic.Tests/ForcesViewModelTests.cs index b6d0ff4b..e02cb4e1 100644 --- a/tests/LageBuch.AppLogic.Tests/ForcesViewModelTests.cs +++ b/tests/LageBuch.AppLogic.Tests/ForcesViewModelTests.cs @@ -1096,6 +1096,19 @@ public void Selecting_an_Abschnitt_moves_the_unit_and_the_row_label_follows() Assert.Null(Assert.Single(vm.Forces[1].NodeChoices, c => c.IsCurrent).NodeId); } + [Fact] + public void Selecting_an_Abschnitt_sets_the_status_to_im_einsatz() + { + var (vm, session, _) = NewVmWithUnits(); + var row = vm.Forces[0]; + var nord = session.Incident.CommandNodes.Single(); + + row.NodeChoices.Single(c => c.NodeId == nord.Id).SelectCommand.Execute(null); + + Assert.Equal("Im Einsatz", session.Incident.Forces.Single(f => f.Id == row.Id).Status); + Assert.Equal("Im Einsatz", row.Status); + } + [Fact] public void Selecting_the_current_entry_does_nothing() { diff --git a/tests/LageBuch.AppLogic.Tests/LeadershipBoardCommandTests.cs b/tests/LageBuch.AppLogic.Tests/LeadershipBoardCommandTests.cs index 3c84dcaa..9a0ed3db 100644 --- a/tests/LageBuch.AppLogic.Tests/LeadershipBoardCommandTests.cs +++ b/tests/LageBuch.AppLogic.Tests/LeadershipBoardCommandTests.cs @@ -36,6 +36,19 @@ public void A_unit_can_be_moved_to_another_node_or_unplaced() Assert.True(vm.CanMoveUnit(unit, null)); } + [Fact] + public void Moving_a_unit_into_a_bereitstellungsraum_sets_its_kraefte_status() + { + var session = NewSession(); + var br = AddNode(session, CommandNodeKind.Bereitstellung, "BR Nord"); + var unit = AddUnit(session, "FF Emmering", "Florian Emmering 40/1"); + using var vm = NewVm(session, masterData: MasterDataSet.Empty with { UnitStatus = ["Alarmiert", "Bereitstellungsraum"] }); + + Assert.True(vm.MoveUnit(unit, br)); + + Assert.Equal("Bereitstellungsraum", session.Incident.Forces.Single().Status); + } + [Fact] public void A_unit_cannot_be_moved_on_a_read_only_incident() { diff --git a/tests/LageBuch.AppLogic.Tests/UnitPlacementTests.cs b/tests/LageBuch.AppLogic.Tests/UnitPlacementTests.cs new file mode 100644 index 00000000..8d93a220 --- /dev/null +++ b/tests/LageBuch.AppLogic.Tests/UnitPlacementTests.cs @@ -0,0 +1,174 @@ +using LageBuch.AppLogic.Services; +using LageBuch.Domain.Leadership; +using static LageBuch.AppLogic.Tests.LeadershipBoard; + +namespace LageBuch.AppLogic.Tests; + +// #827: where Organisation places a Fahrzeug decides what the Kräfte tab shows as its status. +public class UnitPlacementTests +{ + private static readonly string[] Catalogue = ["Alarmiert", "Auf Anfahrt", "Bereitstellungsraum", "Im Einsatz"]; + + [Fact] + public void Placing_a_unit_in_a_bereitstellungsraum_sets_its_status_to_bereitstellungsraum() + { + var session = NewSession(); + var br = AddNode(session, CommandNodeKind.Bereitstellung, "BR Nord"); + var unit = AddUnit(session, "FF Emmering", "Florian Emmering 40/1"); + + UnitPlacement.Move(session, Catalogue, unit, br); + + var placed = session.Incident.Forces.Single(); + Assert.Equal(br, placed.NodeId); + Assert.Equal("Bereitstellungsraum", placed.Status); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public void Placing_a_unit_in_an_abschnitt_sets_its_status_to_im_einsatz(bool unterabschnitt) + { + var session = NewSession(); + var target = AddNode(session, CommandNodeKind.Abschnitt, "Nord"); + if (unterabschnitt) + { + target = AddNode(session, CommandNodeKind.Unterabschnitt, "Nord-West", target); + } + + var unit = AddUnit(session, "FF Emmering", "Florian Emmering 40/1"); + + UnitPlacement.Move(session, Catalogue, unit, target); + + Assert.Equal("Im Einsatz", session.Incident.Forces.Single().Status); + } + + [Fact] + public void Moving_from_bereitstellungsraum_to_abschnitt_logs_the_move_and_the_status_change() + { + var session = NewSession(); + var br = AddNode(session, CommandNodeKind.Bereitstellung, "BR Nord"); + var nord = AddNode(session, CommandNodeKind.Abschnitt, "Nord"); + var unit = AddUnit(session, "FF Emmering", "Florian Emmering 40/1"); + UnitPlacement.Move(session, Catalogue, unit, br); + var before = session.Incident.Journal.Count; + + UnitPlacement.Move(session, Catalogue, unit, nord); + + var texts = session.Incident.Journal.Skip(before).Select(e => e.Text).ToList(); + Assert.Equal(2, texts.Count); + Assert.EndsWith("→ Nord", texts[0], StringComparison.Ordinal); + Assert.EndsWith("Status Bereitstellungsraum → Im Einsatz", texts[1], StringComparison.Ordinal); + } + + [Fact] + public void Unplacing_a_unit_leaves_its_status_alone() + { + var session = NewSession(); + var nord = AddNode(session, CommandNodeKind.Abschnitt, "Nord"); + var unit = AddUnit(session, "FF Emmering", "Florian Emmering 40/1"); + UnitPlacement.Move(session, Catalogue, unit, nord); + + UnitPlacement.Move(session, Catalogue, unit, null); + + var unplaced = session.Incident.Forces.Single(); + Assert.Null(unplaced.NodeId); + Assert.Equal("Im Einsatz", unplaced.Status); + } + + [Fact] + public void A_unit_already_im_einsatz_gets_no_second_status_entry() + { + var session = NewSession(); + var nord = AddNode(session, CommandNodeKind.Abschnitt, "Nord"); + var sued = AddNode(session, CommandNodeKind.Abschnitt, "Süd"); + var unit = AddUnit(session, "FF Emmering", "Florian Emmering 40/1"); + UnitPlacement.Move(session, Catalogue, unit, nord); + var before = session.Incident.Journal.Count; + + UnitPlacement.Move(session, Catalogue, unit, sued); + + var text = Assert.Single(session.Incident.Journal.Skip(before)).Text; + Assert.EndsWith("→ Süd", text, StringComparison.Ordinal); + } + + [Fact] + public void Moving_a_unit_to_where_it_already_is_does_not_touch_its_status() + { + var session = NewSession(); + var nord = AddNode(session, CommandNodeKind.Abschnitt, "Nord"); + var unit = AddUnit(session, "FF Emmering", "Florian Emmering 40/1", nord); + session.UpdateForceUnit(unit, "Auf Anfahrt", null); + var before = session.Incident.Journal.Count; + + UnitPlacement.Move(session, Catalogue, unit, nord); + + Assert.Equal("Auf Anfahrt", session.Incident.Forces.Single().Status); + Assert.Equal(before, session.Incident.Journal.Count); + } + + [Fact] + public void The_status_takes_the_catalogue_spelling() + { + var session = NewSession(); + var nord = AddNode(session, CommandNodeKind.Abschnitt, "Nord"); + var unit = AddUnit(session, "FF Emmering", "Florian Emmering 40/1"); + + UnitPlacement.Move(session, ["alarmiert", "im einsatz"], unit, nord); + + Assert.Equal("im einsatz", session.Incident.Forces.Single().Status); + } + + [Fact] + public void An_empty_catalogue_still_gets_the_fixed_status() + { + var session = NewSession(); + var br = AddNode(session, CommandNodeKind.Bereitstellung, "BR Nord"); + var unit = AddUnit(session, "FF Emmering", "Florian Emmering 40/1"); + + UnitPlacement.Move(session, [], unit, br); + + Assert.Equal("Bereitstellungsraum", session.Incident.Forces.Single().Status); + } + + [Fact] + public void The_bemerkung_survives_the_status_change() + { + var session = NewSession(); + var nord = AddNode(session, CommandNodeKind.Abschnitt, "Nord"); + var unit = AddUnit(session, "FF Emmering", "Florian Emmering 40/1"); + session.UpdateForceUnit(unit, "Auf Anfahrt", "über B2"); + + UnitPlacement.Move(session, Catalogue, unit, nord); + + var placed = session.Incident.Forces.Single(); + Assert.Equal("Im Einsatz", placed.Status); + Assert.Equal("über B2", placed.Notes); + } + + [Fact] + public void An_unknown_unit_or_node_surfaces_the_sessions_refusal() + { + var session = NewSession(); + var nord = AddNode(session, CommandNodeKind.Abschnitt, "Nord"); + var unit = AddUnit(session, "FF Emmering", "Florian Emmering 40/1"); + + Assert.Throws(() => UnitPlacement.Move(session, Catalogue, Guid.NewGuid(), nord)); + Assert.Throws(() => UnitPlacement.Move(session, Catalogue, unit, Guid.NewGuid())); + } + + [Fact] + public void A_refused_move_changes_no_status() + { + var session = NewSession(); + var nord = AddNode(session, CommandNodeKind.Abschnitt, "Nord"); + session.DissolveCommandNode(nord); + var unit = AddUnit(session, "FF Emmering", "Florian Emmering 40/1"); + session.UpdateForceUnit(unit, "Auf Anfahrt", null); + var before = session.Incident.Journal.Count; + + Assert.Throws(() => UnitPlacement.Move(session, Catalogue, unit, nord)); + + Assert.Equal("Auf Anfahrt", session.Incident.Forces.Single().Status); + Assert.Equal(before, session.Incident.Journal.Count); + } +}