Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
- let functions = require('firebase-functions');
- // The Firebase Admin SDK to access the Firebase Realtime Database.
- let admin = require('firebase-admin');
- admin.initializeApp(functions.config().firebase);// // Create and Deploy Your First Cloud Functions
- // // https://firebase.google.com/docs/functions/write-firebase-functions
- //
- // exports.helloWorld = functions.https.onRequest((request, response) => {
- // response.send("Hello from Firebase!");
- // });
- exports.onComplaintCreated = functions.firestore
- .document('complaints/{id}')
- .onCreate((snap, context) => {
- // Get an object representing the document
- // e.g. {'name': 'Marie', 'age': 66}
- var flatId = snap.get('flat');
- var complaint = snap.get('contents');
- var flatmates;
- var flatName;
- var ref1 = admin.firestore().collection('flats').doc(flatId);
- return ref1.get()
- .then(function(doc){
- const data = doc.data();
- console.log(data);
- flatmates = data.flatmates;
- console.log(flatmates);
- flatName = data.name;
- const payload = {
- notification: {
- title: 'New complaint in ' + data.name,
- body: complaint
- }
- };
- const options = {
- priority: 'high',
- timeToLive: 60 * 60 * 24, // 1 day
- };
- for(i = 0; i < flatmates.length; i++)
- {
- var uid = flatmates[i];
- //if(token !== null || token !==""){
- findAndSend(uid, payload, options);
- //}
- }
- return;
- // access a particular field as you would any JS property
- //admin.firestore.doc('complaints/{id}').set(newValue);
- // perform desired operations ...
- }, function (errorObject) {
- console.log("The read failed: " + errorObject.code);
- });
- });
- exports.onFlatCreated = functions.firestore
- .document('flats/{id}')
- .onCreate((snap, context) => {
- // Get an object representing the document
- // e.g. {'name': 'Marie', 'age': 66}
- var flatmates = snap.get('toAccept');
- var flatName = snap.get('name');
- const payload = {
- notification: {
- title: 'New invitation',
- body: 'You have been invited to flat '+flatName
- }
- };
- const options = {
- priority: 'high',
- timeToLive: 60 * 60 * 24, // 1 day
- };
- for(i = 0; i < flatmates.length; i++)
- {
- var uid = flatmates[i];
- //if(token !== null || token !==""){
- findAndSend(uid, payload, options);
- //}
- }
- return;
- // access a particular field as you would any JS property
- //admin.firestore.doc('complaints/{id}').set(newValue);
- // perform desired operations ...
- });
- exports.onItemAdded = functions.firestore
- .document('shoppingList/{id}')
- .onCreate((snap, context) => {
- // Get an object representing the document
- // e.g. {'name': 'Marie', 'age': 66}
- var flatId = snap.get('flat');
- var item = snap.get('contents');
- var flatmates;
- var flatName;
- var ref1 = admin.firestore().collection('flats').doc(flatId);
- return ref1.get()
- .then(function(doc){
- const data = doc.data();
- console.log(data);
- flatmates = data.flatmates;
- console.log(flatmates);
- flatName = data.name;
- const payload = {
- notification: {
- title: 'New item added to shopping list in ' + data.name,
- body: item
- }
- };
- const options = {
- priority: 'high',
- timeToLive: 60 * 60 * 24, // 1 day
- };
- for(i = 0; i < flatmates.length; i++)
- {
- var uid = flatmates[i];
- //if(token !== null || token !==""){
- findAndSend(uid, payload, options);
- //}
- }
- return;
- // access a particular field as you would any JS property
- //admin.firestore.doc('complaints/{id}').set(newValue);
- // perform desired operations ...
- }, function (errorObject) {
- console.log("The read failed: " + errorObject.code);
- });
- });
- exports.onItemUpdated = functions.firestore
- .document('shoppingList/{id}')
- .onUpdate((snap, context) => {
- // Get an object representing the document
- // e.g. {'name': 'Marie', 'age': 66}
- const change = snap.after.data();
- var flatId = change.flat;
- var item = change.contents;
- var author = change.author;
- var authorName = "";
- if(author !== null && author !== "")
- {
- var flatmates;
- var flatName;
- var ref1 = admin.firestore().collection('flats').doc(flatId);
- return ref1.get()
- .then(function(doc){
- const data = doc.data();
- console.log(data);
- flatmates = data.flatmates;
- console.log(flatmates);
- flatName = data.name;
- return admin.firestore().collection('users').doc(author).get()
- .then(function(doc2){
- const data2 = doc2.data();
- authorName = data2.nickname;
- const payload = {
- notification: {
- title: 'Shopping list in ' + data.name,
- body: authorName +" is assigned to buy "+ item
- }
- };
- const options = {
- priority: 'high',
- timeToLive: 60 * 60 * 24, // 1 day
- };
- for(i = 0; i < flatmates.length; i++)
- {
- var uid = flatmates[i];
- //if(token !== null || token !==""){
- findAndSend(uid, payload, options);
- //}
- }
- return;
- // access a particular field as you would any JS property
- //admin.firestore.doc('complaints/{id}').set(newValue);
- // perform desired operations ...
- });
- }, function (errorObject) {
- console.log("The read failed: " + errorObject.code);
- });
- }
- });
- exports.onItemDeleted = functions.firestore
- .document('shoppingList/{id}')
- .onDelete((snap, context) => {
- // Get an object representing the document
- // e.g. {'name': 'Marie', 'age': 66}
- var flatId = snap.get('flat');
- var item = snap.get('contents');
- var flatmates;
- var flatName;
- var ref1 = admin.firestore().collection('flats').doc(flatId);
- return ref1.get()
- .then(function(doc){
- const data = doc.data();
- console.log(data);
- flatmates = data.flatmates;
- console.log(flatmates);
- flatName = data.name;
- const payload = {
- notification: {
- title: 'Shopping list in ' + data.name,
- body: 'Item ' +item + ' removed from shoppping list.'
- }
- };
- const options = {
- priority: 'high',
- timeToLive: 60 * 60 * 24, // 1 day
- };
- for(i = 0; i < flatmates.length; i++)
- {
- var uid = flatmates[i];
- //if(token !== null || token !==""){
- findAndSend(uid, payload, options);
- //}
- }
- return;
- // access a particular field as you would any JS property
- //admin.firestore.doc('complaints/{id}').set(newValue);
- // perform desired operations ...
- }, function (errorObject) {
- console.log("The read failed: " + errorObject.code);
- });
- });
- function findAndSend(uid, payload, options)
- {
- console.log()
- return admin.firestore().collection('users').doc(uid).get()
- .then(function(doc2){
- var token = doc2.data().token;
- if(token !== null && token !==""){
- return admin.messaging().sendToDevice(token, payload, options);
- }
- return;
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement