From b5ef0d17f0f553eed6b001c2ff3c8327f82b1d3a Mon Sep 17 00:00:00 2001 From: Pedro Paiva Date: Fri, 4 Oct 2024 15:39:01 -0300 Subject: [PATCH 1/2] fix: request to save in the wishlistMD the url of the product with the store domain --- node/clients/index.ts | 5 +++ node/clients/product.ts | 29 +++++++++++++++++ node/resolvers/updateWishlist.ts | 54 ++++++++++++++++++++++---------- react/utils/jsonSchema.tsx | 19 +++++++++-- 4 files changed, 88 insertions(+), 19 deletions(-) create mode 100644 node/clients/product.ts diff --git a/node/clients/index.ts b/node/clients/index.ts index 3a68789..ff6babd 100644 --- a/node/clients/index.ts +++ b/node/clients/index.ts @@ -6,6 +6,7 @@ import MasterDataClient from './masterdata' import VtexId from './vtexId' import { DATA_ENTITY_NAME } from '../utils/constant' import RequestHub from '../utils/HUB' +import ProductSearchClient from './product' export class Clients extends IOClients { public get wishlist() { @@ -26,4 +27,8 @@ export class Clients extends IOClients { public get hub() { return this.getOrSet('hub', RequestHub) } + + public get product() { + return this.getOrSet('product', ProductSearchClient) + } } diff --git a/node/clients/product.ts b/node/clients/product.ts new file mode 100644 index 0000000..55ac851 --- /dev/null +++ b/node/clients/product.ts @@ -0,0 +1,29 @@ +import type { InstanceOptions, IOContext } from '@vtex/api' +import { JanusClient } from '@vtex/api' +import { Product } from 'vtex.product-context/react/ProductTypes' + +export default class ProductSearchClient extends JanusClient { + constructor(context: IOContext, options?: InstanceOptions) { + super(context, { + ...options, + headers: { + ...options?.headers, + VtexIdclientAutCookie: context.authToken, + 'Cache-Control': 'no-cache', + 'Content-Type': 'application/json', + }, + }) + } + + public async getProducts( + skuIdArray: string[] + ): Promise { + if (skuIdArray.length === 0) return [] + + const queryString = skuIdArray.map((skuId) => `fq=skuId:${skuId}`).join('&') + + return this.http.get( + `/api/catalog_system/pub/products/search?${queryString}` + ) + } +} diff --git a/node/resolvers/updateWishlist.ts b/node/resolvers/updateWishlist.ts index aa68cfa..ddc602c 100644 --- a/node/resolvers/updateWishlist.ts +++ b/node/resolvers/updateWishlist.ts @@ -14,26 +14,48 @@ export const updateWishlist = async ( const { email } = await auth(ctx) - const { wishlist } = args || {} + const { wishlist: wishlistArgs } = args || {} - if (!wishlist?.id) { + if (!wishlistArgs?.id) { throw new Error('An id must be provided') } - const { email: emailUser, id, wishlistType, isPublic, products } = - (await md.getWishlist(wishlist.id)) || {} + const { + email: emailUser, + id, + wishlistType, + isPublic, + products: productsWishMD, + } = (await md.getWishlist(wishlistArgs.id)) || {} const existWishlist = id - const data = wishlist?.products.map((prod) => { - const d = products?.find((pro) => pro.ID === prod.ID) + const skuIds = wishlistArgs.products.map((prod) => prod.ID).join(',') + + const skuResponse = await ctx.clients.product.getProducts( + skuIds.split(',').map((item) => item.trim()) + ) + + const skusWithLinks = skuResponse?.reduce((acc, sku) => { + acc[sku.productReference] = sku.link + + return acc + }, {} as Record) + + const data = wishlistArgs?.products.map((prodWishArgs) => { + const matchWishProd = productsWishMD?.find( + (prod) => prod.ID === prodWishArgs.ID + ) return { - ...prod, - quantityProduct: prod.quantityProduct - ? prod.quantityProduct - : d?.quantityProduct ?? 1, - notes: prod.notes ? prod.notes : d?.notes, + ...prodWishArgs, + quantityProduct: prodWishArgs.quantityProduct + ? prodWishArgs.quantityProduct + : matchWishProd?.quantityProduct ?? 1, + notes: prodWishArgs.notes ?? matchWishProd?.notes, + linkProduct: skusWithLinks + ? skusWithLinks[prodWishArgs.skuCodeReference] + : `/${prodWishArgs.skuCodeReference}/p`, } }) @@ -47,16 +69,16 @@ export const updateWishlist = async ( const updatedWishlist = { id, - email: wishlist?.email || emailUser, - wishlistType: wishlist?.wishlistType || wishlistType, - isPublic: wishlist?.isPublic || isPublic, + email: wishlistArgs?.email || emailUser, + wishlistType: wishlistArgs?.wishlistType || wishlistType, + isPublic: wishlistArgs?.isPublic || isPublic, products: data as Products[], } - await md.updateWishlist(wishlist.id, updatedWishlist) + await md.updateWishlist(wishlistArgs.id, updatedWishlist) return { - id: wishlist.id, + id: wishlistArgs.id, success: true, } } diff --git a/react/utils/jsonSchema.tsx b/react/utils/jsonSchema.tsx index 6efa17c..b3a3390 100644 --- a/react/utils/jsonSchema.tsx +++ b/react/utils/jsonSchema.tsx @@ -21,6 +21,19 @@ interface CellProps { cellRenderer: (value: any, rowData: any) => JSX.Element // Tipo genérico para a função de renderização } +interface ProductItemRowData { + bundle: string | null + department: string + id: number + image: string + itemId: number + linkProduct: string + name: string + notes: string | null + quantity: number + skuReferenceCode: string +} + interface IJsonSchema { properties: { image: CellProps @@ -36,12 +49,12 @@ interface IJsonSchema { } } -export const getProductPath = (rowData: any) => { +export const getProductPath = (rowData: ProductItemRowData) => { const isFastStore = (window?.location?.hostname ?? '').startsWith('secure') const linkUrl = rowData.linkProduct || '' const parts = linkUrl.split('.br/') - const id = rowData.itemId || rowData.ID + const id = rowData.itemId || rowData.id // eslint-disable-next-line prefer-destructuring const productSlug = parts[parts.length - 1]?.split('/')?.[0] ?? '' @@ -204,7 +217,7 @@ export const JsonSchema = ({ return } - let jsonschema: IJsonSchema = { + const jsonschema: IJsonSchema = { properties: { image: { title: 'Image', From 9bccdb1c4111f636ad4b4eb69a717ff45561824b Mon Sep 17 00:00:00 2001 From: Pedro Paiva Date: Wed, 9 Oct 2024 11:23:55 -0300 Subject: [PATCH 2/2] fix: added function to return the link, searching for the skuId within the product --- node/resolvers/updateWishlist.ts | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/node/resolvers/updateWishlist.ts b/node/resolvers/updateWishlist.ts index ddc602c..e723951 100644 --- a/node/resolvers/updateWishlist.ts +++ b/node/resolvers/updateWishlist.ts @@ -1,4 +1,5 @@ import { AuthenticationError } from '@vtex/api' +import { Product } from 'vtex.product-context/react/ProductTypes' import { auth } from '../middleware/auth' import type { Products, WishlistUpdateArs } from '../typings/wishlist' @@ -36,11 +37,22 @@ export const updateWishlist = async ( skuIds.split(',').map((item) => item.trim()) ) - const skusWithLinks = skuResponse?.reduce((acc, sku) => { - acc[sku.productReference] = sku.link + const findProductLinkBySkuId = ( + products: Product[], + skuId: number + ): string | null => { + for (const product of products) { + const foundItem = product.items.find( + (item) => item.itemId === String(skuId) + ) + + if (foundItem) { + return product.link + } + } - return acc - }, {} as Record) + return null + } const data = wishlistArgs?.products.map((prodWishArgs) => { const matchWishProd = productsWishMD?.find( @@ -53,8 +65,8 @@ export const updateWishlist = async ( ? prodWishArgs.quantityProduct : matchWishProd?.quantityProduct ?? 1, notes: prodWishArgs.notes ?? matchWishProd?.notes, - linkProduct: skusWithLinks - ? skusWithLinks[prodWishArgs.skuCodeReference] + linkProduct: skuResponse + ? findProductLinkBySkuId(skuResponse, prodWishArgs.ID) : `/${prodWishArgs.skuCodeReference}/p`, } })