/** * Send an email listing the top posts every Sunday. */ function startWeeklyTopPostEmailer() { // Run this job every Sunday at 2:30pm. schedule.scheduleJob({hour: 14, minute: 30, dayOfWeek: 0}, function () { // List the top 5 posts. // [START top_posts_query] const topPostsRef = firebase.database().ref('/posts').orderByChild('starCount').limitToLast(5); // [END top_posts_query] const allUserRef = firebase.database().ref('/users'); Promise.all([topPostsRef.once('value'), allUserRef.once('value')]).then(function(resp) { const topPosts = resp[0].val(); const allUsers = resp[1].val(); const emailText = createWeeklyTopPostsEmailHtml(topPosts); sendWeeklyTopPostEmail(allUsers, emailText); }).catch(function(error) { console.log('Failed to start weekly top posts emailer:', error); }); }); console.log('Weekly top posts emailer started...'); }
/** * Sends the weekly top post email to all users in the given `users` object. */ function sendWeeklyTopPostEmail(users, emailHtml) { Object.keys(users).forEach(function(uid) { const user = users[uid]; if (user.email) { const mailOptions = { from: '"Firebase Database Quickstart" <noreply@firebase.com>', to: user.email, subject: 'This week\'s top posts!', html: emailHtml }; mailTransport.sendMail(mailOptions).then(function() { console.log('Weekly top posts email sent to: ' + user.email); // Save the date at which we sent the weekly email. // [START basic_write] return firebase.database().ref().child('/users/' + uid + '/lastSentWeeklyTimestamp') .set(firebase.database.ServerValue.TIMESTAMP); // [END basic_write] }).catch(function(error) { console.log('Failed to send weekly top posts email:', error); }); } }); }
const userRef = firebase.database().ref('/users/' + uid); userRef.once('value').then(function(snapshot) { const email = snapshot.val().email; firebase.database.ServerValue.TIMESTAMP; update['/user-posts/' + uid + '/' + postId + '/lastNotificationTimestamp'] = firebase.database.ServerValue.TIMESTAMP; firebase.database().ref().update(update);
app.put('/:id', (req: any, res: any, next: any) => { admin.database().ref('/contacts/' + req.params.id).update({ firstname: req.body.firstname, lastname: req.body.lastname, phone: req.body.phone, email: req.body.email }) res.send(req.body) next() })
Request.post(Request.endpoints.LABELS_CREATE, params, {user: req.carrier_settings.shipstation.options.api_key, pass: req.carrier_settings.shipstation.options.api_secret}).then((label) => { let labelRef = admin.database().ref(`/shipping_labels/${req.headers.organization_id}`).push(); labelRef.set(label); return res.json(label); }).catch((err) => { return res.code(err.code).json({code: err.code, message: err.message}); });
functions.https.onRequest(async (req, res) => { // // const original = req.query.text; // const result = []; const snapshot = await admin.database().ref('/comments'); // // res.redirect(303, snapshot.ref.toString() ); res.send('Hello from FIrebase') })
functions.database.ref('todos').onWrite((snapshot, context) => { const email = context.auth.token.email; const result = snapshot.after.val(); let lastValue = {}; for (const prop in result) { lastValue = {id: prop, value: result[prop]} } admin.database().ref('replicateTodos').child(lastValue.id).set({description: lastValue.value.description , status: lastValue.value.status , title: lastValue.value.title, email: email }) })
getSettings(organizationId) { return new Promise((resolve, reject) => { admin.database().ref(`/organization_apps/${organizationId}`).once('value').then((snapshot) => { let settings = snapshot.val(); if(!settings) { return reject({code: 404, message: 'App settings not found for organization'}) } return resolve(settings); }) }); }
// Wait for data to be updated before exiting app firebaseAdmin.database().ref(DATABASE_DATA_KEY) .on('value', (data) => { if (data && data.val()) { console.log('Database updated'); } });
app.get('/:id', (req: any, res: any, next: any) => { admin.database().ref('/contacts/' + req.params.id).once('value', (data) => { var sn = data.val() res.send({ 'res': sn }) next() },(err: any) => res.send({res: err}) ) })
/** * Send an email listing the top posts every Sunday. */ function startWeeklyTopPostEmailer() { // Run this job every Sunday at 2:30pm. schedule.scheduleJob({hour: 14, minute: 30, dayOfWeek: 0}, function () { // List the top 5 posts. // [START top_posts_query] const topPostsRef = firebase.database().ref('/posts').orderByChild('starCount').limitToLast(5); // [END top_posts_query] const allUserRef = firebase.database().ref('/users'); Promise.all([topPostsRef.once('value'), allUserRef.once('value')]).then(function(resp) { const topPosts = resp[0].val(); const allUsers = resp[1].val(); const emailText = createWeeklyTopPostsEmailHtml(topPosts); sendWeeklyTopPostEmail(allUsers, emailText); }).catch(function(error) { console.log('Failed to start weekly top posts emailer:', error); }); }); console.log('Weekly top posts emailer started...'); }
/** * Sends the weekly top post email to all users in the given `users` object. */ function sendWeeklyTopPostEmail(users, emailHtml) { Object.keys(users).forEach(function(uid) { const user = users[uid]; if (user.email) { const mailOptions = { from: '"Firebase Database Quickstart" <noreply@firebase.com>', to: user.email, subject: 'This week\'s top posts!', html: emailHtml }; mailTransport.sendMail(mailOptions).then(function() { console.log('Weekly top posts email sent to: ' + user.email); // Save the date at which we sent the weekly email. // [START basic_write] return firebase.database().ref().child('/users/' + uid + '/lastSentWeeklyTimestamp') .set(firebase.database.ServerValue.TIMESTAMP); // [END basic_write] }).catch(function(error) { console.log('Failed to send weekly top posts email:', error); }); } }); }
const userRef = firebase.database().ref('/users/' + uid); userRef.once('value').then(function(snapshot) { const email = snapshot.val().email; firebase.database.ServerValue.TIMESTAMP; update['/user-posts/' + uid + '/' + postId + '/lastNotificationTimestamp'] = firebase.database.ServerValue.TIMESTAMP; firebase.database().ref().update(update);