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

Add site_id to the SFCC datasource #353

Closed
wants to merge 6 commits into from

Conversation

ingeniumed
Copy link
Contributor

Description

The purpose of this PR is to add the site_id to any communications with the SFCC API instead of using the hardcoded value of RefArchGlobal.

This PR does have 2 temporary pieces of code in it, in order to get site_id in place. There is an internal discussion and plan to add migration in place for such changes, to avoid future cases like this.

Testing

  • Create an existing SFCC data source before testing this branch out. Ensure there are blocks on the editor made using this datasource.
  • Switch to this branch and ensure your site doesn't break. Open up the settings, and play around with the blocks. It should use RefArchGlobal and allow you to change this as well.

Future

Once this goes in, I'll create a follow up PR that'll drop the default value and the nullable as well. Once the existing test sites have been upgrade so that the site_id is populated for them, that can then be merged.

@ingeniumed ingeniumed self-assigned this Feb 6, 2025
): WP_Error|string {
$client_auth_url = sprintf( '%s/shopper/auth/v1/organizations/%s/oauth2/token', $endpoint, $organization_id );
$client_credentials = base64_encode( sprintf( '%s:%s', $client_id, $client_secret ) );

$client_auth_response = wp_remote_post($client_auth_url, [
'body' => [
'grant_type' => 'client_credentials',
'channel_id' => 'RefArch',
'channel_id' => $site_id,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed this was different than the site_id hardcoded elsewhere of RefArchGlobal so I've corrected this.



// ToDo: Remove this once existing SFCC data sources have been migrated.
if ( empty( $config['service_config']['site_id'] ) ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is present to stop existing test sites from breaking. It adds the previous value as a default value. Once these test sites have been migrated, this can be removed.

I've added this as an interim fix to add the site_id in place with the intention that the internal plan to add migration as a feature would prevent this down the line.

@@ -21,6 +21,8 @@ protected static function get_service_config_schema(): array {
'enable_blocks' => Types::nullable( Types::boolean() ),
'organization_id' => Types::string(),
'shortcode' => Types::string(),
// ToDo: Remove the nullable existing SFCC data sources have been migrated.
'site_id' => Types::nullable( Types::string() ),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is set to nullable only until the existing test sites are migrated. After that, the nullable part can be dropped.

Same thing as below, it's an interim fix to add the site_id in place with the intention that the internal plan to add migration would negate this in the future.

@alecgeatches
Copy link
Contributor

@ingeniumed I'm able to see some on-page errors when I switch sources from trunk. Here's what I did:

  1. Set up a new SFCC data source on trunk.

  2. Create a page and use the data source to select a product.

  3. Publish the page.

  4. Switch to the fix/cafe-1196-sfcc-connection branch.

  5. Re-open the page on the frontend, and see error:

    Screenshot 2025-02-06 at 9 55 11 AM

    Here's the full call-stack:

    Fatal error: Uncaught Error: RemoteDataBlocks\Integrations\SalesforceB2C\Auth\SalesforceB2CAuth::generate_token(): Argument #5 ($site_id) must be of type string, null given, called in /var/www/html/wp-content/plugins/remote-data-blocks/inc/Integrations/SalesforceB2C/SalesforceB2CIntegration.php on line 40
    in /var/www/html/wp-content/plugins/remote-data-blocks/inc/Integrations/SalesforceB2C/Auth/SalesforceB2CAuth.php on line 24
    
    Call stack:
    
    RemoteDataBlocks\I\S\A\SalesforceB2CAuth::generate_token()
    wp-content/plugins/remote-data-blocks/inc/Integrations/SalesforceB2C/SalesforceB2CIntegration.php:40
    RemoteDataBlocks\I\S\SalesforceB2CIntegration::RemoteDataBlocks\I\S\{closure}()
    call_user_func_array()
    wp-content/plugins/remote-data-blocks/inc/Config/ArraySerializable.php:22
    RemoteDataBlocks\Config\ArraySerializable::get_or_call_from_config()
    wp-content/plugins/remote-data-blocks/inc/Config/Query/HttpQuery.php:105
    RemoteDataBlocks\C\Q\HttpQuery::get_request_headers()
    wp-content/plugins/remote-data-blocks/inc/Config/QueryRunner/QueryRunner.php:37
    RemoteDataBlocks\C\Q\QueryRunner::get_request_details()
    wp-content/plugins/remote-data-blocks/inc/Config/QueryRunner/QueryRunner.php:118
    RemoteDataBlocks\C\Q\QueryRunner::get_raw_response_data()
    wp-content/plugins/remote-data-blocks/inc/Config/QueryRunner/QueryRunner.php:208
    RemoteDataBlocks\C\Q\QueryRunner::execute()
    wp-content/plugins/remote-data-blocks/inc/Config/Query/HttpQuery.php:26
    RemoteDataBlocks\C\Q\HttpQuery::execute()
    wp-content/plugins/remote-data-blocks/inc/Editor/DataBinding/BlockBindings.php:137
    RemoteDataBlocks\E\D\BlockBindings::execute_query()
    wp-content/plugins/remote-data-blocks/inc/Editor/DataBinding/BlockBindings.php:201
    RemoteDataBlocks\E\D\BlockBindings::get_value()
    wp-includes/class-wp-block-bindings-source.php:86
    WP_Block_Bindings_Source::get_value()
    wp-includes/class-wp-block.php:323
    WP_Block::process_block_bindings()
    wp-includes/class-wp-block.php:484
    WP_Block::render()
    wp-includes/class-wp-block.php:517
    WP_Block::render()
    wp-includes/class-wp-block.php:517
    WP_Block::render()
    wp-includes/class-wp-block.php:517
    WP_Block::render()
    wp-includes/class-wp-block.php:517
    WP_Block::render()
    wp-includes/blocks.php:2190
    render_block()
    wp-includes/blocks.php:2242
    do_blocks()
    wp-includes/class-wp-hook.php:324
    WP_Hook::apply_filters()
    wp-includes/plugin.php:205
    apply_filters()
    wp-includes/blocks/post-content.php:50
    render_block_core_post_content()
    wp-includes/class-wp-block.php:537
    WP_Block::render()
    wp-includes/class-wp-block.php:517
    WP_Block::render()
    wp-includes/class-wp-block.php:517
    WP_Block::render()
    wp-includes/blocks.php:2190
    render_block()
    wp-includes/blocks.php:2242
    do_blocks()
    wp-includes/block-template.php:260
    get_the_block_template_html()
    wp-includes/template-canvas.php:12
    include()
    wp-includes/template-loader.php:106
    require_once()
    wp-blog-header.php:19
    require()
    index.php:17
    

    I can also see the same error when I open the editor:

    image

I think it's due to $site_id being null, which is a type error because it's typed as a string. You may need to switch the consumers to use ?string instead, or maybe there's a better way to fill in a value earlier in the process so the types don't need to change.

onChange={ siteId => {
handleOnChange( 'site_id', siteId ?? '' );
} }
value={ state.site_id ?? 'RefArchGlobal' }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Provided a default value similar to what we had before of RefArchGlobal. Figured since there isn't exactly an API to get a list of this, I've left it as a textbox with a helpful value.

@ingeniumed ingeniumed closed this Feb 9, 2025
@ingeniumed
Copy link
Contributor Author

Closed this PR, as I realized the fixes I'm doing are going to lead to more work when the migration pathway work goes in. It's better to lay the ground work for that, rather than try to get this in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants