Skip to content

Commit

Permalink
V1.5.8
Browse files Browse the repository at this point in the history
  • Loading branch information
Revadike committed Dec 18, 2021
1 parent 17222e8 commit 1f213db
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "epicgames-freebies-claimer",
"version": "1.5.7",
"version": "1.5.8",
"description": "Claim free game promotions from the Epic Game Store",
"author": {
"name": "Revadike",
Expand Down Expand Up @@ -60,4 +60,4 @@
"epicgames-client": "Revadike/node-epicgames-client#develop",
"tracer": "^1.0.3"
}
}
}
22 changes: 15 additions & 7 deletions src/gamePromotions.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,33 @@ async function getAddonForSlug(client, slug) {
}

async function getOfferId(client, promo, locale = "en-US") {
let offerId = null;
let id = null;
let namespace = null;
let slug = (promo.productSlug || promo.urlSlug).split("/")[0];
let isBundle = (promo) => Boolean(promo.categories.find((cat) => cat.path === "bundles"));
let isAddon = (promo) => promo.offerType === "EDITION" || Boolean(promo.categories.find((cat) => cat.path === "addons"));

if (isAddon(promo)) {
let result = await getAddonForSlug(client, slug, locale);
// eslint-disable-next-line prefer-destructuring
offerId = result.data.StorePageMapping.mapping.mappings.offerId;
id = result.data.StorePageMapping.mapping.mappings.offerId;
} else if (isBundle(promo)) {
let result = await client.getBundleForSlug(slug, locale);
let page = result.pages ? result.pages.find((p) => p.offer.id === promo.id) || result.pages[0] : result;
offerId = page.offer.id;
// eslint-disable-next-line prefer-destructuring
id = page.offer.id;
// eslint-disable-next-line prefer-destructuring
namespace = page.offer.namespace;
} else {
let result = await client.getProductForSlug(slug, locale);
let page = result.pages ? result.pages.find((p) => p.offer.id === promo.id) || result.pages[0] : result;
offerId = page.offer.id;
// eslint-disable-next-line prefer-destructuring
id = page.offer.id;
// eslint-disable-next-line prefer-destructuring
namespace = page.offer.namespace;
}

return offerId;
return { namespace, id };
}

async function freeGamesPromotions(client, country = "US", allowCountries = "US", locale = "en-US") {
Expand All @@ -48,8 +55,9 @@ async function freeGamesPromotions(client, country = "US", allowCountries = "US"
&& offer.promotions.promotionalOffers[0].promotionalOffers.find((p) => p.discountSetting.discountPercentage === 0));

let freeOffers = await Promise.all(free.map(async(promo) => {
let { title, namespace } = promo;
let id = await getOfferId(client, promo, locale);
let { title } = promo;
let { namespace, id } = await getOfferId(client, promo, locale);
namespace = namespace || promo.namespace;
return { title, id, namespace };
}));

Expand Down

0 comments on commit 1f213db

Please sign in to comment.