diff --git a/src/Apps/W1/Shopify/App/src/Companies/Codeunits/ShpfyCompanyExport.Codeunit.al b/src/Apps/W1/Shopify/App/src/Companies/Codeunits/ShpfyCompanyExport.Codeunit.al index 4d6be95342..f8a7a584c4 100644 --- a/src/Apps/W1/Shopify/App/src/Companies/Codeunits/ShpfyCompanyExport.Codeunit.al +++ b/src/Apps/W1/Shopify/App/src/Companies/Codeunits/ShpfyCompanyExport.Codeunit.al @@ -41,7 +41,6 @@ codeunit 30284 "Shpfy Company Export" Shop: Record "Shpfy Shop"; CompanyAPI: Codeunit "Shpfy Company API"; CatalogAPI: Codeunit "Shpfy Catalog API"; - MetafieldAPI: Codeunit "Shpfy Metafield API"; SkippedRecord: Codeunit "Shpfy Skipped Record"; CreateCustomers: Boolean; CountyCodeTooLongLbl: Label 'Can not export customer %1 %2. The length of the string is %3, but it must be less than or equal to %4 characters. Value: %5, field: %6', Comment = '%1 - Customer No., %2 - Customer Name, %3 - Length, %4 - Max Length, %5 - Value, %6 - Field Name'; @@ -213,7 +212,6 @@ codeunit 30284 "Shpfy Company Export" Shop := ShopifyShop; CompanyAPI.SetShop(Shop); CatalogAPI.SetShop(Shop); - MetafieldAPI.SetShop(Shop); end; local procedure UpdateShopifyCompany(Customer: Record Customer; CompanyId: BigInteger) @@ -252,8 +250,10 @@ codeunit 30284 "Shpfy Company Export" end; local procedure UpdateMetafields(ComppanyId: BigInteger) + var + Metafields: Codeunit "Shpfy Metafields"; begin - MetafieldAPI.CreateOrUpdateMetafieldsInShopify(Database::"Shpfy Company", ComppanyId); + Metafields.SyncMetafieldsToShopify(Database::"Shpfy Company", ComppanyId, Shop.Code); end; internal procedure SetCreateCompanies(NewCustomers: Boolean) diff --git a/src/Apps/W1/Shopify/App/src/Customers/Codeunits/ShpfyCustomerExport.Codeunit.al b/src/Apps/W1/Shopify/App/src/Customers/Codeunits/ShpfyCustomerExport.Codeunit.al index 6153e9a04b..5043f72eb0 100644 --- a/src/Apps/W1/Shopify/App/src/Customers/Codeunits/ShpfyCustomerExport.Codeunit.al +++ b/src/Apps/W1/Shopify/App/src/Customers/Codeunits/ShpfyCustomerExport.Codeunit.al @@ -49,7 +49,6 @@ codeunit 30116 "Shpfy Customer Export" var Shop: Record "Shpfy Shop"; CustomerApi: Codeunit "Shpfy Customer API"; - MetafieldAPI: Codeunit "Shpfy Metafield API"; SkippedRecord: Codeunit "Shpfy Skipped Record"; CreateCustomers: Boolean; CountyCodeTooLongLbl: Label 'Can not export customer %1 %2. The length of the string is %3, but it must be less than or equal to %4 characters. Value: %5, field: %6', Comment = '%1 - Customer No., %2 - Customer Name, %3 - Length, %4 - Max Length, %5 - Value, %6 - Field Name'; @@ -228,7 +227,6 @@ codeunit 30116 "Shpfy Customer Export" begin Shop := ShopifyShop; CustomerApi.SetShop(Shop); - MetafieldAPI.SetShop(Shop) end; /// @@ -286,7 +284,9 @@ codeunit 30116 "Shpfy Customer Export" end; local procedure UpdateMetafields(CustomerId: BigInteger) + var + Metafields: Codeunit "Shpfy Metafields"; begin - MetafieldAPI.CreateOrUpdateMetafieldsInShopify(Database::"Shpfy Customer", CustomerId); + Metafields.SyncMetafieldsToShopify(Database::"Shpfy Customer", CustomerId, Shop.Code); end; } diff --git a/src/Apps/W1/Shopify/App/src/Metafields/Codeunits/ShpfyMetafieldAPI.Codeunit.al b/src/Apps/W1/Shopify/App/src/Metafields/Codeunits/ShpfyMetafieldAPI.Codeunit.al index 36d34992da..b9d790a07c 100644 --- a/src/Apps/W1/Shopify/App/src/Metafields/Codeunits/ShpfyMetafieldAPI.Codeunit.al +++ b/src/Apps/W1/Shopify/App/src/Metafields/Codeunits/ShpfyMetafieldAPI.Codeunit.al @@ -21,7 +21,42 @@ codeunit 30316 "Shpfy Metafield API" CommunicationMgt.SetShop(Shop); end; + local procedure SetShop(ShopCode: Code[20]) + begin + Shop.Get(ShopCode); + SetShop(Shop); + end; + #region To Shopify + internal procedure SyncMetafieldToShopify(var Metafield: Record "Shpfy Metafield"; ShopCode: Code[20]): BigInteger + var + UserErrorOnShopifyErr: Label 'Something went wrong while sending the metafield to Shopify. Check Shopify Log Entries for more details.'; + GraphQuery: TextBuilder; + JResponse: JsonToken; + JMetafields: JsonArray; + JUserErrors: JsonArray; + JItem: JsonToken; + begin + SetShop(ShopCode); + CreateMetafieldQuery(Metafield, GraphQuery); + JResponse := UpdateMetafields(GraphQuery.ToText()); + + JsonHelper.GetJsonArray(JResponse, JUserErrors, 'data.metafieldsSet.userErrors'); + + if JUserErrors.Count() = 0 then begin + JsonHelper.GetJsonArray(JResponse, JMetafields, 'data.metafieldsSet.metafields'); + JMetafields.Get(0, JItem); + exit(JsonHelper.GetValueAsBigInteger(JItem, 'legacyResourceId')); + end else + Error(UserErrorOnShopifyErr); + end; + + internal procedure SyncMetafieldsToShopify(ParentTableNo: Integer; OwnerId: BigInteger; ShopCode: Code[20]) + begin + SetShop(ShopCode); + CreateOrUpdateMetafieldsInShopify(ParentTableNo, OwnerId); + end; + /// /// Creates or updates the metafields in Shopify. /// @@ -177,6 +212,12 @@ codeunit 30316 "Shpfy Metafield API" DeleteUnusedMetafields(MetafieldIds); end; + internal procedure GetMetafieldDefinitions(ParentTableNo: Integer; OwnerId: BigInteger; ShopCode: Code[20]) + begin + SetShop(ShopCode); + GetMetafieldDefinitions(ParentTableNo, OwnerId); + end; + /// /// Retrieves the metafield definitions from Shopify. /// @@ -186,7 +227,7 @@ codeunit 30316 "Shpfy Metafield API" /// /// Table id of the parent resource. /// Id of the parent resource. - internal procedure GetMetafieldDefinitions(ParentTableNo: Integer; OwnerId: BigInteger) + local procedure GetMetafieldDefinitions(ParentTableNo: Integer; OwnerId: BigInteger) var Metafield: Record "Shpfy Metafield"; OwnerType: Enum "Shpfy Metafield Owner Type"; diff --git a/src/Apps/W1/Shopify/App/src/Metafields/Codeunits/ShpfyMetafields.Codeunit.al b/src/Apps/W1/Shopify/App/src/Metafields/Codeunits/ShpfyMetafields.Codeunit.al new file mode 100644 index 0000000000..e96ffd8aae --- /dev/null +++ b/src/Apps/W1/Shopify/App/src/Metafields/Codeunits/ShpfyMetafields.Codeunit.al @@ -0,0 +1,51 @@ +// ------------------------------------------------------------------------------------------------ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. +// ------------------------------------------------------------------------------------------------ + +namespace Microsoft.Integration.Shopify; + +/// +/// Provides functionality for managing Shopify metafields. +/// +codeunit 30418 "Shpfy Metafields" +{ + Access = Public; + + var + MetafieldAPI: Codeunit "Shpfy Metafield API"; + + /// + /// Retrieves the metafield definitions from Shopify for the specified resource. + /// + /// Table id of the parent resource (e.g., Database::"Shpfy Product"). + /// Id of the parent resource in Shopify. + /// The code of the Shopify shop. + procedure GetMetafieldDefinitions(ParentTableNo: Integer; OwnerId: BigInteger; ShopCode: Code[20]) + begin + MetafieldAPI.GetMetafieldDefinitions(ParentTableNo, OwnerId, ShopCode); + end; + + /// + /// Synchronizes a single metafield to Shopify, creating or updating it as needed. + /// + /// The metafield record to synchronize to Shopify. + /// The code of the Shopify shop. + /// The Shopify Id of the metafield. + procedure SyncMetafieldToShopify(var Metafield: Record "Shpfy Metafield"; ShopCode: Code[20]): BigInteger + begin + exit(MetafieldAPI.SyncMetafieldToShopify(Metafield, ShopCode)); + end; + + /// + /// Synchronizes all metafields for the specified resource to Shopify. + /// Only metafields that have been updated in BC since the last sync will be sent. + /// + /// Table id of the parent resource (e.g., Database::"Shpfy Product"). + /// Id of the parent resource in Shopify. + /// The code of the Shopify shop. + procedure SyncMetafieldsToShopify(ParentTableNo: Integer; OwnerId: BigInteger; ShopCode: Code[20]) + begin + MetafieldAPI.SyncMetafieldsToShopify(ParentTableNo, OwnerId, ShopCode); + end; +} diff --git a/src/Apps/W1/Shopify/App/src/Metafields/Pages/ShpfyMetafields.Page.al b/src/Apps/W1/Shopify/App/src/Metafields/Pages/ShpfyMetafields.Page.al index b19309285b..4b2d5d5ff9 100644 --- a/src/Apps/W1/Shopify/App/src/Metafields/Pages/ShpfyMetafields.Page.al +++ b/src/Apps/W1/Shopify/App/src/Metafields/Pages/ShpfyMetafields.Page.al @@ -78,14 +78,36 @@ page 30163 "Shpfy Metafields" trigger OnAction() var - MetafieldAPI: Codeunit "Shpfy Metafield API"; + Metafields: Codeunit "Shpfy Metafields"; ParentTableNo: Integer; OwnerId: BigInteger; begin Evaluate(ParentTableNo, Rec.GetFilter("Parent Table No.")); Evaluate(OwnerId, Rec.GetFilter("Owner Id")); - MetafieldAPI.SetShop(Shop); - MetafieldAPI.GetMetafieldDefinitions(ParentTableNo, OwnerId); + Metafields.GetMetafieldDefinitions(ParentTableNo, OwnerId, Shop.Code); + end; + } + action(SyncToShopify) + { + ApplicationArea = All; + Caption = 'Sync to Shopify'; + Image = Export; + ToolTip = 'Send the selected metafields to Shopify.'; + Visible = IsPageEditable; + Promoted = true; + PromotedOnly = true; + PromotedCategory = Process; + + trigger OnAction() + var + Metafield: Record "Shpfy Metafield"; + Metafields: Codeunit "Shpfy Metafields"; + begin + CurrPage.SetSelectionFilter(Metafield); + if Metafield.FindSet() then + repeat + Metafields.SyncMetafieldToShopify(Metafield, Shop.Code); + until Metafield.Next() = 0; end; } } @@ -113,18 +135,19 @@ page 30163 "Shpfy Metafields" Rec.TestField(Name); Rec.Validate(Value); - Rec.Id := SendMetafieldToShopify(); + Rec.Id := Metafields.SyncMetafieldToShopify(Rec, Shop.Code); end; trigger OnModifyRecord(): Boolean begin if Rec.Id < 0 then if xRec.Value <> Rec.Value then - Rec.Rename(SendMetafieldToShopify()); + Rec.Rename(Metafields.SyncMetafieldToShopify(Rec, Shop.Code)); end; var Shop: Record "Shpfy Shop"; + Metafields: Codeunit "Shpfy Metafields"; IsPageEditable: Boolean; IsValueEditable: Boolean; @@ -149,29 +172,4 @@ page 30163 "Shpfy Metafields" CurrPage.SetTableView(Metafield); CurrPage.RunModal(); end; - - local procedure SendMetafieldToShopify(): BigInteger - var - JsonHelper: Codeunit "Shpfy Json Helper"; - MetafieldAPI: Codeunit "Shpfy Metafield API"; - UserErrorOnShopifyErr: Label 'Something went wrong while sending the metafield to Shopify. Check Shopify Log Entries for more details.'; - GraphQuery: TextBuilder; - JResponse: JsonToken; - JMetafields: JsonArray; - JUserErrors: JsonArray; - JItem: JsonToken; - begin - MetafieldAPI.SetShop(Shop); - MetafieldAPI.CreateMetafieldQuery(Rec, GraphQuery); - JResponse := MetafieldAPI.UpdateMetafields(GraphQuery.ToText()); - - JsonHelper.GetJsonArray(JResponse, JUserErrors, 'data.metafieldsSet.userErrors'); - - if JUserErrors.Count() = 0 then begin - JsonHelper.GetJsonArray(JResponse, JMetafields, 'data.metafieldsSet.metafields'); - JMetafields.Get(0, JItem); - exit(JsonHelper.GetValueAsBigInteger(JItem, 'legacyResourceId')); - end else - Error(UserErrorOnShopifyErr); - end; } \ No newline at end of file diff --git a/src/Apps/W1/Shopify/App/src/PermissionSets/ShpfyObjects.PermissionSet.al b/src/Apps/W1/Shopify/App/src/PermissionSets/ShpfyObjects.PermissionSet.al index 1632e349ef..ad135db30b 100644 --- a/src/Apps/W1/Shopify/App/src/PermissionSets/ShpfyObjects.PermissionSet.al +++ b/src/Apps/W1/Shopify/App/src/PermissionSets/ShpfyObjects.PermissionSet.al @@ -299,6 +299,7 @@ permissionset 30104 "Shpfy - Objects" codeunit "Shpfy Log Entries Delete" = X, codeunit "Shpfy Math" = X, codeunit "Shpfy Metafield API" = X, + codeunit "Shpfy Metafields" = X, codeunit "Shpfy Metafield Owner Company" = X, codeunit "Shpfy Metafield Owner Customer" = X, codeunit "Shpfy Metafield Owner Product" = X, diff --git a/src/Apps/W1/Shopify/App/src/Products/Codeunits/ShpfyProductEvents.Codeunit.al b/src/Apps/W1/Shopify/App/src/Products/Codeunits/ShpfyProductEvents.Codeunit.al index 075ac8013c..e6ae2b3a5d 100644 --- a/src/Apps/W1/Shopify/App/src/Products/Codeunits/ShpfyProductEvents.Codeunit.al +++ b/src/Apps/W1/Shopify/App/src/Products/Codeunits/ShpfyProductEvents.Codeunit.al @@ -82,6 +82,15 @@ codeunit 30177 "Shpfy Product Events" begin end; + /// + /// Raised before updating product metafields in Shopify. + /// + /// The Shopify product Id. + [IntegrationEvent(false, false)] + internal procedure OnBeforeUpdateProductMetafields(ProductId: BigInteger) + begin + end; + #pragma warning disable AS0027 /// /// Raised After Find Item Template. diff --git a/src/Apps/W1/Shopify/App/src/Products/Codeunits/ShpfyProductExport.Codeunit.al b/src/Apps/W1/Shopify/App/src/Products/Codeunits/ShpfyProductExport.Codeunit.al index 3312f04bdb..87df66b2ec 100644 --- a/src/Apps/W1/Shopify/App/src/Products/Codeunits/ShpfyProductExport.Codeunit.al +++ b/src/Apps/W1/Shopify/App/src/Products/Codeunits/ShpfyProductExport.Codeunit.al @@ -68,7 +68,6 @@ codeunit 30178 "Shpfy Product Export" ProductEvents: Codeunit "Shpfy Product Events"; ProductPriceCalc: Codeunit "Shpfy Product Price Calc."; VariantApi: Codeunit "Shpfy Variant API"; - MetafieldAPI: Codeunit "Shpfy Metafield API"; SkippedRecord: Codeunit "Shpfy Skipped Record"; OnlyUpdatePrice: Boolean; RecordCount: Integer; @@ -578,7 +577,6 @@ codeunit 30178 "Shpfy Product Export" ProductApi.SetShop(Shop); VariantApi.SetShop(Shop); ProductPriceCalc.SetShop(Shop); - MetafieldAPI.SetShop(Shop); end; /// @@ -765,17 +763,20 @@ codeunit 30178 "Shpfy Product Export" local procedure UpdateMetafields(ProductId: BigInteger) var ShpfyVariant: Record "Shpfy Variant"; + Metafields: Codeunit "Shpfy Metafields"; begin if OnlyUpdatePrice then exit; - MetafieldAPI.CreateOrUpdateMetafieldsInShopify(Database::"Shpfy Product", ProductId); + ProductEvents.OnBeforeUpdateProductMetafields(ProductId); + + Metafields.SyncMetafieldsToShopify(Database::"Shpfy Product", ProductId, Shop.Code); ShpfyVariant.SetRange("Product Id", ProductId); ShpfyVariant.ReadIsolation := IsolationLevel::ReadCommitted; if ShpfyVariant.FindSet() then repeat - MetafieldAPI.CreateOrUpdateMetafieldsInShopify(Database::"Shpfy Variant", ShpfyVariant.Id); + Metafields.SyncMetafieldsToShopify(Database::"Shpfy Variant", ShpfyVariant.Id, Shop.Code); until ShpfyVariant.Next() = 0; end;