Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const runtimeOpts: any = {
- timeoutSeconds: 540, // 9 minutes
- memory: '2GB'
- }
- // Take the existing DB struct and restruct to accommodate this app's schema.
- exports.dbReconfig = functions
- .runWith(runtimeOpts)
- .https.onCall((data) => {
- // Get each record doc then each child from within the doc and save that child to
- // the top-level 'students' collection.
- return db.collection('records').get()
- .then((querySnapshot: QuerySnapshot) => {
- querySnapshot.forEach((doc: QueryDocumentSnapshot) => {
- const recordId = doc.id;
- const _fatherEmail = doc.data().fatherEmail;
- const _motherEmail = doc.data().motherEmail;
- const children = doc.data().children;
- if (children) {
- const childrenArr = convertMapToArray(children);
- childrenArr.forEach(child => {
- const fDob = new Date(child.dob._seconds * 1000);
- const childObj = {
- dob: fDob,
- recordId,
- fatherEmail: _fatherEmail,
- motherEmail: _motherEmail,
- ...child,
- }
- return db.collection('students').doc(child.id).set(childObj)
- .then( () => {
- return db.collection('financials').get()
- .then((qs: QuerySnapshot) => {
- qs.forEach((fDoc: QueryDocumentSnapshot) => {
- // A way to filter out the unwanted properties and keep desired ones.
- // See https://codeburst.io/use-es2015-object-rest-operator-to-omit-properties-38a3ecffe90
- const {childFirstName, childLastName, fatherEmail, motherEmail, grade, ...keep} = fDoc.data();
- return db.collection('students').doc(fDoc.id).collection('financials').doc(fDoc.id).set(keep)
- .then(()=> {
- // Go to the top-level 'financials' collection and
- // get all the docs from each subcollection with each financial doc.
- const collections = ['extendedCareCharges',
- 'extendedCarePayments',
- 'lunchCharges',
- 'lunchPayments',
- 'miscCharges',
- 'miscPayments',
- 'tuitionCharges',
- 'tuitionPayments'];
- collections.forEach(collection => {
- return db.collection('financials').doc(fDoc.id).collection(collection)
- .get()
- .then((qss: QuerySnapshot) => {
- if (qss) { // collection may not exist.
- console.log(`MD: qss`, qss);
- // Write each subcollection and their docs to subcollections under the applicable doc in the students collection.
- qss.forEach((d: QueryDocumentSnapshot) => {
- console.log(`MD: d.data()`, d.data());
- return db.collection('students').doc(fDoc.id).collection('financials').doc(fDoc.id)
- .collection(collection).doc(d.id).set(d.data());
- })
- }
- })
- })
- })
- })
- })
- });
- }); // end forEach(child => ...)
- } else {
- console.log("No children for this record.");
- }
- });
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement