Skip to content
Open
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: 8 additions & 3 deletions src/app/api/listings/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import {
listingSelect,
QueryParamsSchema,
} from '@/features/listings/constants/schema';
import { buildListingQuery } from '@/features/listings/utils/query-builder';
import {
buildListingQuery,
shouldPrioritizeFeatured,
} from '@/features/listings/utils/query-builder';
import { reorderFeaturedOngoing } from '@/features/listings/utils/reorderFeaturedOngoing';

export async function GET(request: NextRequest) {
Expand Down Expand Up @@ -85,7 +88,9 @@ export async function GET(request: NextRequest) {
},
});

const reorderedListings = reorderFeaturedOngoing(listings);
const orderedListings = shouldPrioritizeFeatured(queryData)
? reorderFeaturedOngoing(listings)
: listings;

const isBookmarksContext = queryData.context === 'bookmarks';
const headersInit = isBookmarksContext
Expand All @@ -98,7 +103,7 @@ export async function GET(request: NextRequest) {
Vary: 'Cookie',
};

return NextResponse.json(reorderedListings, {
return NextResponse.json(orderedListings, {
headers: headersInit,
});
} catch (error) {
Expand Down
15 changes: 11 additions & 4 deletions src/features/listings/utils/query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ interface ListingQueryResult {
readonly take?: number;
}

export function shouldPrioritizeFeatured(
args: Pick<BuildListingQueryArgs, 'sortBy' | 'order' | 'status'>,
): boolean {
return (
args.sortBy === 'Date' &&
args.order === 'asc' &&
(args.status === 'open' || args.status === 'all')
);
}

function getSkillFilter(
category: z.infer<typeof ListingCategorySchema>,
): BountiesWhereInput | null {
Expand Down Expand Up @@ -141,10 +151,7 @@ function getOrderBy(
}

// add isFeatured prioritization only for default sorting (date + asc) and open or all status
const isDefaultSort =
sortBy === 'Date' &&
order === 'asc' &&
(status === 'open' || status === 'all');
const isDefaultSort = shouldPrioritizeFeatured(args);

return isDefaultSort ? [{ isFeatured: 'desc' }, primarySort] : primarySort;
}
Expand Down