From fade459a753f999cf21fe486256e1126c6701351 Mon Sep 17 00:00:00 2001 From: vijayakumarv-trika <77378833+vijayakumarv-trika@users.noreply.github.com> Date: Tue, 3 Aug 2021 12:15:28 +0530 Subject: [PATCH] Products intigrated with normalize method --- .../api/operations/get-all-products.ts | 9 +- framework/elasticpath/next.config.js | 2 +- framework/elasticpath/utils/normalize.js | 85 +++++++++++++++++++ 3 files changed, 92 insertions(+), 4 deletions(-) create mode 100644 framework/elasticpath/utils/normalize.js diff --git a/framework/elasticpath/api/operations/get-all-products.ts b/framework/elasticpath/api/operations/get-all-products.ts index cd3c01efe..7c6cd8458 100644 --- a/framework/elasticpath/api/operations/get-all-products.ts +++ b/framework/elasticpath/api/operations/get-all-products.ts @@ -4,6 +4,9 @@ import type { OperationContext } from '@commerce/api/operations' import type { ElasticpathConfig, Provider } from '../index' import { gateway as MoltinGateway } from '@moltin/sdk' import data from '../../data.json' +import normalizeProduct from '../../utils/normalize' +import { debug } from 'console' +import { connect } from 'http2' const Moltin = MoltinGateway({ client_id: process.env.NEXT_PUBLIC_ELASTICPATH_CLIENTID @@ -22,11 +25,11 @@ export default function getAllProductsOperation({ config?: Partial preview?: boolean } = {}): Promise<{ products: Product[] | any[] }> { - //elastic path get all products + // elastic path get all products let products = await Moltin.Products.Limit(200).All(); - console.log("All products", products); + let normalizeProducts = await normalizeProduct(products.data) return { - products: data.products, + products: normalizeProducts, } } return getAllProducts diff --git a/framework/elasticpath/next.config.js b/framework/elasticpath/next.config.js index eb0d034cd..9b928414b 100644 --- a/framework/elasticpath/next.config.js +++ b/framework/elasticpath/next.config.js @@ -3,7 +3,7 @@ const commerce = require('./commerce.config.json') module.exports = { commerce, images: { - domains: ['localhost'], + domains: ['s3-eu-west-1.amazonaws.com'], }, webpack: (config, { isServer }) => { if (!isServer) { diff --git a/framework/elasticpath/utils/normalize.js b/framework/elasticpath/utils/normalize.js new file mode 100644 index 000000000..e15b9f017 --- /dev/null +++ b/framework/elasticpath/utils/normalize.js @@ -0,0 +1,85 @@ +import { + gateway as MoltinGateway +} from '@moltin/sdk' +const Moltin = MoltinGateway({ + client_id: process.env.NEXT_PUBLIC_ELASTICPATH_CLIENTID +}) + +const normalizeProduct = async(products) => { + let normalizeProducts = [] + + const productImageGet = async(fileId) => { + try { + let apiImage = await Moltin.Files.Get(fileId); + return apiImage; + } catch (error) { + + } + } + + const normalizeProductImages = async(productId) => { + let fileId; + if (productId.relationships.hasOwnProperty("main_image")) { + fileId = productId.relationships.main_image.data.id; + let productImageObject = await productImageGet(fileId); + return productImageObject.data.link.href; + } + return ''; + } + + const normalizeProductVariants = (productVariants) => { + return ''; + } + + for (let index in products) { + let product = products[index]; + + normalizeProducts.push({ + "id": product.hasOwnProperty("id") ? product.id : null, + "name": product.hasOwnProperty("name") ? product.name : null, + "vendor": "trika", + "path": product.hasOwnProperty("name") ? "/" + product.name : null, + "slug": `${product.hasOwnProperty("slug") ? product.slug:null}`, + "price": { + "value": product.hasOwnProperty("price") ? product.price[0].hasOwnProperty("amount") ? product.price[0].amount : null : null, + "currencyCode": product.hasOwnProperty("price") ? product.price[0].hasOwnProperty("currency") ? product.price[0].currency : null : null + }, + "descriptionHtml": product.hasOwnProperty("description") ? product.description : null, + "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" + } + ] + } + ] + }) + } + return normalizeProducts; +} + +export default normalizeProduct; \ No newline at end of file