Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functions for Task Submission Onclick and for getting Volunteer Count in methods.js #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 131 additions & 2 deletions methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,11 @@ async function getCompletedJobs(){
async function getJobsDoneEachMonth(){
try{
const user_completed_jobs = await getCompletedJobs();
let jobs_each_month = {};
user_completed_jobs.forEach((job) => {
const job_start_date = job["Start Date"];
const job_end_date = job["End Date"];
const jobs_each_month = {};

const job_start_month = job_start_date.split("/")[1];
const job_end_month = job_end_date.split("/")[1];
const job_start_year = job_start_date.split("/")[2];
Expand Down Expand Up @@ -147,11 +148,139 @@ async function getJobsDoneEachMonth(){
}


//Pass in the task_id of the task of which to add a volunteer
//task_id in OrgTaskDescription is data.data.taskID
//Task End Date not inputted when Task created. So Task End Date for now is being considered as Task Start Date
//Once Task End Date field is added to Task Form the Task End Date will be passed instead of Task Start Date due to conditions in the function

async function onPressJobCompleted(task_id){

try{
await auth.onAuthStateChanged(async function (org) {
if (org) {
const task_volunteeres_query = await query_db("TaskID","==",task_id,volunteers_collection);
const task_query = await query_db("Task ID","==",task_id,tasks_collection);
const task_query_res = task_query.docs[0].data();

const org_query = await query_db("Email","==",org.email,organisations_collection);
const org_query_res = org_query.docs[0].data();
let org_completed_jobs = org_query_res["Completed Projects"];
org_completed_jobs.push(task_query_res);
const org_doc_id = org_query.docs[0].id;
const org_doc_ref = doc(db, organisations_collection, org_doc_id);
await updateDoc(org_doc_ref, {
"Completed Projects": org_completed_jobs
});

if(!org_query_res.OrgID == task_query_res.OrgID){
throw "User is not the owner of the task";
}


task_volunteeres_query.forEach(async (volunteer_doc) => {
const volunteer_doc_data = volunteer_doc.data();
if(volunteers_doc_data["Status"]!="Pending"){
throw "Task "+ task_id + " is not Pending. Wait for everyone to rate!";
}
const volunteer_email = volunteer_doc_data.Email;
const volunteer_query = await query_db("Email","==",volunteer_email,users_collection);
const volunteer_query_res = volunteer_query.docs[0].data();
let volunteer_completed_jobs = volunteer_query_res.Completed_Jobs;
const task_end_date = task_query_res["Task End Date"];
if(task_end_date == undefined){
task_end_date = task_query_res["Task Start Date"];
}
const completed_job = {
"Start Date": task_query_res["Start Date"],
"End Date": task_end_date,
"Task": task_query_res.Name,
"OrgID": org_id,
"TaskID": task_id,

}
volunteer_completed_jobs.push(completed_job);
const volunteer_query_doc_id = volunteer_query.docs[0].id;
const volunteer_query_doc_ref = doc(db, users_collection, volunteer_query_doc_id);
await updateDoc(volunteer_query_doc_ref, {
Completed_Jobs: volunteer_completed_jobs,
});

const volunteer_doc_id = volunteer_doc.id;
const volunteer_doc_ref = doc(db, volunteers_collection, volunteer_doc_id);
await updateDoc(volunteer_doc_ref, {
Status: "Completed",
});
});



}
else{
throw "No user logged in";
}
});
}
catch(error){
console.error("Error querying document: ", error);
throw error;
}

}




async function getTotalVolunteersEachMonth(){
try{
auth.onAuthStateChanged(async function (org) {
if (org) {
let total_volunteers_each_month = {};
const org_query = await query_db("Email","==",org.email,organisations_collection);
const org_query_res = org_query.docs[0].data();
const org_id = org_query_res.OrgID;
const tasks_query = await query_db("OrgID","==",org_id,tasks_collection);
tasks_query.forEach((task) => {
const volunteers_count = task["Volunteers Registered"];
const task_start_date = task["Task Start Date"];
const task_end_date = task["Task End Date"];
const task_start_month = task_start_date.split("/")[1];
const task_end_month = task_end_date.split("/")[1];
const task_start_year = task_start_date.split("/")[2];
const task_end_year = task_end_date.split("/")[2];
if(total_volunteers_each_month.task_start_year == undefined){
total_volunteers_each_month.task_start_year = {};
}
if(total_volunteers_each_month.task_start_year.task_start_month == undefined){
total_volunteers_each_month.task_start_year.task_start_month = 0;
}
if(total_volunteers_each_month.task_end_year == undefined){
total_volunteers_each_month.task_end_year = {};
}
if(total_volunteers_each_month.task_end_year.task_end_month == undefined){
total_volunteers_each_month.task_end_year.task_end_month = 0;
}
if(!task_start_month == task_end_month || !task_start_year == task_end_year){
total_volunteers_each_month.task_end_year.task_end_month += volunteers_count;
}
total_volunteers_each_month.task_start_year.task_start_month += volunteers_count;


});
return total_volunteers_each_month;
}
else{
throw "No user logged in";
}
});
}
catch(error){
console.error("Error querying document: ", error);
throw error;
}
}





export {addNewDoc,query_db,users_collection,organisations_collection,auth,provider,environment,isNewUser,tasks_collection,task_images_storage_path,volunteers_collection,getJobsDoneEachMonth};
export {addNewDoc,query_db,users_collection,organisations_collection,auth,provider,environment,isNewUser,tasks_collection,task_images_storage_path,volunteers_collection,getJobsDoneEachMonth,onPressJobCompleted};