get_my/imports/api/listItems.js
2025-07-30 07:11:50 -05:00

198 lines
6.8 KiB
JavaScript

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({
async 'add.listItem' (itemName, listId) {
check(itemName, String);
check(listId, 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.');
}
let iname = itemName.charAt(0).toUpperCase() + itemName.slice(1);
// look up the item from the Products collection
let prodInfo = await Products.findOneAsync({ prodName: iname });
try {
if (!prodInfo) {
// add product info first
let added = await Meteor.callAsync("add.product", itemName, [""]);
if (!added) {
// console.log(" ERROR adding item to products: " + err);
} else {
// console.log(" SUCCESS adding item to Products.");
return await ListItems.insertAsync({
itemName: iname,
listId: listId,
prodId: added,
addedBy: this.userId,
itemStore: "",
itemOrdered: false,
itemReceived: false,
dateAddedToList: new Date(),
});
}
} else {
return await ListItems.insertAsync({
itemName: iname,
listId: listId,
prodId: prodInfo._id,
addedBy: this.userId,
itemStore: prodInfo.prodStore,
itemOrdered: false,
itemReceived: false,
dateAddedToList: new Date(),
});
}
} catch(error) {
console.log(" ERROR adding new product and item: " + error);
}
},
async 'add.itemsFromMenuItem' (itemIds, listId) {
check(itemIds, [String]);
check(listId, String);
if (!this.userId) {
throw new Meteor.Error('You are not allowed to add items from a menu. Make sure you are logged in with valid user credentials.');
}
console.dir(itemIds);
let noitems = itemIds.length;
let i;
for (i=0; i < noitems; i++) {
// let's check and make sure the product isn't already on the list
let onList = await ListItems.find({ listId: listId, prodId: itemIds[i] }).countAsync();
// console.log("Number On List: " + onList);
if (onList == 0) {
// now pull the product
let prodInfo = await Products.findOneAsync({ _id: itemIds[i] });
if (!prodInfo) {
console.log("Unable to load product info.");
} else {
console.log("Run " + i);
ListItems.insertAsync({
itemName: prodInfo.prodName,
listId: listId,
prodId: prodInfo._id,
addedBy: this.userId,
itemStore: prodInfo.prodStore,
itemOrdered: false,
itemReceived: false,
dateAddedToList: new Date(),
});
}
} else {
// product exists on list, move on to next
console.log("Product Exists on Selected List.");
}
if (i == (noitems - 1)) {
console.log("Returning now.");
return true;
}
}
},
async '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 await ListItems.updateAsync({ _id: itemId }, {
$set: {
itemOrdered: true,
dateOrdered: new Date(),
}
});
},
async 'setAllOrdered.listItem' (shopListId) {
// set all items that are not already set as ordered, or set as received to ordered on this list
},
async '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 await ListItems.updateAsync({ _id: itemId }, {
$set: {
itemOrdered: false,
dateUnOrdered: new Date(),
}
});
},
async '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 await ListItems.updateAsync({ _id: itemId }, {
$set: {
itemReceived: true,
dateReceived: new Date(),
}
});
},
async '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 await ListItems.updateAsync({ _id: itemId }, {
$set: {
itemReceived: false,
dateNotReceived: new Date(),
}
});
},
async 'setAllReceived.listItem' () {
// set all items that are not already listed as received to received on this list
},
async 'edit.listItem' (itemId, itemName) {
check(itemId, String);
check(itemName, 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.');
}
return await ListItems.updateAsync({ _id: itemId }, {
$set: {
itemName: itemName,
}
});
},
async '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.');
}
return await ListItems.removeAsync({ _id: itemId });
}
});