Merge pull request #64 from beatz174-bit/codex/add-dropdown-and-checkboxes-for-categories-7h33gk
Deduplicate prefilled pick list products
This commit is contained in:
@@ -133,4 +133,36 @@ describe('StartPickListScreen', () => {
|
||||
expect(item.status).toBe('pending');
|
||||
});
|
||||
});
|
||||
|
||||
it('deduplicates products when selected categories include overlaps', async () => {
|
||||
const user = userEvent.setup();
|
||||
productsToArrayMock.mockResolvedValue([
|
||||
...productsMock,
|
||||
{ ...productsMock[0] },
|
||||
{ ...productsMock[1], id: 'prod-2-duplicate' },
|
||||
]);
|
||||
|
||||
render(
|
||||
<MemoryRouter>
|
||||
<StartPickListScreen />
|
||||
</MemoryRouter>,
|
||||
);
|
||||
|
||||
await user.click(screen.getByLabelText(/area/i));
|
||||
await user.click(screen.getByRole('option', { name: /front counter/i }));
|
||||
|
||||
await user.click(screen.getByRole('checkbox', { name: /drinks/i }));
|
||||
await user.click(screen.getByRole('checkbox', { name: /snacks/i }));
|
||||
|
||||
await user.click(screen.getByRole('button', { name: /save pick list/i }));
|
||||
|
||||
await waitFor(() => expect(pickItemsBulkAddMock).toHaveBeenCalled());
|
||||
|
||||
const pickItems = pickItemsBulkAddMock.mock.calls[0][0];
|
||||
const uniqueProductIds = new Set(pickItems.map((item: any) => item.product_id));
|
||||
|
||||
expect(pickItems).toHaveLength(2);
|
||||
expect(uniqueProductIds.size).toBe(2);
|
||||
expect(uniqueProductIds).toEqual(new Set(['prod-1', 'prod-2']));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -66,12 +66,23 @@ export const StartPickListScreen = () => {
|
||||
(product) => selectedCategoryNames.includes(product.category) && !product.archived,
|
||||
);
|
||||
|
||||
const uniqueProducts: typeof productsInCategories = [];
|
||||
const seenNames = new Set<string>();
|
||||
|
||||
productsInCategories.forEach((product) => {
|
||||
const nameKey = product.name.toLowerCase();
|
||||
if (!seenNames.has(nameKey)) {
|
||||
seenNames.add(nameKey);
|
||||
uniqueProducts.push(product);
|
||||
}
|
||||
});
|
||||
|
||||
if (productsInCategories.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
await db.pickItems.bulkAdd(
|
||||
productsInCategories.map((product) => ({
|
||||
uniqueProducts.map((product) => ({
|
||||
id: uuidv4(),
|
||||
pick_list_id: pickListId,
|
||||
product_id: product.id,
|
||||
|
||||
Reference in New Issue
Block a user