feat: add public listing catalog boundary
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
"nodemailer": "^7.0.6",
|
||||
"otpauth": "^9.4.1",
|
||||
"pg": "^8.16.3",
|
||||
"redis": "^5.8.2"
|
||||
"redis": "^5.8.2",
|
||||
"sharp": "^0.35.4"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import { registerAuthRoutes } from '../modules/auth/routes/auth-routes.js';
|
||||
import { registerAdminInventoryRoutes } from '../modules/inventory/routes/admin-inventory-routes.js';
|
||||
import { registerAdminImageRoutes } from '../modules/inventory/routes/admin-image-routes.js';
|
||||
import { registerAdminListingRoutes } from '../modules/listings/routes/admin-listing-routes.js';
|
||||
import { registerPublicListingRoutes } from '../modules/listings/routes/public-listing-routes.js';
|
||||
import { registerAdminProductRoutes } from '../modules/products/routes/admin-product-routes.js';
|
||||
import { registerPublicProductRoutes } from '../modules/products/routes/public-product-routes.js';
|
||||
import { registerPublicSearchRoutes } from '../modules/search/routes/public-search-routes.js';
|
||||
@@ -60,6 +61,10 @@ export function buildApp(dependencies) {
|
||||
...dependencies,
|
||||
prefix: '/api/v1',
|
||||
});
|
||||
app.register(registerPublicListingRoutes, {
|
||||
...dependencies,
|
||||
prefix: '/api/v1',
|
||||
});
|
||||
|
||||
return app;
|
||||
}
|
||||
@@ -22,6 +22,7 @@ import { createLocalImageStorage } from '../infrastructure/images/local-image-st
|
||||
import { createConditionRepository } from '../modules/inventory/repositories/condition-repository.js';
|
||||
import { createListingRepository } from '../modules/listings/repositories/listing-repository.js';
|
||||
import { createListingService } from '../modules/listings/services/listing-service.js';
|
||||
import { createPublicListingService } from '../modules/listings/services/public-listing-service.js';
|
||||
import { createProductRepository } from '../modules/products/repositories/product-repository.js';
|
||||
import { createProductService } from '../modules/products/services/product-service.js';
|
||||
import { createPublicProductService } from '../modules/products/services/public-product-service.js';
|
||||
@@ -86,6 +87,7 @@ async function startServer() {
|
||||
inventoryImageRepository,
|
||||
}),
|
||||
listingService: createListingService({ listingRepository, inventoryImageRepository }),
|
||||
publicListingService: createPublicListingService({ listingRepository }),
|
||||
productService: createProductService({ productRepository }),
|
||||
publicProductService: createPublicProductService({ productRepository }),
|
||||
searchService: createSearchService({ listingRepository }),
|
||||
|
||||
@@ -121,6 +121,21 @@ export function createListingRepository(db) {
|
||||
};
|
||||
},
|
||||
|
||||
async findPublicById(listingId) {
|
||||
const [result] = await db
|
||||
.select({ listing: listings })
|
||||
.from(listings)
|
||||
.innerJoin(inventoryItems, eq(inventoryItems.id, listings.inventoryItemId))
|
||||
.innerJoin(products, eq(products.id, inventoryItems.productId))
|
||||
.where(and(
|
||||
eq(listings.id, listingId),
|
||||
inArray(listings.status, ['AVAILABLE', 'PENDING']),
|
||||
eq(inventoryItems.status, 'ACTIVE'),
|
||||
));
|
||||
|
||||
return result?.listing ?? null;
|
||||
},
|
||||
|
||||
async updateStatus(listingId, status) {
|
||||
const [updatedListing] = await db
|
||||
.update(listings)
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import { toPublicListingDto } from '@pokemon-inventory/contracts/public-listing';
|
||||
|
||||
function coerceNumber(value, fallback) {
|
||||
if (value === undefined || value === null || value === '') {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
const parsed = Number.parseInt(String(value), 10);
|
||||
return Number.isNaN(parsed) ? fallback : parsed;
|
||||
}
|
||||
|
||||
export async function registerPublicListingRoutes(app, options) {
|
||||
const { publicListingService } = options;
|
||||
|
||||
app.get('/public/listings', async (request) => {
|
||||
const result = await publicListingService.list({
|
||||
page: coerceNumber(request.query.page, 1),
|
||||
limit: coerceNumber(request.query.limit, 20),
|
||||
});
|
||||
|
||||
return {
|
||||
data: {
|
||||
...result,
|
||||
items: result.items.map(toPublicListingDto),
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
app.get('/public/listings/:id', async (request) => ({
|
||||
data: toPublicListingDto(await publicListingService.getById(request.params.id)),
|
||||
}));
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { createApplicationError } from '../../../shared/errors/application-error.js';
|
||||
|
||||
export function createPublicListingService({ listingRepository }) {
|
||||
return {
|
||||
async list(input = {}) {
|
||||
return listingRepository.searchPublic({
|
||||
query: '',
|
||||
page: input.page,
|
||||
limit: input.limit,
|
||||
});
|
||||
},
|
||||
|
||||
async getById(listingId) {
|
||||
const listing = await listingRepository.findPublicById(listingId);
|
||||
|
||||
if (!listing) {
|
||||
throw createApplicationError('LISTING_NOT_FOUND', 'Listing was not found.', 404);
|
||||
}
|
||||
|
||||
return listing;
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
|
||||
import { createPublicListingService } from '../../src/modules/listings/services/public-listing-service.js';
|
||||
|
||||
test('lists visible public listings through the shared repository search boundary', async () => {
|
||||
const service = createPublicListingService({
|
||||
listingRepository: {
|
||||
async searchPublic(filters) {
|
||||
assert.deepEqual(filters, { query: '', page: 2, limit: 5 });
|
||||
return { items: [{ id: 'listing-1' }], total: 1 };
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
assert.deepEqual(await service.list({ page: 2, limit: 5 }), {
|
||||
items: [{ id: 'listing-1' }],
|
||||
total: 1,
|
||||
});
|
||||
});
|
||||
|
||||
test('returns a not-found application error for a hidden public listing', async () => {
|
||||
const service = createPublicListingService({
|
||||
listingRepository: { async findPublicById() { return null; } },
|
||||
});
|
||||
|
||||
await assert.rejects(
|
||||
() => service.getById('listing-1'),
|
||||
(error) => error.code === 'LISTING_NOT_FOUND' && error.statusCode === 404,
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,77 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
|
||||
import { buildApp } from '../../src/app/build-app.js';
|
||||
|
||||
function createTestApp() {
|
||||
return buildApp({
|
||||
publicListingService: {
|
||||
async list() {
|
||||
return {
|
||||
items: [{
|
||||
id: 'listing-1',
|
||||
inventoryItemId: 'private-inventory-id',
|
||||
productName: 'Pikachu Plush',
|
||||
categoryId: 'category-1',
|
||||
conditionId: 'condition-1',
|
||||
askingPriceMinor: 2499,
|
||||
status: 'PENDING',
|
||||
}],
|
||||
pagination: { page: 1, limit: 20, total: 1, totalPages: 1 },
|
||||
};
|
||||
},
|
||||
async getById(id) {
|
||||
if (id !== 'listing-1') {
|
||||
const error = new Error('Listing was not found.');
|
||||
error.code = 'LISTING_NOT_FOUND';
|
||||
error.statusCode = 404;
|
||||
throw error;
|
||||
}
|
||||
|
||||
return {
|
||||
id,
|
||||
inventoryItemId: 'private-inventory-id',
|
||||
productName: 'Pikachu Plush',
|
||||
categoryId: 'category-1',
|
||||
conditionId: 'condition-1',
|
||||
askingPriceMinor: 2499,
|
||||
status: 'PENDING',
|
||||
};
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
test('returns public listing collections and omits inventory identity', async (testContext) => {
|
||||
const app = createTestApp();
|
||||
testContext.after(() => app.close());
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/public/listings?page=1&limit=20',
|
||||
});
|
||||
|
||||
assert.equal(response.statusCode, 200);
|
||||
assert.equal(response.json().data.items[0].id, 'listing-1');
|
||||
assert.equal('inventoryItemId' in response.json().data.items[0], false);
|
||||
assert.equal(response.json().data.pagination.total, 1);
|
||||
});
|
||||
|
||||
test('returns a public listing detail or a not-found response', async (testContext) => {
|
||||
const app = createTestApp();
|
||||
testContext.after(() => app.close());
|
||||
|
||||
const detailResponse = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/public/listings/listing-1',
|
||||
});
|
||||
const missingResponse = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/public/listings/hidden-listing',
|
||||
});
|
||||
|
||||
assert.equal(detailResponse.statusCode, 200);
|
||||
assert.equal('inventoryItemId' in detailResponse.json().data, false);
|
||||
assert.equal(missingResponse.statusCode, 404);
|
||||
assert.equal(missingResponse.json().error.code, 'LISTING_NOT_FOUND');
|
||||
});
|
||||
@@ -141,6 +141,10 @@ test('searches public listings with pagination and hides sold items by default',
|
||||
assert.equal(visible.total, 1);
|
||||
assert.equal(visible.items.length, 1);
|
||||
assert.equal(visible.items[0].status, 'PENDING');
|
||||
assert.equal(
|
||||
(await listingRepository.findPublicById(visible.items[0].id))?.id,
|
||||
visible.items[0].id,
|
||||
);
|
||||
|
||||
await database.db
|
||||
.update(listings)
|
||||
@@ -153,6 +157,7 @@ test('searches public listings with pagination and hides sold items by default',
|
||||
|
||||
const hidden = await listingRepository.searchPublic({ page: 1, limit: 10 });
|
||||
assert.equal(hidden.total, 0);
|
||||
assert.equal(await listingRepository.findPublicById(visible.items[0].id), null);
|
||||
|
||||
const included = await listingRepository.searchPublic({
|
||||
page: 1,
|
||||
|
||||
Reference in New Issue
Block a user