functions.database.ref('/organizations/{organizationId}').onWrite(event => { if(event.data.previous.exists()) { // we only want to do this once return; } defaultTemplates.forEach((tpl) => { let ntRef = admin.database().ref(`/notification_templates/${event.params.organizationId}`).push(); ntRef.set(tpl) }) })
function create(data) { // Get a keys. var modelKey = Model.push().key; Model.child(modelKey).set(data); return utils.makeResponse(201,{id:modelKey},null,"Resource created.") }
addCallHistory(provider, data) { const callHistoryRef = app.locals.db.ref('/call-history/' + provider + '/'); callHistoryRef.push(data) .then(() => { console.log('Call history added!', data); }) .catch(console.error); }
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}); });
function writePost(data) { return post.push(data); }
app.post('/api/v1/address', function(request, response){ if (request.body) { addressRef.push().set(request.body); addressRef.once('value').then(function(snapshot) { response.send({data: snapshot.val()}); }, function (errorObject) { console.log("The read failed: " + errorObject.code); response.send(errorObject); }); } });
functions.https.onRequest((request: any, response: any) => { cors()(request, response, () => { contactsRef.push({ firstname: request.body.firstname, lastname: request.body.lastname, phone: request.body.phone, email: request.body.email }) }) response.send({'msg': 'Done', 'data': { firstname: request.body.firstname, lastname: request.body.lastname, phone: request.body.phone, email: request.body.email }}); })
app.get('/api/v1/testfirebase', function(request, response){ addressRef.once('value').then(function(snapshot) { console.log('snapshot', snapshot.val()); response.send(snapshot.val()); }, function (errorObject) { console.log("The read failed: " + errorObject.code); }); addressRef.child("id_1").set({ "street": "72 Le Thanh Ton 1", "ward": "Ben Nghe", "district": "1", "city": "Ho Chi Minh", "country": "Vietnam" }); addressRef.child("id_2").update({ "street": "138 Hai Ba Trung 1" }); addressRef.push().set({ "street": "72 Le Thanh Ton 2", "ward": "Ben Nghe", "district": "1", "city": "Ho Chi Minh", "country": "Vietnam" }); });
// post an comment releated to a rooms announcement app.post('/api/announcement/postAnnouncementComment', authenticate, (req, res) => { // get annoucement ref const commentsRef = db.ref(`rooms/${req.body.roomID}/announcements/${req.body.announcementID}/comments`); // get the value and proceed to update comments commentsRef.once('value', snapshot => { // add new comment to comments ref commentsRef.push(req.body.commentData); // send back response with status res.status(200).json({ success: true, message: 'Comment successfully posted', commentators: snapshot.exists() ? [...new Set(Object.values(snapshot.val()) .filter(comment => comment.author.userID !== req.body.uid) .map(comment => comment.author.userID))] : [] }); }); });
app.post('/functions/sendMessage', async (req, res) => { try { const { email, message } = req.body; // Validate email request if (!email || !/(.+)@(.+){2,}\.(.+){2,}/.test(email)) { return res.status(400).json({ error: 'Please enter a valid email address' }); } else if (!message) { return res.status(400).json({ error: 'Please enter a message' }); } else if (email.length > MAX_EMAIL_LENGTH) { return res.status(400).json({ error: `Please enter an email fewer than ${MAX_EMAIL_LENGTH} characters`, }); } else if (message.length > MAX_MESSAGE_LENGTH) { return res.status(400).json({ error: `Please enter a message fewer than ${MAX_MESSAGE_LENGTH} characters`, }); } await admin.database().ref('/messages').push({ email, message }); return res.status(200).json({ message: 'Message sent successfully' }); } catch (error) { console.error(error); return res.status(500).json({ error: 'Message rejected' }); } });
userRef.child('notifications').push({ message: req.body.data.body, timestamp: new Date().getTime(),
function create(data) { // Get a keys. var modelKey = Model.push().key; Model.child(modelKey).set(data); return utils.makeResponse(201,{id:modelKey},null,"Resource created.") }