Advertisement
jakariamasud

cloud-functions

Mar 17th, 2020
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.41 KB | None | 0 0
  1. const functions = require('firebase-functions');
  2. const admin = require('firebase-admin');
  3. admin.initializeApp();
  4.  
  5. exports.requestNotification=functions.database.ref('/Users/{userId}/Book_requests/{requestId}')
  6. .onCreate((snap, context) =>{
  7. const object=snap.val();
  8. const recevierId=context.params.userId;
  9. const requestId=context.params.requestId;
  10. const requesterId=object['requesterId'];
  11. const bookId=object['bookId'];
  12.  
  13. const requesterRef=admin.database().ref('/Users/'+requesterId).once('value');
  14. const bookRef=admin.database().ref('/Books/'+bookId).once('value');
  15. const recieverRef=admin.database().ref('/Users/'+recevierId).once('value');
  16.  
  17. return Promise.all([requesterRef,recieverRef,bookRef]).then((result=>{
  18. const requesterName=result[0].val()['name'];
  19. const reciverToken=result[1].val()['deviceToken'];
  20. const bookTitle=result[2].val()['title'];
  21. const recieverName=result[1].val()['name'];
  22. const requesterHistory='you have requested to '+recieverName+' for the book '+bookTitle;
  23. const recieverHistory=requesterName+' requests for the books named '+bookTitle;
  24. const historyRefRequester=admin.database().ref('/Users/'+requesterId+'/History/').push({
  25. historyType:"request",
  26. historyMsg:requesterHistory
  27. });
  28. const historyRefReciever=admin.database().ref('/Users/'+recevierId+'/History/').push({
  29. historyType:"request",
  30. historyMsg:recieverHistory
  31. });
  32. return Promise.all([historyRefRequester,historyRefReciever]).then((result=>{
  33. const payload={
  34. notification:{
  35. title:" Book Request ",
  36. body:requesterName+" wants one of your book named "+bookTitle
  37. },
  38. data:{
  39. Type:"request"
  40. }
  41. }
  42. return admin.messaging().sendToDevice(reciverToken,payload).catch((error=>{
  43. console.log('error occured',error);
  44. }));
  45.  
  46. }))
  47.  
  48.  
  49.  
  50. }))
  51.  
  52.  
  53. });
  54.  
  55. exports.acceptNotification=functions.database.ref('/Users/{userId}/Book_requests/{requestId}')
  56. .onUpdate((change, context) =>{
  57. const updatedObj=change.after.val();
  58. const borrowerId=updatedObj['requesterId'];
  59. const requestId=context.params.requestId
  60. const lenderId =context.params.userId;
  61. const bookId=updatedObj['bookId'];
  62. const bookRef=admin.database().ref('/Books/'+bookId).once('value');
  63. const borrowerRef=admin.database().ref('/Users/'+borrowerId).once('value');
  64. const lenderRef=admin.database().ref('/Users/'+lenderId).once('value');
  65.  
  66. return Promise.all([borrowerRef,lenderRef,bookRef]).then((result=>{
  67. const bookTitle=result[2].val()['title'];
  68. const borrowerName=result[0].val()['name'];
  69. const lenderName=result[1].val()['name'];
  70. const borrowerHistoy='your request for the book '+bookTitle+' has been accepted by '+lenderName;
  71. const lenderHistory='you have accepted '+borrowerName+' requests for the book'+bookTitle;
  72. const borrowerToken=result[0].val()['deviceToken'];
  73. const historyRefBorrower=admin.database().ref('/Users/'+borrowerId+'/History/').push({
  74. historyType:"request accepted",
  75. historyMsg:borrowerHistoy
  76. });
  77. const historyRefLender=admin.database().ref('/Users/'+lenderId+'/History/').push({
  78. historyType:"request accepted",
  79. historyMsg:lenderHistory
  80. });
  81. const updateBookStatus=admin.database().ref('/Books/'+bookId+'/').update({
  82. status:'Not Available'
  83. });
  84. const originalBook=result[2].val();
  85. originalBook['borrowerId']=borrowerId;
  86. const deleteFromUserBookList=admin.database().ref('/Users/'+lenderId+'/Books/Available/'+bookId).remove();
  87. const requestDelete=admin.database().ref('/Users/'+lenderId+'/Book_requests/'+requestId).remove();
  88. const borrowedBookListRef=admin.database().ref('/Users/'+borrowerId+'/Books/Borrowed/'+bookId).set(originalBook);
  89. const lendBookListRef=admin.database().ref('/Users/'+lenderId+'/Books/Lend/'+bookId).set(originalBook);
  90. return Promise.all([historyRefBorrower,historyRefLender,updateBookStatus,borrowedBookListRef,lendBookListRef,deleteFromUserBookList,requestDelete]).then((result=>{
  91. const payload={
  92. notification:{
  93. title:" Book Request Accepted ",
  94. body:lenderName+" accepted your request for the book "+bookTitle
  95. },
  96. data:{
  97. Type:"requestAccept"
  98. }
  99. }
  100.  
  101. return admin.messaging().sendToDevice(borrowerToken,payload).catch((error=>{
  102. console.log("error occured in"+error);
  103. }));
  104. }))
  105.  
  106.  
  107. }));
  108.  
  109.  
  110.  
  111. });
  112.  
  113.  
  114. exports.returnRequestNotification=functions.database.ref('/Users/{recieverId}/Return_requests/{requestId}')
  115. .onCreate((snapshot,context)=>{
  116. const recevierId=context.params.recieverId;
  117. const requestId=context.params.requestId;
  118. const object=snapshot.val();
  119. const requesterName=object['requesterName'];
  120. const requesterId=object['requesterId'];
  121. const bookTitle=object['bookTitle'];
  122. const recieverRef=admin.database().ref('/Users/'+recevierId).once('value');
  123. return recieverRef.then((result=>{
  124. const recieverToken=result.val()['deviceToken'];
  125. const recieverName=result.val()['name'];
  126. const requesterHistory='you have requested to '+recieverName+' to return the book '+bookTitle;
  127. const recieverHistory=requesterName+' wants to return the book '+bookTitle;
  128. const historyRefRequester=admin.database().ref('/Users/'+requesterId+'/History/').push({
  129. historyType:"return request",
  130. historyMsg:requesterHistory
  131. });
  132. const historyRefReciever=admin.database().ref('/Users/'+recevierId+'/History/').push({
  133. historyType:"return request",
  134. historyMsg:recieverHistory
  135. });
  136.  
  137. return Promise.all([historyRefRequester,historyRefReciever]).then((result=>{
  138. const payload={
  139. notification:{
  140. title:" Book Return Request ",
  141. body:requesterName+" wants to return your book named "+bookTitle
  142. },
  143. data:{
  144. Type:"returnRequest"
  145. }
  146. }
  147.  
  148. return admin.messaging().sendToDevice(recieverToken,payload).catch((error=>{
  149. console.log("error occured in"+error);
  150. }))
  151.  
  152. }))
  153.  
  154. }))
  155.  
  156. })
  157. exports.acceptReturnRequestNotification=functions.database.ref('/Users/{recieverId}/Return_requests/{requestId}')
  158. .onUpdate((change,context)=>{
  159. const object=change.after.val();
  160. const recieverId=context.params.recieverId;
  161. const requestId=context.params.requestId;
  162. const requesterId=object['requesterId'];
  163. const bookId=object['bookId'];
  164. const bookTitle=object['bookTitle'];
  165. const requesterRef=admin.database().ref('/Users/'+requesterId).once('value');
  166. const recieverRef=admin.database().ref('/Users/'+recieverId).once('value');
  167. return Promise.all([requesterRef,recieverRef]).then((result=>{
  168. const requesterName=result[0].val()['name'];
  169. const requesterToken=result[0].val()['deviceToken'];
  170. const recieverName=result[1].val()['name'];
  171. const requesterHistoy='your return request for the book '+bookTitle+' has been accepted by '+recieverName;
  172. const recieverHistory='you have accepted return requests of '+requesterName+' for the book '+bookTitle;
  173.  
  174. const historyRefRequester=admin.database().ref('/Users/'+requesterId+'/History/').push({
  175. historyType:"return request accepted",
  176. historyMsg:requesterHistoy
  177. });
  178. const historyRefReciever=admin.database().ref('/Users/'+recieverId+'/History/').push({
  179. historyType:"return request accepted",
  180. historyMsg:recieverHistory
  181. });
  182. const updateBookStatus=admin.database().ref('/Books/'+bookId+'/').update({
  183. status:'Available'
  184. });
  185.  
  186.  
  187. return Promise.all([historyRefRequester,historyRefReciever,updateBookStatus])
  188. .then((result=>{
  189. const bookRef=admin.database().ref('/Books/'+bookId).once('value');
  190. return bookRef.then((result=>{
  191. const bookObj=result.val();
  192. bookObj['borrowerId']="";
  193. const deleteFromlendList=admin.database().ref('/Users/'+recieverId+'/Books/Lend/'+bookId).remove();
  194. const deleteFromBorrowList=admin.database().ref('/Users/'+requesterId+'/Books/Borrowed/'+bookId).remove();
  195. const insertIntoAvailableList=admin.database().ref('/Users/'+recieverId+'/Books/Available/'+bookId).set(bookObj);
  196. const deleteRequest=admin.database().ref('/Users/'+recieverId+'/Return_requests/'+requestId).remove();
  197. return Promise.all([deleteFromlendList,deleteFromBorrowList,insertIntoAvailableList,deleteRequest])
  198. .then((result=>{
  199. const payload={
  200. notification:{
  201. title:"Return Book Request Accepted ",
  202. body:recieverName+" accepted your return request for the book "+bookTitle
  203. },
  204. data:{
  205. Type:"returnRequestAccept"
  206. }
  207. }
  208.  
  209. return admin.messaging().sendToDevice(requesterToken,payload).catch((error=>{
  210. console.log(error);
  211. }))
  212.  
  213. }))
  214.  
  215.  
  216.  
  217. }))
  218.  
  219. }))
  220. }))
  221.  
  222.  
  223.  
  224. })
  225.  
  226. exports.scheduledFunctionCrontab = functions.pubsub
  227. .schedule('50 19 * * *').onRun((context) => {
  228. console.log('This will be run every day at 19:50 AM UTC!');
  229. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement