Advertisement
stackoversnow

Untitled

Oct 26th, 2022
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. exports.checkSchedine = functions.pubsub
  2.   .schedule('45 * * * *')
  3.   .onRun(async (context) => {
  4.     var currentTime = Math.floor(Date.now() / 1000);
  5.    
  6.     const risultati = await firestore.collection("Risultati").doc("risultati").get();
  7.     const ref1 = await firestore.collection("Schedine").get();
  8.     await ref1.forEach(async doc => {
  9.       const notification1 = {
  10.         headings: {
  11.           'en' : 'Schedina vinta!'
  12.         },
  13.         contents: {
  14.           'en': 'Hai vinto una schedina. Controlla il tuo nuovo saldo.',
  15.         },
  16.         include_external_user_ids: [doc.id],
  17.         android_accent_color:"F44336",
  18.  
  19.       };
  20.  
  21.       const notification2 = {
  22.         headings: {
  23.           'en' : 'Schedina persa!'
  24.         },
  25.         contents: {
  26.           'en': 'Hai perso una schedina.',
  27.         },
  28.         include_external_user_ids: [doc.id],
  29.         android_accent_color:"F44336",
  30.  
  31.       };
  32.  
  33.       const ref2 = await firestore.collection("Schedine").doc(doc.id).collection("in corso").get();
  34.       await ref2.forEach(async doc2 => {
  35.         var i =0;
  36.         var j = 0;
  37.         for(matchId in doc2.data()["Match"]){
  38.           j++;
  39.  
  40.  
  41.           for(id in risultati.data()){
  42.             if(matchId == id && risultati.data()[id]["status"]!=0 && doc2.data()["Match"][matchId]["status"]==0){
  43.               if((doc2.data()["Match"][matchId]["Bet"]==1 || doc2.data()["Match"][matchId]["Bet"]==2 || doc2.data()["Match"][matchId]["Bet"]==3) && doc2.data()["Match"][matchId]["Bet"]==risultati.data()[id].ris){
  44.                 await firestore.collection('Schedine').doc(doc.id).collection("in corso").doc(doc2.id).update({
  45.                     ['Match.'+matchId+'.status']  : 1,
  46.  
  47.               });
  48.  
  49.  
  50.  
  51.  
  52.             } else if(doc2.data()["Match"][matchId]["Bet"]==4 && risultati.data()[id].goal ==1){
  53.               await firestore.collection('Schedine').doc(doc.id).collection("in corso").doc(doc2.id).update({
  54.                   ['Match.'+matchId+'.status']  : 1,
  55.  
  56.             });
  57.  
  58.           } else if(doc2.data()["Match"][matchId]["Bet"]==5 && risultati.data()[id].goal ==0){
  59.               await firestore.collection('Schedine').doc(doc.id).collection("in corso").doc(doc2.id).update({
  60.                   ['Match.'+matchId+'.status']  : 1,
  61.  
  62.             });
  63.  
  64.           } else if(doc2.data()["Match"][matchId]["Bet"]==6 && risultati.data()[id].over ==1){
  65.                 await firestore.collection('Schedine').doc(doc.id).collection("in corso").doc(doc2.id).update({
  66.                     ['Match.'+matchId+'.status']  : 1,
  67.  
  68.               });
  69.  
  70.             } else if(doc2.data()["Match"][matchId]["Bet"]==7 && risultati.data()[id].over ==0){
  71.                   await firestore.collection('Schedine').doc(doc.id).collection("in corso").doc(doc2.id).update({
  72.                       ['Match.'+matchId+'.status']  : 1,
  73.  
  74.                 });
  75.  
  76.                 }  else {
  77.               await firestore.collection('Schedine').doc(doc.id).collection("in corso").doc(doc2.id).update({
  78.                 ['Match.'+matchId+'.status']  : 2,
  79.  
  80.               });
  81.  
  82.               await firestore.collection('Schedine').doc(doc.id).collection("finite").doc(doc2.id).set(doc2.data());
  83.               await firestore.collection('Schedine').doc(doc.id).collection("finite").doc(doc2.id).update({
  84.                 "esito" : 2, "timestamp": currentTime
  85.               });
  86.               await firestore.collection('Schedine').doc(doc.id).collection("in corso").doc(doc2.id).delete();
  87.               try {
  88.               const response = await client.createNotification(notification2);
  89.             } catch (e) {}
  90.  
  91.             }
  92.  
  93.           }
  94.           }
  95.  
  96.           if(doc2.data()["Match"][matchId]["status"]==1 ){
  97.             i++;
  98.           }
  99.  
  100.  
  101.  
  102.         }
  103.  
  104.  
  105.  
  106.         if(i==j && i!=0){
  107.  
  108.             await firestore.collection("Users").doc(doc.id).update({ "vinte" : fieldValue.increment(1)});
  109.           await firestore.collection('Schedine').doc(doc.id).collection("finite").doc(doc2.id).set(doc2.data());
  110.           await firestore.collection('Schedine').doc(doc.id).collection("finite").doc(doc2.id).update({
  111.             "esito" : 1,  "timestamp": currentTime,
  112.           });
  113.           try {
  114.           const response = await client.createNotification(notification1);
  115.         } catch (e) {}
  116.         const refuser = await firestore.collection("Users").doc(doc.id).get();
  117.         var newbalance = refuser.data()["balance"] + doc2.data()["vincita"];
  118.         await firestore.collection("Users").doc(doc.id).update({
  119.           "balance" : newbalance
  120.         });
  121.         await firestore.collection('Schedine').doc(doc.id).collection("in corso").doc(doc2.id).delete();
  122.  
  123.  
  124.  
  125.  
  126.         }
  127.       });
  128.     });
  129.  
  130.  
  131.  
  132.  
  133.  
  134.   return null;
  135. });
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement