Add the first bulk inventory operation through a validated, CSRF-protected API route with lifecycle and ownership enforcement, tests, and implementation-status documentation.
This commit is contained in:
@@ -31,6 +31,10 @@ export function transitionInventoryStatus(currentStatus, nextStatus) {
|
||||
return nextStatus;
|
||||
}
|
||||
|
||||
export function archiveInventoryItem(status) {
|
||||
return transitionInventoryStatus(status, INVENTORY_STATUS.ARCHIVED);
|
||||
}
|
||||
|
||||
export function assertInventoryItemEditable(status) {
|
||||
if (status === INVENTORY_STATUS.SOLD || status === INVENTORY_STATUS.ARCHIVED) {
|
||||
throw new Error('Inventory item is locked against ordinary edits.');
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { and, asc, count, desc, eq, inArray } from 'drizzle-orm';
|
||||
import { and, asc, count, desc, eq, exists, inArray, sql } from 'drizzle-orm';
|
||||
|
||||
import {
|
||||
inventoryItems,
|
||||
@@ -35,6 +35,28 @@ export function createInventoryItemRepository(db) {
|
||||
.then((rows) => rows.map(({ item }) => item));
|
||||
},
|
||||
|
||||
async archiveByIdsForSeller(inventoryItemIds, sellerId) {
|
||||
if (inventoryItemIds.length === 0) return [];
|
||||
|
||||
return db
|
||||
.update(inventoryItems)
|
||||
.set({ status: 'ARCHIVED', updatedAt: new Date() })
|
||||
.where(and(
|
||||
inArray(inventoryItems.id, inventoryItemIds),
|
||||
eq(inventoryItems.status, 'ACTIVE'),
|
||||
exists(
|
||||
db
|
||||
.select({ one: sql`1` })
|
||||
.from(products)
|
||||
.where(and(
|
||||
eq(products.id, inventoryItems.productId),
|
||||
eq(products.sellerId, sellerId),
|
||||
)),
|
||||
),
|
||||
))
|
||||
.returning();
|
||||
},
|
||||
|
||||
async searchPublic({ page = 1, limit = 20 } = {}) {
|
||||
const conditions = [
|
||||
eq(inventoryItems.status, 'ACTIVE'),
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import { toAdminInventoryItemDto } from '@pokemon-inventory/contracts/inventory';
|
||||
import { createInventoryItemSchema } from '@pokemon-inventory/validation/inventory';
|
||||
import {
|
||||
bulkArchiveInventorySchema,
|
||||
createInventoryItemSchema,
|
||||
} from '@pokemon-inventory/validation/inventory';
|
||||
|
||||
import { createApplicationError } from '../../../shared/errors/application-error.js';
|
||||
|
||||
@@ -58,4 +61,23 @@ export async function registerAdminInventoryRoutes(app, options) {
|
||||
app.get('/inventory', async (request) => ({
|
||||
data: (await inventoryItemService.listBySellerId(request.admin.sellerId)).map(toAdminInventoryItemDto),
|
||||
}));
|
||||
|
||||
app.post('/bulk', async (request, reply) => {
|
||||
await authorizeUnsafeRequest?.(
|
||||
request.cookies[sessionCookieName],
|
||||
request.headers['x-csrf-token'],
|
||||
);
|
||||
const result = bulkArchiveInventorySchema.safeParse(request.body);
|
||||
|
||||
if (!result.success) {
|
||||
return validationError(reply, result);
|
||||
}
|
||||
|
||||
const inventoryItems = await inventoryItemService.archiveMany({
|
||||
inventoryItemIds: result.data.inventoryItemIds,
|
||||
sellerId: request.admin.sellerId,
|
||||
});
|
||||
|
||||
return reply.send({ data: inventoryItems.map(toAdminInventoryItemDto) });
|
||||
});
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { createInventoryItem } from '../domain/inventory-item.js';
|
||||
import { archiveInventoryItem, createInventoryItem } from '../domain/inventory-item.js';
|
||||
import { createApplicationError } from '../../../shared/errors/application-error.js';
|
||||
|
||||
export function createInventoryItemService({
|
||||
@@ -35,5 +35,17 @@ export function createInventoryItemService({
|
||||
|
||||
return inventoryItemRepository.create(inventoryItem);
|
||||
},
|
||||
|
||||
async archiveMany({ inventoryItemIds, sellerId }) {
|
||||
const items = await inventoryItemRepository.listBySellerId(sellerId);
|
||||
const requestedItems = items.filter(({ id }) => inventoryItemIds.includes(id));
|
||||
|
||||
if (requestedItems.length !== inventoryItemIds.length) {
|
||||
throw createApplicationError('INVENTORY_NOT_FOUND', 'One or more inventory items were not found.', 404);
|
||||
}
|
||||
|
||||
requestedItems.forEach(({ status }) => archiveInventoryItem(status));
|
||||
return inventoryItemRepository.archiveByIdsForSeller(inventoryItemIds, sellerId);
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -7,7 +7,13 @@ function createService({ product, condition }) {
|
||||
return createInventoryItemService({
|
||||
productRepository: { async findById() { return product; } },
|
||||
conditionRepository: { async findById() { return condition; } },
|
||||
inventoryItemRepository: { async create(inventoryItem) { return inventoryItem; } },
|
||||
inventoryItemRepository: {
|
||||
async create(inventoryItem) { return inventoryItem; },
|
||||
async listBySellerId() { return []; },
|
||||
async archiveByIdsForSeller(inventoryItemIds) {
|
||||
return inventoryItemIds.map((id) => ({ id, status: 'ARCHIVED' }));
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -31,6 +37,48 @@ test('creates an inventory item when its condition belongs to the product catego
|
||||
});
|
||||
});
|
||||
|
||||
test('archives owned active inventory items in bulk', async () => {
|
||||
const service = createInventoryItemService({
|
||||
productRepository: { async findById() { return null; } },
|
||||
conditionRepository: { async findById() { return null; } },
|
||||
inventoryItemRepository: {
|
||||
async listBySellerId() {
|
||||
return [
|
||||
{ id: 'item-1', status: 'ACTIVE' },
|
||||
{ id: 'item-2', status: 'ACTIVE' },
|
||||
];
|
||||
},
|
||||
async archiveByIdsForSeller(ids) {
|
||||
return ids.map((id) => ({ id, status: 'ARCHIVED' }));
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
assert.deepEqual(
|
||||
await service.archiveMany({ inventoryItemIds: ['item-1', 'item-2'], sellerId: 'seller-id' }),
|
||||
[
|
||||
{ id: 'item-1', status: 'ARCHIVED' },
|
||||
{ id: 'item-2', status: 'ARCHIVED' },
|
||||
],
|
||||
);
|
||||
});
|
||||
|
||||
test('does not archive sold inventory items', async () => {
|
||||
const service = createInventoryItemService({
|
||||
productRepository: { async findById() { return null; } },
|
||||
conditionRepository: { async findById() { return null; } },
|
||||
inventoryItemRepository: {
|
||||
async listBySellerId() { return [{ id: 'item-1', status: 'SOLD' }]; },
|
||||
async archiveByIdsForSeller() { throw new Error('should not update'); },
|
||||
},
|
||||
});
|
||||
|
||||
await assert.rejects(
|
||||
() => service.archiveMany({ inventoryItemIds: ['item-1'], sellerId: 'seller-id' }),
|
||||
/Inventory status cannot transition from SOLD to ARCHIVED/,
|
||||
);
|
||||
});
|
||||
|
||||
test('rejects conditions from another category', async () => {
|
||||
const service = createService({
|
||||
product: { id: 'product-id', sellerId: 'seller-id', categoryId: 'cards' },
|
||||
|
||||
@@ -7,6 +7,7 @@ import { createProductService } from '../../src/modules/products/services/produc
|
||||
|
||||
function createTestApp({ authenticateAdmin, authorizeUnsafeRequest } = {}) {
|
||||
const products = [];
|
||||
const inventoryItems = [];
|
||||
const productRepository = {
|
||||
async create(product) {
|
||||
const createdProduct = {
|
||||
@@ -49,13 +50,24 @@ function createTestApp({ authenticateAdmin, authorizeUnsafeRequest } = {}) {
|
||||
},
|
||||
inventoryItemRepository: {
|
||||
async create(inventoryItem) {
|
||||
return {
|
||||
id: 'inventory-1',
|
||||
const createdItem = {
|
||||
id: '8a0a836a-a597-454d-8c5e-e146d27dbbf2',
|
||||
...inventoryItem,
|
||||
status: 'ACTIVE',
|
||||
createdAt: new Date('2026-09-07T00:00:00.000Z'),
|
||||
updatedAt: new Date('2026-09-07T00:00:00.000Z'),
|
||||
};
|
||||
inventoryItems.push(createdItem);
|
||||
return createdItem;
|
||||
},
|
||||
async listBySellerId() {
|
||||
return inventoryItems;
|
||||
},
|
||||
async archiveByIdsForSeller(ids) {
|
||||
return inventoryItems.filter(({ id }) => ids.includes(id)).map((item) => {
|
||||
item.status = 'ARCHIVED';
|
||||
return item;
|
||||
});
|
||||
},
|
||||
},
|
||||
}),
|
||||
@@ -146,6 +158,41 @@ test('creates inventory only for the authenticated sellers product', async (test
|
||||
assert.equal(response.json().data.status, 'ACTIVE');
|
||||
});
|
||||
|
||||
test('archives selected inventory through the authenticated bulk route', async (testContext) => {
|
||||
const app = createTestApp({
|
||||
authenticateAdmin: async () => ({ sellerId: 'b078adf4-6a91-4b3d-a263-740246c11e3f' }),
|
||||
});
|
||||
testContext.after(() => app.close());
|
||||
|
||||
const productResponse = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/admin/products',
|
||||
payload: {
|
||||
name: 'Pikachu Plush',
|
||||
categoryId: 'ab8150c7-96c1-4b55-8d69-7d0b77d48958',
|
||||
},
|
||||
});
|
||||
await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/admin/inventory',
|
||||
payload: {
|
||||
productId: productResponse.json().data.id,
|
||||
conditionId: '21eb218b-7a95-4f80-a9e3-e306f7ca1f2e',
|
||||
},
|
||||
});
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/admin/bulk',
|
||||
payload: {
|
||||
action: 'ARCHIVE',
|
||||
inventoryItemIds: ['8a0a836a-a597-454d-8c5e-e146d27dbbf2'],
|
||||
},
|
||||
});
|
||||
|
||||
assert.equal(response.statusCode, 200);
|
||||
assert.equal(response.json().data[0].status, 'ARCHIVED');
|
||||
});
|
||||
|
||||
test('rejects an authenticated product write without a CSRF token', async (testContext) => {
|
||||
const app = createTestApp({
|
||||
authenticateAdmin: async () => ({ sellerId: 'b078adf4-6a91-4b3d-a263-740246c11e3f' }),
|
||||
|
||||
Reference in New Issue
Block a user