From 766ae427192417d4105930d098a006fc795ac031 Mon Sep 17 00:00:00 2001 From: Revadike Date: Thu, 14 Oct 2021 20:48:18 +0200 Subject: [PATCH 1/2] Bugfix getting slug (for addon) --- .eslintrc.json | 3 +- src/gamePromotions.js | 64 ++++++++++++++++++++++++++++--------------- 2 files changed, 43 insertions(+), 24 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index c3e26e2..adafba8 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -3,8 +3,7 @@ "node": true, "es6": true, "es2017": true, - "es2020": true, - "es2021": true + "es2020": true }, "extends": "eslint:recommended", "parser": "@babel/eslint-parser", diff --git a/src/gamePromotions.js b/src/gamePromotions.js index 7268186..10508e4 100644 --- a/src/gamePromotions.js +++ b/src/gamePromotions.js @@ -1,5 +1,44 @@ "use strict"; +async function getAddonForSlug(client, slug) { + // eslint-disable-next-line max-len + const URL = "https://www.epicgames.com/graphql?operationName=getMappingByPageSlug&variables=%7B%22pageSlug%22:%22{{slug}}%22%7D&extensions=%7B%22persistedQuery%22:%7B%22version%22:1,%22sha256Hash%22:%225a08e9869c983776596498e0c4052c55f9e54c79e18a303cd5eb9a46be55c7d7%22%7D%7D"; + + try { + const { data } = await client.http.sendGet( + URL.replace("{{slug}}", slug), + ); + + return data; + } catch (err) { + client.debug.print(new Error(err)); + } + + return false; +} + +async function getOfferId(client, promo, locale = "en-US") { + let offerId = null; + let isBundle = (promo) => Boolean(promo.categories.find((cat) => cat.path === "bundles")); + let isAddon = (promo) => Boolean(promo.categories.find((cat) => cat.path === "addons")); + + if (isAddon(promo)) { + let result = await getAddonForSlug(client, promo.urlSlug.split("/")[0], locale); + // eslint-disable-next-line prefer-destructuring + offerId = result.data.StorePageMapping.mapping.mappings.offerId; + } else if (isBundle(promo)) { + let result = await client.getBundleForSlug(promo.urlSlug.split("/")[0], locale); + let page = result.pages.find((p) => p.offer.id === promo.id) || result.pages[0]; + offerId = page.offer.id; + } else { + let result = await client.getProductForSlug(promo.urlSlug.split("/")[0], locale); + let page = result.pages.find((p) => p.offer.id === promo.id) || result.pages[0]; + offerId = page.offer.id; + } + + return offerId; +} + async function freeGamesPromotions(client, country = "US", allowCountries = "US", locale = "en-US") { let { data } = await client.freeGamesPromotions(country, allowCountries, locale); let { elements } = data.Catalog.searchStore; @@ -7,29 +46,10 @@ async function freeGamesPromotions(client, country = "US", allowCountries = "US" && offer.promotions.promotionalOffers.length > 0 && offer.promotions.promotionalOffers[0].promotionalOffers.find((p) => p.discountSetting.discountPercentage === 0)); - let isBundle = (promo) => Boolean(promo.categories.find((cat) => cat.path === "bundles")); - // eslint-disable-next-line no-confusing-arrow - let getOffer = (promo) => isBundle(promo) - ? client.getBundleForSlug(promo.productSlug.split("/")[0], locale) - : client.getProductForSlug(promo.productSlug.split("/")[0], locale); - let freeOffers = await Promise.all(free.map(async(promo) => { - let offer = await getOffer(promo); - let page = offer; - - if (offer.pages) { - page = offer.pages.find((p) => p.offer.id === promo.id); - } - - if (!page) { - [page] = offer.pages; - } - - return { - "title": offer.productName || offer._title, - "id": page.offer.id, - "namespace": page.offer.namespace, - }; + let { title, namespace } = promo; + let id = await getOfferId(client, promo, locale); + return { title, id, namespace }; })); return freeOffers; From b904b067aa9744e51d168dfd6fc07cb3ad0a582d Mon Sep 17 00:00:00 2001 From: Revadike Date: Thu, 14 Oct 2021 21:38:58 +0200 Subject: [PATCH 2/2] Some fixes --- .vscode/settings.json | 3 ++- src/gamePromotions.js | 11 ++++++----- test/gamePromotions.test.js | 5 +++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 3dc3d05..7e9fd4f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,10 +1,11 @@ { "eslint.format.enable": true, "editor.formatOnSave": true, + "files.eol": "\n", "eslint.alwaysShowStatus": true, "prettier.requireConfig": true, "editor.defaultFormatter": "dbaeumer.vscode-eslint", "[javascript]": { "editor.defaultFormatter": "dbaeumer.vscode-eslint" } -} \ No newline at end of file +} diff --git a/src/gamePromotions.js b/src/gamePromotions.js index 10508e4..02496a3 100644 --- a/src/gamePromotions.js +++ b/src/gamePromotions.js @@ -19,20 +19,21 @@ async function getAddonForSlug(client, slug) { async function getOfferId(client, promo, locale = "en-US") { let offerId = null; + let slug = promo.productSlug || promo.urlSlug; let isBundle = (promo) => Boolean(promo.categories.find((cat) => cat.path === "bundles")); let isAddon = (promo) => Boolean(promo.categories.find((cat) => cat.path === "addons")); if (isAddon(promo)) { - let result = await getAddonForSlug(client, promo.urlSlug.split("/")[0], locale); + let result = await getAddonForSlug(client, slug.split("/")[0], locale); // eslint-disable-next-line prefer-destructuring offerId = result.data.StorePageMapping.mapping.mappings.offerId; } else if (isBundle(promo)) { - let result = await client.getBundleForSlug(promo.urlSlug.split("/")[0], locale); - let page = result.pages.find((p) => p.offer.id === promo.id) || result.pages[0]; + let result = await client.getBundleForSlug(slug.split("/")[0], locale); + let page = result.pages ? result.pages.find((p) => p.offer.id === promo.id) || result.pages[0] : result; offerId = page.offer.id; } else { - let result = await client.getProductForSlug(promo.urlSlug.split("/")[0], locale); - let page = result.pages.find((p) => p.offer.id === promo.id) || result.pages[0]; + let result = await client.getProductForSlug(slug.split("/")[0], locale); + let page = result.pages ? result.pages.find((p) => p.offer.id === promo.id) || result.pages[0] : result; offerId = page.offer.id; } diff --git a/test/gamePromotions.test.js b/test/gamePromotions.test.js index c5c2d45..4ae999a 100644 --- a/test/gamePromotions.test.js +++ b/test/gamePromotions.test.js @@ -2,6 +2,7 @@ "use strict"; const { readFileSync } = require("fs"); +const { resolve } = require("path"); const gamePromotions = require("../src/gamePromotions.js"); const { expect } = require("chai"); @@ -10,7 +11,7 @@ function readData(name, date = null) { if (date) { filename += `_${date}`; } - return JSON.parse(readFileSync(`${__dirname}/data/${filename}.json`).toString()); + return JSON.parse(readFileSync(resolve(__dirname, "data", `${filename}.json`)).toString()); } let client = {}; @@ -40,7 +41,7 @@ describe("freeGamesPromotions", () => { }); expect(freeGames).to.deep.include({ - "title": "Hitman 2016", + "title": "HITMAN", "id": "e8efad3d47a14284867fef2c347c321d", "namespace": "3c06b15a8a2845c0b725d4f952fe00aa", });