Advertisement
FiringBlanks

dialogAddSchool (TS)

Oct 27th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. addSchool(user, schoolToAddName, schoolToAddCountry, schoolToAddState, schoolToAddCity, confirmationCost){
  2. console.log('addSchool: ' + schoolToAddName);
  3.  
  4. let schoolObj;
  5.  
  6. //Check if country is in United States
  7. if(schoolToAddCountry == 'United States'){
  8. schoolObj = {
  9. name: schoolToAddName,
  10. schoolNameDisplay: schoolToAddName,
  11. country: schoolToAddCountry,
  12. state: schoolToAddState,
  13. city: schoolToAddCity,
  14. currentWeek: 0,
  15. maxWeeks: 16,
  16. creatorUID: user.uid,
  17. creatorName: user.displayName,
  18. creatorPhotoURL: user.photoURL,
  19. creationDate: Date.now(),
  20. currentOwnerUID: user.uid,
  21. currentOwnerName: user.displayName,
  22. currentOwnerPhotoURL: user.photoURL,
  23. autoIncrementWeeks: true
  24. }
  25. } else { //Else not in US, so 'state' property is not needed
  26. schoolObj = {
  27. name: schoolToAddName,
  28. schoolNameDisplay: schoolToAddName,
  29. country: schoolToAddCountry,
  30. state: null,
  31. city: schoolToAddCity,
  32. currentWeek: 0,
  33. maxWeeks: 16,
  34. creatorUID: user.uid,
  35. creatorName: user.displayName,
  36. creatorPhotoURL: user.photoURL,
  37. creationDate: Date.now(),
  38. currentOwnerUID: user.uid,
  39. currentOwnerName: user.displayName,
  40. currentOwnerPhotoURL: user.photoURL,
  41. autoIncrementWeeks: true
  42. }
  43.  
  44. }
  45.  
  46. //Add school under Schools Collection
  47. this.afs.collection('Schools').doc(schoolToAddName).set(schoolObj).then(() => {
  48.  
  49. //Navigate user to newly created school's Browse Teachers page
  50.  
  51. //Persist values
  52. this.persistingData.changeSchool(schoolToAddName);
  53.  
  54. //Route to newly created School
  55. this.router.navigateByUrl(schoolToAddName);
  56.  
  57. //?Increment user's createdSchools count. Deprecated - hold full docs of created items instead of basic counters of them
  58. // this.afs.collection('Users').doc(user.uid).ref.update({
  59. // createdSchools: firebase.firestore.FieldValue.increment(1)
  60. // });
  61.  
  62. let pathCreated = `Users/${user.uid}/Created Schools/${schoolToAddName}`;
  63.  
  64. //Add school to Users > User's UID > Created > School Name
  65. this.afs.doc(pathCreated).set({
  66. schoolName: schoolObj.name,
  67. schoolNameDisplay: schoolToAddName,
  68. country: schoolObj.country,
  69. state: schoolObj.state,
  70. city: schoolObj.city,
  71. currentWeek: schoolObj.currentWeek,
  72. maxWeeks: schoolObj.maxWeeks,
  73. creatorUID: schoolObj.creatorUID,
  74. creatorName: schoolObj.creatorName,
  75. creatorPhotoURL: schoolObj.creatorPhotoURL,
  76. creationDate: schoolObj.creationDate,
  77. currentOwnerUID: user.uid,
  78. currentOwnerName: user.displayName,
  79. currentOwnerPhotoURL: user.photoURL
  80. }).then(() => {
  81. //Increment adding user's usersCounters.totalCreationsSchools counter
  82. this.afs.doc(`Users/${user.uid}`).ref.update({
  83. 'usersCounters.totalCreationsSchools': firebase.firestore.FieldValue.increment(1)
  84. });
  85. });
  86.  
  87. //Close all dialogs
  88. // this.dialog.closeAll();
  89.  
  90. })
  91.  
  92. //Add Can Edit School collection under newly added school
  93. this.afs.collection('Schools').doc(schoolToAddName).collection('Can Edit School').doc(user.uid).set({
  94. uid: user.uid,
  95. displayName: user.displayName,
  96. photoURL: user.photoURL,
  97. isCreator: true,
  98. isCurrentOwner: true,
  99. userRole: user.userRole
  100. });
  101.  
  102. //Bump user's points
  103. this.bumpUsersPointsCount(user, confirmationCost);
  104.  
  105. //Add activity to User and Master Log
  106. this.addActivityToActivityLogs(user, schoolToAddName, undefined, undefined, `Added school '${schoolToAddName}'`, true, false, true, null, false);
  107.  
  108. this.snackbar.open('School added', 'Got it', {duration: 2000});
  109. this.dialog.closeAll();
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement