Created
November 11, 2021 19:19
-
-
Save arutkayb/2f6122f3c73696c83ee34cdf4a9d1ff8 to your computer and use it in GitHub Desktop.
Firebase functions campaign cronjob
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
import * as timeUtils from "./util/time_utils"; | |
const functions = require("firebase-functions"); | |
const admin = require("firebase-admin"); | |
admin.initializeApp(); | |
// Remote config: | |
// { | |
// "discount_start_date": "2021.10.23 00:00:00", | |
// "discount_end_date": "2021.10.26 00:00:00", | |
// "discount_type": "_discount_common", | |
// "discount_rate": "0.74" | |
// } | |
const PARAMETERS_KEY = "parameters"; | |
const DISCOUNT_DATA_KEY = "discount_info"; | |
const DEFAULT_VALUE_KEY = "defaultValue"; | |
const DISCOUNT_DATE_START_KEY = "discount_start_date"; | |
const DISCOUNT_DATE_END_KEY = "discount_end_date"; | |
const DISCOUNT_TYPE_KEY = "discount_type"; | |
const DISCOUNT_RATE_KEY = "discount_rate"; | |
const DISCOUNT_SUFFIX_COMMON = "_discount_common"; | |
const DISCOUNT_RATE = "0.74"; | |
exports.scheduledFunction = functions.pubsub.schedule("every 47 hours").onRun(() => { | |
setupCampaignConfig(); | |
return null; | |
}); | |
function setupCampaignConfig() { | |
getUpdatedConfigTemplate().then((template) => { | |
validateTemplate(template); | |
}); | |
} | |
async function getUpdatedConfigTemplate() { | |
var config = admin.remoteConfig(); | |
const template = await config.getTemplate(); | |
console.log("ETag from server: " + template.etag); | |
var templateStr = JSON.stringify(template); | |
console.log("Template: " + templateStr); | |
let values = template[PARAMETERS_KEY][DISCOUNT_DATA_KEY][DEFAULT_VALUE_KEY]; | |
console.log("Values: " + JSON.stringify(values)); | |
values.value = "{" | |
+ "\"" + DISCOUNT_DATE_START_KEY + "\":\"" + timeUtils.getDifferentDate(-1).toString() + "\"," | |
+ "\"" + DISCOUNT_DATE_END_KEY + "\":\"" + timeUtils.getDifferentDate(2).toString() + "\"," | |
+ "\"" + DISCOUNT_TYPE_KEY + "\":\"" + DISCOUNT_SUFFIX_COMMON + "\"," | |
+ "\"" + DISCOUNT_RATE_KEY + "\":\"" + DISCOUNT_RATE + "\"" | |
+ "}" | |
console.log("New Template: " + JSON.stringify(template)); | |
console.log("New Values: " + JSON.stringify(values)); | |
return template; | |
} | |
function validateTemplate(template: any) { | |
admin.remoteConfig().validateTemplate(template) | |
.then(function (validatedTemplate: any) { | |
console.log("Template was valid and safe to use"); | |
publishTemplate(validatedTemplate); | |
}) | |
.catch(function (err: any) { | |
console.error("Template is invalid and cannot be published"); | |
console.error(err); | |
}); | |
} | |
function publishTemplate(template: any) { | |
var config = admin.remoteConfig(); | |
config.publishTemplate(template) | |
.then(function (updatedTemplate: any) { | |
console.log("Template has been published"); | |
console.log("ETag from server: " + updatedTemplate.etag); | |
}) | |
.catch(function (err: any) { | |
console.error("Unable to publish template."); | |
console.error(err); | |
}); | |
} |
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
import * as moment from 'moment'; | |
const CONFIG_DATE_FORMAT = 'YYYY.MM.DD HH:00:00'; | |
export function getDifferentDate(daysDifference: number, referenceDate: Date = new Date()) { | |
return (moment(referenceDate)).add(daysDifference, 'days').format(CONFIG_DATE_FORMAT) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment