Fixed page breaking if no valid campaigns are found.
This commit is contained in:
parent
7808a3e059
commit
0f6bbaf536
|
@ -57,25 +57,25 @@ export async function getRandomCampaign(options = {}, context = {}) {
|
||||||
return true;
|
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) {
|
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)
|
const randomCampaign = (primaryCampaigns.length > 0 ? primaryCampaigns[crypto.randomInt(primaryCampaigns.length)] : null)
|
||||||
|| (randomEntityCampaigns.length > 0 ? randomEntityCampaigns[crypto.randomInt(randomEntityCampaigns.length)] : null)
|
|| (randomEntityCampaigns.length > 0 ? randomEntityCampaigns[crypto.randomInt(randomEntityCampaigns.length)] : null)
|
||||||
|| validCampaigns[crypto.randomInt(validCampaigns.length)];
|
|| validCampaigns[crypto.randomInt(validCampaigns.length)];
|
||||||
|
|
Loading…
Reference in New Issue