60 lines
1.5 KiB
TypeScript
60 lines
1.5 KiB
TypeScript
|
|
import { extractLegacyId, normalizeOrderId } from "./activity.server";
|
||
|
|
import type { OrderDetails } from "./activity.server";
|
||
|
|
|
||
|
|
type AdminGraphqlClient = {
|
||
|
|
graphql: (query: string, options?: { variables?: Record<string, unknown> }) => Promise<Response>;
|
||
|
|
};
|
||
|
|
|
||
|
|
const ORDER_QUERY = `#graphql
|
||
|
|
query DreameOrder($id: ID!) {
|
||
|
|
order: node(id: $id) {
|
||
|
|
... on Order {
|
||
|
|
id
|
||
|
|
name
|
||
|
|
email
|
||
|
|
currencyCode
|
||
|
|
customer {
|
||
|
|
id
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
shop {
|
||
|
|
myshopifyDomain
|
||
|
|
}
|
||
|
|
}
|
||
|
|
`;
|
||
|
|
|
||
|
|
export async function getOrderDetails(admin: AdminGraphqlClient, orderId: string): Promise<OrderDetails> {
|
||
|
|
const normalizedOrderId = normalizeOrderId(orderId);
|
||
|
|
const response = await admin.graphql(ORDER_QUERY, { variables: { id: normalizedOrderId } });
|
||
|
|
const payload = (await response.json()) as {
|
||
|
|
data?: {
|
||
|
|
order?: {
|
||
|
|
id: string;
|
||
|
|
name?: string;
|
||
|
|
email?: string;
|
||
|
|
currencyCode?: string;
|
||
|
|
customer?: { id?: string } | null;
|
||
|
|
} | null;
|
||
|
|
shop?: {
|
||
|
|
myshopifyDomain?: string;
|
||
|
|
};
|
||
|
|
};
|
||
|
|
};
|
||
|
|
|
||
|
|
const order = payload.data?.order;
|
||
|
|
if (!order) {
|
||
|
|
throw new Error(`Order not found: ${normalizedOrderId}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
return {
|
||
|
|
id: order.id,
|
||
|
|
legacyId: extractLegacyId(order.id),
|
||
|
|
orderNumber: order.name,
|
||
|
|
email: order.email || undefined,
|
||
|
|
customerId: order.customer?.id ? extractLegacyId(order.customer.id) : undefined,
|
||
|
|
currency: order.currencyCode || "",
|
||
|
|
shopDomain: payload.data?.shop?.myshopifyDomain || "",
|
||
|
|
};
|
||
|
|
}
|