Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Shopify] Sales Channels publishing for new products #27540

Merged
merged 25 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
8d88a53
add sales channels import
petemchlk Oct 15, 2024
eb80a51
add publishing process on create, modify structure to be able to choo…
petemchlk Oct 16, 2024
51f882e
code refactor
petemchlk Oct 16, 2024
3fb3416
Create import test
petemchlk Oct 17, 2024
93891bb
add tests, refactor code
petemchlk Oct 17, 2024
8156596
add tests
petemchlk Oct 18, 2024
28f339c
remove empty line
petemchlk Oct 21, 2024
477b5d4
udpate gql query, add default field and default channel publishing in…
petemchlk Oct 23, 2024
b399ab0
fix tests, add test for trgering publish from create prod
petemchlk Oct 23, 2024
5f01f2c
PR fixes
petemchlk Oct 24, 2024
d252953
update object ids, premission sets, gql type enum ids fixed
petemchlk Oct 25, 2024
7877b78
fix summaries
petemchlk Oct 25, 2024
fa87320
create get next sales channels
petemchlk Oct 30, 2024
06a17b0
fix function name
petemchlk Oct 30, 2024
4a452af
saleschannels pagination
petemchlk Oct 30, 2024
a1994c2
adjust tests to sales channels paging
petemchlk Oct 30, 2024
7a7fe99
refactor
petemchlk Oct 30, 2024
b0dae35
fix tooltip
petemchlk Oct 31, 2024
966a398
Merge branch 'main' into dev/pmi/SalesChannels
JesperSchulz Nov 1, 2024
9cbd1f0
PR fixes
petemchlk Nov 5, 2024
ee0eb34
Merge branch 'dev/pmi/SalesChannels' of https://github.com/GediminasG…
petemchlk Nov 5, 2024
a4caf5c
Update Apps/W1/Shopify/app/src/Products/Tables/ShpfySalesChannel.Tabl…
onbuyuka Nov 6, 2024
673391b
Merge branch 'main' into dev/pmi/SalesChannels
JesperSchulz Nov 21, 2024
e924c5a
Fix ID clashes
JesperSchulz Dec 2, 2024
1bdc4a2
Update IDs
JesperSchulz Dec 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Apps/W1/Shopify/app/src/Base/Pages/ShpfyShopCard.Page.al
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,19 @@ page 30101 "Shpfy Shop Card"
RunPageLink = "Shop Code" = field(Code);
ToolTip = 'View a list of Shopify Languages for the shop.';
}
action(SalesChannels)
{
ApplicationArea = All;
Caption = 'Sales Channels';
Image = List;
Promoted = true;
PromotedCategory = Category4;
PromotedIsBig = true;
PromotedOnly = true;
RunObject = Page "Shpfy Sales Channels";
RunPageLink = "Shop Code" = field(Code);
ToolTip = 'View a list of Shopify Sales Channels for the shop and choose ones used for new product publishing.';
}
}
area(Processing)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
namespace Microsoft.Integration.Shopify;
/// <summary>
/// Codeunit Shpfy GQL Get Fulfillments (ID 30317) implements Interface Shpfy IGraphQL.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace Microsoft.Integration.Shopify;
/// <summary>
/// Codeunit Shpfy GQL Get Next S. Channels (ID 30375) implements Interface Shpfy IGraphQL.
/// </summary>
codeunit 30384 "Shpfy GQL Get Next S. Channels" implements "Shpfy IGraphQL"
{
Access = Internal;

/// <summary>
/// GetGraphQL.
/// </summary>
/// <returns>Return value of type Text.</returns>
internal procedure GetGraphQL(): Text
begin
exit('{"query" : "{publications(first: 25, after:\"{{After}}\", catalogType: APP) { pageInfo{ hasNextPage } edges { cursor node { id catalog { id ... on AppCatalog { apps(first: 1) { edges { node { id handle title } } } } } } } } }"}');
end;

/// <summary>
/// GetExpectedCost.
/// </summary>
/// <returns>Return value of type Integer.</returns>
internal procedure GetExpectedCost(): Integer
begin
exit(32);
end;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace Microsoft.Integration.Shopify;

/// <summary>
/// Codeunit Shpfy GQL Get SalesChannels (ID 30371) implements Interface Shpfy IGraphQL.
/// </summary>
codeunit 30371 "Shpfy GQL Get SalesChannels" implements "Shpfy IGraphQL"
{
Access = Internal;

/// <summary>
/// GetGraphQL.
/// </summary>
/// <returns>Return value of type Text.</returns>
internal procedure GetGraphQL(): Text
begin
exit('{"query": "{publications(first: 25, catalogType: APP) { pageInfo{ hasNextPage } edges { cursor node { id catalog { id ... on AppCatalog { apps(first: 1) { edges { node { id handle title } } } } } } } } }"}');
end;

/// <summary>
/// GetExpectedCost.
/// </summary>
/// <returns>Return value of type Integer.</returns>
internal procedure GetExpectedCost(): Integer
begin
exit(22);
end;
}
10 changes: 10 additions & 0 deletions Apps/W1/Shopify/app/src/GraphQL/Enums/ShpfyGraphQLType.Enum.al
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,16 @@ enum 30111 "Shpfy GraphQL Type" implements "Shpfy IGraphQL"
Caption = 'Get Product Image';
Implementation = "Shpfy IGraphQL" = "Shpfy GQL GetProductImage";
}
value(101; GetSalesChannels)
{
Caption = 'Get Sales Channels';
Implementation = "Shpfy IGraphQL" = "Shpfy GQL Get SalesChannels";
}
value(102; GetNextSalesChannels)
{
Caption = 'Get Next Sales Channels';
Implementation = "Shpfy IGraphQL" = "Shpfy GQL Get Next S. Channels";
}
value(103; CustomerMetafieldIds)
{
Caption = 'Customer Metafield Ids';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ permissionset 30102 "Shpfy - Edit"
tabledata "Shpfy Payout" = IMD,
tabledata "Shpfy Product" = IMD,
tabledata "Shpfy Registered Store New" = imd,
tabledata "Shpfy Sales Channel" = IMD,
tabledata "Shpfy Shipment Method Mapping" = IMD,
tabledata "Shpfy Shop" = IMD,
tabledata "Shpfy Shop Collection Map" = IMD,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ permissionset 30100 "Shpfy - Read"
tabledata "Shpfy Refund Line" = R,
tabledata "Shpfy Return Header" = R,
tabledata "Shpfy Return Line" = R,
tabledata "Shpfy Sales Channel" = R,
tabledata "Shpfy Shipment Method Mapping" = R,
tabledata "Shpfy Shop" = R,
tabledata "Shpfy Shop Collection Map" = R,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ codeunit 30176 "Shpfy Product API"
GraphQuery.Append(CommunicationMgt.EscapeGraphQLData(ShopifyProduct.Vendor));
GraphQuery.Append('\"');
end;

if ShopifyProduct."Has Variants" or (ShopifyVariant."UoM Option Id" > 0) then begin
GraphQuery.Append(', productOptions: [{name: \"');
GraphQuery.Append(CommunicationMgt.EscapeGraphQLData(ShopifyVariant."Option 1 Name"));
Expand Down Expand Up @@ -122,6 +123,8 @@ codeunit 30176 "Shpfy Product API"
VariantApi.AddProductVariant(ShopifyVariant);
end;

PublishProduct(NewShopifyProduct);

exit(NewShopifyProduct.Id);
end;

Expand Down Expand Up @@ -590,4 +593,65 @@ codeunit 30176 "Shpfy Product API"
foreach JOption in JOptions do
Options.Add(JsonHelper.GetValueAsText(JOption, 'id'), JsonHelper.GetValueAsText(JOption, 'name'));
end;

/// <summary>
/// Publish product to selected Shopify Sales Channels
/// </summary>
/// <param name="ShopifyProduct">Shopify product to be published</param>
internal procedure PublishProduct(ShopifyProduct: Record "Shpfy Product")
var
SalesChannel: Record "Shpfy Sales Channel";
GraphQuery: Text;
JResponse: JsonToken;
begin
if ShopifyProduct.Status <> Enum::"Shpfy Product Status"::Active then
exit;

if not FilterSalesChannelsToPublishTo(SalesChannel, ShopifyProduct."Shop Code") then
exit;

GraphQuery := CreateProductPublishGraphQuery(ShopifyProduct, SalesChannel);

JResponse := CommunicationMgt.ExecuteGraphQL(GraphQuery);
end;

local procedure FilterSalesChannelsToPublishTo(var SalesChannel: Record "Shpfy Sales Channel"; ShopCode: Code[20]): Boolean
var
SalesChannelAPI: Codeunit "Shpfy Sales Channel API";
begin
SalesChannel.SetRange("Shop Code", ShopCode);
if SalesChannel.IsEmpty() then
onbuyuka marked this conversation as resolved.
Show resolved Hide resolved
SalesChannelAPI.RetrieveSalesChannelsFromShopify(ShopCode);

SalesChannel.SetRange(SalesChannel."Use for publication", true);
if SalesChannel.IsEmpty() then begin
SalesChannel.SetRange("Use for publication");
SalesChannel.SetRange(SalesChannel.Default, true);
onbuyuka marked this conversation as resolved.
Show resolved Hide resolved
if SalesChannel.IsEmpty() then
exit(false);
end;

exit(true);
end;

local procedure CreateProductPublishGraphQuery(ShopifyProduct: Record "Shpfy Product"; var SalesChannel: Record "Shpfy Sales Channel"): Text
var
PublicationIds: TextBuilder;
PublicationIdTok: Label '{ publicationId: \"gid://shopify/Publication/%1\"},', Locked = true;
GraphQueryBuilder: TextBuilder;
begin
GraphQueryBuilder.Append('{"query":"mutation {publishablePublish(id: \"gid://shopify/Product/');
GraphQueryBuilder.Append(Format(ShopifyProduct.Id));
GraphQueryBuilder.Append('\" ');
GraphQueryBuilder.Append('input: [');
SalesChannel.FindSet();
repeat
PublicationIds.Append(StrSubstNo(PublicationIdTok, Format(SalesChannel.Id)));
until SalesChannel.Next() = 0;
GraphQueryBuilder.Append(PublicationIds.ToText().TrimEnd(','));
GraphQueryBuilder.Append('])');
GraphQueryBuilder.Append('{userErrors {field, message}}');
GraphQueryBuilder.Append('}"}');
exit(GraphQueryBuilder.ToText());
end;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
namespace Microsoft.Integration.Shopify;

/// <summary>
/// Codeunit Shpfy Sales Channel API (ID 30372).
/// </summary>
codeunit 30372 "Shpfy Sales Channel API"
{
Access = Internal;

var
JsonHelper: Codeunit "Shpfy Json Helper";
CommunicationMgt: Codeunit "Shpfy Communication Mgt.";

/// <summary>
/// Retrieves the sales channels from Shopify and updates the table with the new sales channels.
/// </summary>
/// <param name="ShopCode">The code of the shop.</param>
internal procedure RetrieveSalesChannelsFromShopify(ShopCode: Code[20])
var
GraphQLType: Enum "Shpfy GraphQL Type";
JResponse: JsonToken;
JPublications: JsonArray;
Cursor: Text;
Parameters: Dictionary of [Text, Text];
CurrentChannels: List of [BigInteger];
begin
CurrentChannels := CollectChannels(ShopCode);

CommunicationMgt.SetShop(ShopCode);
GraphQLType := GraphQLType::GetSalesChannels;

repeat
JResponse := CommunicationMgt.ExecuteGraphQL(GraphQLType, Parameters);
if JsonHelper.GetJsonArray(JResponse, JPublications, 'data.publications.edges') then begin
ExtractSalesChannels(JPublications, ShopCode, CurrentChannels, Cursor);
if Parameters.ContainsKey('After') then
Parameters.Set('After', Cursor)
else
Parameters.Add('After', Cursor);
GraphQLType := GraphQLType::GetNextSalesChannels;
end;
until not JsonHelper.GetValueAsBoolean(JResponse, 'data.publications.pageInfo.hasNextPage');

RemoveNotExistingChannels(CurrentChannels);
end;

local procedure CollectChannels(ShopCode: Code[20]): List of [BigInteger]
var
SalesChannel: Record "Shpfy Sales Channel";
Channels: List of [BigInteger];
begin
SalesChannel.SetRange("Shop Code", ShopCode);
if SalesChannel.FindSet() then
repeat
Channels.Add(SalesChannel.Id);
until SalesChannel.Next() = 0;
exit(Channels);
end;

local procedure RemoveNotExistingChannels(CurrentChannels: List of [BigInteger])
var
SalesChannel: Record "Shpfy Sales Channel";
ChannelId: BigInteger;
begin
foreach ChannelId in CurrentChannels do begin
SalesChannel.Get(ChannelId);
SalesChannel.Delete(true);
end;
end;

local procedure ExtractSalesChannels(JPublications: JsonArray; ShopCode: Code[20]; CurrentChannels: List of [BigInteger]; var Cursor: Text)
var
SalesChannel: Record "Shpfy Sales Channel";
JPublication: JsonToken;
ChannelId: BigInteger;
JCatalogEdges: JsonArray;
JCatalogEdge: JsonToken;
Handle: Text;
begin
foreach JPublication in JPublications do begin
Cursor := JsonHelper.GetValueAsText(JPublication, 'cursor');
ChannelId := CommunicationMgt.GetIdOfGId(JsonHelper.GetValueAsText(JPublication, '$.node.id'));
if not SalesChannel.Get(ChannelId) then begin
SalesChannel.Init();
SalesChannel.Validate(Id, ChannelId);
JCatalogEdges := JsonHelper.GetJsonArray(JPublication, '$.node.catalog.apps.edges');
JCatalogEdges.Get(0, JCatalogEdge);
SalesChannel.Validate(Name, JsonHelper.GetValueAsText(JCatalogEdge, '$.node.title'));
Handle := JsonHelper.GetValueAsText(JCatalogEdge, '$.node.handle');
if Handle = 'online_store' then
SalesChannel.Default := true;
SalesChannel."Shop Code" := ShopCode;
SalesChannel.Insert(true);
end else
CurrentChannels.Remove(ChannelId);
end;
end;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
namespace Microsoft.Integration.Shopify;

page 30167 "Shpfy Sales Channels"
{
ApplicationArea = All;
Caption = 'Shopify Sales Channels';
PageType = List;
SourceTable = "Shpfy Sales Channel";
InsertAllowed = false;
DeleteAllowed = false;
UsageCategory = None;


layout
{
area(Content)
{
repeater(General)
{
field(Id; Rec.Id) { }
field(Name; Rec.Name) { }
field("Use for publication"; Rec."Use for publication") { }
field(Default; Rec.Default) { }
}
}
}

actions
{
area(Processing)
{
action(GetSalesChannels)
{
ApplicationArea = All;
Caption = 'Get Sales Channels';
Promoted = true;
PromotedOnly = true;
PromotedCategory = Process;
Image = UpdateDescription;
ToolTip = 'Retrieves the sales channels from Shopify.';

trigger OnAction()
var
ShpfySalesChannelAPI: Codeunit "Shpfy Sales Channel API";
begin
ShpfySalesChannelAPI.RetrieveSalesChannelsFromShopify(CopyStr(Rec.GetFilter("Shop Code"), 1, 20));
end;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
namespace Microsoft.Integration.Shopify;

/// <summary>
/// Table Shpfy Sales Channel (ID 30159).
/// </summary>
table 30160 "Shpfy Sales Channel"
{
Caption = 'Shopify Sales Channel';
DataClassification = CustomerContent;

fields
{
field(1; Id; BigInteger)
{
Caption = 'Id';
Editable = false;
ToolTip = 'Specifies the unique identifier of the sales channel.';
}
field(2; Name; Text[100])
{
Caption = 'Name';
Editable = false;
ToolTip = 'Specifies the name of the sales channel.';
}
field(3; "Shop Code"; Code[20])
{
Caption = 'Shop Code';
Editable = false;
ToolTip = 'Specifies the code of the shop.';
}
field(4; "Use for publication"; Boolean)
{
Caption = 'Use for publication';
ToolTip = 'Specifies if the sales channel is used for new products publication.';
}
field(5; Default; Boolean)
{
Caption = 'Default';
Editable = false;
ToolTip = 'Specifies if the sales channel is the default one. Used for new products publication if no other channel is selected';
}
}
keys
{
key(PK; Id)
{
Clustered = true;
}
}
}
Loading
Loading