get_my/imports/api/tasks.js
2022-09-05 09:39:54 -05:00

93 lines
No EOL
2.8 KiB
JavaScript

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({
'add.task' (taskName, assignedTo, taskDate) {
check(taskName, String);
check(assignedTo, String);
check(taskDate, String);
if (!this.userId) {
throw new Meteor.Error('You are not allowed to add tasks. Make sure you are logged in with valid user credentials.');
}
return TaskItems.insert({
taskName: taskName,
taskDate: taskDate,
assignedTo: assignedTo,
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,
}
});
},
});