Skip to content

Commit 5170b52

Browse files
Piyush Singh GaurPiyush Singh Gaur
authored andcommitted
fix(provider): fix sonar issues
fix sonar issue GH-0
1 parent 0a1ea48 commit 5170b52

File tree

3 files changed

+39
-20
lines changed

3 files changed

+39
-20
lines changed

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

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable @typescript-eslint/naming-convention */
2-
import {randomUUID} from 'crypto';
2+
import {randomUUID} from 'node:crypto';
33
import {inject} from '@loopback/core';
44
import chargebee from 'chargebee';
55
import {
@@ -21,6 +21,8 @@ import {
2121
import {ChargeBeeBindings} from './key';
2222
import {
2323
ChargeBeeConfig,
24+
ChargebeePeriodUnit,
25+
ChargebeePricingModel,
2426
IChargeBeeCustomer,
2527
IChargeBeeInvoice,
2628
IChargeBeePaymentSource,
@@ -316,8 +318,8 @@ export class ChargeBeeService implements IChargeBeeService {
316318
// Strip item_family_id from metadata — it is a top-level Chargebee param,
317319
// and Chargebee rejects it if it appears inside metadata.
318320
// eslint-disable-next-line @typescript-eslint/no-unused-vars
319-
const {item_family_id: _ignored, ...restMetadata} = (product.metadata ??
320-
{}) as Record<string, unknown>;
321+
const {item_family_id: _ignored, ...restMetadata} =
322+
product.metadata ?? {};
321323
const hasExtraMetadata = Object.keys(restMetadata).length > 0;
322324

323325
const result = await chargebee.item
@@ -367,17 +369,9 @@ export class ChargeBeeService implements IChargeBeeService {
367369
currency_code: price.currency.toUpperCase(),
368370
price: price.unitAmount,
369371
pricing_model: (this.chargeBeeConfig.defaultPricingModel ??
370-
'flat_fee') as
371-
| 'flat_fee'
372-
| 'per_unit'
373-
| 'tiered'
374-
| 'volume'
375-
| 'stairstep',
372+
'flat_fee') as ChargebeePricingModel,
376373
period_unit: price.recurring?.interval as
377-
| 'day'
378-
| 'week'
379-
| 'month'
380-
| 'year'
374+
| ChargebeePeriodUnit
381375
| undefined,
382376
period: price.recurring?.intervalCount,
383377
tax_providers_fields: [], // Required by SDK type but can be empty
@@ -585,10 +579,11 @@ export class ChargeBeeService implements IChargeBeeService {
585579
const result = await chargebee.item.retrieve(productId).request();
586580
return result.item.status === 'active';
587581
} catch (error) {
582+
const HTTP_NOT_FOUND = 404;
588583
const cbError = error as {api_error_code?: string; http_status?: number};
589584
if (
590585
cbError.api_error_code === 'resource_not_found' ||
591-
cbError.http_status === 404
586+
cbError.http_status === HTTP_NOT_FOUND
592587
) {
593588
return false;
594589
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,12 @@ export interface ChargeBeeConfig {
3636
*/
3737
defaultCancelReasonCode?: string;
3838
}
39+
40+
export type ChargebeePricingModel =
41+
| 'flat_fee'
42+
| 'per_unit'
43+
| 'tiered'
44+
| 'volume'
45+
| 'stairstep';
46+
47+
export type ChargebeePeriodUnit = 'day' | 'week' | 'month' | 'year';

src/providers/sdk/stripe/adapter/subscription.adapter.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,11 @@ export class StripeSubscriptionAdapter
3434
* @param resp - Raw Stripe Subscription returned by the SDK.
3535
*/
3636
adaptToModel(resp: Stripe.Subscription): TSubscriptionResult {
37+
const customerId = this.extractCustomerId(resp.customer);
3738
return {
3839
id: resp.id,
3940
status: resp.status,
40-
customerId:
41-
typeof resp.customer === 'string'
42-
? resp.customer
43-
: resp.customer && 'id' in resp.customer
44-
? resp.customer.id
45-
: undefined,
41+
customerId,
4642
currentPeriodStart: resp.current_period_start,
4743
currentPeriodEnd: resp.current_period_end,
4844
cancelAtPeriodEnd: resp.cancel_at_period_end,
@@ -66,4 +62,23 @@ export class StripeSubscriptionAdapter
6662
}),
6763
};
6864
}
65+
66+
/**
67+
* Extracts customer ID from Stripe customer field.
68+
* Handles string ID, expanded Customer object, and DeletedCustomer edge case.
69+
*
70+
* @param customer - Stripe customer field (string | Customer | DeletedCustomer)
71+
* @returns Customer ID or undefined if customer was deleted
72+
*/
73+
private extractCustomerId(
74+
customer: string | Stripe.Customer | Stripe.DeletedCustomer,
75+
): string | undefined {
76+
if (typeof customer === 'string') {
77+
return customer;
78+
}
79+
if (customer && 'id' in customer) {
80+
return customer.id;
81+
}
82+
return undefined;
83+
}
6984
}

0 commit comments

Comments
 (0)