Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- addSchool(user, schoolToAddName, schoolToAddCountry, schoolToAddState, schoolToAddCity, confirmationCost){
- console.log('addSchool: ' + schoolToAddName);
- let schoolObj;
- //Check if country is in United States
- if(schoolToAddCountry == 'United States'){
- schoolObj = {
- name: schoolToAddName,
- schoolNameDisplay: schoolToAddName,
- country: schoolToAddCountry,
- state: schoolToAddState,
- city: schoolToAddCity,
- currentWeek: 0,
- maxWeeks: 16,
- creatorUID: user.uid,
- creatorName: user.displayName,
- creatorPhotoURL: user.photoURL,
- creationDate: Date.now(),
- currentOwnerUID: user.uid,
- currentOwnerName: user.displayName,
- currentOwnerPhotoURL: user.photoURL,
- autoIncrementWeeks: true
- }
- } else { //Else not in US, so 'state' property is not needed
- schoolObj = {
- name: schoolToAddName,
- schoolNameDisplay: schoolToAddName,
- country: schoolToAddCountry,
- state: null,
- city: schoolToAddCity,
- currentWeek: 0,
- maxWeeks: 16,
- creatorUID: user.uid,
- creatorName: user.displayName,
- creatorPhotoURL: user.photoURL,
- creationDate: Date.now(),
- currentOwnerUID: user.uid,
- currentOwnerName: user.displayName,
- currentOwnerPhotoURL: user.photoURL,
- autoIncrementWeeks: true
- }
- }
- //Add school under Schools Collection
- this.afs.collection('Schools').doc(schoolToAddName).set(schoolObj).then(() => {
- //Navigate user to newly created school's Browse Teachers page
- //Persist values
- this.persistingData.changeSchool(schoolToAddName);
- //Route to newly created School
- this.router.navigateByUrl(schoolToAddName);
- //?Increment user's createdSchools count. Deprecated - hold full docs of created items instead of basic counters of them
- // this.afs.collection('Users').doc(user.uid).ref.update({
- // createdSchools: firebase.firestore.FieldValue.increment(1)
- // });
- let pathCreated = `Users/${user.uid}/Created Schools/${schoolToAddName}`;
- //Add school to Users > User's UID > Created > School Name
- this.afs.doc(pathCreated).set({
- schoolName: schoolObj.name,
- schoolNameDisplay: schoolToAddName,
- country: schoolObj.country,
- state: schoolObj.state,
- city: schoolObj.city,
- currentWeek: schoolObj.currentWeek,
- maxWeeks: schoolObj.maxWeeks,
- creatorUID: schoolObj.creatorUID,
- creatorName: schoolObj.creatorName,
- creatorPhotoURL: schoolObj.creatorPhotoURL,
- creationDate: schoolObj.creationDate,
- currentOwnerUID: user.uid,
- currentOwnerName: user.displayName,
- currentOwnerPhotoURL: user.photoURL
- }).then(() => {
- //Increment adding user's usersCounters.totalCreationsSchools counter
- this.afs.doc(`Users/${user.uid}`).ref.update({
- 'usersCounters.totalCreationsSchools': firebase.firestore.FieldValue.increment(1)
- });
- });
- //Close all dialogs
- // this.dialog.closeAll();
- })
- //Add Can Edit School collection under newly added school
- this.afs.collection('Schools').doc(schoolToAddName).collection('Can Edit School').doc(user.uid).set({
- uid: user.uid,
- displayName: user.displayName,
- photoURL: user.photoURL,
- isCreator: true,
- isCurrentOwner: true,
- userRole: user.userRole
- });
- //Bump user's points
- this.bumpUsersPointsCount(user, confirmationCost);
- //Add activity to User and Master Log
- this.addActivityToActivityLogs(user, schoolToAddName, undefined, undefined, `Added school '${schoolToAddName}'`, true, false, true, null, false);
- this.snackbar.open('School added', 'Got it', {duration: 2000});
- this.dialog.closeAll();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement