@@ -1,6 +1,6 @@
|
|||||||
import { toAdminInventoryItemDto } from '@pokemon-inventory/contracts/inventory';
|
import { toAdminInventoryItemDto } from '@pokemon-inventory/contracts/inventory';
|
||||||
import {
|
import {
|
||||||
bulkArchiveInventorySchema,
|
bulkInventorySchema,
|
||||||
createInventoryItemSchema,
|
createInventoryItemSchema,
|
||||||
} from '@pokemon-inventory/validation/inventory';
|
} from '@pokemon-inventory/validation/inventory';
|
||||||
|
|
||||||
@@ -67,17 +67,29 @@ export async function registerAdminInventoryRoutes(app, options) {
|
|||||||
request.cookies[sessionCookieName],
|
request.cookies[sessionCookieName],
|
||||||
request.headers['x-csrf-token'],
|
request.headers['x-csrf-token'],
|
||||||
);
|
);
|
||||||
const result = bulkArchiveInventorySchema.safeParse(request.body);
|
const result = bulkInventorySchema.safeParse(request.body);
|
||||||
|
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
return validationError(reply, result);
|
return validationError(reply, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
const inventoryItems = await inventoryItemService.archiveMany({
|
const inventoryItems = result.data.action === 'ARCHIVE'
|
||||||
inventoryItemIds: result.data.inventoryItemIds,
|
? await inventoryItemService.archiveMany({
|
||||||
sellerId: request.admin.sellerId,
|
inventoryItemIds: result.data.inventoryItemIds,
|
||||||
});
|
sellerId: request.admin.sellerId,
|
||||||
|
})
|
||||||
|
: await inventoryItemService.refreshPricingMany({
|
||||||
|
inventoryItemIds: result.data.inventoryItemIds,
|
||||||
|
providerId: result.data.providerId,
|
||||||
|
sellerId: request.admin.sellerId,
|
||||||
|
});
|
||||||
|
|
||||||
return reply.send({ data: inventoryItems.map(toAdminInventoryItemDto) });
|
if (result.data.action === 'ARCHIVE') {
|
||||||
|
return reply.send({ data: inventoryItems.map(toAdminInventoryItemDto) });
|
||||||
|
}
|
||||||
|
|
||||||
|
return reply.code(202).send({
|
||||||
|
data: inventoryItems.map(({ id, status, createdAt }) => ({ id, status, createdAt })),
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -5,6 +5,7 @@ export function createInventoryItemService({
|
|||||||
conditionRepository,
|
conditionRepository,
|
||||||
inventoryItemRepository,
|
inventoryItemRepository,
|
||||||
productRepository,
|
productRepository,
|
||||||
|
pricingService,
|
||||||
}) {
|
}) {
|
||||||
return {
|
return {
|
||||||
async listBySellerId(sellerId) {
|
async listBySellerId(sellerId) {
|
||||||
@@ -47,5 +48,25 @@ export function createInventoryItemService({
|
|||||||
requestedItems.forEach(({ status }) => archiveInventoryItem(status));
|
requestedItems.forEach(({ status }) => archiveInventoryItem(status));
|
||||||
return inventoryItemRepository.archiveByIdsForSeller(inventoryItemIds, sellerId);
|
return inventoryItemRepository.archiveByIdsForSeller(inventoryItemIds, sellerId);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
async refreshPricingMany({ inventoryItemIds, sellerId, providerId }) {
|
||||||
|
if (!pricingService) {
|
||||||
|
throw createApplicationError('PRICING_QUEUE_UNAVAILABLE', 'Pricing refresh queue is unavailable.', 503);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.all(requestedItems.map((item) => pricingService.enqueueRefresh({
|
||||||
|
sellerId,
|
||||||
|
inventoryItemId: item.id,
|
||||||
|
providerId,
|
||||||
|
productId: item.productId,
|
||||||
|
conditionId: item.conditionId,
|
||||||
|
})));
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -79,6 +79,40 @@ test('does not archive sold inventory items', async () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('enqueues pricing refresh jobs for owned inventory items', async () => {
|
||||||
|
const enqueued = [];
|
||||||
|
const service = createInventoryItemService({
|
||||||
|
productRepository: { async findById() { return null; } },
|
||||||
|
conditionRepository: { async findById() { return null; } },
|
||||||
|
inventoryItemRepository: {
|
||||||
|
async listBySellerId() {
|
||||||
|
return [{ id: 'item-1', productId: 'product-1', conditionId: 'condition-1', status: 'ACTIVE' }];
|
||||||
|
},
|
||||||
|
},
|
||||||
|
pricingService: {
|
||||||
|
async enqueueRefresh(input) {
|
||||||
|
enqueued.push(input);
|
||||||
|
return { id: 'job-1', status: 'PENDING' };
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const jobs = await service.refreshPricingMany({
|
||||||
|
inventoryItemIds: ['item-1'],
|
||||||
|
sellerId: 'seller-id',
|
||||||
|
providerId: 'fixture-provider',
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.deepEqual(enqueued, [{
|
||||||
|
sellerId: 'seller-id',
|
||||||
|
inventoryItemId: 'item-1',
|
||||||
|
providerId: 'fixture-provider',
|
||||||
|
productId: 'product-1',
|
||||||
|
conditionId: 'condition-1',
|
||||||
|
}]);
|
||||||
|
assert.deepEqual(jobs, [{ id: 'job-1', status: 'PENDING' }]);
|
||||||
|
});
|
||||||
|
|
||||||
test('rejects conditions from another category', async () => {
|
test('rejects conditions from another category', async () => {
|
||||||
const service = createService({
|
const service = createService({
|
||||||
product: { id: 'product-id', sellerId: 'seller-id', categoryId: 'cards' },
|
product: { id: 'product-id', sellerId: 'seller-id', categoryId: 'cards' },
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import { buildApp } from '../../src/app/build-app.js';
|
|||||||
import { createInventoryItemService } from '../../src/modules/inventory/services/inventory-item-service.js';
|
import { createInventoryItemService } from '../../src/modules/inventory/services/inventory-item-service.js';
|
||||||
import { createProductService } from '../../src/modules/products/services/product-service.js';
|
import { createProductService } from '../../src/modules/products/services/product-service.js';
|
||||||
|
|
||||||
function createTestApp({ authenticateAdmin, authorizeUnsafeRequest } = {}) {
|
function createTestApp({ authenticateAdmin, authorizeUnsafeRequest, pricingService } = {}) {
|
||||||
const products = [];
|
const products = [];
|
||||||
const inventoryItems = [];
|
const inventoryItems = [];
|
||||||
const productRepository = {
|
const productRepository = {
|
||||||
@@ -70,6 +70,7 @@ function createTestApp({ authenticateAdmin, authorizeUnsafeRequest } = {}) {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
pricingService,
|
||||||
}),
|
}),
|
||||||
productService: createProductService({ productRepository }),
|
productService: createProductService({ productRepository }),
|
||||||
});
|
});
|
||||||
@@ -193,6 +194,60 @@ test('archives selected inventory through the authenticated bulk route', async (
|
|||||||
assert.equal(response.json().data[0].status, 'ARCHIVED');
|
assert.equal(response.json().data[0].status, 'ARCHIVED');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('enqueues selected inventory pricing refreshes through the bulk route', async (testContext) => {
|
||||||
|
const enqueued = [];
|
||||||
|
const app = createTestApp({
|
||||||
|
authenticateAdmin: async () => ({ sellerId: 'b078adf4-6a91-4b3d-a263-740246c11e3f' }),
|
||||||
|
pricingService: {
|
||||||
|
async enqueueRefresh(input) {
|
||||||
|
enqueued.push(input);
|
||||||
|
return {
|
||||||
|
id: 'job-1',
|
||||||
|
status: 'PENDING',
|
||||||
|
createdAt: new Date('2026-09-10T00:00:00.000Z'),
|
||||||
|
};
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
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: 'PRICING_REFRESH',
|
||||||
|
inventoryItemIds: ['8a0a836a-a597-454d-8c5e-e146d27dbbf2'],
|
||||||
|
providerId: 'fixture-provider',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.equal(response.statusCode, 202);
|
||||||
|
assert.deepEqual(enqueued, [{
|
||||||
|
sellerId: 'b078adf4-6a91-4b3d-a263-740246c11e3f',
|
||||||
|
inventoryItemId: '8a0a836a-a597-454d-8c5e-e146d27dbbf2',
|
||||||
|
providerId: 'fixture-provider',
|
||||||
|
productId: productResponse.json().data.id,
|
||||||
|
conditionId: '21eb218b-7a95-4f80-a9e3-e306f7ca1f2e',
|
||||||
|
}]);
|
||||||
|
assert.equal(response.json().data[0].status, 'PENDING');
|
||||||
|
});
|
||||||
|
|
||||||
test('rejects an authenticated product write without a CSRF token', async (testContext) => {
|
test('rejects an authenticated product write without a CSRF token', async (testContext) => {
|
||||||
const app = createTestApp({
|
const app = createTestApp({
|
||||||
authenticateAdmin: async () => ({ sellerId: 'b078adf4-6a91-4b3d-a263-740246c11e3f' }),
|
authenticateAdmin: async () => ({ sellerId: 'b078adf4-6a91-4b3d-a263-740246c11e3f' }),
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ export const BulkArchiveSelection = {
|
|||||||
docs: {
|
docs: {
|
||||||
description: {
|
description: {
|
||||||
story:
|
story:
|
||||||
"The physical inventory section supports selecting active copies and archiving them in one authenticated operation.",
|
"The physical inventory section supports selecting active copies for archive or asynchronous pricing refresh operations.",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import {
|
|||||||
listAdminConditions,
|
listAdminConditions,
|
||||||
listAdminInventory,
|
listAdminInventory,
|
||||||
listAdminProducts,
|
listAdminProducts,
|
||||||
|
refreshAdminInventoryPricing,
|
||||||
} from "@/services/admin-catalog-service.js";
|
} from "@/services/admin-catalog-service.js";
|
||||||
import {
|
import {
|
||||||
createAdminListing,
|
createAdminListing,
|
||||||
@@ -213,6 +214,24 @@ async function archiveSelectedInventory() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function refreshSelectedPricing() {
|
||||||
|
if (!selectedInventoryIds.value.length) return;
|
||||||
|
isSaving.value = true;
|
||||||
|
try {
|
||||||
|
const jobs = await refreshAdminInventoryPricing(
|
||||||
|
selectedInventoryIds.value,
|
||||||
|
pricingProvider.value,
|
||||||
|
csrfToken(),
|
||||||
|
);
|
||||||
|
pricingJobs.value = [...jobs, ...pricingJobs.value];
|
||||||
|
selectedInventoryIds.value = [];
|
||||||
|
} catch (requestError) {
|
||||||
|
error.value = requestError;
|
||||||
|
} finally {
|
||||||
|
isSaving.value = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function createListing() {
|
async function createListing() {
|
||||||
const item = inventory.value.find(
|
const item = inventory.value.find(
|
||||||
(candidate) => candidate.id === listingInventoryId.value,
|
(candidate) => candidate.id === listingInventoryId.value,
|
||||||
@@ -956,6 +975,14 @@ onMounted(load);
|
|||||||
>
|
>
|
||||||
Archive selected
|
Archive selected
|
||||||
</button>
|
</button>
|
||||||
|
<button
|
||||||
|
class="button button--secondary"
|
||||||
|
type="button"
|
||||||
|
:disabled="isSaving || !selectedInventoryIds.length"
|
||||||
|
@click="refreshSelectedPricing"
|
||||||
|
>
|
||||||
|
Refresh pricing
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<p v-if="!inventory.length && !isLoading" class="state state--empty">
|
<p v-if="!inventory.length && !isLoading" class="state state--empty">
|
||||||
No physical copies yet.
|
No physical copies yet.
|
||||||
|
|||||||
@@ -30,4 +30,12 @@ export function archiveAdminInventory(inventoryItemIds, csrfToken) {
|
|||||||
headers: { 'x-csrf-token': csrfToken },
|
headers: { 'x-csrf-token': csrfToken },
|
||||||
body: JSON.stringify({ action: 'ARCHIVE', inventoryItemIds }),
|
body: JSON.stringify({ action: 'ARCHIVE', inventoryItemIds }),
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function refreshAdminInventoryPricing(inventoryItemIds, providerId, csrfToken) {
|
||||||
|
return request('/admin/bulk', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'x-csrf-token': csrfToken },
|
||||||
|
body: JSON.stringify({ action: 'PRICING_REFRESH', inventoryItemIds, providerId }),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
@@ -68,4 +68,28 @@ test('archives selected admin inventory items through the bulk API', async () =>
|
|||||||
} finally {
|
} finally {
|
||||||
globalThis.fetch = originalFetch;
|
globalThis.fetch = originalFetch;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('enqueues selected admin inventory pricing refreshes through the bulk API', async () => {
|
||||||
|
const originalFetch = globalThis.fetch;
|
||||||
|
globalThis.fetch = async (url, options) => {
|
||||||
|
assert.equal(url, '/api/v1/admin/bulk');
|
||||||
|
assert.equal(options.method, 'POST');
|
||||||
|
assert.equal(options.headers['x-csrf-token'], 'csrf-token');
|
||||||
|
assert.deepEqual(JSON.parse(options.body), {
|
||||||
|
action: 'PRICING_REFRESH',
|
||||||
|
inventoryItemIds: ['item-1'],
|
||||||
|
providerId: 'fixture-provider',
|
||||||
|
});
|
||||||
|
return { ok: true, async json() { return { data: [{ id: 'job-1', status: 'PENDING' }] }; } };
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { refreshAdminInventoryPricing } = await import('../src/services/admin-catalog-service.js');
|
||||||
|
assert.deepEqual(await refreshAdminInventoryPricing(['item-1'], 'fixture-provider', 'csrf-token'), [
|
||||||
|
{ id: 'job-1', status: 'PENDING' },
|
||||||
|
]);
|
||||||
|
} finally {
|
||||||
|
globalThis.fetch = originalFetch;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
@@ -148,7 +148,7 @@ Remaining roadmap areas are bulk status/tag/pricing actions, richer exports/sett
|
|||||||
- Pricing refresh enqueue now resolves its provider from an active Product mapping and rejects unmapped Products explicitly.
|
- Pricing refresh enqueue now resolves its provider from an active Product mapping and rejects unmapped Products explicitly.
|
||||||
- Local development and CI can enable a deterministic fixture provider for end-to-end refresh coverage; production provider adapters remain explicitly configured deployment boundaries.
|
- Local development and CI can enable a deterministic fixture provider for end-to-end refresh coverage; production provider adapters remain explicitly configured deployment boundaries.
|
||||||
- Pricing refresh jobs have a bounded three-attempt retry policy with exponential backoff. The worker schedules completed-job cleanup using configurable retention days and cleanup interval settings.
|
- Pricing refresh jobs have a bounded three-attempt retry policy with exponential backoff. The worker schedules completed-job cleanup using configurable retention days and cleanup interval settings.
|
||||||
- Bulk archive is implemented through the seller-scoped API boundary; bulk status changes, tags, and bulk pricing refresh remain.
|
- Bulk archive and asynchronous bulk pricing refresh are implemented through the seller-scoped API boundary; bulk status changes and tags remain.
|
||||||
- PostgreSQL restore verification and image snapshot restore scripts are implemented and covered by shell tests as part of the deployment drill workflow.
|
- PostgreSQL restore verification and image snapshot restore scripts are implemented and covered by shell tests as part of the deployment drill workflow.
|
||||||
- `npm audit --omit=dev` currently reports zero production vulnerabilities. Re-run the audit after dependency changes and review development-dependency findings separately before production releases.
|
- `npm audit --omit=dev` currently reports zero production vulnerabilities. Re-run the audit after dependency changes and review development-dependency findings separately before production releases.
|
||||||
|
|
||||||
|
|||||||
@@ -13,4 +13,17 @@ export const bulkArchiveInventorySchema = z
|
|||||||
action: z.literal('ARCHIVE'),
|
action: z.literal('ARCHIVE'),
|
||||||
inventoryItemIds: z.array(z.uuid()).min(1).max(100),
|
inventoryItemIds: z.array(z.uuid()).min(1).max(100),
|
||||||
})
|
})
|
||||||
.strict();
|
.strict();
|
||||||
|
|
||||||
|
export const bulkPricingRefreshInventorySchema = z
|
||||||
|
.object({
|
||||||
|
action: z.literal('PRICING_REFRESH'),
|
||||||
|
inventoryItemIds: z.array(z.uuid()).min(1).max(100),
|
||||||
|
providerId: z.string().trim().min(1).max(100).optional(),
|
||||||
|
})
|
||||||
|
.strict();
|
||||||
|
|
||||||
|
export const bulkInventorySchema = z.discriminatedUnion('action', [
|
||||||
|
bulkArchiveInventorySchema,
|
||||||
|
bulkPricingRefreshInventorySchema,
|
||||||
|
]);
|
||||||
Reference in New Issue
Block a user