From 8ae2f6c6559cb54ea38b2b65287079b9ecd3dbf7 Mon Sep 17 00:00:00 2001 From: Thomas Rittson Date: Fri, 4 Oct 2024 12:37:39 +1000 Subject: [PATCH] Rewrite RequiredPolicies implementation to use linq --- .../Services/Implementations/PolicyServicevNext.cs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Core/AdminConsole/Services/Implementations/PolicyServicevNext.cs b/src/Core/AdminConsole/Services/Implementations/PolicyServicevNext.cs index 8437a1fca..3a7616576 100644 --- a/src/Core/AdminConsole/Services/Implementations/PolicyServicevNext.cs +++ b/src/Core/AdminConsole/Services/Implementations/PolicyServicevNext.cs @@ -60,13 +60,15 @@ public class PolicyServicevNext : IPolicyServicevNext // If enabling this policy - check that all policy requirements are satisfied if (currentPolicy is not { Enabled: true } && policy.Enabled) { - foreach (var requiredPolicyType in policyDefinition.RequiredPolicies) + var missingRequiredPolicyTypes = policyDefinition.RequiredPolicies + .Where(requiredPolicyType => + allSavedPolicies.SingleOrDefault(p => p.Type == requiredPolicyType) is not { Enabled: true }) + .ToList(); + + if (missingRequiredPolicyTypes.Count != 0) { - if (allSavedPolicies.SingleOrDefault(p => p.Type == requiredPolicyType) is not { Enabled: true }) - { - // TODO: would be better to reference the name instead of the enum - throw new BadRequestException("Policy requires PolicyType " + requiredPolicyType + " to be enabled first."); - } + // TODO: would be better to reference the name instead of the enum + throw new BadRequestException("Policy requires PolicyType " + missingRequiredPolicyTypes.First() + " to be enabled first."); } }