get_my/imports/api/tasks.js

107 lines
3.2 KiB
JavaScript
Raw Normal View History

import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { check } from 'meteor/check';
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-05 15:30:20 -05:00
'add.task' (taskName, assignedTo, assignedToId, taskDate, actDate) {
check(taskName, String);
check(assignedTo, String);
check(taskDate, String);
2022-09-05 15:30:20 -05:00
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.');
}
2022-09-05 15:30:20 -05:00
let username;
if (assignedTo == "self") {
let userInfo = Meteor.users.findOne({ _id: this.userId });
username = userInfo.profile.fullname;
assignedToId = this.userId;
} else {
username = assignedTo;
}
return TaskItems.insert({
taskName: taskName,
taskDate: taskDate,
2022-09-05 15:30:20 -05:00
actualDate: actDate,
assignedTo: username,
assignedToId: 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.update({ _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.remove({ _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.update({ _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.update({ _id: taskId }, {
$set: {
isComplete: false,
markedUncomplteOn: new Date(),
markedUncompleteBy: this.userId,
}
});
},
});