Skip to content

Commit

Permalink
fix(composables): adding promotion code (#1099)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdanilowicz authored Jul 18, 2024
1 parent 8b49869 commit 3bde5fe
Show file tree
Hide file tree
Showing 13 changed files with 216 additions and 92 deletions.
5 changes: 5 additions & 0 deletions .changeset/twenty-drinks-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@shopware-pwa/composables-next": patch
---

`useCart` - Fixed adding promotion code
23 changes: 16 additions & 7 deletions examples/adyen-dropin-component/api-types/storeApiTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7400,13 +7400,22 @@ export type operations = {
"sw-language-id"?: string;
};
body: {
// TODO: [OpenAPI][addLineItem] - add proper request body type with required fields
items: Array<{
id: string; // TODO: [OpenAPI][addLineItem] - check if this is used at all?
referencedId?: string;
quantity?: number;
type: "product" | "promotion" | "custom" | "credit"; // TODO: [OpenAPI][addLineItem] - add proper type -> see also #456
}>;
items: (
| {
id: string;
quantity: number;
referencedId?: string;
/** @enum {string} */
type: "product" | "custom" | "credit" | "discount" | "container";
}
| {
id?: string;
quantity?: number;
referencedId: string;
/** @enum {string} */
type: "promotion";
}
)[];
};
response: components["schemas"]["Cart"];
responseCode: 200;
Expand Down
23 changes: 16 additions & 7 deletions examples/b2b-quote-management/api-types/storeApiTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7292,13 +7292,22 @@ export type operations = {
"sw-language-id"?: string;
};
body: {
// TODO: [OpenAPI][addLineItem] - add proper request body type with required fields
items: Array<{
id: string; // TODO: [OpenAPI][addLineItem] - check if this is used at all?
referencedId?: string;
quantity?: number;
type: "product" | "promotion" | "custom" | "credit"; // TODO: [OpenAPI][addLineItem] - add proper type -> see also #456
}>;
items: (
| {
id: string;
quantity: number;
referencedId?: string;
/** @enum {string} */
type: "product" | "custom" | "credit" | "discount" | "container";
}
| {
id?: string;
quantity?: number;
referencedId: string;
/** @enum {string} */
type: "promotion";
}
)[];
};
response: components["schemas"]["Cart"];
responseCode: 200;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7314,13 +7314,22 @@ export type operations = {
"sw-language-id"?: string;
};
body: {
// TODO: [OpenAPI][addLineItem] - add proper request body type with required fields
items: Array<{
id: string; // TODO: [OpenAPI][addLineItem] - check if this is used at all?
referencedId?: string;
quantity?: number;
type: "product" | "promotion" | "custom" | "credit"; // TODO: [OpenAPI][addLineItem] - add proper type -> see also #456
}>;
items: (
| {
id: string;
quantity: number;
referencedId?: string;
/** @enum {string} */
type: "product" | "custom" | "credit" | "discount" | "container";
}
| {
id?: string;
quantity?: number;
referencedId: string;
/** @enum {string} */
type: "promotion";
}
)[];
};
response: components["schemas"]["Cart"];
responseCode: 200;
Expand Down
23 changes: 16 additions & 7 deletions examples/commercial-quick-order/api-types/storeApiTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7316,13 +7316,22 @@ export type operations = {
"sw-language-id"?: string;
};
body: {
// TODO: [OpenAPI][addLineItem] - add proper request body type with required fields
items: Array<{
id: string; // TODO: [OpenAPI][addLineItem] - check if this is used at all?
referencedId?: string;
quantity?: number;
type: "product" | "promotion" | "custom" | "credit"; // TODO: [OpenAPI][addLineItem] - add proper type -> see also #456
}>;
items: (
| {
id: string;
quantity: number;
referencedId?: string;
/** @enum {string} */
type: "product" | "custom" | "credit" | "discount" | "container";
}
| {
id?: string;
quantity?: number;
referencedId: string;
/** @enum {string} */
type: "promotion";
}
)[];
};
response: components["schemas"]["Cart"];
responseCode: 200;
Expand Down
25 changes: 10 additions & 15 deletions examples/commercial-quick-order/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const successToastMessage = ref("");
// store for received items (from API via search or file-upload action)
const ITEMS_CACHE = ref(new Map());
// store for locally selected items (to be added to the shopping cart later on)
const chosenItems = ref(new Map());
const chosenItems = ref(new Map<string, number>());
// container for product search response
const items = ref<Schemas["B2BProductDefinition"][]>([]);
// indicates whether there are any items found in API
Expand Down Expand Up @@ -83,7 +83,7 @@ const onItemClick = (item: Schemas["B2BProductDefinition"]) => {
if (chosenItems.value.has(item.id)) {
chosenItems.value.set(
item.id,
chosenItems.value.get(item.id) + (item.quantity || 1),
(chosenItems.value.get(item.id) ?? 0) + (item.quantity || 1),
);
} else {
ITEMS_CACHE.value.set(item.id, item);
Expand All @@ -95,23 +95,18 @@ const onItemClick = (item: Schemas["B2BProductDefinition"]) => {
};
const onAddToCartClick = async () => {
const lineItemsPayload = Array.from(chosenItems.value.entries()).map(
([productId, quantity]) => ({
referencedId: productId,
quantity,
const lineItemsPayload =
Array.from(chosenItems.value.entries()).map(([productId, quantity]) => ({
id: productId,
quantity: quantity ?? 0,
type: "product",
}),
);
})) || null;
if (lineItemsPayload) return;
await apiClient.invoke("addLineItem post /checkout/cart/line-item", {
body: {
// TODO(MD): fix types in component and avoid this cast
items: lineItemsPayload as Array<{
id: string;
referencedId?: string;
quantity?: number;
type: "product" | "promotion" | "custom" | "credit";
}>,
items: lineItemsPayload,
},
});
Expand Down
23 changes: 16 additions & 7 deletions examples/express-checkout/api-types/storeApiTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7292,13 +7292,22 @@ export type operations = {
"sw-language-id"?: string;
};
body: {
// TODO: [OpenAPI][addLineItem] - add proper request body type with required fields
items: Array<{
id: string; // TODO: [OpenAPI][addLineItem] - check if this is used at all?
referencedId?: string;
quantity?: number;
type: "product" | "promotion" | "custom" | "credit"; // TODO: [OpenAPI][addLineItem] - add proper type -> see also #456
}>;
items: (
| {
id: string;
quantity: number;
referencedId?: string;
/** @enum {string} */
type: "product" | "custom" | "credit" | "discount" | "container";
}
| {
id?: string;
quantity?: number;
referencedId: string;
/** @enum {string} */
type: "promotion";
}
)[];
};
response: components["schemas"]["Cart"];
responseCode: 200;
Expand Down
23 changes: 16 additions & 7 deletions examples/mollie-credit-card/api-types/storeApiTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7292,13 +7292,22 @@ export type operations = {
"sw-language-id"?: string;
};
body: {
// TODO: [OpenAPI][addLineItem] - add proper request body type with required fields
items: Array<{
id: string; // TODO: [OpenAPI][addLineItem] - check if this is used at all?
referencedId?: string;
quantity?: number;
type: "product" | "promotion" | "custom" | "credit"; // TODO: [OpenAPI][addLineItem] - add proper type -> see also #456
}>;
items: (
| {
id: string;
quantity: number;
referencedId?: string;
/** @enum {string} */
type: "product" | "custom" | "credit" | "discount" | "container";
}
| {
id?: string;
quantity?: number;
referencedId: string;
/** @enum {string} */
type: "promotion";
}
)[];
};
response: components["schemas"]["Cart"];
responseCode: 200;
Expand Down
71 changes: 71 additions & 0 deletions packages/api-client/api-types/storeApiSchema.overrides.json
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,77 @@
}
]
},
"/checkout/cart/line-item": {
"post": [
{
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"required": ["items"],
"properties": {
"items": {
"type": "array",
"items": {
"anyOf": [
{
"type": "object",
"required": ["id", "type", "quantity"],
"properties": {
"id": {
"type": "string"
},
"referencedId": {
"type": "string"
},
"quantity": {
"type": "number"
},
"type": {
"type": "string",
"enum": [
"product",
"custom",
"credit",
"discount",
"container"
]
}
}
},
{
"type": "object",
"required": ["referencedId", "type"],
"properties": {
"id": {
"type": "string"
},
"referencedId": {
"type": "string"
},
"quantity": {
"type": "number"
},
"type": {
"type": "string",
"enum": ["promotion"]
}
}
}
]
}
}
},
"$ref": "_DELETE_"
}
}
}
}
}
]
},
"/quote/detail/{id}": {
"post": [
{
Expand Down
23 changes: 16 additions & 7 deletions packages/api-client/api-types/storeApiTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7292,13 +7292,22 @@ export type operations = {
"sw-language-id"?: string;
};
body: {
// TODO: [OpenAPI][addLineItem] - add proper request body type with required fields
items: Array<{
id: string; // TODO: [OpenAPI][addLineItem] - check if this is used at all?
referencedId?: string;
quantity?: number;
type: "product" | "promotion" | "custom" | "credit"; // TODO: [OpenAPI][addLineItem] - add proper type -> see also #456
}>;
items: (
| {
id: string;
quantity: number;
referencedId?: string;
/** @enum {string} */
type: "product" | "custom" | "credit" | "discount" | "container";
}
| {
id?: string;
quantity?: number;
referencedId: string;
/** @enum {string} */
type: "promotion";
}
)[];
};
response: components["schemas"]["Cart"];
responseCode: 200;
Expand Down
19 changes: 0 additions & 19 deletions packages/api-client/api-types/storeApiTypes.overrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,25 +109,6 @@ export type operations = {
response: components["schemas"]["Customer"];
responseCode: 200;
};
"addLineItem post /checkout/cart/line-item": {
contentType?: "application/json";
accept?: "application/json";
headers?: {
/** Instructs Shopware to return the response in the given language. */
"sw-language-id"?: string;
};
body: {
// TODO: [OpenAPI][addLineItem] - add proper request body type with required fields
items: Array<{
id: string; // TODO: [OpenAPI][addLineItem] - check if this is used at all?
referencedId?: string;
quantity?: number;
type: "product" | "promotion" | "custom" | "credit"; // TODO: [OpenAPI][addLineItem] - add proper type -> see also #456
}>;
};
response: components["schemas"]["Cart"];
responseCode: 200;
};
"updateLineItem patch /checkout/cart/line-item": {
contentType?: "application/json";
accept?: "application/json";
Expand Down
4 changes: 2 additions & 2 deletions packages/composables/src/useCart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export function useCartFunction(): UseCartReturn {
return addProducts([
{
id: params.id,
quantity: params.quantity,
quantity: params.quantity ?? 0,
type: "product",
},
]);
Expand Down Expand Up @@ -190,7 +190,7 @@ export function useCartFunction(): UseCartReturn {
body: {
items: [
{
id: promotionCode,
referencedId: promotionCode,
type: "promotion",
},
],
Expand Down
Loading

0 comments on commit 3bde5fe

Please sign in to comment.