Skip to content

Commit

Permalink
Merge pull request #136 from Revadike/bugfix/addon-slug
Browse files Browse the repository at this point in the history
Bugfix getting slug (for addon)
  • Loading branch information
Revadike authored Oct 14, 2021
2 parents 93b0e56 + b904b06 commit 1dfd3bb
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
65 changes: 43 additions & 22 deletions src/gamePromotions.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,56 @@
"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 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, 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(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(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;
}

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;
let free = elements.filter((offer) => offer.promotions
&& 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;
Expand Down
5 changes: 3 additions & 2 deletions test/gamePromotions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"use strict";

const { readFileSync } = require("fs");
const { resolve } = require("path");
const gamePromotions = require("../src/gamePromotions.js");
const { expect } = require("chai");

Expand All @@ -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 = {};
Expand Down Expand Up @@ -40,7 +41,7 @@ describe("freeGamesPromotions", () => {
});

expect(freeGames).to.deep.include({
"title": "Hitman 2016",
"title": "HITMAN",
"id": "e8efad3d47a14284867fef2c347c321d",
"namespace": "3c06b15a8a2845c0b725d4f952fe00aa",
});
Expand Down

0 comments on commit 1dfd3bb

Please sign in to comment.