diff --git a/src/constants.ts b/src/constants.ts index 0d8e7ec7..8d06ccd2 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,3 +1,4 @@ export const HUNDRED_TB = 109951162777600; export const FREE_PLAN_BYTES_SPACE = 1 * 1024 * 1024 * 1024; export const VERIFICATION_CHARGE = 100; +export const DEFAULT_CUSTOMER_NAME = 'Internxt User'; diff --git a/src/infrastructure/domain/entities/customer.ts b/src/infrastructure/domain/entities/customer.ts index 7446fcfe..f12dda25 100644 --- a/src/infrastructure/domain/entities/customer.ts +++ b/src/infrastructure/domain/entities/customer.ts @@ -1,6 +1,7 @@ import Stripe from 'stripe'; import { BadRequestError } from '../../../errors/Errors'; import { Address } from '../types'; +import { DEFAULT_CUSTOMER_NAME } from '../../../constants'; export interface CreateCustomerParams { name: string; @@ -26,9 +27,7 @@ export class Customer { ) {} static toDomain(stripeCustomer: Stripe.Customer): Customer { - if (!stripeCustomer.name) { - throw new BadRequestError('Customer name is required'); - } + const customerName = stripeCustomer.name ?? DEFAULT_CUSTOMER_NAME; if (!stripeCustomer.email) { throw new BadRequestError('Customer email is required'); @@ -36,7 +35,7 @@ export class Customer { return new Customer( stripeCustomer.id, - stripeCustomer.name, + customerName, stripeCustomer.email, { line1: stripeCustomer.address?.line1, diff --git a/tests/src/infrastructure/domain/entities/customer.test.ts b/tests/src/infrastructure/domain/entities/customer.test.ts index d7300afa..44efb282 100644 --- a/tests/src/infrastructure/domain/entities/customer.test.ts +++ b/tests/src/infrastructure/domain/entities/customer.test.ts @@ -1,6 +1,7 @@ import { getCustomer } from '../../../fixtures'; import { Customer } from '../../../../../src/infrastructure/domain/entities/customer'; import { BadRequestError } from '../../../../../src/errors/Errors'; +import { DEFAULT_CUSTOMER_NAME } from '../../../../../src/constants'; describe('Customer entity', () => { const mockedCustomer = getCustomer(); @@ -49,11 +50,12 @@ describe('Customer entity', () => { }); }); - test('When converting a customer without name, then an error is thrown', () => { - const badRequestNotFoundError = new BadRequestError('Customer name is required'); + test('When converting a customer without name, then the default name is used', () => { const customerWithoutName = { ...mockedCustomer, name: null }; - expect(() => Customer.toDomain(customerWithoutName)).toThrow(badRequestNotFoundError); + const customer = Customer.toDomain(customerWithoutName); + + expect(customer.name).toStrictEqual(DEFAULT_CUSTOMER_NAME); }); test('When converting a customer without email, then an error is thrown', () => {