@shopbite-de/storefront 2.6.0 → 2.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -54,30 +54,50 @@ export function useAddToCart() {
54
54
  quantity: selectedQuantity.value,
55
55
  }));
56
56
 
57
- const generateSortedExtrasString = (extras: AssociationItemProduct[]) =>
58
- extras
57
+ /** The removed ingredients as the plugin takes them (`payload.without`). */
58
+ const removedIngredients = () => [
59
+ ...new Set(
60
+ deselectedIngredients.value
61
+ .map((name) => name.trim())
62
+ .filter((name) => name !== ""),
63
+ ),
64
+ ];
65
+
66
+ /**
67
+ * Seed of the container line item id. Shopware merges a line item into an
68
+ * existing one with the same id (quantity added, label and children kept),
69
+ * so the whole configuration belongs in it: the extras and the removed
70
+ * ingredients. Without removals the seed stays the product id plus the
71
+ * sorted extra ids, so lines already in a cart keep their id.
72
+ */
73
+ const generateLineItemSeed = (
74
+ baseId: string,
75
+ extras: AssociationItemProduct[],
76
+ without: string[],
77
+ ) => {
78
+ const extrasSeed = extras
59
79
  .map((extra) => extra.value)
60
80
  .sort()
61
81
  .join("");
82
+ const withoutSeed = [
83
+ ...new Set(without.map((name) => name.toLocaleLowerCase("de"))),
84
+ ]
85
+ .sort()
86
+ .join("|");
62
87
 
63
- const generateProductId = (
64
- baseId: string,
65
- extras: AssociationItemProduct[],
66
- ) => (extras.length ? baseId + generateSortedExtrasString(extras) : baseId);
88
+ return withoutSeed
89
+ ? `${baseId}${extrasSeed}|without:${withoutSeed}`
90
+ : `${baseId}${extrasSeed}`;
91
+ };
67
92
 
68
93
  function createCartItems(): operations["addLineItem post /checkout/cart/line-item"]["body"]["items"] {
69
94
  if (!selectedProduct.value) return [];
70
95
 
71
96
  const extras = createExtras();
97
+ const without = removedIngredients();
72
98
 
73
- // Container product when extras are selected
74
- const generatedUuid = uuidv5(
75
- generateProductId(selectedProduct.value.id, selectedExtras.value),
76
- UUID_NAMESPACE,
77
- );
78
-
79
- // Simple product when no extras
80
- if (extras.length === 0 && deselectedIngredients.value.length === 0) {
99
+ // Simple product when no extras and nothing removed
100
+ if (extras.length === 0 && without.length === 0) {
81
101
  return [
82
102
  {
83
103
  id: selectedProduct.value.id,
@@ -87,9 +107,17 @@ export function useAddToCart() {
87
107
  ];
88
108
  }
89
109
 
110
+ // Container product when extras are selected or ingredients removed
90
111
  return [
91
112
  {
92
- id: generatedUuid,
113
+ id: uuidv5(
114
+ generateLineItemSeed(
115
+ selectedProduct.value.id,
116
+ selectedExtras.value,
117
+ without,
118
+ ),
119
+ UUID_NAMESPACE,
120
+ ),
93
121
  quantity: selectedQuantity.value,
94
122
  type: LINE_ITEM_CONTAINER,
95
123
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
@@ -97,6 +125,7 @@ export function useAddToCart() {
97
125
  label: cartItemLabel.value,
98
126
  payload: {
99
127
  productNumber: selectedProduct.value.productNumber,
128
+ without,
100
129
  },
101
130
  children: [
102
131
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shopbite-de/storefront",
3
- "version": "2.6.0",
3
+ "version": "2.6.1",
4
4
  "main": "nuxt.config.ts",
5
5
  "description": "Shopware storefront for food delivery shops",
6
6
  "keywords": [
@@ -4,6 +4,7 @@ import { mockNuxtImport } from "@nuxt/test-utils/runtime";
4
4
  import { useAddToCart } from "~/composables/useAddToCart";
5
5
  import type { Schemas } from "#shopware";
6
6
  import { nextTick } from "vue";
7
+ import { v5 as uuidv5 } from "uuid";
7
8
 
8
9
  // Use vi.hoisted for variables used in mocks
9
10
  const {
@@ -184,4 +185,88 @@ describe("useAddToCart", () => {
184
185
  }),
185
186
  ]);
186
187
  });
188
+ describe("line identity of configured dishes", () => {
189
+ const extra = { label: "Extra Cheese", value: "cheese", price: "" };
190
+
191
+ async function addConfigured(
192
+ without: string[],
193
+ extras: (typeof extra)[] = [],
194
+ ) {
195
+ const {
196
+ setSelectedProduct,
197
+ setSelectedExtras,
198
+ setDeselectedIngredients,
199
+ addToCart,
200
+ } = useAddToCart();
201
+ setSelectedProduct(mockProduct);
202
+ setSelectedExtras(extras);
203
+ setDeselectedIngredients(without);
204
+ mockAddProducts.mockResolvedValue({ id: "cart-123" });
205
+ await addToCart();
206
+ const items = mockAddProducts.mock.calls.at(-1)![0];
207
+ return items[0] as {
208
+ id: string;
209
+ type: string;
210
+ label: string;
211
+ payload: { productNumber: string; without: string[] };
212
+ };
213
+ }
214
+
215
+ it("keeps dishes that differ only in removed ingredients apart", async () => {
216
+ const withoutOnions = await addConfigured(["Zwiebeln"]);
217
+ const withoutOlives = await addConfigured(["Oliven"]);
218
+
219
+ expect(withoutOnions.type).toBe("container");
220
+ expect(withoutOlives.type).toBe("container");
221
+ expect(withoutOnions.id).not.toBe(withoutOlives.id);
222
+ expect(withoutOnions.label).toBe("Test Pizza -Zwiebeln");
223
+ expect(withoutOlives.label).toBe("Test Pizza -Oliven");
224
+ });
225
+
226
+ it("keeps dishes with the same extras but other removals apart", async () => {
227
+ const a = await addConfigured(["Zwiebeln"], [extra]);
228
+ const b = await addConfigured(["Oliven"], [extra]);
229
+ const c = await addConfigured([], [extra]);
230
+
231
+ expect(new Set([a.id, b.id, c.id]).size).toBe(3);
232
+ });
233
+
234
+ it("gives the same configuration the same id, so the cart adds up the quantity", async () => {
235
+ const first = await addConfigured(["Zwiebeln", "Oliven"], [extra]);
236
+ const second = await addConfigured(["Zwiebeln", "Oliven"], [extra]);
237
+
238
+ expect(first.id).toBe(second.id);
239
+ });
240
+
241
+ it("does not depend on the order or case of the removed ingredients", async () => {
242
+ const a = await addConfigured(["Zwiebeln", "Oliven"]);
243
+ const b = await addConfigured(["Oliven", "Zwiebeln"]);
244
+ const c = await addConfigured([" oliven", "ZWIEBELN "]);
245
+
246
+ expect(b.id).toBe(a.id);
247
+ expect(c.id).toBe(a.id);
248
+ });
249
+
250
+ it("keeps the id of dishes without removed ingredients", async () => {
251
+ const item = await addConfigured([], [extra]);
252
+
253
+ expect(item.id).toBe(
254
+ uuidv5("product-123cheese", "b098ef7e-0fa2-4073-b002-7ceec4360fbf"),
255
+ );
256
+ });
257
+
258
+ it("sends the removed ingredients as payload.without", async () => {
259
+ const removed = await addConfigured(["Zwiebeln", "Oliven"]);
260
+ const extrasOnly = await addConfigured([], [extra]);
261
+
262
+ expect(removed.payload).toEqual({
263
+ productNumber: "SW10123",
264
+ without: ["Zwiebeln", "Oliven"],
265
+ });
266
+ expect(extrasOnly.payload).toEqual({
267
+ productNumber: "SW10123",
268
+ without: [],
269
+ });
270
+ });
271
+ });
187
272
  });