Last active
January 10, 2022 16:51
-
-
Save brenogcota/39c182494b17c0f0ee5a16b64be743d9 to your computer and use it in GitHub Desktop.
Linx helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** GET KEYS REGISTERED IN THE PANEL */ | |
const getAppConfig = async (app: string) => { | |
try{ | |
return (await axios({ | |
url: `/_v/${app}`, | |
headers:{ | |
'Content-Type': 'application/json', | |
Accept: 'application/json', | |
} | |
})).data; | |
}catch{ | |
setTimeout(function(){ | |
return getAppConfig(); | |
}, 500); | |
} | |
} | |
/* Normalize linx response - recommendations */ | |
const normalizeProduct = (product: IProductsB) => { | |
if( product ){ | |
const normalizeProduct: any = {}; | |
const skuList = product.skus[0]; | |
normalizeProduct.link = product.url; | |
normalizeProduct.linkText = product.url.split('/')[1]; | |
normalizeProduct.trackingUrl = product.trackingUrl; | |
let Installments = []; | |
if(skuList && skuList.details?.sellers[0] && skuList.details?.sellers[0].installment){ | |
Installments.push({ | |
NumberOfInstallments: skuList.details?.sellers[0].installment.count, | |
TotalValuePlusInterestRate: skuList.details?.sellers[0].installment.price, | |
Value: skuList.details?.sellers[0].installment.price, | |
}) | |
} | |
let detailsQuantity = product.details?.availableQuantity ? product.details.availableQuantity : 10 | |
let skuDetailsQuantity = skuList.specs?.availableQuantity ? skuList.specs.availableQuantity[0] : 10 | |
const commertialOffer = { | |
AvailableQuantity: detailsQuantity ? detailsQuantity : (skuDetailsQuantity ? skuDetailsQuantity : 10 ), | |
Price: skuList.details?.sellers[0].price, | |
ListPrice: skuList.details?.sellers[0].oldPrice, | |
Installments, | |
discountHighlights: [], | |
teasers: product.details.teasers?.length > 0? [{ name : product.details.teasers[0] }] : [] | |
} | |
const image = { | |
imageUrl: product.images.default | |
} | |
const images = [ | |
image | |
] | |
const listPrice = { | |
highPrice: skuList.details?.sellers[0].oldPrice, | |
lowPrice: skuList.details?.sellers[0].price | |
} | |
const priceRange = { | |
listPrice, | |
sellingPrice: listPrice, | |
} | |
normalizeProduct.priceRange = priceRange; | |
normalizeProduct.sku = { | |
...skuList, | |
itemId: skuList.sku, | |
seller: { | |
...skuList.details?.sellers[0], | |
commertialOffer | |
}, | |
sellers: [ | |
{ | |
...skuList.details?.sellers[0], | |
commertialOffer, | |
sellerId: skuList.details?.sellers[0].sellerId | |
} | |
], | |
image, | |
images, | |
priceRange, | |
} | |
normalizeProduct.productId = product.id; | |
normalizeProduct.productName = product.name; | |
normalizeProduct.items = [ | |
normalizeProduct.sku | |
] | |
return normalizeProduct; | |
} | |
} | |
/* Normalize linx response - Search */ | |
function handleNormalize(product: IProducts){ | |
let normalizedProduct = {} as IProductSummaryVtex; | |
let detailsQuantity = product.details?.availableQuantity ? product.details.availableQuantity[0] : 10 | |
let skuDetailsQuantity = product.skus[0].specs?.availableQuantity ? product.skus[0].specs.availableQuantity[0] : 10 | |
let isAvailable = product.status.toUpperCase() == 'AVAILABLE'; | |
detailsQuantity = detailsQuantity == 0 && isAvailable ? 10 : detailsQuantity; | |
const items: ISku[] = []; | |
const sellers: ISeller[] = []; | |
const commertialOffer: ICommertialOffer = { | |
Installments:[ | |
{ | |
NumberOfInstallments: product.installment.count, | |
TotalValuePlusInterestRate: product.installment.price, | |
Value: product.installment.price / product.installment.count | |
} | |
], | |
ListPrice: product.oldPrice, | |
Price: product.price, | |
discountHighlights: [], | |
teasers: product.details.teasers?.length > 0? [{ name : product.details.teasers[0] }] : [], | |
AvailableQuantity: Number(detailsQuantity ? detailsQuantity : (skuDetailsQuantity ? skuDetailsQuantity : 10 )) | |
}; | |
product.skus.map(item => { | |
sellers.push({ | |
... product.skus[0].properties.details?.sellers[0], | |
commertialOffer, | |
sellerId: product.skus[0].properties.details?.sellers[0].sellerId | |
}) | |
items.push({ | |
itemId: item.sku, | |
seller: { | |
commertialOffer, | |
sellerId: item.properties.details?.sellers[0].sellerId | |
}, | |
image: { | |
imageUrl: item.properties.images.default, | |
}, | |
images: [ | |
{ | |
imageUrl: item.properties.images.default, | |
} | |
], | |
priceRange: { | |
listPrice: { | |
highPrice: item.properties.oldPrice, | |
lowPrice: item.properties.oldPrice, | |
}, | |
sellingPrice: { | |
highPrice: item.properties.price, | |
lowPrice: item.properties.price, | |
}, | |
}, | |
sellers | |
}) | |
}) | |
normalizedProduct = { | |
productName: product.name, | |
link: `https://${window.location.hostname}${product.url}`, | |
linkText: product.url.replace('/', ''), | |
clickUrl: product.clickUrl, | |
href: product.url.replace('/', ''), | |
commertialOffer, | |
productId: product.id, | |
priceRange: { | |
listPrice: { | |
highPrice: product.oldPrice, | |
lowPrice: product.oldPrice, | |
}, | |
sellingPrice: { | |
highPrice: product.price, | |
lowPrice: product.price, | |
}, | |
}, | |
items, | |
sku: { | |
itemId: product.skus[0]?.sku, | |
seller: { | |
commertialOffer, | |
sellerId: product.skus[0]?.properties.details?.sellers[0].sellerId | |
}, | |
image: { | |
imageUrl: product.images.default, | |
}, | |
images: [ | |
{ | |
imageUrl: product.images.default, | |
} | |
], | |
priceRange: { | |
listPrice: { | |
highPrice: product.oldPrice, | |
lowPrice: product.price, | |
}, | |
sellingPrice: { | |
highPrice: product.oldPrice, | |
lowPrice: product.price, | |
}, | |
}, | |
sellers | |
} | |
} | |
return normalizedProduct; | |
} | |
/* Sales channels */ | |
async function getSallesChannel(multiChannels: boolean = true){ | |
const session = await axios.get('/api/sessions?items=*'); | |
/* SEM DADOS */ | |
if(Object.keys(session.data).length === 0) return '&salesChannel=1'; | |
if(session.data.namespaces.store.channel?.value){ | |
let regionId = session.data.namespaces.checkout?.regionId?.value || false; | |
const sc = session.data.namespaces.store.channel?.value | |
return formatSalesChannel(regionId, sc, multiChannels); | |
}else{ | |
return '&salesChannel=1'; | |
} | |
} | |
function formatSalesChannel(regionId: any, sc: any, multiChannels: boolean) { | |
if(!regionId) return `&salesChannel=${sc}`; | |
const parseBase64 = atob(regionId); | |
if (multiChannels) { | |
let franchises = parseBase64.replace('SW#', '') | |
.split(';') | |
.map(channel => `${channel}-${sc}`); | |
return '&salesChannel='+franchises.join('&salesChannel=') | |
} else { | |
let franchise = parseBase64.replace('SW#', '') | |
.split(';')[0]; | |
return `&salesChannel=${franchise}-${sc}`; | |
} | |
} | |
/* Cookies */ | |
/** GET COOKIE */ | |
const getCookie = (name: string) => { | |
const value = `; ${document.cookie}`; | |
const parts = value.split(`; ${name}=`); | |
if (parts.length === 2) return parts?.pop()?.split(';').shift(); | |
return 'null'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment