Add helper function to generate unsubscribe links

This is so that we can bulk-generate them to add to places like
ChipChoc.
This commit is contained in:
Badri Sunderarajan 2023-03-12 14:12:42 +05:30
parent df2973722d
commit 702a303c28

View file

@ -0,0 +1,61 @@
let {
Pledge,
makeSerialiser,
} = require('./server')
function generateBackoutLink(email, unsubscribe=true) {
let serialiser = makeSerialiser(email)
// format "unsubscribe" for serialisation
let staySubscribed
if (unsubscribe) {
staySubscribed = 'no'
} else {
staySubscribed = 'yes'
}
let verificationLink = `https://fund.snipettemag.com/back-out?email=${encodeURIComponent(email)}&key=${encodeURIComponent(serialiser.dumps(staySubscribed))}`
return verificationLink
}
async function generateBackoutLinks(unsubscribe=true) {
// get list of pledges
let pledges = (
await Pledge
.forge()
.orderBy('created_at', 'DESC')
.fetchAll()
).models
let verificationLinks = []
// get the links
pledges.forEach((pledge) => {
verificationLinks.push(
generateBackoutLink(pledge.get('email'), unsubscribe)
)
})
return verificationLinks
}
if (!module.parent) {
// figure out if we should unsubscribe
let unsubscribe
if (process.argv[2] == 'yes' || process.argv[2] == 'stay-subscribed') {
console.log('Backing out but staying subscribed')
unsubscribe = false
} else {
console.log('Backing out and unsubscribing')
unsubscribe = true
}
// get the links and print 'em out
generateBackoutLinks(unsubscribe).then((links) => {
links.forEach(link => console.log(link))
return
})
}