Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
  3. let functions = require('firebase-functions');
  4.  
  5. // The Firebase Admin SDK to access the Firebase Realtime Database.
  6. let admin = require('firebase-admin');
  7. admin.initializeApp(functions.config().firebase);// // Create and Deploy Your First Cloud Functions
  8. // // https://firebase.google.com/docs/functions/write-firebase-functions
  9. //
  10. // exports.helloWorld = functions.https.onRequest((request, response) => {
  11. //  response.send("Hello from Firebase!");
  12. // });
  13.  
  14. exports.onComplaintCreated =  functions.firestore
  15. .document('complaints/{id}')
  16. .onCreate((snap, context) => {
  17.   // Get an object representing the document
  18.   // e.g. {'name': 'Marie', 'age': 66}
  19.   var flatId = snap.get('flat');
  20.  
  21.   var complaint = snap.get('contents');
  22.  
  23.  
  24.   var flatmates;
  25.   var flatName;
  26.   var ref1 = admin.firestore().collection('flats').doc(flatId);
  27.   return ref1.get()
  28.     .then(function(doc){
  29.  
  30.         const data = doc.data();
  31.         console.log(data);
  32.         flatmates = data.flatmates;
  33.         console.log(flatmates);
  34.         flatName = data.name;
  35.  
  36.       const payload = {
  37.         notification: {
  38. title: 'New complaint in ' + data.name,
  39. body: complaint
  40. }
  41.         };
  42.  
  43.         const options = {
  44. priority: 'high',
  45. timeToLive: 60 * 60 * 24, // 1 day
  46. };
  47.  
  48.         for(i = 0; i < flatmates.length; i++)
  49.        {
  50.           var uid = flatmates[i];
  51.           //if(token !== null || token !==""){
  52.               findAndSend(uid, payload, options);
  53.           //}
  54.         }
  55.  
  56.         return;
  57.   // access a particular field as you would any JS property
  58.  
  59.  
  60.   //admin.firestore.doc('complaints/{id}').set(newValue);
  61.  
  62.   // perform desired operations ...
  63. }, function (errorObject) {
  64.         console.log("The read failed: " + errorObject.code);
  65.     });
  66. });
  67.  
  68. exports.onFlatCreated =  functions.firestore
  69. .document('flats/{id}')
  70. .onCreate((snap, context) => {
  71.   // Get an object representing the document
  72.   // e.g. {'name': 'Marie', 'age': 66}
  73.  
  74.  
  75.  
  76.   var flatmates = snap.get('toAccept');
  77.   var flatName = snap.get('name');
  78.  
  79.  
  80.       const payload = {
  81.         notification: {
  82. title: 'New invitation',
  83. body: 'You have been invited to flat '+flatName
  84. }
  85.         };
  86.  
  87.         const options = {
  88. priority: 'high',
  89. timeToLive: 60 * 60 * 24, // 1 day
  90. };
  91.  
  92.         for(i = 0; i < flatmates.length; i++)
  93.        {
  94.           var uid = flatmates[i];
  95.           //if(token !== null || token !==""){
  96.               findAndSend(uid, payload, options);
  97.           //}
  98.         }
  99.  
  100.         return;
  101.   // access a particular field as you would any JS property
  102.  
  103.  
  104.   //admin.firestore.doc('complaints/{id}').set(newValue);
  105.  
  106.   // perform desired operations ...
  107.  
  108. });
  109.  
  110.  
  111. exports.onItemAdded =  functions.firestore
  112. .document('shoppingList/{id}')
  113. .onCreate((snap, context) => {
  114.   // Get an object representing the document
  115.   // e.g. {'name': 'Marie', 'age': 66}
  116.   var flatId = snap.get('flat');
  117.  
  118.   var item = snap.get('contents');
  119.  
  120.  
  121.   var flatmates;
  122.   var flatName;
  123.   var ref1 = admin.firestore().collection('flats').doc(flatId);
  124.   return ref1.get()
  125.     .then(function(doc){
  126.  
  127.         const data = doc.data();
  128.         console.log(data);
  129.         flatmates = data.flatmates;
  130.         console.log(flatmates);
  131.         flatName = data.name;
  132.  
  133.       const payload = {
  134.         notification: {
  135. title: 'New item added to shopping list in ' + data.name,
  136. body: item
  137. }
  138.         };
  139.  
  140.         const options = {
  141. priority: 'high',
  142. timeToLive: 60 * 60 * 24, // 1 day
  143. };
  144.  
  145.         for(i = 0; i < flatmates.length; i++)
  146.        {
  147.           var uid = flatmates[i];
  148.           //if(token !== null || token !==""){
  149.               findAndSend(uid, payload, options);
  150.           //}
  151.         }
  152.  
  153.         return;
  154.   // access a particular field as you would any JS property
  155.  
  156.  
  157.   //admin.firestore.doc('complaints/{id}').set(newValue);
  158.  
  159.   // perform desired operations ...
  160. }, function (errorObject) {
  161.         console.log("The read failed: " + errorObject.code);
  162.     });
  163. });
  164.  
  165.  
  166. exports.onItemUpdated =  functions.firestore
  167. .document('shoppingList/{id}')
  168. .onUpdate((snap, context) => {
  169.   // Get an object representing the document
  170.   // e.g. {'name': 'Marie', 'age': 66}
  171.   const change = snap.after.data();
  172.   var flatId = change.flat;
  173.  
  174.   var item = change.contents;
  175.   var author = change.author;
  176.   var authorName = "";
  177.  
  178.   if(author !== null && author !== "")
  179.   {
  180.  
  181.   var flatmates;
  182.   var flatName;
  183.   var ref1 = admin.firestore().collection('flats').doc(flatId);
  184.   return ref1.get()
  185.     .then(function(doc){
  186.  
  187.         const data = doc.data();
  188.         console.log(data);
  189.         flatmates = data.flatmates;
  190.         console.log(flatmates);
  191.         flatName = data.name;
  192.       return  admin.firestore().collection('users').doc(author).get()
  193.           .then(function(doc2){
  194.             const data2 = doc2.data();
  195.             authorName = data2.nickname;
  196.       const payload = {
  197.         notification: {
  198. title: 'Shopping list in ' + data.name,
  199. body: authorName +" is assigned to buy "+ item
  200. }
  201.         };
  202.  
  203.         const options = {
  204. priority: 'high',
  205. timeToLive: 60 * 60 * 24, // 1 day
  206. };
  207.  
  208.         for(i = 0; i < flatmates.length; i++)
  209.        {
  210.           var uid = flatmates[i];
  211.           //if(token !== null || token !==""){
  212.               findAndSend(uid, payload, options);
  213.           //}
  214.         }
  215.  
  216.         return;
  217.   // access a particular field as you would any JS property
  218.  
  219.  
  220.   //admin.firestore.doc('complaints/{id}').set(newValue);
  221.  
  222.   // perform desired operations ...
  223. });
  224. }, function (errorObject) {
  225.         console.log("The read failed: " + errorObject.code);
  226.     });
  227.   }
  228. });
  229.  
  230.  
  231.  
  232. exports.onItemDeleted =  functions.firestore
  233. .document('shoppingList/{id}')
  234. .onDelete((snap, context) => {
  235.   // Get an object representing the document
  236.   // e.g. {'name': 'Marie', 'age': 66}
  237.   var flatId = snap.get('flat');
  238.  
  239.   var item = snap.get('contents');
  240.  
  241.  
  242.   var flatmates;
  243.   var flatName;
  244.   var ref1 = admin.firestore().collection('flats').doc(flatId);
  245.   return ref1.get()
  246.     .then(function(doc){
  247.  
  248.         const data = doc.data();
  249.         console.log(data);
  250.         flatmates = data.flatmates;
  251.         console.log(flatmates);
  252.         flatName = data.name;
  253.  
  254.       const payload = {
  255.         notification: {
  256. title: 'Shopping list in ' + data.name,
  257. body: 'Item ' +item + ' removed from shoppping list.'
  258. }
  259.         };
  260.  
  261.         const options = {
  262. priority: 'high',
  263. timeToLive: 60 * 60 * 24, // 1 day
  264. };
  265.  
  266.         for(i = 0; i < flatmates.length; i++)
  267.        {
  268.           var uid = flatmates[i];
  269.           //if(token !== null || token !==""){
  270.               findAndSend(uid, payload, options);
  271.           //}
  272.         }
  273.  
  274.         return;
  275.   // access a particular field as you would any JS property
  276.  
  277.  
  278.   //admin.firestore.doc('complaints/{id}').set(newValue);
  279.  
  280.   // perform desired operations ...
  281. }, function (errorObject) {
  282.         console.log("The read failed: " + errorObject.code);
  283.     });
  284. });
  285.  
  286.  
  287.  
  288.  
  289.  
  290. function findAndSend(uid, payload, options)
  291. {
  292.   console.log()
  293.   return admin.firestore().collection('users').doc(uid).get()
  294.     .then(function(doc2){
  295.       var token = doc2.data().token;
  296.       if(token !== null && token !==""){
  297.         return admin.messaging().sendToDevice(token, payload, options);
  298.       }
  299.       return;
  300.     })
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement