Skip to content

Commit 42ed8c2

Browse files
committed
adding tenant scope
1 parent 28d87d7 commit 42ed8c2

File tree

4 files changed

+24
-13
lines changed

4 files changed

+24
-13
lines changed

packages/b2c-cli/src/utils/scapi/replications.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,37 @@ export abstract class GranularReplicationsCommand<T extends typeof Command> exte
2626
* Requires:
2727
* - shortCode configuration (via b2c config:set --short-code <code>)
2828
* - OAuth credentials
29-
* - organizationId (extracted from tenant-id)
29+
* - tenantId (from --tenant-id flag or config)
3030
*/
3131
protected get granularReplicationsClient(): GranularReplicationsClient {
3232
if (!this._granularReplicationsClient) {
3333
const shortCode = this.resolvedConfig.values.shortCode;
34-
const organizationId = this.getOrganizationId();
34+
const tenantId = this.getTenantId();
3535

3636
if (!shortCode) {
3737
this.error('shortCode configuration is required. Run: b2c config:set --short-code <code>');
3838
}
3939

4040
this._granularReplicationsClient = createGranularReplicationsClient(
41-
{shortCode, organizationId},
41+
{shortCode, tenantId},
4242
this.getOAuthStrategy(),
4343
);
4444
}
4545
return this._granularReplicationsClient;
4646
}
4747

4848
/**
49-
* Get the organization ID from resolved config.
50-
* @throws Error if tenant ID is not provided through any source
49+
* Get the organization ID (with f_ecom_ prefix) for API path parameters.
5150
*/
5251
protected getOrganizationId(): string {
52+
return toOrganizationId(this.getTenantId());
53+
}
54+
55+
/**
56+
* Get the tenant ID from resolved config.
57+
* @throws Error if tenant ID is not provided through any source
58+
*/
59+
protected getTenantId(): string {
5360
const {tenantId} = this.resolvedConfig.values;
5461
if (!tenantId) {
5562
this.error(
@@ -59,6 +66,6 @@ export abstract class GranularReplicationsCommand<T extends typeof Command> exte
5966
),
6067
);
6168
}
62-
return toOrganizationId(tenantId);
69+
return tenantId;
6370
}
6471
}

packages/b2c-tooling-sdk/src/cli/base-command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
560560
}
561561

562562
// Use oclif's error() for proper exit code and display
563-
this.error(err.message, {exit: exitCode});
563+
return this.error(err.message, {exit: exitCode});
564564
}
565565

566566
/**

packages/b2c-tooling-sdk/src/clients/granular-replications.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import type {paths, components} from './granular-replications.generated.js';
99
import {createAuthMiddleware, createLoggingMiddleware, createRateLimitMiddleware} from './middleware.js';
1010
import {globalMiddlewareRegistry, type MiddlewareRegistry} from './middleware-registry.js';
1111
import {OAuthStrategy} from '../auth/oauth.js';
12+
import {buildTenantScope, toOrganizationId, normalizeTenantId} from './custom-apis.js';
13+
14+
export {toOrganizationId, normalizeTenantId, buildTenantScope};
1215

1316
export type {paths, components};
1417
export type GranularReplicationsClient = Client<paths>;
@@ -26,7 +29,7 @@ export type PublishIdResponse = components['schemas']['PublishIdResponse'];
2629

2730
export interface GranularReplicationsClientConfig {
2831
shortCode: string;
29-
organizationId: string;
32+
tenantId: string;
3033
scopes?: string[];
3134
middlewareRegistry?: MiddlewareRegistry;
3235
}
@@ -37,7 +40,7 @@ export interface GranularReplicationsClientConfig {
3740
* The Granular Replications API enables programmatic publishing of individual items
3841
* (products, price tables, content assets) from staging to production environments.
3942
*
40-
* @param config - Client configuration with shortCode and organizationId
43+
* @param config - Client configuration with shortCode and tenantId
4144
* @param auth - OAuth authentication strategy
4245
* @returns Typed Granular Replications API client
4346
*
@@ -53,7 +56,7 @@ export interface GranularReplicationsClientConfig {
5356
*
5457
* const client = createGranularReplicationsClient({
5558
* shortCode: 'kv7kzm78',
56-
* organizationId: 'f_ecom_zzxy_prd'
59+
* tenantId: 'zzxy_prd'
5760
* }, auth);
5861
*
5962
* // Queue a product for publishing
@@ -83,8 +86,8 @@ export function createGranularReplicationsClient(
8386
baseUrl: `https://${config.shortCode}.api.commercecloud.salesforce.com/operation/replications/v1`,
8487
});
8588

86-
// OAuth scope handling
87-
const requiredScopes = config.scopes ?? ['sfcc.granular-replications.rw'];
89+
// Build required scopes: domain scope + tenant-specific scope
90+
const requiredScopes = config.scopes ?? ['sfcc.granular-replications.rw', buildTenantScope(config.tenantId)];
8891
const scopedAuth = auth instanceof OAuthStrategy ? auth.withAdditionalScopes(requiredScopes) : auth;
8992

9093
client.use(createAuthMiddleware(scopedAuth));

packages/b2c-tooling-sdk/test/clients/granular-replications.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {createGranularReplicationsClient} from '../../src/clients/granular-repli
1010
import {MockAuthStrategy} from '../helpers/mock-auth.js';
1111

1212
const SHORT_CODE = 'kv7kzm78';
13+
const TENANT_ID = 'zzxy_prd';
1314
const ORG_ID = 'f_ecom_zzxy_prd';
1415
const BASE_URL = `https://${SHORT_CODE}.api.commercecloud.salesforce.com/operation/replications/v1`;
1516

@@ -24,7 +25,7 @@ describe('Granular Replications Client', () => {
2425

2526
beforeEach(() => {
2627
mockAuth = new MockAuthStrategy();
27-
client = createGranularReplicationsClient({shortCode: SHORT_CODE, organizationId: ORG_ID}, mockAuth);
28+
client = createGranularReplicationsClient({shortCode: SHORT_CODE, tenantId: TENANT_ID}, mockAuth);
2829
});
2930

3031
it('should create client with config', () => {

0 commit comments

Comments
 (0)