Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
RemoveExpiredCampaigns removes campaigns with expired dates from a list of ongoing campaigns.
// sample social media campaigns and their expiry dates
const campaigns =
[
{
"type": "campaign",
"target": "Facebook",
"expiry": "2020-03-07T16:32:12.011Z",
"id": "campaign:05:target:facebook:campaignNo:4AB12"
},
{
"type": "campaign",
"target": "Instagram",
"expiry": "2020-11-07T16:32:12.011Z",
"id": "campaign:07:target:instagram:campaignNo:4AB2"
},
{
"type": "campaign",
"target": "Facebook",
"expiry": "2020-09-07T16:32:12.011Z",
"id": "campaign:02:target:facebook:campaignNo:4AB19"
}
]
// sample campaigns created by the campaign manager
const allTeamCampaigns = {
"campaign:amanda-pina23": {
"total": 205,
"availableTotal": 180,
"campaigns": {
"campaign:05:target:facebook:campaignNo:4AB12": 109,
"campaign:07:target:instagram:campaignNo:4AB2": 96
}
},
"campaign:amanda-pina31": {
"total": 50,
"availableTotal": 10,
"campaigns": {
"campaign:05:target:facebook:campaignNo:4AB12": 40,
"campaign:02:target:facebook:campaignNo:4AB19": 10
}
}
}
const getExpiredCampaigns = (campaigns) => {
const today = new Date()
return expiredCampaigns = campaigns.filter(campaign => {
return (new Date(campaign.expiry)) < today
}).map(campaign => campaign.id)
}
const removeExpiredCampaignsFromLedger = (allTeamCampaigns, allCampaigns) => {
return Object.keys(allTeamCampaigns).map((lkey) => {
const currentTeamCampaigns = allTeamCampaigns[lkey]
const campaigns = currentTeamCampaigns.campaigns
const goodCampaigns = Object.keys(campaigns).filter((campaignId) => !getExpiredCampaigns(allCampaigns).includes(campaignId))
.reduce((prevCampaignSet, campaignId) => {
const newCampaignSet = {}
newCampaignSet[campaignId] = campaigns[campaignId]
return Object.assign(prevCampaignSet, newCampaignSet)
}, {})
return Object.assign(currentTeamCampaigns, { campaigns: goodCampaigns, id: lkey })
}).reduce((prevCampaignSet, newCampaignSet) => {
const formattedCampaigns = Object.assign({}, newCampaignSet)
delete formattedCampaigns.id
return Object.assign(prevCampaignSet, { [newCampaignSet.id]: formattedCampaigns })
}, {})
}
const formatted = removeExpiredCampaignsFromLedger(allTeamCampaigns, campaigns)
console.log(formatted)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment