mirror of
https://github.com/vercel/commerce.git
synced 2025-07-23 04:36:49 +00:00
finish activate warranty step
This commit is contained in:
@@ -11,6 +11,7 @@ import {
|
||||
SHOPIFY_GRAPHQL_CUSTOMER_API_ENDPOINT,
|
||||
TAGS,
|
||||
VARIANT_METAFIELD_PREFIX,
|
||||
WARRANTY_FIELDS,
|
||||
YEAR_FILTER_ID
|
||||
} from 'lib/constants';
|
||||
import { isShopifyError } from 'lib/type-guards';
|
||||
@@ -26,6 +27,7 @@ import {
|
||||
setCartAttributesMutation
|
||||
} from './mutations/cart';
|
||||
import { createFileMutation, createStageUploads } from './mutations/file';
|
||||
import { updateOrderMetafieldsMutation } from './mutations/order';
|
||||
import { getCartQuery } from './queries/cart';
|
||||
import {
|
||||
getCollectionProductsQuery,
|
||||
@@ -57,6 +59,7 @@ import {
|
||||
Image,
|
||||
LineItem,
|
||||
Menu,
|
||||
Metafield,
|
||||
Metaobject,
|
||||
Money,
|
||||
Order,
|
||||
@@ -97,9 +100,11 @@ import {
|
||||
ShopifySetCartAttributesOperation,
|
||||
ShopifyStagedUploadOperation,
|
||||
ShopifyUpdateCartOperation,
|
||||
ShopifyUpdateOrderMetafieldsOperation,
|
||||
Transaction,
|
||||
TransmissionType,
|
||||
UploadInput
|
||||
UploadInput,
|
||||
WarrantyStatus
|
||||
} from './types';
|
||||
|
||||
const domain = process.env.SHOPIFY_STORE_DOMAIN
|
||||
@@ -592,6 +597,7 @@ function reshapeOrder(shopifyOrder: ShopifyOrder): Order {
|
||||
totalPrice: reshapeMoney(item.totalPrice)
|
||||
})) || [];
|
||||
|
||||
console.log(shopifyOrder);
|
||||
const order: Order = {
|
||||
id: shopifyOrder.id,
|
||||
normalizedId: shopifyOrder.id.replace('gid://shopify/Order/', ''),
|
||||
@@ -1072,5 +1078,45 @@ export const createFile = async (params: FileCreateInput) => {
|
||||
variables: { files: [params] }
|
||||
});
|
||||
|
||||
return res.body.data;
|
||||
return res.body.data.fileCreate.files;
|
||||
};
|
||||
|
||||
export const updateOrderMetafields = async ({
|
||||
orderId,
|
||||
metafields
|
||||
}: {
|
||||
orderId: string;
|
||||
metafields: { key: string; value: string | undefined | null; type: string }[];
|
||||
}) => {
|
||||
const validMetafields = (
|
||||
metafields.filter((field) => Boolean(field)) as Array<Omit<Metafield, 'namespace'>>
|
||||
).map((field) => ({
|
||||
...field,
|
||||
namespace: 'custom'
|
||||
}));
|
||||
|
||||
const shouldSetWarrantyStatusToActivated = WARRANTY_FIELDS.every((field) =>
|
||||
validMetafields.find(({ key }) => key === field)
|
||||
);
|
||||
|
||||
const response = await adminFetch<ShopifyUpdateOrderMetafieldsOperation>({
|
||||
query: updateOrderMetafieldsMutation,
|
||||
variables: {
|
||||
input: {
|
||||
metafields: shouldSetWarrantyStatusToActivated
|
||||
? validMetafields.concat([
|
||||
{
|
||||
key: 'warranty_status',
|
||||
value: WarrantyStatus.Activated,
|
||||
namespace: 'custom',
|
||||
type: 'single_line_text_field'
|
||||
}
|
||||
])
|
||||
: validMetafields,
|
||||
id: orderId
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return response.body.data.orderUpdate.order.id;
|
||||
};
|
||||
|
@@ -18,9 +18,7 @@ export const createFileMutation = /* GraphQL */ `
|
||||
fileCreate(files: $files) {
|
||||
files {
|
||||
fileStatus
|
||||
... on MediaImage {
|
||||
id
|
||||
}
|
||||
id
|
||||
}
|
||||
userErrors {
|
||||
field
|
||||
|
13
lib/shopify/mutations/order.ts
Normal file
13
lib/shopify/mutations/order.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
export const updateOrderMetafieldsMutation = /* GraphQL */ `
|
||||
mutation updateOrderMetafields($input: OrderInput!) {
|
||||
orderUpdate(input: $input) {
|
||||
order {
|
||||
id
|
||||
}
|
||||
userErrors {
|
||||
message
|
||||
field
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
@@ -1,3 +1,4 @@
|
||||
/* eslint-disable no-unused-vars */
|
||||
export type Maybe<T> = T | null;
|
||||
|
||||
export type Connection<T> = {
|
||||
@@ -825,3 +826,33 @@ export type ShopifyCreateFileOperation = {
|
||||
};
|
||||
variables: { files: FileCreateInput[] };
|
||||
};
|
||||
|
||||
export type Metafield = {
|
||||
namespace: string;
|
||||
value: string;
|
||||
key: string;
|
||||
type: string;
|
||||
};
|
||||
|
||||
export type ShopifyUpdateOrderMetafieldsOperation = {
|
||||
data: {
|
||||
orderUpdate: {
|
||||
order: {
|
||||
id: string;
|
||||
};
|
||||
userErrors: { field: string; message: string }[];
|
||||
};
|
||||
};
|
||||
variables: {
|
||||
input: {
|
||||
metafields: Metafield[];
|
||||
id: string;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
export enum WarrantyStatus {
|
||||
Activated = 'Activated',
|
||||
NotActivated = 'Not Activated',
|
||||
LimitedActivated = 'Limited Activation'
|
||||
}
|
||||
|
Reference in New Issue
Block a user