Fixed page breaking if no valid campaigns are found.

This commit is contained in:
DebaucheryLibrarian 2024-06-14 01:09:23 +02:00
parent 7808a3e059
commit 0f6bbaf536
1 changed files with 18 additions and 18 deletions

View File

@ -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)];