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
56 changes: 44 additions & 12 deletions src/modules/creators/creator-list-item.mapper.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,51 @@
import { strict as assert } from 'assert';
import { mapCreatorListItem } from './creator-list-item.mapper';
import { requestContextStorage } from '../../utils/als.utils';
import { logger } from '../../utils/logger.utils';

function run() {
const input = { id: '1', displayName: 'John', avatarUrl: null } as any;
jest.mock('../../utils/logger.utils', () => ({
logger: { warn: jest.fn() },
}));

const result = mapCreatorListItem(input);
const warnMock = logger.warn as jest.Mock;

assert.deepEqual(result, {
id: '1',
name: 'John',
avatar: null,
followers: 0,
beforeEach(() => {
warnMock.mockClear();
});

describe('mapCreatorListItem()', () => {
it('maps the public creator list item shape', () => {
const input = { id: '1', displayName: 'John', avatarUrl: null } as any;

const result = mapCreatorListItem(input);

expect(result).toEqual({
id: '1',
name: 'John',
avatar: null,
followers: 0,
});
expect(warnMock).not.toHaveBeenCalled();
});

console.log('creator-list-item.mapper test passed');
}
it('warns when a schema-required creator field is unexpectedly null', () => {
const input = { id: 'creator-1', displayName: null, avatarUrl: null } as any;

run();
const result = requestContextStorage.run(
{ path: '/api/v1/creators', method: 'GET', requestId: 'req-333' },
() => mapCreatorListItem(input)
);

expect(result).toEqual({
id: 'creator-1',
name: null,
avatar: null,
followers: 0,
});
expect(warnMock).toHaveBeenCalledWith({
msg: 'Unexpected null creator field in database result',
fieldName: 'displayName',
creatorId: 'creator-1',
requestId: 'req-333',
});
});
});
22 changes: 22 additions & 0 deletions src/modules/creators/creator-list-item.mapper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { CreatorProfile } from '../../types/profile.types';
import { requestContextStorage } from '../../utils/als.utils';
import { logger } from '../../utils/logger.utils';
import { safeRead } from '../../utils/safe-nested-read.utils';

/**
Expand All @@ -12,13 +14,33 @@ export type CreatorListItem = {
followers: number;
};

function warnIfUnexpectedNullCreatorField(
creator: CreatorProfile,
fieldName: 'displayName'
): void {
const rawCreator = creator as CreatorProfile & Record<string, unknown>;

if (rawCreator[fieldName] !== null) {
return;
}

logger.warn({
msg: 'Unexpected null creator field in database result',
fieldName,
creatorId: creator.id,
requestId: requestContextStorage.getStore()?.requestId ?? null,
});
}

/**
* Pure, dumb mapper from a full `CreatorProfile` to a `CreatorListItem`.
* No filtering, no business logic — deterministic and predictable.
*/
export const mapCreatorListItem = (
creator: CreatorProfile
): CreatorListItem => {
warnIfUnexpectedNullCreatorField(creator, 'displayName');

return {
id: creator.id,
name: safeRead(creator, 'displayName', null),
Expand Down
Loading