Created
July 6, 2018 06:04
-
-
Save 1hakr/4ce1ea08119fb21a764a454b26e3cdde to your computer and use it in GitHub Desktop.
Cloud functions for referral rewards and notifications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.referralUpdate = functions.database.ref('/rewards/{uid}/referredBy').onCreate((data, context) => { | |
const snapshot = data; | |
const uid = context.params.uid; | |
const referredByUid = snapshot.val().uid; | |
const referredByClientIdKey = snapshot.val().clientIdKey; | |
return admin.database().ref(`/users/${referredByUid}/clients`).once('value').then(dataSnapshot => { | |
var validReferral = true; | |
var value = dataSnapshot.val(); | |
if (value) { | |
dataSnapshot.forEach(function(snapshot) { | |
if (referredByClientIdKey == snapshot.key) { | |
validReferral = false; | |
} | |
}); | |
} | |
if(!validReferral) { | |
return utils.logInfo("Invalid referral", {user: uid, from : referredByUid}); | |
} | |
//Compare client ids | |
return admin.database().ref(`/rewards/${referredByUid}/referrals/${uid}`).set(true).then(result => { | |
return utils.logInfo("Added referral", {user: uid, from : referredByUid}); | |
}).catch(error => { | |
return utils.reportError(error, { type: 'database_write', context: 'referral update2'}); | |
}); | |
}).catch(error => { | |
return utils.reportError(error, { type: 'database_write', context: 'referral update1'}); | |
}); | |
}); | |
exports.referralReward = functions.database.ref('/rewards/{uid}/referrals').onUpdate((data, context) => { | |
const uid = context.params.uid; | |
const snapshot = data; | |
return admin.database().ref(`/users/${uid}`).once('value').then(dataSnapshot => { | |
const referralCount = snapshot.numChildren(); | |
const purchaseToken = dataSnapshot.val().subscriptionToken; | |
const subscriptionId = dataSnapshot.val().subscriptionId; | |
const orderId = dataSnapshot.val().orderId; | |
const subscriptionStatus = dataSnapshot.val().subscriptionStatus; | |
const instanceId = dataSnapshot.val().instanceId; | |
const notificationKey = dataSnapshot.val().notificationKey; | |
// if referrals are more than 52/2*7 then stop | |
if(referralCount > 182){ | |
return utils.logInfo("Referral limit reached", {user: uid}); | |
} | |
if(subscriptionStatus == 0){ | |
//TODO: Remove previous referrals if subscription is cancelled | |
return utils.logInfo("Not subscribed", {user: uid}); | |
} | |
// send notification for every 7 referrals | |
const giveReward = referralCount % 7 == 0; | |
if (!giveReward) { | |
const message = `One of your referrals has just signed up. Your total referral count is ${referralCount}`; | |
return sendReferralNotifications(message, uid, instanceId, notificationKey); | |
} | |
if (orderId.startsWith('GPA')){ | |
if (!purchaseToken) { | |
return utils.reportError("No purchaseToken", {user: uid, context: 'referral reward'}); | |
} | |
return utils.extendSubscription(uid, purchaseToken, subscriptionId).then(result => { | |
utils.logInfo("Reward referral", {user: uid}); | |
const message = "You have been rewarded a week subcription FREE"; | |
return sendReferralNotifications(message, uid, instanceId, notificationKey); | |
}).catch(error => { | |
return utils.reportError(error, {user: uid, context: 'referral reward'}); | |
}); | |
} else{ | |
//TODO: figure out something for iOS users | |
return utils.logInfo("Not reward for iOS users", {user: uid}); | |
} | |
}).catch(error => { | |
return utils.reportError(error, {type: 'database_query', context: 'referral reward'}); | |
}); | |
}); | |
function sendReferralNotifications(message, userId, instanceId, notificationKey) { | |
const ttl = 60 * 60 * 24; | |
const payload = { | |
notification: { | |
title: "Reward", | |
body: message, | |
sound: 'default', | |
icon: 'ic_reward', | |
android_channel_id: "info_channel", | |
tag: "Reward" | |
}, | |
data: { | |
title: "Reward", | |
body: message, | |
sound: 'default', | |
icon: 'ic_reward', | |
type: "reward", | |
android_channel_id: "info_channel", | |
tag: "Reward" | |
} | |
}; | |
var options = { | |
priority: "normal", | |
timeToLive: ttl, | |
mutableContent: true, | |
}; | |
return utils.sendNotification(userId, instanceId, notificationKey, payload, options); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment