Skip to content

Commit 17ce1fe

Browse files
Piyush Singh GaurPiyush Singh Gaur
authored andcommitted
fix(provider): narrow the type for defaultPricingModel and throws descriptive error if itemId is missing
narrow the type for defaultPricingModel and throws descriptive error if itemId is missing
1 parent b2b4b0f commit 17ce1fe

File tree

4 files changed

+23
-17
lines changed

4 files changed

+23
-17
lines changed

src/__tests__/unit/chargebee-subscription.service.unit.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ describe('ChargeBeeService - Subscription Management', () => {
166166
expect(result.product).to.equal('enterprise-plan');
167167
expect(result.recurring?.interval).to.equal(RecurringInterval.MONTH);
168168
expect(result.recurring?.intervalCount).to.equal(1);
169-
expect(result.active).to.be.true();
169+
expect(result.active).to.be.true;
170170
});
171171

172172
it('returns undefined recurring when no period_unit is set', async () => {
@@ -190,7 +190,7 @@ describe('ChargeBeeService - Subscription Management', () => {
190190
product: 'setup',
191191
});
192192

193-
expect(result.recurring).to.be.undefined();
193+
expect(result.recurring).to.be.undefined;
194194
});
195195
});
196196

@@ -258,7 +258,7 @@ describe('ChargeBeeService - Subscription Management', () => {
258258
expect(result.customerId).to.equal('cust_tenant_abc');
259259
expect(result.currentPeriodStart).to.equal(1700000000);
260260
expect(result.currentPeriodEnd).to.equal(1702592000);
261-
expect(result.cancelAtPeriodEnd).to.be.false();
261+
expect(result.cancelAtPeriodEnd).to.be.false;
262262
});
263263
});
264264

@@ -301,7 +301,7 @@ describe('ChargeBeeService - Subscription Management', () => {
301301

302302
const [, params] = updateStub.firstCall.args;
303303
// ProrationBehavior.NONE -> prorate: false
304-
expect(params!.prorate).to.be.false();
304+
expect(params!.prorate).to.be.false;
305305
});
306306
});
307307

@@ -325,7 +325,7 @@ describe('ChargeBeeService - Subscription Management', () => {
325325
Record<string, unknown>,
326326
];
327327
expect(subId).to.equal('sub_cb_001');
328-
expect(params.end_of_term).to.be.false();
328+
expect(params.end_of_term).to.be.false;
329329
expect(params.cancel_reason_code).to.equal('customer_request');
330330
});
331331

@@ -348,7 +348,7 @@ describe('ChargeBeeService - Subscription Management', () => {
348348
string,
349349
Record<string, unknown>,
350350
];
351-
expect(params.end_of_term).to.be.true();
351+
expect(params.end_of_term).to.be.true;
352352
expect(params.cancel_reason_code).to.equal('not_paid');
353353
});
354354
});
@@ -453,7 +453,7 @@ describe('ChargeBeeService - Subscription Management', () => {
453453

454454
const result = await service.checkProductExists('enterprise-plan');
455455

456-
expect(result).to.be.true();
456+
expect(result).to.be.true;
457457
});
458458

459459
it('returns false when the item is archived', async () => {
@@ -463,7 +463,7 @@ describe('ChargeBeeService - Subscription Management', () => {
463463

464464
const result = await service.checkProductExists('old-plan');
465465

466-
expect(result).to.be.false();
466+
expect(result).to.be.false;
467467
});
468468

469469
it('returns false when Chargebee signals resource_not_found', async () => {
@@ -478,7 +478,7 @@ describe('ChargeBeeService - Subscription Management', () => {
478478

479479
const result = await service.checkProductExists('missing-plan');
480480

481-
expect(result).to.be.false();
481+
expect(result).to.be.false;
482482
});
483483

484484
it('re-throws unexpected errors from Chargebee', async () => {

src/__tests__/unit/stripe-subscription.service.unit.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ describe('StripeService - Subscription Management', () => {
185185
expect(result.product).to.equal('prod_enterprise_123');
186186
expect(result.recurring?.interval).to.equal(RecurringInterval.MONTH);
187187
expect(result.recurring?.intervalCount).to.equal(1);
188-
expect(result.active).to.be.true();
188+
expect(result.active).to.be.true;
189189
});
190190

191191
it('handles a one-time price (no recurring field)', async () => {
@@ -209,7 +209,7 @@ describe('StripeService - Subscription Management', () => {
209209

210210
const result = await service.createPrice(priceInput);
211211

212-
expect(result.recurring).to.be.undefined();
212+
expect(result.recurring).to.be.undefined;
213213
});
214214
});
215215

@@ -347,7 +347,7 @@ describe('StripeService - Subscription Management', () => {
347347
expect(result.customerId).to.equal('cus_tenant_abc');
348348
expect(result.currentPeriodStart).to.equal(1700000000);
349349
expect(result.currentPeriodEnd).to.equal(1702592000);
350-
expect(result.cancelAtPeriodEnd).to.be.false();
350+
expect(result.cancelAtPeriodEnd).to.be.false;
351351
});
352352
});
353353

@@ -643,15 +643,15 @@ describe('StripeService - Subscription Management', () => {
643643

644644
const result = await service.checkProductExists('prod_active_001');
645645

646-
expect(result).to.be.true();
646+
expect(result).to.be.true;
647647
});
648648

649649
it('returns false when the product is archived (active: false)', async () => {
650650
stripeStub.products.retrieve.resolves({active: false});
651651

652652
const result = await service.checkProductExists('prod_archived_002');
653653

654-
expect(result).to.be.false();
654+
expect(result).to.be.false;
655655
});
656656

657657
it('returns false when Stripe signals resource_missing', async () => {
@@ -662,7 +662,7 @@ describe('StripeService - Subscription Management', () => {
662662

663663
const result = await service.checkProductExists('prod_gone_003');
664664

665-
expect(result).to.be.false();
665+
expect(result).to.be.false;
666666
});
667667

668668
it('re-throws unexpected errors from Stripe', async () => {

src/providers/sdk/chargebee/charge-bee.service.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,13 @@ export class ChargeBeeService implements IChargeBeeService {
321321
.replaceAll(/[^a-z0-9]+/g, '-')
322322
.replaceAll(/^-|-$/g, '');
323323

324+
if (!itemId) {
325+
throw new Error(
326+
`Cannot derive a valid Chargebee item ID from product name "${product.name}". ` +
327+
'The name must contain at least one alphanumeric character.',
328+
);
329+
}
330+
324331
// Strip item_family_id from metadata — it is a top-level Chargebee param,
325332
// and Chargebee rejects it if it appears inside metadata.
326333
// eslint-disable-next-line @typescript-eslint/no-unused-vars

src/providers/sdk/chargebee/type/chargebee-config.type.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ export interface ChargeBeeConfig {
1818
/**
1919
* Pricing model applied when creating ItemPrices.
2020
* Defaults to `'flat_fee'` (single fixed recurring charge).
21-
* Other Chargebee values: `'per_unit'`, `'tiered'`, `'volume'`, `'stairstep'`.
2221
*/
23-
defaultPricingModel?: string;
22+
defaultPricingModel?: ChargebeePricingModel;
2423

2524
/**
2625
* When `true`, subscriptions are cancelled at the end of the current billing

0 commit comments

Comments
 (0)