Skip to content

Commit

Permalink
Feature Management. Add an overload to IsEnabled procedure. (#1720)
Browse files Browse the repository at this point in the history
<!-- Thank you for submitting a Pull Request. If you're new to
contributing to BCApps please read our pull request guideline below
* https://github.com/microsoft/BCApps/Contributing.md
-->
#### Summary <!-- Provide a general summary of your changes -->
Add an overload to IsEnabled procedure to be able to avoid inserts when
checking the feature enabled state, since in some circumstances (init of
a report) we cannot afford a transaction without error.

#### Work Item(s) <!-- Add the issue number here after the #. The issue
needs to be open and approved. Submitting PRs with no linked issues or
unapproved issues is highly discouraged. -->
Fixes
[AB#543746](https://dynamicssmb2.visualstudio.com/1fcb79e7-ab07-432a-a3c6-6cf5a88ba4a5/_workitems/edit/543746)
  • Loading branch information
Vadim-Stepanenko committed Aug 12, 2024
1 parent 99ab94a commit ed9b54c
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ codeunit 2611 "Feature Management Facade"
exit(FeatureManagementImpl.IsEnabled(FeatureId));
end;

/// <summary>
/// Returns true if the feature is enabled and data update, if required, is complete.
/// </summary>
/// <param name="FeatureId">the feature id in the system table "Feature Key"</param>
/// <param name="AllowInsert">specifies if inserts are allowed while checking for feature being enabled</param>
/// <returns>if the feature is fully enabled</returns>
procedure IsEnabled(FeatureId: Text[50]; AllowInsert: Boolean): Boolean;
begin
exit(FeatureManagementImpl.IsEnabled(FeatureId, AllowInsert));
end;

/// <summary>
/// Updates the status in "Feature Data Update Status" records related to all companies.
/// Also sends the notification reminding user to sign in again after feature is enabled/disabled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ codeunit 2610 "Feature Management Impl."
/// <summary>
/// Inserts record to "Feature Data Update Status" table to show the feature status per company.
/// </summary>
local procedure InitializeFeatureDataUpdateStatus(FeatureKey: Record "Feature Key"; var FeatureDataUpdateStatus: Record "Feature Data Update Status")
local procedure InitializeFeatureDataUpdateStatus(FeatureKey: Record "Feature Key"; var FeatureDataUpdateStatus: Record "Feature Data Update Status"; AllowInsert: Boolean)
var
FeatureManagementFacade: Codeunit "Feature Management Facade";
InitializeHandled: Boolean;
Expand All @@ -106,7 +106,8 @@ codeunit 2610 "Feature Management Impl."
end;
// If the table extension is not in sync during upgrade then Get() always returns False,
// so the following insert will fail if the record does exist.
if FeatureDataUpdateStatus.Insert() then;
if AllowInsert then
if FeatureDataUpdateStatus.Insert() then;
end;

/// <summary>
Expand Down Expand Up @@ -162,7 +163,7 @@ codeunit 2610 "Feature Management Impl."
if FeatureDataUpdateStatus."Background Task" then
UpdateBackgroundTaskStatus(FeatureDataUpdateStatus);
end else
InitializeFeatureDataUpdateStatus(FeatureKey, FeatureDataUpdateStatus);
InitializeFeatureDataUpdateStatus(FeatureKey, FeatureDataUpdateStatus, true);
end;

/// <summary>
Expand Down Expand Up @@ -322,15 +323,26 @@ codeunit 2610 "Feature Management Impl."
/// <summary>
/// Returns true if the feature is enabled and data update, if required, is complete.
/// </summary>
/// <param name="FeatureId">the feature id in the system table "Feature Key"</param>
/// <returns>if the feature is fully enabled</returns>
/// <param name="FeatureId">The feature id in the system table "Feature Key"</param>
/// <returns>If the feature is fully enabled</returns>
procedure IsEnabled(FeatureId: Text[50]): Boolean;
begin
exit(IsEnabled(FeatureId, true));
end;

/// <summary>
/// Returns true if the feature is enabled and data update, if required, is complete.
/// </summary>
/// <param name="FeatureId">The feature id in the system table "Feature Key"</param>
/// <param name="AllowInsert">Specifies if inserts are allowed while checking for feature being enabled</param>
/// <returns>If the feature is fully enabled</returns>
procedure IsEnabled(FeatureId: Text[50]; AllowInsert: Boolean): Boolean;
var
FeatureKey: Record "Feature Key";
FeatureDataUpdateStatus: Record "Feature Data Update Status";
begin
if FeatureKey.Get(FeatureId) then begin
InitializeFeatureDataUpdateStatus(FeatureKey, FeatureDataUpdateStatus);
InitializeFeatureDataUpdateStatus(FeatureKey, FeatureDataUpdateStatus, AllowInsert);
exit(FeatureDataUpdateStatus."Feature Status" in ["Feature Status"::Complete, "Feature Status"::Enabled])
end;
end;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,63 @@ codeunit 135003 "Feature Key Test"
asserterror error('') // roll back
end;

[Test]
procedure T108_IsEnabledAllowInsertTrue()
var
FeatureDataUpdateStatus: Record "Feature Data Update Status";
Enabled: Option "None","All Users";
ID: Text[50];
begin
PermissionsMock.Set('Feature Key Admin');
Initialize();
ID := 'SalesPrices';
// [GIVEN] Feature Key 'X', where "Enabled" is 'All Users' with data update required
SetFeatureEnabled(ID, Enabled::"All Users");

// [GIVEN] FeatureDataUpdateStatus for Company 'A' does not exist
FeatureDataUpdateStatus.DeleteAll();

// [WHEN] run IsEnabled() with AllowInsert = true
// [THEN] result is False, because it requires data update
Assert.IsFalse(FeatureManagementFacade.IsEnabled(ID, true), 'should be disabled');

// [THEN] FeatureDataUpdateStatus for Company 'A' is created
FeatureDataUpdateStatus.SetRange("Feature Key", ID);
FeatureDataUpdateStatus.SetRange("Company Name", CopyStr(CompanyName(), 1, MaxStrLen(FeatureDataUpdateStatus."Company Name")));
Assert.RecordIsNotEmpty(FeatureDataUpdateStatus);

asserterror Error('') // roll back
end;

[Test]
procedure T109_IsEnabledAllowInsertFalse()
var
FeatureDataUpdateStatus: Record "Feature Data Update Status";
Enabled: Option "None","All Users";
ID: Text[50];
begin
PermissionsMock.Set('Feature Key Admin');
Initialize();
ID := 'SalesPrices';
// [GIVEN] Feature Key 'X', where "Enabled" is 'All Users' with data update required
SetFeatureEnabled(ID, Enabled::"All Users");

// [GIVEN] FeatureDataUpdateStatus for Company 'A' does not exist
FeatureDataUpdateStatus.DeleteAll();

// [WHEN] run IsEnabled() with AllowInsert = false
// [THEN] result is False, because it requires data update
Assert.IsFalse(FeatureManagementFacade.IsEnabled(ID, false), 'should be disabled');

// [THEN] FeatureDataUpdateStatus for Company 'A' is not created
FeatureDataUpdateStatus.SetRange("Feature Key", ID);
FeatureDataUpdateStatus.SetRange("Company Name", CopyStr(CompanyName(), 1, MaxStrLen(FeatureDataUpdateStatus."Company Name")));
Assert.RecordIsEmpty(FeatureDataUpdateStatus);

asserterror Error('') // roll back
end;


local procedure Initialize()
begin
LibraryVariableStorage.Clear();
Expand All @@ -528,12 +585,9 @@ codeunit 135003 "Feature Key Test"

local procedure MockFeatureStatus(ID: Text[50]; Enabled: Option "None","All Users"; Status: Enum "Feature Status")
var
FeatureKey: Record "Feature Key";
FeatureDataUpdateStatus: Record "Feature Data Update Status";
begin
FeatureKey.Get(ID);
FeatureKey.Enabled := Enabled;
FeatureKey.Modify();
SetFeatureEnabled(ID, Enabled);

FeatureDataUpdateStatus.DeleteAll();
FeatureDataUpdateStatus."Feature Key" := ID;
Expand All @@ -543,6 +597,15 @@ codeunit 135003 "Feature Key Test"
FeatureDataUpdateStatus.Insert();
end;

local procedure SetFeatureEnabled(ID: Text[50]; Enabled: Option "None","All Users")
var
FeatureKey: Record "Feature Key";
begin
FeatureKey.Get(ID);
FeatureKey.Enabled := Enabled;
FeatureKey.Modify();
end;

local procedure MockStatusInAnotherCompany(ID: Text[50]; DataUpdateRequired: Boolean; FeatureStatus: Enum "Feature Status") Name: Text[30]
var
FeatureDataUpdateStatus: Record "Feature Data Update Status";
Expand Down

0 comments on commit ed9b54c

Please sign in to comment.