Fixing add task calls for async await

This commit is contained in:
Brian McGonagill 2025-07-22 08:34:24 -05:00
parent ca7bcb1a8f
commit febb36d75f
5 changed files with 623 additions and 484 deletions

View file

@ -3,7 +3,7 @@
<form class="row" style="gap: 1em;">
<div class="col s12 m6 l4 chips chips-placeholder" id="taskName">
</div>
<div class="col s12 m6 l4 input-field outlined">
<div class="col s12 m6 l4 input-field">
<select name="taskUser" id="taskUser" class="taskUser">
<option value="" disabled selected>Assign to user...</option>
{{#each taskUsers}}
@ -11,7 +11,7 @@
{{/each}}
</select>
</div>
<div class="col s12 m6 l4 input-field outlined">
<div class="col s12 m6 l4 input-field">
<input type="text" class="datepicker" id="taskDate" />
<label for="taskDate">Task Date (multiple entries)</label>
<div class="row">

View file

@ -69,7 +69,6 @@ Template.taskForm.events({
let taskDateErr = false;
let userInfo;
let actDate = [];
// console.dir(taskNameArr);
if (taskNameArr == null || taskNameArr == []) {
taskNameErr = true;

View file

@ -33,36 +33,26 @@ Template.myTasksForm.events({
let taskDateArray = Session.get("taskDateArr");
let actDate = [];
console.dir(taskNameArray);
console.dir(taskDateArray);
if (taskNameArray == null || taskNameArray == [] || taskNameArray == "") {
taskNameErr = true;
}
if (taskDateArray == null || taskDateArray == []|| taskDateArray == "") {
taskDateErr = true;
} else {
for (let i = 0; i < taskDateArray.length; i++) {
// console.log(taskDateArray[i]);
let actDateTask = new Date(taskDateArray[i]);
actDate.push(actDateTask);
}
}
// console.log("Date Error: " + taskDateErr + " - Name Error: " + taskNameErr);
if (taskDateErr == false && taskNameErr == false) {
const addTask = async() => {
let result = await Meteor.callAsync("add.task", taskNameArray, "self", "selfId", taskDateArray, actDate);
if (!result) {
console.log(" ERROR adding task for self: ");
showSnackbar("Error adding task for self!", "red");
} else {
console.log(" SUCCESS adding task for self.");
Session.set("taskDateArr", []);
$("#myTaskName").val("");
$("#myTaskDate").val("");
showSnackbar("Added Tasks Successfully!", "green");
for (const task of taskNameArray) {
for (const date of taskDateArray) {
let actDate = new Date(date);
addTask(task.id, date, actDate);
}
}
addTask();
} else {
showSnackbar("Error! Both Task & Date are Required!", "red");
}
@ -93,4 +83,18 @@ Template.myTasksForm.events({
taskDateArr.push(taskDate);
Session.set("taskDateArr", taskDateArr);
},
});
});
const addTask = async(task, date, actDate) => {
let result = await Meteor.callAsync("add.myTask", task, date, actDate);
if (!result) {
console.log(" ERROR adding task for self: ");
showSnackbar("Error adding task for self!", "red");
} else {
console.log(" SUCCESS adding task for self.");
// Session.set("taskDateArr", []);
// $("#myTaskName").val("");
// $("#myTaskDate").val("");
showSnackbar("Added Tasks Successfully!", "green");
}
}

View file

@ -13,7 +13,7 @@ TaskItems.allow({
});
Meteor.methods({
'add.task' (taskNameArr, assignedTo, assignedToId, taskDateArr, actDate) {
async 'add.task' (taskNameArr, assignedTo, assignedToId, taskDateArr, actDate) {
check(taskNameArr, [Object]);
check(assignedTo, String);
check(taskDateArr, [String]);
@ -23,41 +23,15 @@ Meteor.methods({
if (!this.userId) {
throw new Meteor.Error('You are not allowed to add tasks. Make sure you are logged in with valid user credentials.');
}
let username;
let usInfo;
if (assignedTo == "self") {
const uInfo = async() => {
let userInfo = await Meteor.users.findOneAsync({ _id: this.userId });
if (!userInfo) {
console.log("No matching user info found.")
} else {
username = userInfo.profile.fullname;
assignedToId = this.userId;
let usInf = { "username": username, "assignedToId": assignedToId };
console.log("setting username = " + username +", and id = " + assignedToId);
return usInf;
}
}
usInfo = uInfo();
console.dir(usInfo);
} else {
username = assignedTo;
usInfo = { "username": assignedTo, "assignedToId": assignedToId };
console.log("Set username = " + assignedTo);
}
console.log("assignedToId = " + usInfo.assignedToId);
for (let i=0; i < taskDateArr.length; i++) {
for (let j=0; j < taskNameArr.length; j++) {
TaskItems.insertAsync({
await TaskItems.insertAsync({
taskName: taskNameArr[j].id,
taskDate: taskDateArr[i],
actualDate: actDate[i],
assignedTo: usInfo.username,
assignedToId: usInfo.assignedToId,
assignedTo: assignedTo,
assignedToId: assignedToId,
isComplete: false,
completedOn: null,
assignedOn: new Date(),
@ -65,9 +39,8 @@ Meteor.methods({
});
}
}
return;
},
'add.mytask' (taskName, assignedTo, assignedToId, taskDate, actDate) {
async 'add.myTask' (taskName, taskDate, actDate) {
check(taskName, String);
check(taskDate, String);
check(actDate, Date);
@ -76,40 +49,28 @@ Meteor.methods({
throw new Meteor.Error('You are not allowed to add tasks. Make sure you are logged in with valid user credentials.');
}
let username;
let usInfo;
if (assignedTo == "self") {
const uInfo = async() => {
let userInfo = await Meteor.users.findOneAsync({ _id: this.userId });
if (!userInfo) {
console.log("No matching user info found.")
} else {
username = userInfo.profile.fullname;
assignedToId = this.userId;
let usinf = { username: username, assignedToId: assignedToId };
return usinf;
}
}
usInfo = uInfo();
let userInfo = await Meteor.users.findOneAsync({ _id: this.userId });
if (!userInfo) {
console.log("No matching user info found.")
} else {
username = assignedTo;
usInfo = { username: assignedTo, assignedToId: assignedToId };
try {
return await TaskItems.insertAsync({
taskName: taskName,
taskDate: taskDate,
actualDate: actDate,
assignedTo: userInfo.profile.fullname,
assignedToId: this.userId,
isComplete: false,
completedOn: null,
assignedOn: new Date(),
assignedBy: this.userId,
});
} catch(error) {
console.log(" ERROR adding tasksL " + error.message);
}
}
return TaskItems.insertAsync({
taskName: taskName,
taskDate: taskDate,
actualDate: actDate,
assignedTo: usInfo.username,
assignedToId: usInfo.assignedToId,
isComplete: false,
completedOn: null,
assignedOn: new Date(),
assignedBy: this.userId,
});
},
'edit.task' (taskId, taskName, assignedTo, taskDate) {
async 'edit.task' (taskId, taskName, assignedTo, taskDate) {
check(taskId, String);
check(taskName, String);
check(assignedTo, String);
@ -119,7 +80,7 @@ Meteor.methods({
throw new Meteor.Error('You are not allowed to edit tasks. Make sure you are logged in with valid user credentials.');
}
return TaskItems.updateAsync({ _id: taskId }, {
return await TaskItems.updateAsync({ _id: taskId }, {
$set: {
taskName: taskName,
taskDate: taskDate,
@ -129,23 +90,23 @@ Meteor.methods({
}
});
},
'delete.task' (taskId) {
async 'delete.task' (taskId) {
check(taskId, String);
if (!this.userId) {
throw new Meteor.Error('You are not allowed to delete tasks. Make sure you are logged in with valid user credentials.');
}
return TaskItems.removeAsync({ _id: taskId });
return await TaskItems.removeAsync({ _id: taskId });
},
'markTask.complete' (taskId) {
async 'markTask.complete' (taskId) {
check(taskId, String);
if (!this.userId) {
throw new Meteor.Error('You are not allowed to mark tasks complete. Make sure you are logged in with valid user credentials.');
}
return TaskItems.updateAsync({ _id: taskId }, {
return await TaskItems.updateAsync({ _id: taskId }, {
$set: {
isComplete: true,
completedOn: new Date(),
@ -153,14 +114,14 @@ Meteor.methods({
}
});
},
'markTask.notComplete' (taskId) {
async 'markTask.notComplete' (taskId) {
check(taskId, String);
if (!this.userId) {
throw new Meteor.Error('You are not allowed to mark tasks not complete. Make sure you are logged in with valid user credentials.');
}
return TaskItems.updateAsync({ _id: taskId }, {
return await TaskItems.updateAsync({ _id: taskId }, {
$set: {
isComplete: false,
markedUncomplteOn: new Date(),
@ -168,7 +129,7 @@ Meteor.methods({
}
});
},
'clean.Tasks' (timeFrame) {
async 'clean.Tasks' (timeFrame) {
check(timeFrame, String);
if (!this.userId) {
@ -202,6 +163,6 @@ Meteor.methods({
break;
}
return TaskItems.removeAsync({ actualDate: { $lt: new Date((new Date()) - upToDate )}});
return await TaskItems.removeAsync({ actualDate: { $lt: new Date((new Date()) - upToDate )}});
}
});

953
package-lock.json generated

File diff suppressed because it is too large Load diff