Skip to content

Update discount example #618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ 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"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice you didn't bump the api_version in this file. Is that an oversight?

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_generate_run.graphql"
export = "cart-delivery-options-generate-run"
[extensions.build]
command = ""
path = "dist/function.wasm"
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