Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -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';
7 changes: 3 additions & 4 deletions src/infrastructure/domain/entities/customer.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -26,17 +27,15 @@ 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');
}

return new Customer(
stripeCustomer.id,
stripeCustomer.name,
customerName,
stripeCustomer.email,
{
line1: stripeCustomer.address?.line1,
Expand Down
8 changes: 5 additions & 3 deletions tests/src/infrastructure/domain/entities/customer.test.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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', () => {
Expand Down
Loading