feat: add public search pagination

This commit is contained in:
2026-09-08 19:28:13 -04:00
parent 43f4f69894
commit 27fe6c6116
6 changed files with 134 additions and 18 deletions
@@ -9,7 +9,10 @@ test('normalizes public search input and delegates to the repository with public
listingRepository: {
async searchPublic(filters) {
calls.push(filters);
return [{ id: 'listing-1', status: 'AVAILABLE' }];
return {
items: [{ id: 'listing-1', status: 'AVAILABLE' }],
total: 21,
};
},
},
});
@@ -23,7 +26,15 @@ test('normalizes public search input and delegates to the repository with public
limit: 10,
});
assert.deepEqual(result, [{ id: 'listing-1', status: 'AVAILABLE' }]);
assert.deepEqual(result, {
items: [{ id: 'listing-1', status: 'AVAILABLE' }],
pagination: {
page: 2,
limit: 10,
total: 21,
totalPages: 3,
},
});
assert.deepEqual(calls, [{
text: 'pikachu 199 holo',
category: 'Cards',
@@ -39,7 +50,7 @@ test('allows explicit public sold results and keeps the public filter defaults',
const service = createSearchService({
listingRepository: {
async searchPublic(filters) {
return filters;
return { items: [filters], total: 1 };
},
},
});
@@ -52,9 +63,17 @@ test('allows explicit public sold results and keeps the public filter defaults',
});
assert.deepEqual(filters, {
text: 'charizard',
page: 1,
limit: 25,
includeSold: true,
items: [{
text: 'charizard',
page: 1,
limit: 25,
includeSold: true,
}],
pagination: {
page: 1,
limit: 25,
total: 1,
totalPages: 1,
},
});
});
@@ -0,0 +1,35 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { buildApp } from '../../src/app/build-app.js';
test('returns public search results with pagination metadata', async (testContext) => {
const app = buildApp({
searchService: {
async searchPublic(filters) {
assert.equal(filters.page, 2);
assert.equal(filters.limit, 5);
assert.equal(filters.includeSold, true);
return {
items: [{ id: 'listing-1', status: 'CLOSED' }],
pagination: { page: 2, limit: 5, total: 6, totalPages: 2 },
};
},
},
});
testContext.after(() => app.close());
const response = await app.inject({
method: 'GET',
url: '/api/v1/public/search?query=charizard&page=2&limit=5&includeSold=true',
});
assert.equal(response.statusCode, 200);
assert.deepEqual(response.json().data.pagination, {
page: 2,
limit: 5,
total: 6,
totalPages: 2,
});
assert.equal(response.json().data.items[0].id, 'listing-1');
});
@@ -125,6 +125,44 @@ test('lists the most recent draft entries for an inventory item', async () => {
assert.equal(listingsForItem[0].inventoryItemId, inventoryItem.id);
});
test('searches public listings with pagination and hides sold items by default', async () => {
const { category, condition, inventoryItem, product } = await createInventoryFixture();
await listingRepository.create({
inventoryItemId: inventoryItem.id,
productName: product.name,
categoryId: category.id,
conditionId: condition.id,
askingPriceMinor: 2499,
status: 'PENDING',
});
const visible = await listingRepository.searchPublic({ page: 1, limit: 10 });
assert.equal(visible.total, 1);
assert.equal(visible.items.length, 1);
assert.equal(visible.items[0].status, 'PENDING');
await database.db
.update(listings)
.set({ status: 'CLOSED' })
.where(sql`inventory_item_id = ${inventoryItem.id}`);
await database.db
.update(inventoryItems)
.set({ status: 'SOLD' })
.where(sql`id = ${inventoryItem.id}`);
const hidden = await listingRepository.searchPublic({ page: 1, limit: 10 });
assert.equal(hidden.total, 0);
const included = await listingRepository.searchPublic({
page: 1,
limit: 10,
includeSold: true,
});
assert.equal(included.total, 1);
assert.equal(included.items[0].status, 'CLOSED');
});
test('persists image metadata only for the owning seller and counts item images', async () => {
const { seller, inventoryItem } = await createInventoryFixture();
const image = await inventoryImageRepository.createForSeller({