get_my/imports/api/listItems.js

158 lines
5 KiB
JavaScript
Raw Normal View History

import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { check } from 'meteor/check';
import { Products } from './products';
import { ShopLists } from './shopList';
export const ListItems = new Mongo.Collection('listitems');
ListItems.allow({
insert: function(userId, doc){
// if use id exists, allow insert
return !!userId;
},
});
Meteor.methods({
'add.listItem' (itemName, prodId, itemQty, shopListId) {
check(itemName, String);
check(prodId, String);
check(itemQty, String);
check(shopListId, String);
if (!this.userId) {
throw new Meteor.Error('You are not allowed to add items. Make sure you are logged in with valid user credentials.');
}
// look up the item from the Products collection
let prodInfo = Products.findOne({ _id: prodId });
// look up the shopList by shopListId
let shopListInfo = ShopLists.findOne({ _id: shopListId });
return ListItems.insert({
itemName: itemName,
prodId: prodId,
itemQty: itemQty,
itemOwner: shopListInfo.ownerId,
addedBy: this.userId,
itemCategory: prodInfo.prodCategory,
itemStore: prodInfo.prodStore,
itemLocation: prodInfo.prodLocation,
itemOrdered: false,
itemReceived: false,
dateAddedToList: new Date(),
});
},
'setOrdered.listItem' (itemId) {
check(itemId, String);
if (!this.userId) {
throw new Meteor.Error('You are not allowed to set items as ordered. Make sure you are logged in with valid user credentials.');
}
return ListItems.update({ _id: itemId }, {
$set: {
itemOrdered: true,
dateOrdered: new Date(),
}
});
},
'setAllOrdered.listItem' (shopListId) {
// set all items that are not already set as ordered, or set as received to ordered on this list
},
'setNotOrdered.listItem' (itemId) {
check(itemId, String);
if (!this.userId) {
throw new Meteor.Error('You are not allowed to set items as not ordered. Make sure you are logged in with valid user credentials.');
}
return ListItems.update({ _id: itemId }, {
$set: {
itemOrdered: false,
dateUnOrdered: new Date(),
}
});
},
'setReceived.listItem' (itemId) {
check(itemId, String);
if (!this.userId) {
throw new Meteor.Error('You are not allowed to set items as received. Make sure you are logged in with valid user credentials.');
}
return ListItems.update({ _id: itemId }, {
$set: {
itemReceived: true,
dateReceived: new Date(),
}
});
},
'setNotReceived.listItem' (shopListId) {
check(itemId, String);
if (!this.userId) {
throw new Meteor.Error('You are not allowed to set items as not received. Make sure you are logged in with valid user credentials.');
}
return ListItems.update({ _id: itemId }, {
$set: {
itemReceived: false,
dateNotReceived: new Date(),
}
});
},
'setAllReceived.listItem' () {
// set all items that are not already listed as received to received on this list
},
'edit.listItem' (itemId, itemName, prodId, itemQty, shopListId) {
check(itemId, String);
check(itemName, String);
check(prodId, String);
check(itemQty, String);
check(shopListId, String);
if (!this.userId) {
throw new Meteor.Error('You are not allowed to add items. Make sure you are logged in with valid user credentials.');
}
// look up the item from the Products collection
let prodInfo = Products.findOne({ _id: prodId });
// look up the shopList by shopListId
let shopListInfo = ShopLists.findOne({ _id: shopListId });
return ListItems.update({ _id: itemId }, {
$set: {
itemName: itemName,
prodId: prodId,
itemQty: itemQty,
itemOwner: shopListInfo.ownerId,
editedBy: this.userId,
itemCategory: prodInfo.prodCategory,
itemStore: prodInfo.prodStore,
itemLocation: prodInfo.prodLocation,
}
});
},
'delete.listItem' (itemId) {
check(itemId, String);
if (!this.userId) {
throw new Meteor.Error('You are not allowed to delete list items. Make sure you are logged in with valid user credentials.');
}
let itemInfo = ListItems.findOne({ _id: itemId });
let myId = this.userId;
if (myId == itemInfo.owner) {
return ListItems.remove({ _id: itemId });
} else {
console.log("User not allowed to delete this list item. Not the owner!");
return("Not Allowed!");
}
}
});