feat: add public inventory catalog boundary
This commit is contained in:
@@ -5,6 +5,7 @@ import multipart from '@fastify/multipart';
|
||||
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 { registerPublicInventoryRoutes } from '../modules/inventory/routes/public-inventory-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';
|
||||
@@ -65,6 +66,10 @@ export function buildApp(dependencies) {
|
||||
...dependencies,
|
||||
prefix: '/api/v1',
|
||||
});
|
||||
app.register(registerPublicInventoryRoutes, {
|
||||
...dependencies,
|
||||
prefix: '/api/v1',
|
||||
});
|
||||
|
||||
return app;
|
||||
}
|
||||
@@ -16,6 +16,7 @@ import { createRedisLoginRateLimiter } from '../modules/auth/services/login-rate
|
||||
import { createPasswordRecoveryService } from '../modules/auth/services/password-recovery-service.js';
|
||||
import { createInventoryItemRepository } from '../modules/inventory/repositories/inventory-item-repository.js';
|
||||
import { createInventoryItemService } from '../modules/inventory/services/inventory-item-service.js';
|
||||
import { createPublicInventoryService } from '../modules/inventory/services/public-inventory-service.js';
|
||||
import { createInventoryImageRepository } from '../modules/inventory/repositories/inventory-image-repository.js';
|
||||
import { createInventoryImageService } from '../modules/inventory/services/inventory-image-service.js';
|
||||
import { createLocalImageStorage } from '../infrastructure/images/local-image-storage.js';
|
||||
@@ -82,6 +83,7 @@ async function startServer() {
|
||||
inventoryItemRepository,
|
||||
productRepository,
|
||||
}),
|
||||
publicInventoryService: createPublicInventoryService({ inventoryItemRepository }),
|
||||
inventoryImageService: createInventoryImageService({
|
||||
imageStorage: createLocalImageStorage(process.env.IMAGE_STORAGE_ROOT ?? '.local/image-storage'),
|
||||
inventoryImageRepository,
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
import { asc, eq } from 'drizzle-orm';
|
||||
import { and, asc, count, desc, eq, inArray } from 'drizzle-orm';
|
||||
|
||||
import { inventoryItems } from '../../../infrastructure/database/schema.js';
|
||||
import {
|
||||
inventoryItems,
|
||||
listings,
|
||||
products,
|
||||
} from '../../../infrastructure/database/schema.js';
|
||||
|
||||
export function createInventoryItemRepository(db) {
|
||||
return {
|
||||
@@ -20,5 +24,75 @@ export function createInventoryItemRepository(db) {
|
||||
.where(eq(inventoryItems.productId, productId))
|
||||
.orderBy(asc(inventoryItems.createdAt));
|
||||
},
|
||||
|
||||
async searchPublic({ page = 1, limit = 20 } = {}) {
|
||||
const conditions = [
|
||||
eq(inventoryItems.status, 'ACTIVE'),
|
||||
inArray(listings.status, ['AVAILABLE', 'PENDING']),
|
||||
];
|
||||
const offset = (page - 1) * limit;
|
||||
|
||||
const [countResult, results] = await Promise.all([
|
||||
db
|
||||
.select({ total: count() })
|
||||
.from(inventoryItems)
|
||||
.innerJoin(products, eq(products.id, inventoryItems.productId))
|
||||
.innerJoin(listings, eq(listings.inventoryItemId, inventoryItems.id))
|
||||
.where(and(...conditions)),
|
||||
db
|
||||
.select({
|
||||
id: inventoryItems.id,
|
||||
productId: products.id,
|
||||
productName: products.name,
|
||||
productSlug: products.slug,
|
||||
categoryId: products.categoryId,
|
||||
conditionId: inventoryItems.conditionId,
|
||||
askingPriceMinor: listings.askingPriceMinor,
|
||||
listingStatus: listings.status,
|
||||
listingNotes: listings.notes,
|
||||
createdAt: inventoryItems.createdAt,
|
||||
updatedAt: inventoryItems.updatedAt,
|
||||
})
|
||||
.from(inventoryItems)
|
||||
.innerJoin(products, eq(products.id, inventoryItems.productId))
|
||||
.innerJoin(listings, eq(listings.inventoryItemId, inventoryItems.id))
|
||||
.where(and(...conditions))
|
||||
.orderBy(desc(inventoryItems.createdAt))
|
||||
.limit(limit)
|
||||
.offset(offset),
|
||||
]);
|
||||
|
||||
return {
|
||||
items: results,
|
||||
total: Number(countResult[0]?.total ?? 0),
|
||||
};
|
||||
},
|
||||
|
||||
async findPublicById(inventoryItemId) {
|
||||
const [item] = await db
|
||||
.select({
|
||||
id: inventoryItems.id,
|
||||
productId: products.id,
|
||||
productName: products.name,
|
||||
productSlug: products.slug,
|
||||
categoryId: products.categoryId,
|
||||
conditionId: inventoryItems.conditionId,
|
||||
askingPriceMinor: listings.askingPriceMinor,
|
||||
listingStatus: listings.status,
|
||||
listingNotes: listings.notes,
|
||||
createdAt: inventoryItems.createdAt,
|
||||
updatedAt: inventoryItems.updatedAt,
|
||||
})
|
||||
.from(inventoryItems)
|
||||
.innerJoin(products, eq(products.id, inventoryItems.productId))
|
||||
.innerJoin(listings, eq(listings.inventoryItemId, inventoryItems.id))
|
||||
.where(and(
|
||||
eq(inventoryItems.id, inventoryItemId),
|
||||
eq(inventoryItems.status, 'ACTIVE'),
|
||||
inArray(listings.status, ['AVAILABLE', 'PENDING']),
|
||||
));
|
||||
|
||||
return item ?? null;
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { toPublicInventoryItemDto } from '@pokemon-inventory/contracts/public-inventory';
|
||||
|
||||
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 registerPublicInventoryRoutes(app, options) {
|
||||
const { publicInventoryService } = options;
|
||||
|
||||
app.get('/public/inventory', async (request) => {
|
||||
const result = await publicInventoryService.list({
|
||||
page: coerceNumber(request.query.page, 1),
|
||||
limit: coerceNumber(request.query.limit, 20),
|
||||
});
|
||||
|
||||
return {
|
||||
data: {
|
||||
...result,
|
||||
items: result.items.map(toPublicInventoryItemDto),
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
app.get('/public/inventory/:id', async (request) => ({
|
||||
data: toPublicInventoryItemDto(
|
||||
await publicInventoryService.getById(request.params.id),
|
||||
),
|
||||
}));
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import { createApplicationError } from '../../../shared/errors/application-error.js';
|
||||
|
||||
export function createPublicInventoryService({ inventoryItemRepository }) {
|
||||
return {
|
||||
async list(input = {}) {
|
||||
return inventoryItemRepository.searchPublic({
|
||||
page: input.page,
|
||||
limit: input.limit,
|
||||
});
|
||||
},
|
||||
|
||||
async getById(inventoryItemId) {
|
||||
const item = await inventoryItemRepository.findPublicById(inventoryItemId);
|
||||
|
||||
if (!item) {
|
||||
throw createApplicationError('INVENTORY_NOT_FOUND', 'Inventory item was not found.', 404);
|
||||
}
|
||||
|
||||
return item;
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user