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..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' @@ -14,26 +15,59 @@ 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 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 null + } + + 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: skuResponse + ? findProductLinkBySkuId(skuResponse, prodWishArgs.ID) + : `/${prodWishArgs.skuCodeReference}/p`, } }) @@ -47,16 +81,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',