Skip to content

Commit

Permalink
APIs for Fixed Assets and Fixed Asset Locations (microsoft#24169)
Browse files Browse the repository at this point in the history
For one of the community project related to the PowerApp it is needed to
have a APIs for Fixed Assets and Fixed Asset Locations.

Those APIs are needed in standard API collection since. It will also
allow more interact with the date inside Business Central (for example
create Power Automate flows based on Fixed Assets)

---------

Co-authored-by: Bartosz Rudzinski <[email protected]>
  • Loading branch information
kb-dynaway and barud-dyna authored Nov 20, 2023
1 parent 5bcba3b commit d21949c
Show file tree
Hide file tree
Showing 4 changed files with 447 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Apps/W1/APIV2/app/src/pages/APIV2FALocations.Page.al
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
page 30097 "APIV2 - FA Locations"
{
DelayedInsert = true;
APIVersion = 'v2.0';
EntityCaption = 'Fixed Asset Location';
EntitySetCaption = 'Fixed Asset Locations';
PageType = API;
ODataKeyFields = SystemId;
EntityName = 'fixedAssetLocation';
EntitySetName = 'fixedAssetLocations';
SourceTable = "FA Location";
Extensible = false;

layout
{
area(Content)
{
repeater(Group)
{
field(id; Rec.SystemId)
{
Caption = 'Id';
Editable = false;
}
field(code; Rec.Code)
{
Caption = 'Code';
}
field(displayName; Rec.Name)
{
Caption = 'Name';
}
field(lastModifiedDateTime; Rec.SystemModifiedAt)
{
Caption = 'Last Modified Date';
}
}
}
}
}
69 changes: 69 additions & 0 deletions Apps/W1/APIV2/app/src/pages/APIV2FixedAssets.Page.al
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
page 30098 "APIV2 - Fixed Assets"
{
APIVersion = 'v2.0';
EntityCaption = 'Fixed Asset';
EntitySetCaption = 'Fixed Assets';
ChangeTrackingAllowed = true;
DelayedInsert = true;
EntityName = 'fixedAsset';
EntitySetName = 'fixedAssets';
ODataKeyFields = SystemId;
PageType = API;
SourceTable = "Fixed Asset";
Extensible = false;

layout
{
area(content)
{
repeater(Group)
{
field(id; Rec.SystemId)
{
Caption = 'Id';
Editable = false;
}
field(number; Rec."No.")
{
Caption = 'No.';
}
field(displayName; Rec.Description)
{
Caption = 'Description';
}
field(faLocationCode; Rec."FA Location Code")
{
Caption = 'FA Location Code';
}
field(faClassCode; Rec."FA Class Code")
{
Caption = 'FA Class Code';
}
field(faSubclassCode; Rec."FA Subclass Code")
{
Caption = 'FA Subclass Code';
}
field(blocked; Rec.Blocked)
{
Caption = 'Blocked';
}
field(serialNo; Rec."Serial No.")
{
Caption = 'Serial No.';
}
field(responsibleEmployee; Rec."Responsible Employee")
{
Caption = 'Responsible Employee';
}
field(underMaintenance; Rec."Under Maintenance")
{
Caption = 'Under Maintenance';
}
field(lastModifiedDateTime; Rec.SystemModifiedAt)
{
Caption = 'Last Modified Date';
}
}
}
}
}
161 changes: 161 additions & 0 deletions Apps/W1/APIV2/test/src/APIV2FALocationsE2E.Codeunit.al
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
codeunit 139900 "APIV2 - FA Locations E2E"
{
// version Test,W1,All

Subtype = Test;
TestPermissions = Disabled;

trigger OnRun()
begin
// [FEATURE] [Api] [Location]
end;

var
Assert: Codeunit Assert;
LibraryGraphMgt: Codeunit "Library - Graph Mgt";
LibraryUtility: Codeunit "Library - Utility";
IsInitialized: Boolean;
EmptyJSONErr: Label 'JSON should not be empty.';
ServiceNameTxt: Label 'fixedAssetLocations';

local procedure Initialize()
begin
if IsInitialized then
exit;

IsInitialized := true;
Commit();
end;

[Test]
procedure TestGetFALocation()
var
FALocation: Record "FA Location";
Response: Text;
TargetURL: Text;
begin
// [SCENARIO] User can get a Fixed Asset Location with a GET request to the service.
Initialize();

// [GIVEN] A Fixed Asset Location exists in the system.
CreateFALocation(FALocation);

// [WHEN] The user makes a GET request for a given Fixed Asset Location.
TargetURL := LibraryGraphMgt.CreateTargetURL(FALocation.SystemId, Page::"APIV2 - FA Locations", ServiceNameTxt);
LibraryGraphMgt.GetFromWebService(Response, TargetURL);

// [THEN] The response text contains the Fixed Asset Location information.
VerifyProperties(Response, FALocation);
end;

[Test]
procedure TestCreateFALocation()
var
FALocation: Record "FA Location";
TempFALocation: Record "FA Location" temporary;
FALocationJSON: Text;
TargetURL: Text;
Response: Text;
begin
// [SCENARIO] User can create a new Fixed Asset Location through a POST method.
Initialize();

// [GIVEN] The user has constructed a Fixed Asset Location JSON object to send to the service.
CreateFALocation(TempFALocation);
FALocationJSON := GetFALocationJSON(TempFALocation);

// [WHEN] The user posts the JSON to the service.
TargetURL := LibraryGraphMgt.CreateTargetURL('', Page::"APIV2 - FA Locations", ServiceNameTxt);
LibraryGraphMgt.PostToWebService(TargetURL, FALocationJSON, Response);

// [THEN] The Fixed Asset Location has been created in the database with all the details.
FALocation.Get(TempFALocation.Code);
VerifyProperties(Response, FALocation);
end;

[Test]
procedure TestModifyFALocation()
var
FALocation: Record "FA Location";
TempFALocation: Record "FA Location" temporary;
RequestBody: Text;
Response: Text;
TargetURL: Text;
begin
// [SCENARIO] User can modify name in a Fixed Asset Location through a PATCH request.
Initialize();

// [GIVEN] A Fixed Asset Location exists with a name.
CreateFALocation(FALocation);
TempFALocation.TransferFields(FALocation);
FALocation.Code := LibraryUtility.GenerateRandomCodeWithLength(FALocation.FieldNo(Code), Database::"FA Location", 10);
TempFALocation.Name := LibraryUtility.GenerateGUID();
RequestBody := GetFALocationJSON(TempFALocation);

// [WHEN] The user makes a patch request to the service and specifies name field.
TargetURL := LibraryGraphMgt.CreateTargetURL(FALocation.SystemId, Page::"APIV2 - FA Locations", ServiceNameTxt);
LibraryGraphMgt.PatchToWebService(TargetURL, RequestBody, Response);

// [THEN] The response contains the new values.
VerifyProperties(Response, TempFALocation);

// [THEN] The Fixed Asset Location in the database contains the updated value.
FALocation.Get(FALocation.Code);
Assert.AreEqual(FALocation.Name, TempFALocation.Name, 'Names should be equal.');
end;

[Test]
procedure TestDeleteFALocation()
var
FALocation: Record "FA Location";
FALocationCode: Code[10];
TargetURL: Text;
Response: Text;
begin
// [SCENARIO] User can delete a Fixed Asset Location by making a DELETE request.
Initialize();

// [GIVEN] A Fixed Asset Location exists.
CreateFALocation(FALocation);
FALocationCode := FALocation.Code;

// [WHEN] The user makes a DELETE request to the endpoint for the Fixed Asset Location.
TargetURL := LibraryGraphMgt.CreateTargetURL(FALocation.SystemId, Page::"APIV2 - FA Locations", ServiceNameTxt);
LibraryGraphMgt.DeleteFromWebService(TargetURL, '', Response);

// [THEN] The response is empty.
Assert.AreEqual('', Response, 'DELETE response should be empty.');

// [THEN] The Fixed Asset Location is no longer in the database.
FALocation.SetRange(Code, FALocationCode);
Assert.IsTrue(FALocation.IsEmpty(), 'Fixed Asset Location should be deleted.');
end;

local procedure CreateFALocation(var FALocation: Record "FA Location")
begin
FALocation.Init();
FALocation.Code := LibraryUtility.GenerateRandomCodeWithLength(FALocation.FieldNo(Code), Database::"FA Location", 10);
FALocation.Name := LibraryUtility.GenerateGUID();
FALocation.Insert(true);
Commit();
end;

local procedure GetFALocationJSON(var FALocation: Record "FA Location") FALocationJSON: Text
begin
if FALocation.Code = '' then
FALocation.Code := LibraryUtility.GenerateRandomCodeWithLength(FALocation.FieldNo(Code), Database::"FA Location", 10);
if FALocation.Name = '' then
FALocation.Name := LibraryUtility.GenerateGUID();
FALocationJSON := LibraryGraphMgt.AddPropertytoJSON(FALocationJSON, 'code', FALocation.Code);
FALocationJSON := LibraryGraphMgt.AddPropertytoJSON(FALocationJSON, 'displayName', FALocation.Name);
exit(FALocationJSON);
end;

local procedure VerifyProperties(JSON: Text; FALocation: Record "FA Location")
begin
Assert.AreNotEqual('', JSON, EmptyJSONErr);
LibraryGraphMgt.VerifyIDInJSON(JSON);
LibraryGraphMgt.VerifyPropertyInJSON(JSON, 'code', FALocation.Code);
LibraryGraphMgt.VerifyPropertyInJSON(JSON, 'displayName', FALocation.Name);
end;
}
Loading

0 comments on commit d21949c

Please sign in to comment.