Skip to content

Commit

Permalink
Update sprout.js
Browse files Browse the repository at this point in the history
  • Loading branch information
camillereaves authored Sep 23, 2023
1 parent b6a1e98 commit bce840e
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions public/sprout.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ document.getElementById("login_form").addEventListener("submit", async function
let day = String(date.getDay()).padStart(2,"0");
let year = String(date.getFullYear()).slice(2);
let userNameExists = await testUserName(firstName.slice(0,1).toLowerCase() + lastName.toLowerCase() + month + year);
console.log("userNameExists" + userNameExists);
console.log("userNameExists = " + userNameExists);

let username = await generateUsername(userNameExists, firstName, lastName, month, day, year);
console.log("username" + username);
console.log("username = " + username);

const newUser = {
userEmail: userEmail,
Expand All @@ -148,7 +148,7 @@ document.getElementById("login_form").addEventListener("submit", async function
}

let emailAlreadyInUse = await testUserEmail(userEmail);
console.log("emailAlreadyInUse"+ emailAlreadyInUse);
console.log("emailAlreadyInUse = "+ emailAlreadyInUse);

if(!emailAlreadyInUse){
await setDoc(doc(db, 'new_user_requests', username.toString()), newUser);
Expand All @@ -167,20 +167,21 @@ document.getElementById("login_form").addEventListener("submit", async function

function generateUsername(userNameExists, firstName, lastName, month, day, year){
let username = "TBD";
if(!userNameExists){
username = firstName.slice(0,1).toLowerCase() + lastName.toLowerCase() + month + year;
if(userNameExists > 0){
username = String(firstName.slice(0,1).toLowerCase() + lastName.toLowerCase() + userNameExists + month + year);
}
else{
username = firstName.toLowerCase() + lastName.toLowerCase() + month + year;
username = String(firstName.slice(0,1).toLowerCase() + lastName.toLowerCase() + month + year);
}

return username;
}

async function testUserEmail(testEmail){
testEmail = testEmail.toString();
const emailCheck = await query(newUserRequest, where('UserEmail', '==', testEmail));
if (emailCheck.exists){
const q = query(newUserRequest, where('UserEmail', '==', testEmail));
const checkEmail = (await getDocs(q)).docs.length;
if (checkEmail > 0){
return true;
} else{
return false;
Expand All @@ -190,8 +191,9 @@ async function testUserEmail(testEmail){
async function testUserName(testUsername){
testUsername = testUsername.toString();
const docRef = doc(db, 'new_user_requests', testUsername);
const docCheck = await getDoc(docRef);
if (docCheck.exists){
const docCheck = (await getDoc(docRef)).docs.length;
console.log("docCheck = ");
if (docCheck > 0){
return true;
} else{
return false;
Expand Down

0 comments on commit bce840e

Please sign in to comment.