mcp-shopify-yoffi 1.0.8
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.
- package/LICENSE +21 -0
- package/README.md +558 -0
- package/dist/index.js +91 -0
- package/dist/lib/formatters.js +72 -0
- package/dist/lib/shopifyAuth.js +93 -0
- package/dist/lib/toolUtils.js +36 -0
- package/dist/tools/completeDraftOrder.js +76 -0
- package/dist/tools/createAutomaticDiscount.js +149 -0
- package/dist/tools/createCustomer.js +142 -0
- package/dist/tools/createDiscount.js +177 -0
- package/dist/tools/createDraftOrder.js +160 -0
- package/dist/tools/createFulfillment.js +100 -0
- package/dist/tools/createProduct.js +123 -0
- package/dist/tools/createRefund.js +115 -0
- package/dist/tools/deleteCustomer.js +47 -0
- package/dist/tools/deleteMetafields.js +54 -0
- package/dist/tools/deleteProduct.js +43 -0
- package/dist/tools/deleteProductVariants.js +82 -0
- package/dist/tools/getCollectionById.js +123 -0
- package/dist/tools/getCollections.js +88 -0
- package/dist/tools/getCustomerById.js +122 -0
- package/dist/tools/getCustomerOrders.js +131 -0
- package/dist/tools/getCustomers.js +125 -0
- package/dist/tools/getFulfillmentOrders.js +106 -0
- package/dist/tools/getInventoryItems.js +86 -0
- package/dist/tools/getInventoryLevels.js +82 -0
- package/dist/tools/getLocations.js +85 -0
- package/dist/tools/getMarkets.js +91 -0
- package/dist/tools/getMetafieldDefinitions.js +112 -0
- package/dist/tools/getMetafields.js +68 -0
- package/dist/tools/getOrderById.js +212 -0
- package/dist/tools/getOrderRefundDetails.js +131 -0
- package/dist/tools/getOrderTransactions.js +85 -0
- package/dist/tools/getOrders.js +148 -0
- package/dist/tools/getPriceLists.js +92 -0
- package/dist/tools/getProductById.js +171 -0
- package/dist/tools/getProductVariantsDetailed.js +139 -0
- package/dist/tools/getProducts.js +155 -0
- package/dist/tools/getShopInfo.js +74 -0
- package/dist/tools/manageCustomerAddress.js +149 -0
- package/dist/tools/manageProductOptions.js +293 -0
- package/dist/tools/manageProductVariants.js +203 -0
- package/dist/tools/manageTags.js +79 -0
- package/dist/tools/mergeCustomers.js +74 -0
- package/dist/tools/orderCancel.js +77 -0
- package/dist/tools/orderCloseOpen.js +74 -0
- package/dist/tools/orderMarkAsPaid.js +51 -0
- package/dist/tools/registry.js +112 -0
- package/dist/tools/setInventoryQuantities.js +74 -0
- package/dist/tools/setMetafields.js +61 -0
- package/dist/tools/updateCustomer.js +119 -0
- package/dist/tools/updateOrder.js +131 -0
- package/dist/tools/updateProduct.js +132 -0
- package/package.json +67 -0
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
// ── Shared Zod schemas ────────────────────────────────────────────────
|
|
3
|
+
/**
|
|
4
|
+
* Mailing address schema shared by createDraftOrder (shipping + billing)
|
|
5
|
+
* and manageCustomerAddress. Uses countryCode/provinceCode (API input type).
|
|
6
|
+
*
|
|
7
|
+
* NOTE: updateOrder.shippingAddress intentionally uses country/province
|
|
8
|
+
* (different Shopify input type) and is NOT shared here.
|
|
9
|
+
*/
|
|
10
|
+
export const shippingAddressSchema = z.object({
|
|
11
|
+
address1: z.string().optional(),
|
|
12
|
+
address2: z.string().optional(),
|
|
13
|
+
city: z.string().optional(),
|
|
14
|
+
company: z.string().optional(),
|
|
15
|
+
countryCode: z.string().optional().describe("Two-letter country code"),
|
|
16
|
+
firstName: z.string().optional(),
|
|
17
|
+
lastName: z.string().optional(),
|
|
18
|
+
phone: z.string().optional().describe("Phone in E.164 format, e.g. +16135551111"),
|
|
19
|
+
provinceCode: z.string().optional(),
|
|
20
|
+
zip: z.string().optional(),
|
|
21
|
+
});
|
|
22
|
+
/**
|
|
23
|
+
* Format a line-item connection into a clean array.
|
|
24
|
+
* Used by getOrders, getOrderById, and getCustomerOrders.
|
|
25
|
+
*/
|
|
26
|
+
export function formatLineItems(lineItems) {
|
|
27
|
+
return lineItems.edges.map((edge) => {
|
|
28
|
+
const item = edge.node;
|
|
29
|
+
return {
|
|
30
|
+
id: item.id,
|
|
31
|
+
title: item.title,
|
|
32
|
+
quantity: item.quantity,
|
|
33
|
+
originalTotal: item.originalTotalSet.shopMoney,
|
|
34
|
+
variant: item.variant
|
|
35
|
+
? {
|
|
36
|
+
id: item.variant.id,
|
|
37
|
+
title: item.variant.title,
|
|
38
|
+
sku: item.variant.sku,
|
|
39
|
+
}
|
|
40
|
+
: null,
|
|
41
|
+
};
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Format a raw order node into the standard order summary shape.
|
|
46
|
+
* Used by getOrders, getCustomerOrders, and getOrderById (as a base).
|
|
47
|
+
*/
|
|
48
|
+
export function formatOrderSummary(order) {
|
|
49
|
+
return {
|
|
50
|
+
id: order.id,
|
|
51
|
+
name: order.name,
|
|
52
|
+
createdAt: order.createdAt,
|
|
53
|
+
financialStatus: order.displayFinancialStatus,
|
|
54
|
+
fulfillmentStatus: order.displayFulfillmentStatus,
|
|
55
|
+
totalPrice: order.totalPriceSet.shopMoney,
|
|
56
|
+
subtotalPrice: order.subtotalPriceSet.shopMoney,
|
|
57
|
+
totalShippingPrice: order.totalShippingPriceSet.shopMoney,
|
|
58
|
+
totalTax: order.totalTaxSet.shopMoney,
|
|
59
|
+
customer: order.customer
|
|
60
|
+
? {
|
|
61
|
+
id: order.customer.id,
|
|
62
|
+
firstName: order.customer.firstName,
|
|
63
|
+
lastName: order.customer.lastName,
|
|
64
|
+
email: order.customer.defaultEmailAddress?.emailAddress || null,
|
|
65
|
+
}
|
|
66
|
+
: null,
|
|
67
|
+
shippingAddress: order.shippingAddress,
|
|
68
|
+
lineItems: formatLineItems(order.lineItems),
|
|
69
|
+
tags: order.tags,
|
|
70
|
+
note: order.note,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shopify OAuth Client Credentials flow.
|
|
3
|
+
*
|
|
4
|
+
* As of January 1 2026 Shopify no longer exposes static Admin API access
|
|
5
|
+
* tokens in the UI. New apps created in the Dev Dashboard receive a
|
|
6
|
+
* client_id + client_secret pair which must be exchanged for a short-lived
|
|
7
|
+
* access token (expires_in ≈ 86 400 s / 24 h).
|
|
8
|
+
*
|
|
9
|
+
* This module handles the token exchange and transparent refresh so the
|
|
10
|
+
* rest of the codebase can keep using a plain access-token string.
|
|
11
|
+
*/
|
|
12
|
+
// Refresh 5 minutes before actual expiry to avoid race conditions.
|
|
13
|
+
const REFRESH_MARGIN_MS = 5 * 60 * 1000;
|
|
14
|
+
export class ShopifyAuth {
|
|
15
|
+
constructor(config) {
|
|
16
|
+
this.accessToken = null;
|
|
17
|
+
this.expiresAt = 0;
|
|
18
|
+
this.refreshTimer = null;
|
|
19
|
+
this.graphqlClient = null;
|
|
20
|
+
this.config = config;
|
|
21
|
+
}
|
|
22
|
+
/** Attach the GraphQL client so the token can be hot-swapped on refresh. */
|
|
23
|
+
setGraphQLClient(client) {
|
|
24
|
+
this.graphqlClient = client;
|
|
25
|
+
}
|
|
26
|
+
/** Fetch an initial token. Must be called before the server starts. */
|
|
27
|
+
async initialize() {
|
|
28
|
+
await this.fetchToken();
|
|
29
|
+
this.scheduleRefresh();
|
|
30
|
+
return this.accessToken;
|
|
31
|
+
}
|
|
32
|
+
/** Return the current (valid) access token. */
|
|
33
|
+
getAccessToken() {
|
|
34
|
+
if (!this.accessToken) {
|
|
35
|
+
throw new Error("ShopifyAuth not initialized — call initialize() first");
|
|
36
|
+
}
|
|
37
|
+
return this.accessToken;
|
|
38
|
+
}
|
|
39
|
+
/** Stop the background refresh timer (for clean shutdown). */
|
|
40
|
+
destroy() {
|
|
41
|
+
if (this.refreshTimer) {
|
|
42
|
+
clearTimeout(this.refreshTimer);
|
|
43
|
+
this.refreshTimer = null;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
// ---------------------------------------------------------------------------
|
|
47
|
+
// Internal
|
|
48
|
+
// ---------------------------------------------------------------------------
|
|
49
|
+
async fetchToken() {
|
|
50
|
+
const url = `https://${this.config.shopDomain}/admin/oauth/access_token`;
|
|
51
|
+
const body = new URLSearchParams({
|
|
52
|
+
grant_type: "client_credentials",
|
|
53
|
+
client_id: this.config.clientId,
|
|
54
|
+
client_secret: this.config.clientSecret,
|
|
55
|
+
});
|
|
56
|
+
const res = await fetch(url, {
|
|
57
|
+
method: "POST",
|
|
58
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
59
|
+
body,
|
|
60
|
+
});
|
|
61
|
+
if (!res.ok) {
|
|
62
|
+
const text = await res.text();
|
|
63
|
+
throw new Error(`Shopify token exchange failed (${res.status}): ${text}`);
|
|
64
|
+
}
|
|
65
|
+
const data = (await res.json());
|
|
66
|
+
this.accessToken = data.access_token;
|
|
67
|
+
this.expiresAt = Date.now() + data.expires_in * 1000;
|
|
68
|
+
// Hot-swap the header on the existing GraphQL client so every tool
|
|
69
|
+
// automatically picks up the new token.
|
|
70
|
+
if (this.graphqlClient) {
|
|
71
|
+
this.graphqlClient.setHeader("X-Shopify-Access-Token", this.accessToken);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
scheduleRefresh() {
|
|
75
|
+
const msUntilRefresh = this.expiresAt - Date.now() - REFRESH_MARGIN_MS;
|
|
76
|
+
const delay = Math.max(msUntilRefresh, 0);
|
|
77
|
+
this.refreshTimer = setTimeout(async () => {
|
|
78
|
+
try {
|
|
79
|
+
await this.fetchToken();
|
|
80
|
+
this.scheduleRefresh();
|
|
81
|
+
}
|
|
82
|
+
catch (err) {
|
|
83
|
+
console.error("Failed to refresh Shopify access token:", err);
|
|
84
|
+
// Retry in 60 s rather than dying.
|
|
85
|
+
this.refreshTimer = setTimeout(() => this.scheduleRefresh(), 60000);
|
|
86
|
+
}
|
|
87
|
+
}, delay);
|
|
88
|
+
// Allow the Node process to exit even if the timer is pending.
|
|
89
|
+
if (this.refreshTimer && typeof this.refreshTimer === "object" && "unref" in this.refreshTimer) {
|
|
90
|
+
this.refreshTimer.unref();
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// ── Utility functions ─────────────────────────────────────────────────
|
|
2
|
+
/**
|
|
3
|
+
* Throw a formatted error if Shopify userErrors array is non-empty.
|
|
4
|
+
*/
|
|
5
|
+
export function checkUserErrors(errors, operation) {
|
|
6
|
+
if (errors.length > 0) {
|
|
7
|
+
throw new Error(`Failed to ${operation}: ${errors
|
|
8
|
+
.map((e) => `${e.field}: ${e.message}`)
|
|
9
|
+
.join(", ")}`);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Catch handler that doesn't re-wrap errors already thrown by checkUserErrors.
|
|
14
|
+
* Fixes the double-wrapping bug where "Failed to X: Failed to X: actual message"
|
|
15
|
+
* was produced by every mutation tool.
|
|
16
|
+
*/
|
|
17
|
+
export function handleToolError(operation, error) {
|
|
18
|
+
// If the error already has our "Failed to" prefix, re-throw as-is
|
|
19
|
+
if (error instanceof Error && error.message.startsWith("Failed to ")) {
|
|
20
|
+
throw error;
|
|
21
|
+
}
|
|
22
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
23
|
+
throw new Error(`Failed to ${operation}: ${message}`);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Extract nodes from a Shopify connection's edges array.
|
|
27
|
+
*/
|
|
28
|
+
export function edgesToNodes(connection) {
|
|
29
|
+
return connection.edges.map((edge) => edge.node);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Extract shopMoney from a Shopify MoneyBag (e.g. totalPriceSet.shopMoney).
|
|
33
|
+
*/
|
|
34
|
+
export function shopMoney(moneyBag) {
|
|
35
|
+
return moneyBag?.shopMoney ?? null;
|
|
36
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { gql } from "graphql-request";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { checkUserErrors, handleToolError } from "../lib/toolUtils.js";
|
|
4
|
+
const CompleteDraftOrderInputSchema = z.object({
|
|
5
|
+
draftOrderId: z.string().describe("The draft order GID, e.g. gid://shopify/DraftOrder/123"),
|
|
6
|
+
paymentGatewayId: z.string().optional().describe("Payment gateway GID (optional)"),
|
|
7
|
+
});
|
|
8
|
+
let shopifyClient;
|
|
9
|
+
const completeDraftOrder = {
|
|
10
|
+
name: "complete-draft-order",
|
|
11
|
+
description: "Complete a draft order, converting it into a real order. Optionally specify a payment gateway.",
|
|
12
|
+
schema: CompleteDraftOrderInputSchema,
|
|
13
|
+
initialize(client) {
|
|
14
|
+
shopifyClient = client;
|
|
15
|
+
},
|
|
16
|
+
execute: async (input) => {
|
|
17
|
+
try {
|
|
18
|
+
const query = gql `
|
|
19
|
+
#graphql
|
|
20
|
+
|
|
21
|
+
mutation draftOrderComplete($id: ID!, $paymentGatewayId: ID) {
|
|
22
|
+
draftOrderComplete(id: $id, paymentGatewayId: $paymentGatewayId) {
|
|
23
|
+
draftOrder {
|
|
24
|
+
id
|
|
25
|
+
status
|
|
26
|
+
order {
|
|
27
|
+
id
|
|
28
|
+
name
|
|
29
|
+
displayFinancialStatus
|
|
30
|
+
displayFulfillmentStatus
|
|
31
|
+
totalPriceSet {
|
|
32
|
+
shopMoney {
|
|
33
|
+
amount
|
|
34
|
+
currencyCode
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
userErrors {
|
|
40
|
+
field
|
|
41
|
+
message
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
`;
|
|
46
|
+
const variables = {
|
|
47
|
+
id: input.draftOrderId,
|
|
48
|
+
};
|
|
49
|
+
if (input.paymentGatewayId) {
|
|
50
|
+
variables.paymentGatewayId = input.paymentGatewayId;
|
|
51
|
+
}
|
|
52
|
+
const data = (await shopifyClient.request(query, variables));
|
|
53
|
+
checkUserErrors(data.draftOrderComplete.userErrors, "complete draft order");
|
|
54
|
+
const draft = data.draftOrderComplete.draftOrder;
|
|
55
|
+
return {
|
|
56
|
+
draftOrder: {
|
|
57
|
+
id: draft.id,
|
|
58
|
+
status: draft.status,
|
|
59
|
+
order: draft.order
|
|
60
|
+
? {
|
|
61
|
+
id: draft.order.id,
|
|
62
|
+
name: draft.order.name,
|
|
63
|
+
financialStatus: draft.order.displayFinancialStatus,
|
|
64
|
+
fulfillmentStatus: draft.order.displayFulfillmentStatus,
|
|
65
|
+
totalPrice: draft.order.totalPriceSet?.shopMoney,
|
|
66
|
+
}
|
|
67
|
+
: null,
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
handleToolError("complete draft order", error);
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
export { completeDraftOrder };
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { gql } from "graphql-request";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { checkUserErrors, handleToolError } from "../lib/toolUtils.js";
|
|
4
|
+
const CreateAutomaticDiscountInputSchema = z.object({
|
|
5
|
+
title: z.string().describe("Title for the discount, shown to customers at checkout"),
|
|
6
|
+
valueType: z
|
|
7
|
+
.enum(["PERCENTAGE", "FIXED_AMOUNT"])
|
|
8
|
+
.describe("Whether the discount value is a percentage or a fixed amount"),
|
|
9
|
+
value: z
|
|
10
|
+
.number()
|
|
11
|
+
.describe("Discount value. For PERCENTAGE, a decimal between 0 and 1 (e.g. 0.15 for 15%). For FIXED_AMOUNT, the amount in the shop's currency (e.g. 10 for $10 off)."),
|
|
12
|
+
startsAt: z.string().describe("ISO 8601 datetime the discount becomes active, e.g. 2024-01-01T00:00:00Z"),
|
|
13
|
+
endsAt: z.string().optional().describe("ISO 8601 datetime the discount expires (omit for no expiry)"),
|
|
14
|
+
minimumSubtotal: z
|
|
15
|
+
.number()
|
|
16
|
+
.optional()
|
|
17
|
+
.describe("Minimum order subtotal required for the discount to apply (omit for no minimum)"),
|
|
18
|
+
includeCollectionIds: z
|
|
19
|
+
.array(z.string())
|
|
20
|
+
.optional()
|
|
21
|
+
.describe("Collection GIDs (e.g. gid://shopify/Collection/123) the discount applies to. Mutually exclusive with includeProductIds/excludeProductIds. Omit all four to apply store-wide."),
|
|
22
|
+
excludeCollectionIds: z
|
|
23
|
+
.array(z.string())
|
|
24
|
+
.optional()
|
|
25
|
+
.describe("Collection GIDs to exclude from an otherwise store-wide discount"),
|
|
26
|
+
includeProductIds: z
|
|
27
|
+
.array(z.string())
|
|
28
|
+
.optional()
|
|
29
|
+
.describe("Product GIDs (e.g. gid://shopify/Product/123) the discount applies to. Mutually exclusive with includeCollectionIds/excludeCollectionIds."),
|
|
30
|
+
excludeProductIds: z
|
|
31
|
+
.array(z.string())
|
|
32
|
+
.optional()
|
|
33
|
+
.describe("Product GIDs to exclude from an otherwise store-wide discount"),
|
|
34
|
+
combinesWith: z
|
|
35
|
+
.object({
|
|
36
|
+
productDiscounts: z.boolean().optional(),
|
|
37
|
+
orderDiscounts: z.boolean().optional(),
|
|
38
|
+
shippingDiscounts: z.boolean().optional(),
|
|
39
|
+
})
|
|
40
|
+
.optional()
|
|
41
|
+
.describe("Whether this discount can be combined with other active discounts"),
|
|
42
|
+
});
|
|
43
|
+
let shopifyClient;
|
|
44
|
+
const createAutomaticDiscount = {
|
|
45
|
+
name: "create-automatic-discount",
|
|
46
|
+
description: "Create an automatic discount (percentage or fixed amount) that applies at checkout with no code required, store-wide or scoped to specific collections and/or products.",
|
|
47
|
+
schema: CreateAutomaticDiscountInputSchema,
|
|
48
|
+
initialize(client) {
|
|
49
|
+
shopifyClient = client;
|
|
50
|
+
},
|
|
51
|
+
execute: async (input) => {
|
|
52
|
+
try {
|
|
53
|
+
if (input.valueType === "PERCENTAGE" && (input.value < 0 || input.value > 1)) {
|
|
54
|
+
throw new Error("value must be between 0 and 1 for PERCENTAGE discounts");
|
|
55
|
+
}
|
|
56
|
+
if (input.valueType === "FIXED_AMOUNT" && input.value <= 0) {
|
|
57
|
+
throw new Error("value must be greater than 0 for FIXED_AMOUNT discounts");
|
|
58
|
+
}
|
|
59
|
+
const hasCollectionScope = (input.includeCollectionIds && input.includeCollectionIds.length > 0) ||
|
|
60
|
+
(input.excludeCollectionIds && input.excludeCollectionIds.length > 0);
|
|
61
|
+
const hasProductScope = (input.includeProductIds && input.includeProductIds.length > 0) ||
|
|
62
|
+
(input.excludeProductIds && input.excludeProductIds.length > 0);
|
|
63
|
+
if (hasCollectionScope && hasProductScope) {
|
|
64
|
+
throw new Error("Scope the discount to either collections (includeCollectionIds/excludeCollectionIds) or products (includeProductIds/excludeProductIds), not both");
|
|
65
|
+
}
|
|
66
|
+
const query = gql `
|
|
67
|
+
#graphql
|
|
68
|
+
|
|
69
|
+
mutation discountAutomaticBasicCreate($automaticBasicDiscount: DiscountAutomaticBasicInput!) {
|
|
70
|
+
discountAutomaticBasicCreate(automaticBasicDiscount: $automaticBasicDiscount) {
|
|
71
|
+
automaticDiscountNode {
|
|
72
|
+
id
|
|
73
|
+
automaticDiscount {
|
|
74
|
+
... on DiscountAutomaticBasic {
|
|
75
|
+
title
|
|
76
|
+
startsAt
|
|
77
|
+
endsAt
|
|
78
|
+
status
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
userErrors {
|
|
83
|
+
field
|
|
84
|
+
code
|
|
85
|
+
message
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
`;
|
|
90
|
+
const hasItemScope = hasCollectionScope || hasProductScope;
|
|
91
|
+
const automaticBasicDiscount = {
|
|
92
|
+
title: input.title,
|
|
93
|
+
startsAt: input.startsAt,
|
|
94
|
+
endsAt: input.endsAt,
|
|
95
|
+
minimumRequirement: input.minimumSubtotal
|
|
96
|
+
? {
|
|
97
|
+
subtotal: {
|
|
98
|
+
greaterThanOrEqualToSubtotal: input.minimumSubtotal,
|
|
99
|
+
},
|
|
100
|
+
}
|
|
101
|
+
: undefined,
|
|
102
|
+
customerGets: {
|
|
103
|
+
value: {
|
|
104
|
+
percentage: input.valueType === "PERCENTAGE" ? input.value : undefined,
|
|
105
|
+
discountAmount: input.valueType === "FIXED_AMOUNT"
|
|
106
|
+
? {
|
|
107
|
+
amount: input.value,
|
|
108
|
+
appliesOnEachItem: false,
|
|
109
|
+
}
|
|
110
|
+
: undefined,
|
|
111
|
+
},
|
|
112
|
+
items: {
|
|
113
|
+
all: !hasItemScope,
|
|
114
|
+
collections: hasCollectionScope
|
|
115
|
+
? {
|
|
116
|
+
add: input.includeCollectionIds ?? [],
|
|
117
|
+
remove: input.excludeCollectionIds ?? [],
|
|
118
|
+
}
|
|
119
|
+
: undefined,
|
|
120
|
+
products: hasProductScope
|
|
121
|
+
? {
|
|
122
|
+
productsToAdd: input.includeProductIds ?? [],
|
|
123
|
+
productsToRemove: input.excludeProductIds ?? [],
|
|
124
|
+
}
|
|
125
|
+
: undefined,
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
combinesWith: input.combinesWith,
|
|
129
|
+
};
|
|
130
|
+
const data = (await shopifyClient.request(query, { automaticBasicDiscount }));
|
|
131
|
+
checkUserErrors(data.discountAutomaticBasicCreate.userErrors, "create automatic discount");
|
|
132
|
+
const automaticDiscountNode = data.discountAutomaticBasicCreate.automaticDiscountNode;
|
|
133
|
+
const automaticDiscount = automaticDiscountNode.automaticDiscount;
|
|
134
|
+
return {
|
|
135
|
+
discount: {
|
|
136
|
+
id: automaticDiscountNode.id,
|
|
137
|
+
title: automaticDiscount.title,
|
|
138
|
+
startsAt: automaticDiscount.startsAt,
|
|
139
|
+
endsAt: automaticDiscount.endsAt,
|
|
140
|
+
status: automaticDiscount.status,
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
catch (error) {
|
|
145
|
+
handleToolError("create automatic discount", error);
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
export { createAutomaticDiscount };
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { gql } from "graphql-request";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { checkUserErrors, handleToolError, edgesToNodes } from "../lib/toolUtils.js";
|
|
4
|
+
// Input schema for creating a customer
|
|
5
|
+
const CreateCustomerInputSchema = z.object({
|
|
6
|
+
firstName: z.string().optional(),
|
|
7
|
+
lastName: z.string().optional(),
|
|
8
|
+
email: z.string().email().optional(),
|
|
9
|
+
phone: z.string().optional(),
|
|
10
|
+
tags: z.array(z.string()).optional(),
|
|
11
|
+
note: z.string().optional(),
|
|
12
|
+
taxExempt: z.boolean().optional(),
|
|
13
|
+
metafields: z
|
|
14
|
+
.array(z.object({
|
|
15
|
+
namespace: z.string(),
|
|
16
|
+
key: z.string(),
|
|
17
|
+
value: z.string(),
|
|
18
|
+
type: z.string().optional()
|
|
19
|
+
}))
|
|
20
|
+
.optional(),
|
|
21
|
+
addresses: z
|
|
22
|
+
.array(z.object({
|
|
23
|
+
address1: z.string().optional(),
|
|
24
|
+
address2: z.string().optional(),
|
|
25
|
+
city: z.string().optional(),
|
|
26
|
+
provinceCode: z.string().optional(),
|
|
27
|
+
zip: z.string().optional(),
|
|
28
|
+
countryCode: z.string().optional(),
|
|
29
|
+
phone: z.string().optional()
|
|
30
|
+
}))
|
|
31
|
+
.optional()
|
|
32
|
+
});
|
|
33
|
+
// Will be initialized in index.ts
|
|
34
|
+
let shopifyClient;
|
|
35
|
+
const createCustomer = {
|
|
36
|
+
name: "create-customer",
|
|
37
|
+
description: "Create a new customer",
|
|
38
|
+
schema: CreateCustomerInputSchema,
|
|
39
|
+
// Add initialize method to set up the GraphQL client
|
|
40
|
+
initialize(client) {
|
|
41
|
+
shopifyClient = client;
|
|
42
|
+
},
|
|
43
|
+
execute: async (input) => {
|
|
44
|
+
try {
|
|
45
|
+
const query = gql `
|
|
46
|
+
#graphql
|
|
47
|
+
|
|
48
|
+
mutation customerCreate($input: CustomerInput!) {
|
|
49
|
+
customerCreate(input: $input) {
|
|
50
|
+
customer {
|
|
51
|
+
id
|
|
52
|
+
firstName
|
|
53
|
+
lastName
|
|
54
|
+
defaultEmailAddress {
|
|
55
|
+
emailAddress
|
|
56
|
+
}
|
|
57
|
+
defaultPhoneNumber {
|
|
58
|
+
phoneNumber
|
|
59
|
+
}
|
|
60
|
+
tags
|
|
61
|
+
note
|
|
62
|
+
taxExempt
|
|
63
|
+
createdAt
|
|
64
|
+
updatedAt
|
|
65
|
+
defaultAddress {
|
|
66
|
+
address1
|
|
67
|
+
address2
|
|
68
|
+
city
|
|
69
|
+
provinceCode
|
|
70
|
+
zip
|
|
71
|
+
country
|
|
72
|
+
phone
|
|
73
|
+
}
|
|
74
|
+
addressesV2(first: 10) {
|
|
75
|
+
edges {
|
|
76
|
+
node {
|
|
77
|
+
address1
|
|
78
|
+
address2
|
|
79
|
+
city
|
|
80
|
+
provinceCode
|
|
81
|
+
zip
|
|
82
|
+
country
|
|
83
|
+
phone
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
metafields(first: 10) {
|
|
88
|
+
edges {
|
|
89
|
+
node {
|
|
90
|
+
id
|
|
91
|
+
namespace
|
|
92
|
+
key
|
|
93
|
+
value
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
userErrors {
|
|
99
|
+
field
|
|
100
|
+
message
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
`;
|
|
105
|
+
const variables = {
|
|
106
|
+
input: {
|
|
107
|
+
...input
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
const data = (await shopifyClient.request(query, variables));
|
|
111
|
+
checkUserErrors(data.customerCreate.userErrors, "create customer");
|
|
112
|
+
// Format and return the created customer
|
|
113
|
+
const customer = data.customerCreate.customer;
|
|
114
|
+
// Format metafields if they exist
|
|
115
|
+
const metafields = customer.metafields?.edges.map((edge) => edge.node) || [];
|
|
116
|
+
const addresses = customer.addressesV2
|
|
117
|
+
? edgesToNodes(customer.addressesV2)
|
|
118
|
+
: [];
|
|
119
|
+
return {
|
|
120
|
+
customer: {
|
|
121
|
+
id: customer.id,
|
|
122
|
+
firstName: customer.firstName,
|
|
123
|
+
lastName: customer.lastName,
|
|
124
|
+
email: customer.defaultEmailAddress?.emailAddress || null,
|
|
125
|
+
phone: customer.defaultPhoneNumber?.phoneNumber || null,
|
|
126
|
+
tags: customer.tags,
|
|
127
|
+
note: customer.note,
|
|
128
|
+
taxExempt: customer.taxExempt,
|
|
129
|
+
createdAt: customer.createdAt,
|
|
130
|
+
updatedAt: customer.updatedAt,
|
|
131
|
+
defaultAddress: customer.defaultAddress,
|
|
132
|
+
addresses,
|
|
133
|
+
metafields
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
handleToolError("create customer", error);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
export { createCustomer };
|