Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/change url product when update wishlist #32

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions node/clients/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
}
}
29 changes: 29 additions & 0 deletions node/clients/product.ts
Original file line number Diff line number Diff line change
@@ -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<Product[] | undefined> {
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}`
)
}
}
66 changes: 50 additions & 16 deletions node/resolvers/updateWishlist.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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`,
}
})

Expand All @@ -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,
}
}
19 changes: 16 additions & 3 deletions react/utils/jsonSchema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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] ?? ''
Expand Down Expand Up @@ -204,7 +217,7 @@ export const JsonSchema = ({
return <SkuName itemId={rowData.itemId} productUrl={productUrl} />
}

let jsonschema: IJsonSchema = {
const jsonschema: IJsonSchema = {
properties: {
image: {
title: 'Image',
Expand Down