From d785f3c0ef4decaea89a42b2fa1d04cb52889431 Mon Sep 17 00:00:00 2001 From: GunaTrika Date: Fri, 17 Sep 2021 15:14:38 +0530 Subject: [PATCH] New epcc client - Extended motlin sdk and corrected pcm ur - Using new client for below sections - Get all products - Get product - Get all products path - Login & Signup - Customers - Changing product normalizer for pcm products --- .../api/operations/get-all-product-paths.ts | 15 ++- .../api/operations/get-all-products.ts | 8 +- .../elasticpath/api/operations/get-product.ts | 7 +- framework/elasticpath/auth/use-login.tsx | 12 +-- framework/elasticpath/auth/use-signup.tsx | 19 ++-- .../elasticpath/customer/use-customer.tsx | 16 ++-- framework/elasticpath/utils/ep-client.ts | 9 ++ framework/elasticpath/utils/normalize.js | 96 +++++++------------ 8 files changed, 77 insertions(+), 105 deletions(-) create mode 100644 framework/elasticpath/utils/ep-client.ts diff --git a/framework/elasticpath/api/operations/get-all-product-paths.ts b/framework/elasticpath/api/operations/get-all-product-paths.ts index 1252b3ba6..e23f64871 100644 --- a/framework/elasticpath/api/operations/get-all-product-paths.ts +++ b/framework/elasticpath/api/operations/get-all-product-paths.ts @@ -1,10 +1,7 @@ import data from '../../data.json' -import { gateway as MoltinGateway } from '@moltin/sdk' import normalizeProduct from '../../utils/normalize' +import epClient from '../../utils/ep-client' -const Moltin = MoltinGateway({ - client_id: process.env.NEXT_PUBLIC_ELASTICPATH_CLIENTID -}) export type GetAllProductPathsResult = { products: Array<{ path: string }> @@ -12,11 +9,13 @@ export type GetAllProductPathsResult = { export default function getAllProductPathsOperation() { async function getAllProductPaths(): Promise { - let products = await Moltin.Products.Limit(200).All(); - let normalizeProducts = await normalizeProduct(products.data) - let productPaths = normalizeProducts.map(({ path }) => ({ path })); + let products = await epClient.PCM.Limit(200).All(); + let paths = []; + for (let product of products.data) { + paths.push({path: "/"+product.attributes.slug}); + } return await Promise.resolve({ - products: productPaths + products: paths }) } diff --git a/framework/elasticpath/api/operations/get-all-products.ts b/framework/elasticpath/api/operations/get-all-products.ts index 08c82458d..6efbb526b 100644 --- a/framework/elasticpath/api/operations/get-all-products.ts +++ b/framework/elasticpath/api/operations/get-all-products.ts @@ -2,12 +2,8 @@ import { Product } from '@commerce/types/product' import { GetAllProductsOperation } from '@commerce/types/product' import type { OperationContext } from '@commerce/api/operations' import type { ElasticpathConfig, Provider } from '../index' -import { gateway as MoltinGateway } from '@moltin/sdk' import normalizeProduct from '../../utils/normalize' - -const Moltin = MoltinGateway({ - client_id: process.env.NEXT_PUBLIC_ELASTICPATH_CLIENTID -}) +import epClient from '../../utils/ep-client' export default function getAllProductsOperation({ commerce, @@ -23,7 +19,7 @@ export default function getAllProductsOperation({ preview?: boolean } = {}): Promise<{ products: Product[] | any[] }> { // elastic path get all products - let products = await Moltin.Products.Limit(200).All(); + let products = await epClient.PCM.Limit(200).All(); let normalizeProducts = await normalizeProduct(products.data) return { products: normalizeProducts, diff --git a/framework/elasticpath/api/operations/get-product.ts b/framework/elasticpath/api/operations/get-product.ts index 37bb28a45..f611d7355 100644 --- a/framework/elasticpath/api/operations/get-product.ts +++ b/framework/elasticpath/api/operations/get-product.ts @@ -3,12 +3,9 @@ import { Product } from '@commerce/types/product' import { GetProductOperation } from '@commerce/types/product' import data from '../../data.json' import type { OperationContext } from '@commerce/api/operations' -import { gateway as MoltinGateway } from '@moltin/sdk' import normalizeProduct from '../../utils/normalize' +import epClient from '../../utils/ep-client' -const Moltin = MoltinGateway({ - client_id: process.env.NEXT_PUBLIC_ELASTICPATH_CLIENTID -}) export default function getProductOperation({ commerce, @@ -29,7 +26,7 @@ export default function getProductOperation({ } else { variablesS = variables.slug; } - let products = await Moltin.Products.Filter({ + let products = await epClient.PCM.Filter({ eq: { slug: variablesS } diff --git a/framework/elasticpath/auth/use-login.tsx b/framework/elasticpath/auth/use-login.tsx index ed224f9e1..aa21f3dfe 100644 --- a/framework/elasticpath/auth/use-login.tsx +++ b/framework/elasticpath/auth/use-login.tsx @@ -4,13 +4,13 @@ import { CommerceError } from '@commerce/utils/errors' import useLogin, { UseLogin } from '@commerce/auth/use-login' import type { LoginHook } from '../types/login' import useCustomer from '../customer/use-customer' +import epClient from '../utils/ep-client' export default useLogin as UseLogin -export const handler: MutationHook = { +export const handler: MutationHook = { fetchOptions: { - url: '/api/login', - method: 'POST', + query: '' }, async fetcher({ input: { email, password }, options, fetch }) { if (!(email && password)) { @@ -20,10 +20,8 @@ export const handler: MutationHook = { }) } - return fetch({ - ...options, - variables: { email, password }, - }); + let token = await epClient.Customers.TokenViaPassword(email, password); + return token || null; }, useHook: ({ fetch }) => () => { const { revalidate } = useCustomer() diff --git a/framework/elasticpath/auth/use-signup.tsx b/framework/elasticpath/auth/use-signup.tsx index cb423ceb0..5b4b68244 100644 --- a/framework/elasticpath/auth/use-signup.tsx +++ b/framework/elasticpath/auth/use-signup.tsx @@ -3,13 +3,13 @@ import useCustomer from '../customer/use-customer' import { MutationHook } from '@commerce/utils/types' import { CommerceError } from '@commerce/utils/errors' import useSignup, { UseSignup } from '@commerce/auth/use-signup' +import epClient from '../utils/ep-client' export default useSignup as UseSignup export const handler: MutationHook = { fetchOptions: { - url: '/api/signup', - method: 'POST', + query: '', }, async fetcher({ input: { firstName, lastName, email, password }, options, fetch }) { console.log("input", firstName) @@ -20,15 +20,12 @@ export const handler: MutationHook = { }) } - return fetch({ - ...options, - variables: { - firstName, - lastName, - email, - password - }, - }); + const data = await epClient.Customers.Create({ + email, + type: "customer", + name: `${firstName} ${lastName}`, + password }); + return data || null; }, useHook: ({ fetch }) => () => { const { revalidate } = useCustomer() diff --git a/framework/elasticpath/customer/use-customer.tsx b/framework/elasticpath/customer/use-customer.tsx index e6bb1b3bc..e5aaa68bb 100644 --- a/framework/elasticpath/customer/use-customer.tsx +++ b/framework/elasticpath/customer/use-customer.tsx @@ -1,11 +1,7 @@ import { SWRHook } from '@commerce/utils/types' import useCustomer, { UseCustomer } from '@commerce/customer/use-customer' -import { gateway as MoltinGateway } from '@moltin/sdk' import getCustomerCookie from '../utils/get-customer-creds' - -const Moltin = MoltinGateway({ - client_id: process.env.NEXT_PUBLIC_ELASTICPATH_CLIENTID -}) +import epClient from '../utils/ep-client' export default useCustomer as UseCustomer export const handler: SWRHook = { @@ -19,8 +15,14 @@ export const handler: SWRHook = { if(!creds) { return null; } - const data = await Moltin.Customers.Get(creds.customer_id, creds.token); - return data || null; + console.log('moltin sdk', epClient); + const {data:customer} = await epClient.Customers.Get(creds.customer_id, creds.token); + + return { + ...customer, + firstName: customer.name.split(" ")[0], + lastName: customer.name.split(" ")[1] + } }, useHook: ({ useData }) => (input) => { return useData({ diff --git a/framework/elasticpath/utils/ep-client.ts b/framework/elasticpath/utils/ep-client.ts new file mode 100644 index 000000000..74610c054 --- /dev/null +++ b/framework/elasticpath/utils/ep-client.ts @@ -0,0 +1,9 @@ +import { gateway as MoltinGateway, Moltin } from '@moltin/sdk'; + +let client:any = MoltinGateway({ + client_id: process.env.NEXT_PUBLIC_ELASTICPATH_CLIENTID +}); + +client.PCM.endpoint = 'catalog/products'; + +export default client; \ No newline at end of file diff --git a/framework/elasticpath/utils/normalize.js b/framework/elasticpath/utils/normalize.js index 9bd137ca8..31cda0f01 100644 --- a/framework/elasticpath/utils/normalize.js +++ b/framework/elasticpath/utils/normalize.js @@ -5,6 +5,7 @@ const Moltin = MoltinGateway({ client_id: process.env.NEXT_PUBLIC_ELASTICPATH_CLIENTID }) + const normalizeProduct = async(products) => { let normalizeProducts = [] @@ -13,86 +14,59 @@ const normalizeProduct = async(products) => { let apiImage = await Moltin.Files.Get(fileId); return apiImage; } catch (error) { - + console.error(fileId, error); } } + const getPrices = (prices) => { + + if(!prices) { + return [{ + "value": 0, + "currencyCode": 'USD' + }]; + } + + let allPrices = [] + for(let key in prices) { + allPrices.push({ + "value": prices[key].amount/100, + "currencyCode": key + }) + } + return allPrices + } + const normalizeProductImages = async(productId) => { - let fileId; - if (productId.relationships.hasOwnProperty("main_image")) { - fileId = productId.relationships.main_image.data.id; + let fileId = productId.relationships?.files?.data[0]?.id; + if (fileId) { let productImageObject = await productImageGet(fileId); - return productImageObject.data.link.href; + return productImageObject?.data?.link?.href || '/assets/lightweight-jacket-0.png'; } return ''; } - const normalizeProductVariants = (productVariants) => { - return [ - { - "id": "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0LzU0NDczMjUwMjQ0MjAss=", - "options": [ - { - "__typename": "MultipleChoiceOption", - "id": "asd", - "displayName": "Size", - "values": [ - { - "label": "XL" - } - ] - } - ] - } - ]; - } - for (let index in products) { let product = products[index]; - + normalizeProducts.push({ - "id": product.hasOwnProperty("id") ? product.id : '', - "name": product.hasOwnProperty("name") ? product.name : '', + "id": product.id, + "name": product.attributes?.name, "vendor": "trika", - "path": product.hasOwnProperty("slug") ? "/" + product.slug : '', - "slug": `${product.hasOwnProperty("slug") ? product.slug:''}`, - "price": { - "value": product.hasOwnProperty("price") ? product.price[0].hasOwnProperty("amount") ? product.price[0].amount : '' : '', - "currencyCode": product.hasOwnProperty("price") ? product.price[0].hasOwnProperty("currency") ? product.price[0].currency : '' : '' - }, - "descriptionHtml": product.hasOwnProperty("description") ? product.description : null, + "path": "/"+product.attributes?.slug, + "slug": product.attributes?.slug, + "price": getPrices(product.attributes?.price)[0], + "descriptionHtml": product.attributes?.description, "images": [{ "url": await normalizeProductImages(product), "altText": "Shirt", "width": 1000, "height": 1000 }], - "variants": normalizeProductVariants(product), - "options": [{ - "id": "option-color", - "displayName": "Color", - "values": [{ - "label": "color", - "hexColors": [ - "#222" - ] - }] - }, - { - "id": "option-size", - "displayName": "Size", - "values": [{ - "label": "S" - }, - { - "label": "M" - }, - { - "label": "L" - } - ] - } - ] + "variants": [{ + "options": [] + }], + "options": [] }) } return normalizeProducts;