Skip to content

Commit 5ca7e06

Browse files
committed
Update discount example
1 parent f347f17 commit 5ca7e06

18 files changed

+492
-513
lines changed

discounts/javascript/discount/default/shopify.extension.toml.liquid

+4-5
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ description = "t:description"
99

1010
[[extensions.targeting]]
1111
target = "cart.lines.discounts.generate.run"
12-
input_query = "src/generate_cart_run.graphql"
13-
export = "generate-cart-run"
12+
input_query = "src/cart_lines_discounts_generate_run.graphql"
13+
export = "cart-lines-discounts-generate-run"
1414

1515
[[extensions.targeting]]
1616
target = "cart.delivery-options.discounts.generate.run"
17-
input_query = "src/generate_delivery_run.graphql"
18-
export = "generate-delivery-run"
19-
17+
input_query = "src/cart_delivery_options_generate_run.graphql"
18+
export = "cart-delivery-options-generate-run"
2019
[extensions.build]
2120
command = ""
2221
path = "dist/function.wasm"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{%- if flavor contains "vanilla-js" -%}
2+
import {
3+
DeliveryDiscountSelectionStrategy,
4+
DiscountClass,
5+
} from "../generated/api";
6+
7+
/**
8+
* @typedef {import("../generated/api").DeliveryInput} RunInput
9+
* @typedef {import("../generated/api").CartDeliveryOptionsDiscountsGenerateRunResult} CartDeliveryOptionsDiscountsGenerateRunResult
10+
*/
11+
12+
/**
13+
* @param {RunInput} input
14+
* @returns {CartDeliveryOptionsDiscountsGenerateRunResult}
15+
*/
16+
17+
export function cartDeliveryOptionsDiscountsGenerateRun(input) {
18+
const firstDeliveryGroup = input.cart.deliveryGroups[0];
19+
if (!firstDeliveryGroup) {
20+
throw new Error("No delivery groups found");
21+
}
22+
23+
const hasShippingDiscountClass = input.discount.discountClasses.includes(
24+
DiscountClass.Shipping,
25+
);
26+
27+
if (!hasShippingDiscountClass) {
28+
return {operations: []};
29+
}
30+
31+
return {
32+
operations: [
33+
{
34+
deliveryDiscountsAdd: {
35+
candidates: [
36+
{
37+
message: "FREE DELIVERY",
38+
targets: [
39+
{
40+
deliveryGroup: {
41+
id: firstDeliveryGroup.id,
42+
},
43+
},
44+
],
45+
value: {
46+
percentage: {
47+
value: 100,
48+
},
49+
},
50+
},
51+
],
52+
selectionStrategy: DeliveryDiscountSelectionStrategy.All,
53+
},
54+
},
55+
],
56+
};
57+
}
58+
{%- elsif flavor contains "typescript" -%}
59+
import {
60+
DeliveryDiscountSelectionStrategy,
61+
DiscountClass,
62+
DeliveryInput,
63+
CartDeliveryOptionsDiscountsGenerateRunResult,
64+
} from "../generated/api";
65+
66+
export function cartDeliveryOptionsDiscountsGenerateRun(
67+
input: DeliveryInput,
68+
): CartDeliveryOptionsDiscountsGenerateRunResult {
69+
const firstDeliveryGroup = input.cart.deliveryGroups[0];
70+
if (!firstDeliveryGroup) {
71+
throw new Error("No delivery groups found");
72+
}
73+
74+
const hasShippingDiscountClass = input.discount.discountClasses.includes(
75+
DiscountClass.Shipping,
76+
);
77+
78+
if (!hasShippingDiscountClass) {
79+
return {operations: []};
80+
}
81+
82+
return {
83+
operations: [
84+
{
85+
deliveryDiscountsAdd: {
86+
candidates: [
87+
{
88+
message: "FREE DELIVERY",
89+
targets: [
90+
{
91+
deliveryGroup: {
92+
id: firstDeliveryGroup.id,
93+
},
94+
},
95+
],
96+
value: {
97+
percentage: {
98+
value: 100,
99+
},
100+
},
101+
},
102+
],
103+
selectionStrategy: DeliveryDiscountSelectionStrategy.All,
104+
},
105+
},
106+
],
107+
};
108+
}
109+
{%- endif -%}

discounts/javascript/discount/default/src/generate_delivery_run.test.liquid renamed to discounts/javascript/discount/default/src/cart_delivery_options_discounts_generate_run.test.liquid

+30-22
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
{%- if flavor contains "vanilla-js" -%}
2-
import { describe, it, expect } from "vitest";
2+
import {describe, it, expect} from "vitest";
33

4-
import { generateDeliveryRun } from "./generate_delivery_run";
4+
import {cartDeliveryOptionsDiscountsGenerateRun} from "./cart_delivery_options_discounts_generate_run";
55
import {
66
DeliveryDiscountSelectionStrategy,
77
DiscountClass,
88
} from "../generated/api";
99

1010
/**
11-
* @typedef {import("../generated/api").CartDeliveryOptionsDiscountsGenerateRunResult} CartDeliveryOptionsDiscountsGenerateRunResult
12-
* @typedef {import("../generated/api").DeliveryInput} DeliveryInput
13-
*/
11+
* @typedef {import("../generated/api").CartDeliveryOptionsDiscountsGenerateRunResult} CartDeliveryOptionsDiscountsGenerateRunResult
12+
* @typedef {import("../generated/api").DeliveryInput} DeliveryInput
13+
*/
1414

15-
describe("generateDeliveryRun", () => {
15+
describe("cartDeliveryOptionsDiscountsGenerateRun", () => {
1616
const baseInput = {
1717
cart: {
1818
deliveryGroups: [
@@ -23,7 +23,7 @@ describe("generateDeliveryRun", () => {
2323
},
2424
discount: {
2525
discountClasses: [],
26-
}
26+
},
2727
};
2828

2929
it("returns empty operations when no discount classes are present", () => {
@@ -34,7 +34,7 @@ describe("generateDeliveryRun", () => {
3434
},
3535
};
3636

37-
const result = generateDeliveryRun(input);
37+
const result = cartDeliveryOptionsDiscountsGenerateRun(input);
3838
expect(result.operations).toHaveLength(0);
3939
});
4040

@@ -46,7 +46,7 @@ describe("generateDeliveryRun", () => {
4646
},
4747
};
4848

49-
const result = generateDeliveryRun(input);
49+
const result = cartDeliveryOptionsDiscountsGenerateRun(input);
5050
expect(result.operations).toHaveLength(1);
5151
expect(result.operations[0]).toMatchObject({
5252
deliveryDiscountsAdd: {
@@ -82,21 +82,24 @@ describe("generateDeliveryRun", () => {
8282
},
8383
};
8484

85-
expect(() => generateDeliveryRun(input)).toThrow('No delivery groups found');
85+
expect(() => cartDeliveryOptionsDiscountsGenerateRun(input)).toThrow(
86+
"No delivery groups found",
87+
);
8688
});
8789
});
8890
{%- elsif flavor contains "typescript" -%}
89-
import { describe, it, expect } from "vitest";
91+
import {describe, it, expect} from "vitest";
9092

91-
import { generateDeliveryRun } from "./generate_delivery_run";
93+
import {cartDeliveryOptionsDiscountsGenerateRun} from "./cart_delivery_options_discounts_generate_run";
9294
import {
9395
DeliveryDiscountSelectionStrategy,
9496
DiscountClass,
95-
CartDeliveryOptionsDiscountsGenerateRunResult
97+
DeliveryInput,
98+
CartDeliveryOptionsDiscountsGenerateRunResult,
9699
} from "../generated/api";
97100

98-
describe("generateDeliveryRun", () => {
99-
const baseInput = {
101+
describe("cartDeliveryOptionsDiscountsGenerateRun", () => {
102+
const baseInput: DeliveryInput = {
100103
cart: {
101104
deliveryGroups: [
102105
{
@@ -106,30 +109,32 @@ describe("generateDeliveryRun", () => {
106109
},
107110
discount: {
108111
discountClasses: [],
109-
}
112+
},
110113
};
111114

112115
it("returns empty operations when no discount classes are present", () => {
113-
const input = {
116+
const input: DeliveryInput = {
114117
...baseInput,
115118
discount: {
116119
discountClasses: [],
117120
},
118121
};
119122

120-
const result: CartDeliveryOptionsDiscountsGenerateRunResult = generateDeliveryRun(input);
123+
const result: CartDeliveryOptionsDiscountsGenerateRunResult =
124+
cartDeliveryOptionsDiscountsGenerateRun(input);
121125
expect(result.operations).toHaveLength(0);
122126
});
123127

124128
it("returns delivery discount when shipping discount class is present", () => {
125-
const input = {
129+
const input: DeliveryInput = {
126130
...baseInput,
127131
discount: {
128132
discountClasses: [DiscountClass.Shipping],
129133
},
130134
};
131135

132-
const result: CartDeliveryOptionsDiscountsGenerateRunResult = generateDeliveryRun(input);
136+
const result: CartDeliveryOptionsDiscountsGenerateRunResult =
137+
cartDeliveryOptionsDiscountsGenerateRun(input);
133138
expect(result.operations).toHaveLength(1);
134139
expect(result.operations[0]).toMatchObject({
135140
deliveryDiscountsAdd: {
@@ -156,7 +161,7 @@ describe("generateDeliveryRun", () => {
156161
});
157162

158163
it("throws error when no delivery groups are present", () => {
159-
const input = {
164+
const input: DeliveryInput = {
160165
cart: {
161166
deliveryGroups: [],
162167
},
@@ -165,7 +170,10 @@ describe("generateDeliveryRun", () => {
165170
},
166171
};
167172

168-
expect(() => generateDeliveryRun(input)).toThrow('No delivery groups found');
173+
expect(() => cartDeliveryOptionsDiscountsGenerateRun(input)).toThrow(
174+
"No delivery groups found",
175+
);
169176
});
170177
});
178+
171179
{%- endif -%}

0 commit comments

Comments
 (0)