mirror of
https://github.com/vercel/commerce.git
synced 2025-07-23 04:36:49 +00:00
Order Confirmation
This commit is contained in:
@@ -37,9 +37,8 @@ import {
|
||||
import { getCustomerQuery } from './queries/customer';
|
||||
import { getMenuQuery } from './queries/menu';
|
||||
import { getMetaobjectQuery, getMetaobjectsQuery } from './queries/metaobject';
|
||||
import { getFileQuery, getImageQuery, getMetaobjectsByIdsQuery } from './queries/node';
|
||||
import { getCustomerOrderQuery, getOrderMetafieldsQuery } from './queries/order';
|
||||
import { getCustomerOrderMetafieldsQuery, getCustomerOrdersQuery } from './queries/orders';
|
||||
import { getImageQuery, getMetaobjectsByIdsQuery } from './queries/node';
|
||||
import { getCustomerOrdersQuery } from './queries/orders';
|
||||
import { getPageQuery, getPagesQuery } from './queries/page';
|
||||
import {
|
||||
getProductQuery,
|
||||
@@ -53,7 +52,6 @@ import {
|
||||
Collection,
|
||||
Connection,
|
||||
Customer,
|
||||
File,
|
||||
FileCreateInput,
|
||||
Filter,
|
||||
Fulfillment,
|
||||
@@ -64,6 +62,7 @@ import {
|
||||
Metaobject,
|
||||
Money,
|
||||
Order,
|
||||
OrderConfirmationContent,
|
||||
Page,
|
||||
PageInfo,
|
||||
Product,
|
||||
@@ -86,10 +85,10 @@ import {
|
||||
ShopifyImageOperation,
|
||||
ShopifyMenuOperation,
|
||||
ShopifyMetaobject,
|
||||
ShopifyMetaobjectOperation,
|
||||
ShopifyMetaobjectsOperation,
|
||||
ShopifyMoneyV2,
|
||||
ShopifyOrder,
|
||||
ShopifyOrderMetafield,
|
||||
ShopifyPage,
|
||||
ShopifyPageOperation,
|
||||
ShopifyPagesOperation,
|
||||
@@ -109,6 +108,7 @@ import {
|
||||
UploadInput,
|
||||
WarrantyStatus
|
||||
} from './types';
|
||||
import getCustomerOrderQuery from './queries/order';
|
||||
|
||||
const domain = process.env.SHOPIFY_STORE_DOMAIN
|
||||
? ensureStartsWith(process.env.SHOPIFY_STORE_DOMAIN, 'https://')
|
||||
@@ -185,7 +185,7 @@ export async function shopifyFetch<T>({
|
||||
}
|
||||
}
|
||||
|
||||
async function adminFetch<T>({
|
||||
async function shopifyAdminFetch<T>({
|
||||
headers,
|
||||
query,
|
||||
variables,
|
||||
@@ -531,8 +531,7 @@ function reshapeOrders(orders: ShopifyOrder[]): any[] | Promise<Order[]> {
|
||||
}
|
||||
|
||||
function reshapeOrder(shopifyOrder: ShopifyOrder): Order {
|
||||
const reshapeAddress = (address?: ShopifyAddress): Address | undefined => {
|
||||
if (!address) return undefined;
|
||||
const reshapeAddress = (address: ShopifyAddress): Address => {
|
||||
return {
|
||||
address1: address.address1,
|
||||
address2: address.address2,
|
||||
@@ -547,8 +546,7 @@ function reshapeOrder(shopifyOrder: ShopifyOrder): Order {
|
||||
};
|
||||
};
|
||||
|
||||
const reshapeMoney = (money?: ShopifyMoneyV2): Money | undefined => {
|
||||
if (!money) return undefined;
|
||||
const reshapeMoney = (money: ShopifyMoneyV2): Money => {
|
||||
return {
|
||||
amount: money.amount || '0.00',
|
||||
currencyCode: money.currencyCode || 'USD'
|
||||
@@ -619,23 +617,42 @@ function reshapeOrder(shopifyOrder: ShopifyOrder): Order {
|
||||
totalShipping: reshapeMoney(shopifyOrder.totalShipping),
|
||||
totalTax: reshapeMoney(shopifyOrder.totalTax),
|
||||
totalPrice: reshapeMoney(shopifyOrder.totalPrice),
|
||||
createdAt: shopifyOrder.createdAt
|
||||
createdAt: shopifyOrder.createdAt,
|
||||
shippingMethod: {
|
||||
name: shopifyOrder.shippingLine?.title,
|
||||
price: reshapeMoney(shopifyOrder.shippingLine.originalPrice)!
|
||||
}
|
||||
};
|
||||
|
||||
if (shopifyOrder.customer) {
|
||||
order.customer = reshapeCustomer(shopifyOrder.customer);
|
||||
}
|
||||
|
||||
if (shopifyOrder.shippingLine) {
|
||||
order.shippingMethod = {
|
||||
name: shopifyOrder.shippingLine?.title,
|
||||
price: reshapeMoney(shopifyOrder.shippingLine.originalPrice)!
|
||||
};
|
||||
if (shopifyOrder.warrantyStatus) {
|
||||
order.warrantyStatus = shopifyOrder.warrantyStatus.value as WarrantyStatus;
|
||||
}
|
||||
|
||||
if (shopifyOrder.warrantyActivationDeadline) {
|
||||
order.warrantyActivationDeadline = new Date(shopifyOrder.warrantyActivationDeadline.value);
|
||||
}
|
||||
|
||||
if (shopifyOrder.orderConfirmation) {
|
||||
order.orderConfirmation = shopifyOrder.orderConfirmation.value;
|
||||
}
|
||||
|
||||
return order;
|
||||
}
|
||||
|
||||
export function reshapeOrderConfirmationPdf(
|
||||
metaobject: ShopifyMetaobject
|
||||
): OrderConfirmationContent {
|
||||
return {
|
||||
body: metaobject.fields.find((field) => field.key === 'body')?.value || '',
|
||||
logo: metaobject.fields.find((field) => field.key === 'logo')?.reference.image!,
|
||||
color: metaobject.fields.find((field) => field.key === 'color')?.value || '#000000'
|
||||
};
|
||||
}
|
||||
|
||||
export async function createCart(): Promise<Cart> {
|
||||
const res = await shopifyFetch<ShopifyCreateCartOperation>({
|
||||
query: createCartMutation,
|
||||
@@ -895,10 +912,7 @@ export async function getMetaobject({
|
||||
id?: string;
|
||||
handle?: { handle: string; type: string };
|
||||
}) {
|
||||
const res = await shopifyFetch<{
|
||||
data: { metaobject: ShopifyMetaobject };
|
||||
variables: { id?: string; handle?: { handle: string; type: string } };
|
||||
}>({
|
||||
const res = await shopifyFetch<ShopifyMetaobjectOperation>({
|
||||
query: getMetaobjectQuery,
|
||||
variables: { id, handle }
|
||||
});
|
||||
@@ -906,6 +920,15 @@ export async function getMetaobject({
|
||||
return res.body.data.metaobject ? reshapeMetaobjects([res.body.data.metaobject])[0] : null;
|
||||
}
|
||||
|
||||
export async function getOrderConfirmationContent(): Promise<OrderConfirmationContent> {
|
||||
const res = await shopifyFetch<ShopifyMetaobjectOperation>({
|
||||
query: getMetaobjectQuery,
|
||||
variables: { handle: { handle: 'order-confirmation-pdf', type: 'order_confirmation_pdf' } }
|
||||
});
|
||||
|
||||
return reshapeOrderConfirmationPdf(res.body.data.metaobject);
|
||||
}
|
||||
|
||||
export async function getPage(handle: string): Promise<Page> {
|
||||
const res = await shopifyFetch<ShopifyPageOperation>({
|
||||
query: getPageQuery,
|
||||
@@ -1064,7 +1087,7 @@ export const getImage = async (id: string): Promise<Image> => {
|
||||
};
|
||||
|
||||
export const stageUploadFile = async (params: UploadInput) => {
|
||||
const res = await adminFetch<ShopifyStagedUploadOperation>({
|
||||
const res = await shopifyAdminFetch<ShopifyStagedUploadOperation>({
|
||||
query: createStageUploads,
|
||||
variables: { input: [params] }
|
||||
});
|
||||
@@ -1080,7 +1103,7 @@ export const uploadFile = async ({ url, formData }: { url: string; formData: For
|
||||
};
|
||||
|
||||
export const createFile = async (params: FileCreateInput) => {
|
||||
const res = await adminFetch<ShopifyCreateFileOperation>({
|
||||
const res = await shopifyAdminFetch<ShopifyCreateFileOperation>({
|
||||
query: createFileMutation,
|
||||
variables: { files: [params] }
|
||||
});
|
||||
@@ -1103,7 +1126,7 @@ export const updateOrderMetafields = async ({
|
||||
validMetafields.find(({ key }) => (Array.isArray(field) ? field.includes(key) : key === field))
|
||||
);
|
||||
|
||||
const response = await adminFetch<ShopifyUpdateOrderMetafieldsOperation>({
|
||||
const response = await shopifyAdminFetch<ShopifyUpdateOrderMetafieldsOperation>({
|
||||
query: updateOrderMetafieldsMutation,
|
||||
variables: {
|
||||
input: {
|
||||
@@ -1124,72 +1147,3 @@ export const updateOrderMetafields = async ({
|
||||
|
||||
return response.body.data.orderUpdate.order.id;
|
||||
};
|
||||
|
||||
export const getOrdersMetafields = async (): Promise<{ [key: string]: ShopifyOrderMetafield }> => {
|
||||
const customer = await getCustomer();
|
||||
const res = await adminFetch<{
|
||||
data: {
|
||||
customer: {
|
||||
orders: {
|
||||
nodes: Array<
|
||||
{
|
||||
id: string;
|
||||
} & ShopifyOrderMetafield
|
||||
>;
|
||||
};
|
||||
};
|
||||
};
|
||||
variables: {
|
||||
id: string;
|
||||
};
|
||||
}>({
|
||||
query: getCustomerOrderMetafieldsQuery,
|
||||
variables: { id: customer.id },
|
||||
tags: [TAGS.orderMetafields]
|
||||
});
|
||||
|
||||
return res.body.data.customer.orders.nodes.reduce(
|
||||
(acc, order) => ({
|
||||
...acc,
|
||||
[order.id]: order
|
||||
}),
|
||||
{} as { [key: string]: ShopifyOrderMetafield }
|
||||
);
|
||||
};
|
||||
|
||||
export const getOrderMetafields = async (orderId: string): Promise<ShopifyOrderMetafield> => {
|
||||
const res = await adminFetch<{
|
||||
data: {
|
||||
order: {
|
||||
id: string;
|
||||
} & ShopifyOrderMetafield;
|
||||
};
|
||||
variables: {
|
||||
id: string;
|
||||
};
|
||||
}>({
|
||||
query: getOrderMetafieldsQuery,
|
||||
variables: { id: `gid://shopify/Order/${orderId}` },
|
||||
tags: [TAGS.orderMetafields]
|
||||
});
|
||||
|
||||
const order = res.body.data.order;
|
||||
|
||||
return order;
|
||||
};
|
||||
|
||||
export const getFile = async (id: string) => {
|
||||
const res = await shopifyFetch<{
|
||||
data: {
|
||||
node: File;
|
||||
};
|
||||
variables: {
|
||||
id: string;
|
||||
};
|
||||
}>({
|
||||
query: getFileQuery,
|
||||
variables: { id }
|
||||
});
|
||||
|
||||
return res.body.data.node;
|
||||
};
|
||||
|
Reference in New Issue
Block a user