Files
helper-functions/get-all-single-products/script.js

120 lines
2.2 KiB
JavaScript

function idmGetSingleProdGraphQL(prodId) {
const IDM_PRICE_QUERY = (priceType) => `price {
rebateCodeActive
price {
${priceType} {
value
formatted
}
}
omnibusPrice {
${priceType} {
value
formatted
}
}
omnibusPriceDetails {
unit {
${priceType} {
value
formatted
}
}
youSavePercent
omnibusPriceIsHigherThanSellingPrice
newPriceEffectiveUntil {
formatted
}
}
max {
${priceType} {
value
formatted
}
}
unit {
${priceType} {
value
formatted
}
}
unitConvertedPrice {
${priceType} {
value
formatted
}
}
youSavePercent
beforeRebate {
${priceType} {
value
formatted
}
}
beforeRebateDetails {
youSavePercent
unit {
${priceType} {
value
formatted
}
}
}
advancePrice {
${priceType} {
value
formatted
}
}
suggested {
${priceType} {
value
formatted
}
}
rebateNumber {
number
${priceType} {
value
formatted
}
}
}`;
return `
prod${prodId}: product(productId: ${prodId}) {
product {
id
name
link
${IDM_PRICE_QUERY(app_shop.vars.priceType)}
}
}`;
}
async function idmGetAllSingleProd(prodArr) {
try {
const res = await fetch("/graphql/v1/", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
query: `{${prodArr.reduce(
(acc, val) => acc + `${idmGetSingleProdGraphQL(val)}`,
""
)}}`,
}),
});
const data = await res.json();
const products = Object.values(data?.data)?.map((prod) => prod.product);
if (!products)
throw new Error("Coś poszło nie tak! Nie znaleziono produktow");
return products;
} catch (err) {
console.error(err);
}
}