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
11 changes: 6 additions & 5 deletions src/modules/creators/creators.schemas.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { z } from 'zod';
import {
CREATOR_LIST_SORT_OPTIONS,
CREATOR_LIST_SORT_ORDERS,
} from './creators.sort';

/**
* Validation schema for creator list query parameters.
Expand Down Expand Up @@ -29,11 +33,8 @@ export const CreatorListQuerySchema = z.object({
}),

// Sorting
sort: z
.enum(['createdAt', 'updatedAt', 'displayName', 'handle'])
.optional()
.default('createdAt'),
order: z.enum(['asc', 'desc']).optional().default('desc'),
sort: z.enum(CREATOR_LIST_SORT_OPTIONS).optional().default('createdAt'),
order: z.enum(CREATOR_LIST_SORT_ORDERS).optional().default('desc'),

// Filters
verified: z
Expand Down
46 changes: 46 additions & 0 deletions src/modules/creators/creators.sort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Prisma } from '@prisma/client';

/**
* Public sort options accepted by creator list endpoints.
* These remain stable even if the internal query implementation changes.
*/
export const CREATOR_LIST_SORT_OPTIONS = [
'createdAt',
'updatedAt',
'displayName',
'handle',
] as const;

export const CREATOR_LIST_SORT_ORDERS = ['asc', 'desc'] as const;

export type CreatorListSortOption = (typeof CREATOR_LIST_SORT_OPTIONS)[number];
export type CreatorListSortOrder = (typeof CREATOR_LIST_SORT_ORDERS)[number];

const CREATOR_LIST_SORT_FIELD_MAP: Record<
CreatorListSortOption,
keyof Prisma.CreatorProfileOrderByWithRelationInput
> = {
createdAt: 'createdAt',
updatedAt: 'updatedAt',
displayName: 'displayName',
handle: 'handle',
};

/**
* Map a public sort option into an internal Prisma orderBy object.
* Throws for unsupported values so invalid sort input is never passed through silently.
*/
export function mapCreatorListSort(
sort: string,
order: CreatorListSortOrder
): Prisma.CreatorProfileOrderByWithRelationInput {
const field = CREATOR_LIST_SORT_FIELD_MAP[sort as CreatorListSortOption];

if (!field) {
throw new Error(`Unsupported creator sort option: ${sort}`);
}

return {
[field]: order,
} as Prisma.CreatorProfileOrderByWithRelationInput;
}
4 changes: 2 additions & 2 deletions src/modules/creators/creators.utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { prisma } from '../../utils/prisma.utils';
import { CreatorProfile } from '../../types/profile.types';
import { CreatorListQueryType } from './creators.schemas';
import { mapCreatorListSort } from './creators.sort';

type CreatorListWhere = {
isVerified?: boolean;
Expand Down Expand Up @@ -35,8 +36,7 @@ export async function fetchCreatorList(
];
}

// Build order by clause
const orderBy = { [sort]: order };
const orderBy = mapCreatorListSort(sort, order);

// Fetch creators and total count in parallel
const [creators, total] = await Promise.all([
Expand Down
Loading