Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   const runtimeOpts: any = {
  2.     timeoutSeconds: 540, // 9 minutes
  3.     memory: '2GB'
  4.   }
  5.  
  6.   // Take the existing DB struct and restruct to accommodate this app's schema.
  7.   exports.dbReconfig = functions
  8.   .runWith(runtimeOpts)
  9.   .https.onCall((data) => {
  10.     // Get each record doc then each child from within the doc and save that child to
  11.     // the top-level 'students' collection.
  12.    return db.collection('records').get()
  13.     .then((querySnapshot: QuerySnapshot) => {
  14.                       querySnapshot.forEach((doc: QueryDocumentSnapshot) => {
  15.                         const recordId = doc.id;
  16.                         const _fatherEmail = doc.data().fatherEmail;
  17.                         const _motherEmail = doc.data().motherEmail;
  18.                         const children = doc.data().children;
  19.                         if (children) {
  20.                           const childrenArr = convertMapToArray(children);
  21.                           childrenArr.forEach(child => {
  22.                             const fDob = new Date(child.dob._seconds * 1000);
  23.                             const childObj = {
  24.                               dob: fDob,
  25.                               recordId,
  26.                               fatherEmail: _fatherEmail,
  27.                               motherEmail: _motherEmail,
  28.                               ...child,
  29.                             }
  30.                            return db.collection('students').doc(child.id).set(childObj)
  31.                            .then( () => {
  32.                              return db.collection('financials').get()
  33.                               .then((qs: QuerySnapshot) => {
  34.                                 qs.forEach((fDoc: QueryDocumentSnapshot) => {
  35.                                   // A way to filter out the unwanted properties and keep desired ones.
  36.                                   // See https://codeburst.io/use-es2015-object-rest-operator-to-omit-properties-38a3ecffe90
  37.                                   const {childFirstName, childLastName, fatherEmail, motherEmail, grade, ...keep} = fDoc.data();
  38.                                   return db.collection('students').doc(fDoc.id).collection('financials').doc(fDoc.id).set(keep)
  39.                                     .then(()=> {
  40.                                       // Go to the top-level 'financials' collection and
  41.                                       // get all the docs from each subcollection with each financial doc.
  42.                                       const collections = ['extendedCareCharges',
  43.                                                             'extendedCarePayments',
  44.                                                             'lunchCharges',
  45.                                                             'lunchPayments',
  46.                                                             'miscCharges',
  47.                                                             'miscPayments',
  48.                                                             'tuitionCharges',
  49.                                                             'tuitionPayments'];
  50.                                       collections.forEach(collection => {
  51.                                         return db.collection('financials').doc(fDoc.id).collection(collection)
  52.                                         .get()
  53.                                         .then((qss: QuerySnapshot) => {
  54.                                           if (qss) { // collection may not exist.
  55.                                             console.log(`MD: qss`, qss);
  56.                                             // Write each subcollection and their docs to subcollections under the applicable doc in the students collection.
  57.                                             qss.forEach((d: QueryDocumentSnapshot) => {
  58.                                             console.log(`MD: d.data()`, d.data());
  59.                                               return db.collection('students').doc(fDoc.id).collection('financials').doc(fDoc.id)
  60.                                               .collection(collection).doc(d.id).set(d.data());
  61.                                             })
  62.                                           }
  63.                                          
  64.                                         })
  65.                                       })
  66.                                     })
  67.                                 })
  68.                               })
  69.                            });
  70.                           }); // end forEach(child => ...)
  71.                         } else {
  72.                           console.log("No children for this record.");
  73.                         }
  74.                       });
  75.  
  76.   });
  77. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement