Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ functions*/**/run.graphql
functions*/**/fetch.graphql
functions*/**/generate_cart_run.graphql
functions*/**/generate_delivery_run.graphql
functions*/**/cart_delivery_options_discounts_generate_run.graphql
functions*/**/cart_lines_discounts_generate_run.graphql
functions*/**/Cargo.toml
functions*/**/shopify.extension.toml
functions*/**/package.json
Expand All @@ -27,6 +29,10 @@ functions*/**/generate_cart_run.js
functions*/**/generate_cart_run.ts
functions*/**/generate_delivery_run.js
functions*/**/generate_delivery_run.ts
functions*/**/cart_delivery_options_discounts_generate_run.js
functions*/**/cart_delivery_options_discounts_generate_run.ts
functions*/**/cart_lines_discounts_generate_run.js
functions*/**/cart_lines_discounts_generate_run.ts
functions*/**/run.test.js
functions*/**/run.test.ts
functions*/**/fetch.test.js
Expand All @@ -35,6 +41,10 @@ functions*/**/generate_cart_run.test.js
functions*/**/generate_cart_run.test.ts
functions*/**/generate_delivery_run.test.js
functions*/**/generate_delivery_run.test.ts
functions*/**/cart_delivery_options_discounts_generate_run.test.js
functions*/**/cart_delivery_options_discounts_generate_run.test.ts
functions*/**/cart_lines_discounts_generate_run.test.js
functions*/**/cart_lines_discounts_generate_run.test.ts

# Ignore local build files
functions*/generated
Expand Down
8 changes: 4 additions & 4 deletions functions-discount-js/shopify.extension.toml.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ description = "t:description"

[[extensions.targeting]]
target = "cart.lines.discounts.generate.run"
input_query = "src/generate_cart_run.graphql"
export = "generate-cart-run"
input_query = "src/cart_lines_discounts_generate_run.graphql"
export = "cart-lines-discounts-generate-run"

[[extensions.targeting]]
target = "cart.delivery-options.discounts.generate.run"
input_query = "src/generate_delivery_run.graphql"
export = "generate-delivery-run"
input_query = "src/cart_delivery_options_discounts_generate_run.graphql"
export = "cart-delivery-options-discounts-generate-run"

[extensions.build]
command = ""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{%- if flavor contains "vanilla-js" -%}
import {
DeliveryDiscountSelectionStrategy,
DiscountClass,
} from "../generated/api";

/**
* @typedef {import("../generated/api").DeliveryInput} RunInput
* @typedef {import("../generated/api").CartDeliveryOptionsDiscountsGenerateRunResult} CartDeliveryOptionsDiscountsGenerateRunResult
*/

/**
* @param {RunInput} input
* @returns {CartDeliveryOptionsDiscountsGenerateRunResult}
*/

export function cartDeliveryOptionsDiscountsGenerateRun(input) {
const firstDeliveryGroup = input.cart.deliveryGroups[0];
if (!firstDeliveryGroup) {
throw new Error("No delivery groups found");
}

const hasShippingDiscountClass = input.discount.discountClasses.includes(
DiscountClass.Shipping,
);

if (!hasShippingDiscountClass) {
return {operations: []};
}

return {
operations: [
{
deliveryDiscountsAdd: {
candidates: [
{
message: "FREE DELIVERY",
targets: [
{
deliveryGroup: {
id: firstDeliveryGroup.id,
},
},
],
value: {
percentage: {
value: 100,
},
},
},
],
selectionStrategy: DeliveryDiscountSelectionStrategy.All,
},
},
],
};
}
{%- elsif flavor contains "typescript" -%}
import {
DeliveryDiscountSelectionStrategy,
DiscountClass,
DeliveryInput,
CartDeliveryOptionsDiscountsGenerateRunResult,
} from "../generated/api";

export function cartDeliveryOptionsDiscountsGenerateRun(
input: DeliveryInput,
): CartDeliveryOptionsDiscountsGenerateRunResult {
const firstDeliveryGroup = input.cart.deliveryGroups[0];
if (!firstDeliveryGroup) {
throw new Error("No delivery groups found");
}

const hasShippingDiscountClass = input.discount.discountClasses.includes(
DiscountClass.Shipping,
);

if (!hasShippingDiscountClass) {
return {operations: []};
}

return {
operations: [
{
deliveryDiscountsAdd: {
candidates: [
{
message: "FREE DELIVERY",
targets: [
{
deliveryGroup: {
id: firstDeliveryGroup.id,
},
},
],
value: {
percentage: {
value: 100,
},
},
},
],
selectionStrategy: DeliveryDiscountSelectionStrategy.All,
},
},
],
};
}
{%- endif -%}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{%- if flavor contains "vanilla-js" -%}
import { describe, it, expect } from "vitest";
import {describe, it, expect} from "vitest";

import { generateDeliveryRun } from "./generate_delivery_run";
import {cartDeliveryOptionsDiscountsGenerateRun} from "./cart_delivery_options_discounts_generate_run";
import {
DeliveryDiscountSelectionStrategy,
DiscountClass,
} from "../generated/api";

/**
* @typedef {import("../generated/api").CartDeliveryOptionsDiscountsGenerateRunResult} CartDeliveryOptionsDiscountsGenerateRunResult
* @typedef {import("../generated/api").DeliveryInput} DeliveryInput
*/
* @typedef {import("../generated/api").CartDeliveryOptionsDiscountsGenerateRunResult} CartDeliveryOptionsDiscountsGenerateRunResult
* @typedef {import("../generated/api").DeliveryInput} DeliveryInput
*/

describe("generateDeliveryRun", () => {
describe("cartDeliveryOptionsDiscountsGenerateRun", () => {
const baseInput = {
cart: {
deliveryGroups: [
Expand All @@ -23,7 +23,7 @@ describe("generateDeliveryRun", () => {
},
discount: {
discountClasses: [],
}
},
};

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

const result = generateDeliveryRun(input);
const result = cartDeliveryOptionsDiscountsGenerateRun(input);
expect(result.operations).toHaveLength(0);
});

Expand All @@ -46,7 +46,7 @@ describe("generateDeliveryRun", () => {
},
};

const result = generateDeliveryRun(input);
const result = cartDeliveryOptionsDiscountsGenerateRun(input);
expect(result.operations).toHaveLength(1);
expect(result.operations[0]).toMatchObject({
deliveryDiscountsAdd: {
Expand Down Expand Up @@ -82,21 +82,24 @@ describe("generateDeliveryRun", () => {
},
};

expect(() => generateDeliveryRun(input)).toThrow('No delivery groups found');
expect(() => cartDeliveryOptionsDiscountsGenerateRun(input)).toThrow(
"No delivery groups found",
);
});
});
{%- elsif flavor contains "typescript" -%}
import { describe, it, expect } from "vitest";
import {describe, it, expect} from "vitest";

import { generateDeliveryRun } from "./generate_delivery_run";
import {cartDeliveryOptionsDiscountsGenerateRun} from "./cart_delivery_options_discounts_generate_run";
import {
DeliveryDiscountSelectionStrategy,
DiscountClass,
CartDeliveryOptionsDiscountsGenerateRunResult
DeliveryInput,
CartDeliveryOptionsDiscountsGenerateRunResult,
} from "../generated/api";

describe("generateDeliveryRun", () => {
const baseInput = {
describe("cartDeliveryOptionsDiscountsGenerateRun", () => {
const baseInput: DeliveryInput = {
cart: {
deliveryGroups: [
{
Expand All @@ -106,30 +109,32 @@ describe("generateDeliveryRun", () => {
},
discount: {
discountClasses: [],
}
},
};

it("returns empty operations when no discount classes are present", () => {
const input = {
const input: DeliveryInput = {
...baseInput,
discount: {
discountClasses: [],
},
};

const result: CartDeliveryOptionsDiscountsGenerateRunResult = generateDeliveryRun(input);
const result: CartDeliveryOptionsDiscountsGenerateRunResult =
cartDeliveryOptionsDiscountsGenerateRun(input);
expect(result.operations).toHaveLength(0);
});

it("returns delivery discount when shipping discount class is present", () => {
const input = {
const input: DeliveryInput = {
...baseInput,
discount: {
discountClasses: [DiscountClass.Shipping],
},
};

const result: CartDeliveryOptionsDiscountsGenerateRunResult = generateDeliveryRun(input);
const result: CartDeliveryOptionsDiscountsGenerateRunResult =
cartDeliveryOptionsDiscountsGenerateRun(input);
expect(result.operations).toHaveLength(1);
expect(result.operations[0]).toMatchObject({
deliveryDiscountsAdd: {
Expand All @@ -156,7 +161,7 @@ describe("generateDeliveryRun", () => {
});

it("throws error when no delivery groups are present", () => {
const input = {
const input: DeliveryInput = {
cart: {
deliveryGroups: [],
},
Expand All @@ -165,7 +170,10 @@ describe("generateDeliveryRun", () => {
},
};

expect(() => generateDeliveryRun(input)).toThrow('No delivery groups found');
expect(() => cartDeliveryOptionsDiscountsGenerateRun(input)).toThrow(
"No delivery groups found",
);
});
});

{%- endif -%}
Loading