feat: add public inventory catalog boundary
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
|
||||
import { createPublicInventoryService } from '../../src/modules/inventory/services/public-inventory-service.js';
|
||||
|
||||
test('lists visible public inventory through the repository boundary', async () => {
|
||||
const service = createPublicInventoryService({
|
||||
inventoryItemRepository: {
|
||||
async searchPublic(filters) {
|
||||
assert.deepEqual(filters, { page: 2, limit: 5 });
|
||||
return { items: [{ id: 'inventory-1' }], total: 1 };
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
assert.deepEqual(await service.list({ page: 2, limit: 5 }), {
|
||||
items: [{ id: 'inventory-1' }],
|
||||
total: 1,
|
||||
});
|
||||
});
|
||||
|
||||
test('returns a not-found error for hidden public inventory', async () => {
|
||||
const service = createPublicInventoryService({
|
||||
inventoryItemRepository: { async findPublicById() { return null; } },
|
||||
});
|
||||
|
||||
await assert.rejects(
|
||||
() => service.getById('inventory-1'),
|
||||
(error) => error.code === 'INVENTORY_NOT_FOUND' && error.statusCode === 404,
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,82 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
|
||||
import { buildApp } from '../../src/app/build-app.js';
|
||||
|
||||
function createTestApp() {
|
||||
return buildApp({
|
||||
publicInventoryService: {
|
||||
async list() {
|
||||
return {
|
||||
items: [{
|
||||
id: 'inventory-1',
|
||||
productId: 'product-1',
|
||||
productName: 'Pikachu Plush',
|
||||
productSlug: 'pikachu-plush',
|
||||
categoryId: 'category-1',
|
||||
conditionId: 'condition-1',
|
||||
askingPriceMinor: 2499,
|
||||
listingStatus: 'PENDING',
|
||||
listingNotes: 'Available soon.',
|
||||
}],
|
||||
pagination: { page: 1, limit: 20, total: 1, totalPages: 1 },
|
||||
};
|
||||
},
|
||||
async getById(id) {
|
||||
if (id !== 'inventory-1') {
|
||||
const error = new Error('Inventory item was not found.');
|
||||
error.code = 'INVENTORY_NOT_FOUND';
|
||||
error.statusCode = 404;
|
||||
throw error;
|
||||
}
|
||||
|
||||
return {
|
||||
id,
|
||||
productId: 'product-1',
|
||||
productName: 'Pikachu Plush',
|
||||
productSlug: 'pikachu-plush',
|
||||
categoryId: 'category-1',
|
||||
conditionId: 'condition-1',
|
||||
askingPriceMinor: 2499,
|
||||
listingStatus: 'PENDING',
|
||||
listingNotes: 'Available soon.',
|
||||
notes: 'PRIVATE ITEM NOTE',
|
||||
};
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
test('returns public inventory collection data without private item notes', async (testContext) => {
|
||||
const app = createTestApp();
|
||||
testContext.after(() => app.close());
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/public/inventory?page=1&limit=20',
|
||||
});
|
||||
|
||||
assert.equal(response.statusCode, 200);
|
||||
assert.equal(response.json().data.items[0].productSlug, 'pikachu-plush');
|
||||
assert.equal('notes' in response.json().data.items[0], false);
|
||||
assert.equal(response.json().data.pagination.total, 1);
|
||||
});
|
||||
|
||||
test('returns public inventory detail or not found', async (testContext) => {
|
||||
const app = createTestApp();
|
||||
testContext.after(() => app.close());
|
||||
|
||||
const detailResponse = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/public/inventory/inventory-1',
|
||||
});
|
||||
const missingResponse = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/public/inventory/hidden-inventory',
|
||||
});
|
||||
|
||||
assert.equal(detailResponse.statusCode, 200);
|
||||
assert.equal('notes' in detailResponse.json().data, false);
|
||||
assert.equal(missingResponse.statusCode, 404);
|
||||
assert.equal(missingResponse.json().error.code, 'INVENTORY_NOT_FOUND');
|
||||
});
|
||||
@@ -15,6 +15,7 @@ import {
|
||||
users,
|
||||
} from '../../src/infrastructure/database/schema.js';
|
||||
import { createInventoryImageRepository } from '../../src/modules/inventory/repositories/inventory-image-repository.js';
|
||||
import { createInventoryItemRepository } from '../../src/modules/inventory/repositories/inventory-item-repository.js';
|
||||
import { createListingRepository } from '../../src/modules/listings/repositories/listing-repository.js';
|
||||
|
||||
const connectionString = process.env.TEST_DATABASE_URL;
|
||||
@@ -29,6 +30,7 @@ if (!new URL(connectionString).pathname.endsWith('_test')) {
|
||||
|
||||
const database = createDatabase(connectionString);
|
||||
const listingRepository = createListingRepository(database.db);
|
||||
const inventoryItemRepository = createInventoryItemRepository(database.db);
|
||||
const inventoryImageRepository = createInventoryImageRepository(database.db);
|
||||
|
||||
async function createInventoryFixture() {
|
||||
@@ -168,6 +170,29 @@ test('searches public listings with pagination and hides sold items by default',
|
||||
assert.equal(included.items[0].status, 'CLOSED');
|
||||
});
|
||||
|
||||
test('searches public inventory with product and listing fields', 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: 'AVAILABLE',
|
||||
});
|
||||
|
||||
const visible = await inventoryItemRepository.searchPublic({ page: 1, limit: 10 });
|
||||
|
||||
assert.equal(visible.total, 1);
|
||||
assert.equal(visible.items[0].productSlug, product.slug);
|
||||
assert.equal(visible.items[0].askingPriceMinor, 2499);
|
||||
assert.equal((await inventoryItemRepository.findPublicById(inventoryItem.id)).id, inventoryItem.id);
|
||||
|
||||
await database.db.update(inventoryItems).set({ status: 'SOLD' }).where(sql`id = ${inventoryItem.id}`);
|
||||
assert.equal(await inventoryItemRepository.findPublicById(inventoryItem.id), null);
|
||||
});
|
||||
|
||||
test('persists image metadata only for the owning seller and counts item images', async () => {
|
||||
const { seller, inventoryItem } = await createInventoryFixture();
|
||||
const image = await inventoryImageRepository.createForSeller({
|
||||
|
||||
Reference in New Issue
Block a user