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);
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user