From 0f6bbaf5363c797940472f0e7edff4a5785d9fbf Mon Sep 17 00:00:00 2001 From: DebaucheryLibrarian Date: Fri, 14 Jun 2024 01:09:23 +0200 Subject: [PATCH] Fixed page breaking if no valid campaigns are found. --- src/campaigns.js | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/campaigns.js b/src/campaigns.js index 0352456..d20a717 100644 --- a/src/campaigns.js +++ b/src/campaigns.js @@ -57,25 +57,25 @@ export async function getRandomCampaign(options = {}, context = {}) { return true; }); - const campaignsByEntityId = validCampaigns.reduce((acc, campaign) => { - const entityId = campaign.entity.parent?.id || campaign.entity.id; - - if (!acc[entityId]) { - acc[entityId] = []; - } - - acc[entityId].push(campaign); - - return acc; - }, {}); - - // randomize entities first to ensure fair exposure for entities with fewer banners - const entityIds = Object.keys(campaignsByEntityId); - const randomEntityCampaigns = campaignsByEntityId[entityIds[crypto.randomInt(entityIds.length)]]; - - const primaryCampaigns = randomEntityCampaigns.filter((campaign) => campaign.entity.id === options.entityIds?.[0]); - if (validCampaigns.length > 0) { + const campaignsByEntityId = validCampaigns.reduce((acc, campaign) => { + const entityId = campaign.entity.parent?.id || campaign.entity.id; + + if (!acc[entityId]) { + acc[entityId] = []; + } + + acc[entityId].push(campaign); + + return acc; + }, {}); + + // randomize entities first to ensure fair exposure for entities with fewer banners + const entityIds = Object.keys(campaignsByEntityId); + const randomEntityCampaigns = campaignsByEntityId[entityIds[crypto.randomInt(entityIds.length)]]; + + const primaryCampaigns = randomEntityCampaigns.filter((campaign) => campaign.entity.id === options.entityIds?.[0]); + const randomCampaign = (primaryCampaigns.length > 0 ? primaryCampaigns[crypto.randomInt(primaryCampaigns.length)] : null) || (randomEntityCampaigns.length > 0 ? randomEntityCampaigns[crypto.randomInt(randomEntityCampaigns.length)] : null) || validCampaigns[crypto.randomInt(validCampaigns.length)];