get_my/imports/api/tasks.js
2025-07-23 19:44:24 -05:00

168 lines
No EOL
5.6 KiB
JavaScript

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({
async 'add.task' (taskNameArr, assignedTo, assignedToId, taskDateArr, actDate) {
check(taskNameArr, [Object]);
check(assignedTo, String);
check(taskDateArr, [String]);
check(assignedToId, 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.');
}
for (let i=0; i < taskDateArr.length; i++) {
for (let j=0; j < taskNameArr.length; j++) {
await TaskItems.insertAsync({
taskName: taskNameArr[j].id,
taskDate: taskDateArr[i],
actualDate: actDate[i],
assignedTo: assignedTo,
assignedToId: assignedToId,
isComplete: false,
completedOn: null,
assignedOn: new Date(),
assignedBy: this.userId,
});
}
}
},
async 'add.myTask' (taskName, 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 userInfo = await Meteor.users.findOneAsync({ _id: this.userId });
if (!userInfo) {
// console.log("No matching user info found.")
} else {
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);
}
}
},
async '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 await TaskItems.updateAsync({ _id: taskId }, {
$set: {
taskName: taskName,
taskDate: taskDate,
assignedTo: assignedTo,
changedOn: new Date(),
changedBy: this.userId,
}
});
},
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 await TaskItems.removeAsync({ _id: 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 await TaskItems.updateAsync({ _id: taskId }, {
$set: {
isComplete: true,
completedOn: new Date(),
completedBy: this.userId,
}
});
},
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 await TaskItems.updateAsync({ _id: taskId }, {
$set: {
isComplete: false,
markedUncomplteOn: new Date(),
markedUncompleteBy: this.userId,
}
});
},
async '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 await TaskItems.removeAsync({ actualDate: { $lt: new Date((new Date()) - upToDate )}});
}
});