feat: add Product tags and bulk tag updates
Quality / test (push) Canceled after 0s

This commit is contained in:
2026-09-10 00:51:05 -04:00
parent 7c9518923d
commit d6b4dbec5f
22 changed files with 236 additions and 4 deletions
@@ -218,6 +218,7 @@ export const products = pgTable(
cardNumber: text("card_number"),
eventName: text("event_name"),
releaseYear: integer("release_year"),
tags: text("tags").array().default([]).notNull(),
...timestamps,
},
(table) => [
@@ -57,6 +57,24 @@ export function createInventoryItemRepository(db) {
.returning();
},
async updateProductTagsByIdsForSeller(inventoryItemIds, sellerId, addTags, removeTags) {
const rows = await db
.select({ productId: inventoryItems.productId })
.from(inventoryItems)
.innerJoin(products, eq(products.id, inventoryItems.productId))
.where(and(inArray(inventoryItems.id, inventoryItemIds), eq(products.sellerId, sellerId)));
const productIds = [...new Set(rows.map(({ productId }) => productId))];
const updated = [];
for (const productId of productIds) {
const [product] = await db.select().from(products).where(eq(products.id, productId));
const tags = [...new Set([...(product.tags ?? []), ...addTags.map((tag) => tag.toLowerCase())])]
.filter((tag) => !removeTags.map((value) => value.toLowerCase()).includes(tag));
const [result] = await db.update(products).set({ tags, updatedAt: new Date() }).where(eq(products.id, productId)).returning();
updated.push(result);
}
return updated;
},
async searchPublic({ page = 1, limit = 20 } = {}) {
const conditions = [
eq(inventoryItems.status, 'ACTIVE'),
@@ -78,16 +78,25 @@ export async function registerAdminInventoryRoutes(app, options) {
inventoryItemIds: result.data.inventoryItemIds,
sellerId: request.admin.sellerId,
})
: await inventoryItemService.refreshPricingMany({
: result.data.action === 'PRICING_REFRESH'
? await inventoryItemService.refreshPricingMany({
inventoryItemIds: result.data.inventoryItemIds,
providerId: result.data.providerId,
sellerId: request.admin.sellerId,
});
})
: await inventoryItemService.updateTagsMany({
inventoryItemIds: result.data.inventoryItemIds,
sellerId: request.admin.sellerId,
addTags: result.data.add ?? [],
removeTags: result.data.remove ?? [],
});
if (result.data.action === 'ARCHIVE') {
return reply.send({ data: inventoryItems.map(toAdminInventoryItemDto) });
}
if (result.data.action === 'TAGS') return reply.send({ data: inventoryItems });
return reply.code(202).send({
data: inventoryItems.map(({ id, status, createdAt }) => ({ id, status, createdAt })),
});
@@ -68,5 +68,13 @@ export function createInventoryItemService({
conditionId: item.conditionId,
})));
},
async updateTagsMany({ inventoryItemIds, sellerId, addTags = [], removeTags = [] }) {
const items = await inventoryItemRepository.listBySellerId(sellerId);
if (items.filter(({ id }) => inventoryItemIds.includes(id)).length !== inventoryItemIds.length) {
throw createApplicationError('INVENTORY_NOT_FOUND', 'One or more inventory items were not found.', 404);
}
return inventoryItemRepository.updateProductTagsByIdsForSeller(inventoryItemIds, sellerId, addTags, removeTags);
},
};
}
@@ -125,6 +125,7 @@ export function createListingRepository(db) {
ilike(products.description, `%${term}%`),
ilike(products.setName, `%${term}%`),
ilike(products.brand, `%${term}%`),
sql`${products.tags}::text ILIKE ${`%${term}%`}`,
),
),
),
@@ -6,11 +6,13 @@ function requiredText(value, fieldName) {
return value.trim();
}
export function createProduct({ name, categoryId, sellerId }) {
export function createProduct({ name, categoryId, sellerId, tags = [] }) {
const normalizedTags = [...new Set(tags.map((tag) => tag.trim().toLowerCase()))];
return {
name: requiredText(name, 'Product name'),
categoryId: requiredText(categoryId, 'Category ID'),
sellerId: requiredText(sellerId, 'Seller ID'),
tags: normalizedTags,
};
}
@@ -113,6 +113,34 @@ test('enqueues pricing refresh jobs for owned inventory items', async () => {
assert.deepEqual(jobs, [{ id: 'job-1', status: 'PENDING' }]);
});
test('updates Product tags for selected owned inventory items', async () => {
const updates = [];
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' },
{ id: 'item-2', productId: 'product-1', conditionId: 'condition-1' },
];
},
async updateProductTagsByIdsForSeller(...input) {
updates.push(input);
return [{ id: 'product-1', tags: ['binder', 'keep'] }];
},
},
});
assert.deepEqual(await service.updateTagsMany({
inventoryItemIds: ['item-1', 'item-2'],
sellerId: 'seller-id',
addTags: ['Binder'],
removeTags: ['sell'],
}), [{ id: 'product-1', tags: ['binder', 'keep'] }]);
assert.deepEqual(updates, [[['item-1', 'item-2'], 'seller-id', ['Binder'], ['sell']]]);
});
test('rejects conditions from another category', async () => {
const service = createService({
product: { id: 'product-id', sellerId: 'seller-id', categoryId: 'cards' },
+1
View File
@@ -14,6 +14,7 @@ test('creates a product with required product identity fields', () => {
name: 'Charizard ex',
categoryId: 'category-id',
sellerId: 'seller-id',
tags: [],
});
});
@@ -69,6 +69,9 @@ function createTestApp({ authenticateAdmin, authorizeUnsafeRequest, pricingServi
return item;
});
},
async updateProductTagsByIdsForSeller() {
return [];
},
},
pricingService,
}),
@@ -248,6 +251,44 @@ test('enqueues selected inventory pricing refreshes through the bulk route', asy
assert.equal(response.json().data[0].status, 'PENDING');
});
test('updates Product tags 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',
tags: ['Keep'],
},
});
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: 'TAGS',
inventoryItemIds: ['8a0a836a-a597-454d-8c5e-e146d27dbbf2'],
add: ['Binder'],
remove: ['sell'],
},
});
assert.equal(response.statusCode, 200);
assert.deepEqual(response.json().data, []);
});
test('rejects an authenticated product write without a CSRF token', async (testContext) => {
const app = createTestApp({
authenticateAdmin: async () => ({ sellerId: 'b078adf4-6a91-4b3d-a263-740246c11e3f' }),
@@ -50,6 +50,7 @@ test('returns a public product without private seller identity', async (testCont
cardNumber: null,
eventName: null,
releaseYear: null,
tags: [],
createdAt: '2026-09-07T00:00:00.000Z',
updatedAt: '2026-09-07T00:00:00.000Z',
});