get_my/imports/api/tasks.js

207 lines
6.8 KiB
JavaScript
Raw Normal View History

import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { check } from 'meteor/check';
import moment from 'moment';
export const TaskItems = new Mongo.Collection('taskitems');
TaskItems.allow({
insert: function(userId, doc){
// if use id exists, allow insert
return !!userId;
},
});
Meteor.methods({
2022-09-14 19:16:06 -05:00
'add.task' (taskNameArr, assignedTo, assignedToId, taskDateArr, actDate) {
check(taskNameArr, [Object]);
check(assignedTo, String);
2022-09-14 19:16:06 -05:00
check(taskDateArr, [String]);
2022-09-05 15:30:20 -05:00
check(assignedToId, String);
2022-09-14 19:16:06 -05:00
check(actDate, [Date]);
if (!this.userId) {
throw new Meteor.Error('You are not allowed to add tasks. Make sure you are logged in with valid user credentials.');
}
2022-09-05 15:30:20 -05:00
let username;
2025-06-24 13:16:42 -05:00
let usInfo;
2022-09-05 15:30:20 -05:00
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;
2025-06-24 13:16:42 -05:00
let usInf = { "username": username, "assignedToId": assignedToId };
console.log("setting username = " + username +", and id = " + assignedToId);
return usInf;
}
}
2025-06-24 13:16:42 -05:00
usInfo = uInfo();
console.dir(usInfo);
2022-09-05 15:30:20 -05:00
} else {
username = assignedTo;
2025-06-24 13:16:42 -05:00
usInfo = { "username": assignedTo, "assignedToId": assignedToId };
console.log("Set username = " + assignedTo);
2022-09-05 15:30:20 -05:00
}
2025-06-24 13:16:42 -05:00
console.log("assignedToId = " + usInfo.assignedToId);
for (let i=0; i < taskDateArr.length; i++) {
for (let j=0; j < taskNameArr.length; j++) {
TaskItems.insertAsync({
taskName: taskNameArr[j].id,
2022-09-14 19:16:06 -05:00
taskDate: taskDateArr[i],
actualDate: actDate[i],
2025-06-24 13:16:42 -05:00
assignedTo: usInfo.username,
assignedToId: usInfo.assignedToId,
2022-09-14 19:16:06 -05:00
isComplete: false,
completedOn: null,
assignedOn: new Date(),
assignedBy: this.userId,
});
}
}
2025-06-24 13:16:42 -05:00
return;
},
'add.mytask' (taskName, assignedTo, assignedToId, taskDate, actDate) {
check(taskName, String);
check(taskDate, String);
check(actDate, Date);
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;
2025-06-24 13:16:42 -05:00
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;
2025-06-24 13:16:42 -05:00
let usinf = { username: username, assignedToId: assignedToId };
return usinf;
}
}
2025-06-24 13:16:42 -05:00
usInfo = uInfo();
} else {
username = assignedTo;
2025-06-24 13:16:42 -05:00
usInfo = { username: assignedTo, assignedToId: assignedToId };
}
return TaskItems.insertAsync({
taskName: taskName,
taskDate: taskDate,
actualDate: actDate,
2025-06-24 13:16:42 -05:00
assignedTo: usInfo.username,
assignedToId: usInfo.assignedToId,
isComplete: false,
completedOn: null,
assignedOn: new Date(),
assignedBy: this.userId,
});
},
'edit.task' (taskId, taskName, assignedTo, taskDate) {
check(taskId, String);
check(taskName, String);
check(assignedTo, String);
check(taskDate, String);
if (!this.userId) {
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 }, {
$set: {
taskName: taskName,
taskDate: taskDate,
assignedTo: assignedTo,
changedOn: new Date(),
changedBy: this.userId,
}
});
},
'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 });
},
'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 }, {
$set: {
isComplete: true,
completedOn: new Date(),
completedBy: this.userId,
}
});
},
'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 }, {
$set: {
isComplete: false,
markedUncomplteOn: new Date(),
markedUncompleteBy: this.userId,
}
});
},
'clean.Tasks' (timeFrame) {
check(timeFrame, String);
if (!this.userId) {
throw new Meteor.Error('You are not allowed to clean up old Tasks. Make sure you are logged in with valid user credentials.');
}
let d = new Date();
let upToDate = "";
switch(timeFrame) {
case "1-week":
console.log("1 Week");
upToDate = 7 * 24 * 60 * 60 * 1000;
break;
case "2-weeks":
console.log("2 Week");
upToDate = 14 * 24 * 60 * 60 * 1000;
break;
case '1-month':
console.log("1 month");
upToDate = 30 * 24 * 60 * 60 * 1000;
break;
case '3-months':
console.log("3 months");
upToDate = 90 * 24 * 60 * 60 * 1000;
break;
case 'all':
console.log("all");
upToDate = 1 * 24 * 60 * 60 * 1000;
break;
default:
break;
}
return TaskItems.removeAsync({ actualDate: { $lt: new Date((new Date()) - upToDate )}});
}
});