finish activate warranty logic

Signed-off-by: Chloe <pinkcloudvnn@gmail.com>
This commit is contained in:
Chloe
2024-06-26 09:44:58 +07:00
parent 2477fdf84e
commit d801de0cf1
13 changed files with 167 additions and 36 deletions

View File

@@ -29,7 +29,8 @@ export const TAGS = {
collections: 'collections',
products: 'products',
cart: 'cart',
pages: 'pages'
pages: 'pages',
orderMetafields: 'orderMetafields'
};
export const HIDDEN_PRODUCT_TAG = 'nextjs-frontend-hidden';

View File

@@ -39,7 +39,7 @@ import { getMenuQuery } from './queries/menu';
import { getMetaobjectQuery, getMetaobjectsQuery } from './queries/metaobject';
import { getImageQuery, getMetaobjectsByIdsQuery } from './queries/node';
import { getCustomerOrderQuery } from './queries/order';
import { getCustomerOrdersQuery } from './queries/orders';
import { getCustomerOrderMetafieldsQuery, getCustomerOrdersQuery } from './queries/orders';
import { getPageQuery, getPagesQuery } from './queries/page';
import {
getProductQuery,
@@ -63,6 +63,7 @@ import {
Metaobject,
Money,
Order,
OrderMetafield,
Page,
PageInfo,
Product,
@@ -88,6 +89,7 @@ import {
ShopifyMetaobjectsOperation,
ShopifyMoneyV2,
ShopifyOrder,
ShopifyOrderMetafield,
ShopifyPage,
ShopifyPageOperation,
ShopifyPagesOperation,
@@ -185,11 +187,13 @@ export async function shopifyFetch<T>({
async function adminFetch<T>({
headers,
query,
variables
variables,
tags
}: {
headers?: HeadersInit;
query: string;
variables?: ExtractVariables<T>;
tags?: string[];
}): Promise<{ status: number; body: T } | never> {
try {
const result = await fetch(adminEndpoint, {
@@ -202,7 +206,9 @@ async function adminFetch<T>({
body: JSON.stringify({
...(query && { query }),
...(variables && { variables })
})
}),
...(tags && { next: { tags } }),
cache: 'no-store'
});
const body = await result.json();
@@ -505,7 +511,8 @@ function reshapeCustomer(customer: ShopifyCustomer): Customer {
firstName: customer.firstName,
lastName: customer.lastName,
displayName: customer.displayName,
emailAddress: customer.emailAddress.emailAddress
emailAddress: customer.emailAddress.emailAddress,
id: customer.id
};
}
@@ -597,7 +604,6 @@ 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/', ''),
@@ -1120,3 +1126,38 @@ export const updateOrderMetafields = async ({
return response.body.data.orderUpdate.order.id;
};
export const getOrdersMetafields = async (): Promise<{ [key: string]: OrderMetafield }> => {
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]: {
warrantyStatus: order.warrantyStatus?.value ?? null,
warrantyActivationDeadline: order.warrantyActivationDeadline?.value ?? null
}
}),
{} as { [key: string]: OrderMetafield }
);
};

View File

@@ -2,6 +2,7 @@
export const getCustomerQuery = /* GraphQL */ `
query customer {
customer {
id
emailAddress {
emailAddress
}

View File

@@ -13,3 +13,24 @@ export const getCustomerOrdersQuery = `#graphql
${customerFragment}
${customerDetailsFragment}
`;
export const getCustomerOrderMetafieldsQuery = /* GraphQL */ `
query getCustomerOrderMetafields($id: ID!) {
customer(id: $id) {
orders(first: 20, sortKey: PROCESSED_AT, reverse: true) {
nodes {
id
warrantyStatus: metafield(namespace: "custom", key: "warranty_status") {
value
}
warrantyActivationDeadline: metafield(
namespace: "custom"
key: "warranty_activation_deadline"
) {
value
}
}
}
}
}
`;

View File

@@ -52,6 +52,7 @@ export type Customer = {
firstName?: string;
lastName?: string;
displayName?: string;
id: string;
};
export type Image = {
@@ -856,3 +857,13 @@ export enum WarrantyStatus {
NotActivated = 'Not Activated',
LimitedActivated = 'Limited Activation'
}
export type ShopifyOrderMetafield = {
warrantyStatus: { value: WarrantyStatus } | null;
warrantyActivationDeadline: { value: string } | null;
};
export type OrderMetafield = {
warrantyStatus: WarrantyStatus | null;
warrantyActivationDeadline: string | null;
};

View File

@@ -19,7 +19,6 @@ export const validateEnvironmentVariables = () => {
'SHOPIFY_STOREFRONT_ACCESS_TOKEN',
'SHOPIFY_CUSTOMER_ACCOUNT_API_CLIENT_ID',
'SHOPIFY_CUSTOMER_ACCOUNT_API_URL',
'SHOPIFY_CUSTOMER_API_VERSION',
'SHOPIFY_ORIGIN_URL',
'SHOPIFY_ADMIN_API_ACCESS_TOKEN'
];
@@ -96,3 +95,14 @@ export function toPrintDate(date: string) {
day: 'numeric'
});
}
export const isBeforeToday = (date?: string | null) => {
if (!date) return false;
const today = new Date();
const compareDate = new Date(date);
today.setHours(0, 0, 0, 0);
compareDate.setHours(0, 0, 0, 0);
return compareDate <= today;
};