From 9bf53192da36d7b2bb0d30322915ca120dab7c7e Mon Sep 17 00:00:00 2001 From: Jared Goodwin Date: Wed, 26 Jul 2023 15:30:25 -0700 Subject: [PATCH] Reconfigure script relationships. --- Server/Data/AppDb.cs | 8 ++++++++ Shared/Models/SavedScript.cs | 6 ++++++ Shared/Models/ScriptResult.cs | 3 +++ Shared/Models/ScriptRun.cs | 6 ++++++ Tests/Server.Tests/DataServiceTests.cs | 5 ++--- 5 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Server/Data/AppDb.cs b/Server/Data/AppDb.cs index a308ce8f9..7ec77eea5 100644 --- a/Server/Data/AppDb.cs +++ b/Server/Data/AppDb.cs @@ -161,6 +161,10 @@ protected override void OnModelCreating(ModelBuilder builder) .HasMany(x => x.Results) .WithOne(x => x.ScriptRun) .IsRequired(false); + builder.Entity() + .HasOne(x => x.SavedScript) + .WithMany(x => x.ScriptRuns) + .IsRequired(false); builder.Entity() .Property(x => x.ErrorOutput) @@ -181,6 +185,10 @@ protected override void OnModelCreating(ModelBuilder builder) .HasOne(x => x.ScriptRun) .WithMany(x => x.Results) .IsRequired(false); + builder.Entity() + .HasOne(x => x.SavedScript) + .WithMany(x => x.ScriptResults) + .IsRequired(false); builder.Entity() .HasOne(x => x.User) diff --git a/Shared/Models/SavedScript.cs b/Shared/Models/SavedScript.cs index 9e31da0d6..2d91fedb0 100644 --- a/Shared/Models/SavedScript.cs +++ b/Shared/Models/SavedScript.cs @@ -47,4 +47,10 @@ public class SavedScript public string? SendErrorEmailTo { get; set; } public ScriptingShell Shell { get; set; } + + [JsonIgnore] + public List? ScriptRuns { get; set; } + + [JsonIgnore] + public List? ScriptResults { get; set; } } diff --git a/Shared/Models/ScriptResult.cs b/Shared/Models/ScriptResult.cs index 88ec2f1fe..54db01c0f 100644 --- a/Shared/Models/ScriptResult.cs +++ b/Shared/Models/ScriptResult.cs @@ -24,6 +24,9 @@ public class ScriptResult : ScriptResultBase public Organization? Organization { get; set; } public string OrganizationID { get; set; } = string.Empty; + [JsonIgnore] + public SavedScript? SavedScript { get; set; } + [JsonIgnore] public ScriptSchedule? Schedule { get; set; } diff --git a/Shared/Models/ScriptRun.cs b/Shared/Models/ScriptRun.cs index 929dcfc6b..90c610241 100644 --- a/Shared/Models/ScriptRun.cs +++ b/Shared/Models/ScriptRun.cs @@ -32,6 +32,12 @@ public class ScriptRun public DateTimeOffset RunAt { get; set; } public bool RunOnNextConnect { get; set; } + + [JsonIgnore] + public SavedScript? SavedScript { get; set; } public Guid? SavedScriptId { get; set; } + + [JsonIgnore] + public ScriptSchedule? Schedule { get; set; } public int? ScheduleId { get; set; } } diff --git a/Tests/Server.Tests/DataServiceTests.cs b/Tests/Server.Tests/DataServiceTests.cs index afec5c7ce..003f7676e 100644 --- a/Tests/Server.Tests/DataServiceTests.cs +++ b/Tests/Server.Tests/DataServiceTests.cs @@ -162,18 +162,17 @@ public async Task GetPendingScriptRuns_GivenMultipleRunsQueued_ReturnsOnlyLatest Assert.AreEqual(1, pendingRuns.Count()); Assert.AreEqual(2, pendingRuns.First().Id); - var scriptResult = new ScriptResult() + var dto = new ScriptResultDto() { DeviceID = _testData.Org1Device1.ID, InputType = Shared.Enums.ScriptInputType.ScheduledScript, - OrganizationID = _testData.Org1Id, SavedScriptId = savedScript.Id, ScriptRunId = scriptRun.Id, Shell = Shared.Enums.ScriptingShell.PSCore, ScriptInput = "echo test" }; - _dataService.AddOrUpdateScriptResult(scriptResult); + var scriptResult = (await _dataService.AddScriptResult(dto)).Value!; await _dataService.AddScriptResultToScriptRun(scriptResult.ID, scriptRun.Id);