-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #169 from codeforbtv/write-data-to-firestore
Write data to firestore
- Loading branch information
Showing
4 changed files
with
260 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// TODO look into using an existing library instead: https://blog.mailtrap.io/react-native-email-validation | ||
const emailRegex = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | ||
|
||
const isValidEmail = (address) => emailRegex.test(address); | ||
module.exports.isValidEmail = isValidEmail; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const {db} = require('./user'); | ||
let usersCollection = db.collection('users'); | ||
let userDoc; | ||
class Goal { | ||
//TODO: add data validation to all properties | ||
constructor(useruid) { | ||
if (!useruid) { | ||
console.log("Must provide a user uid when creating a new goal"); | ||
return; | ||
// Add better error handling | ||
} | ||
this.goaluid = ''; | ||
this.useruid = useruid;//unique id of the user which this goal corresponds to | ||
this.goalType = ''; | ||
this.goalDueDate = ''; | ||
this.goalNotes = ''; | ||
this.isGoalComplete = ''; | ||
this.created = Date.now(); | ||
userDoc = usersCollection.doc(this.useruid); | ||
} | ||
|
||
printAllFieldsToConsole() { | ||
console.log ( | ||
"User uid: " + this.useruid + "\n" + | ||
"Goal uid: " + this.goaluid + "\n" + | ||
"Goal type: " + this.goalType + "\n" + | ||
"Goal due date: " + this.goalDueDate + "\n" + | ||
"Goal notes: " + this.goalNotes + "\n" + | ||
"Goal Complete?: " + this.isGoalComplete + "\n") | ||
} | ||
|
||
createNewGoalInFirestore() { | ||
console.log("Creating a new goal for user " + this.useruid + " with the following data:\n"); | ||
this.printAllFieldsToConsole(); | ||
userDoc.collection('goals').doc(this.goaluid).set({ | ||
created: this.created, | ||
goaluid: this.goaluid, | ||
useruid: this.useruid, | ||
goalType: this.goalType, | ||
goalDue: this.goalDueDate, | ||
goalNotes: this.goalNotes, | ||
isGoalComplete: this.isGoalComplete | ||
}); | ||
} | ||
|
||
updateExistingGoalInFirestore () { | ||
let goalDoc = userDoc.collection('goals').doc(this.goaluid); | ||
console.log("Updating goal id " + this.goaluid + " with the following:\n"); | ||
this.printAllFieldsToConsole(); | ||
if (this.goalType) { | ||
goalDoc.update({goalType: this.goalType}); | ||
} | ||
if (this.goalDueDate) { | ||
goalDoc.update({goalDue: this.goalDueDate}); | ||
} | ||
if (this.goalNotes) { | ||
goalDoc.update({goalNotes: this.goalNotes}); | ||
} | ||
if (this.isGoalComplete) { | ||
goalDoc.update({isGoalComplete: this.isGoalComplete}); | ||
} | ||
} | ||
} | ||
module.exports = Goal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
const admin = require('firebase-admin'); | ||
// TODO: add consts for field names in the db | ||
admin.initializeApp(); | ||
const db = admin.firestore(); | ||
let usersCollection = db.collection('users'); | ||
class User { | ||
//TODO: add data validation to all properties | ||
constructor(uid) { | ||
if (!uid) { | ||
console.log("Must provide a user uid when creating a new user"); | ||
return; | ||
// TODO: Add better error handling here | ||
} | ||
this.uid = uid; // this corresponds to Outcome Tracker's "System Name ID" | ||
this.email = ''; | ||
this.firstName = ''; | ||
this.lastName = ''; | ||
this.dateCreated = Date.now();//TODO: switch this to a more readable date format | ||
} | ||
printAllFieldsToConsole() { | ||
console.log ( | ||
"uid: " + this.uid + "\n" + | ||
"First name: " + this.firstName + "\n" + | ||
"Last name: " + this.lastName + "\n" + | ||
"email: " + this.email + "\n") | ||
} | ||
createNewUserInFirestore() { | ||
console.log("Creating a new document with uid " + this.uid + " with the following data:\n"); | ||
this.printAllFieldsToConsole(); | ||
usersCollection.doc(this.uid).set({ | ||
created: this.dateCreated, | ||
uid: this.uid, | ||
displayName: this.firstName, | ||
lastName: this.lastName, | ||
email: this.email | ||
}); | ||
} | ||
|
||
updateExistingUserInFirestore () { | ||
console.log("Updating uid " + this.uid + " with the following:\n"); | ||
this.printAllFieldsToConsole(); | ||
if (this.firstName) { | ||
usersCollection.doc(this.uid).update({displayName: this.firstName}); | ||
} | ||
if (this.lastName) { | ||
usersCollection.doc(this.uid).update({lastName: this.lastName}); | ||
} | ||
if (this.email) { | ||
usersCollection.doc(this.uid).update({email: this.email}); | ||
} | ||
} | ||
} | ||
module.exports = User; | ||
module.exports.db = db; |