get_my/imports/api/products.js

77 lines
2.5 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { check } from 'meteor/check';
export const Products = new Mongo.Collection('products');
Products.allow({
insert: function(userId, doc){
// if use id exists, allow insert
return !!userId;
},
});
Meteor.methods({
async 'add.product' (prodName, prodStore) {
check(prodName, String);
check(prodStore, [String]);
if (!this.userId) {
throw new Meteor.Error('You are not allowed to add products. Make sure you are logged in with valid user credentials.');
}
let pname = prodName.charAt(0).toUpperCase() + prodName.slice(1);
// first does this product already exist?
let prodExists = await Products.findOneAsync({ prodName: pname });
try {
if (!prodExists) {
return await Products.insertAsync({
prodName: pname,
prodOwner: this.userId,
prodStore: prodStore,
});
}
} catch(error) {
console.log(" ERROR adding Pdocut: " + error);
}
},
async 'edit.product' (prodId, prodName, prodStore) {
check(prodId, String);
check(prodName, String);
check(prodStore, [String]);
if (!this.userId) {
throw new Meteor.Error('You are not allowed to edit products. Make sure you are logged in with valid user credentials.');
}
let pname = prodName.charAt(0).toUpperCase() + prodName.slice(1);
return await Products.updateAsync({ _id: prodId }, {
$set: {
prodName: pname,
prodStore: prodStore,
}
});
},
async 'delete.product' (prodId) {
check(prodId, String);
if (!this.userId) {
throw new Meteor.Error('You are not allowed to delete products. Make sure you are logged in with valid user credentials.');
}
return await Products.removeAsync({ _id: prodId });
},
async 'clean.Products' () {
if (!this.userId) {
throw new Meteor.Error('You are not allowed to delete products. Make sure you are logged in with valid user credentials.');
}
// first we need to find potential duplicate products
// next we need to see which of those are on incomplete lists
// Then we'll update those to use the main products ID instead
// finally we'll delete the duplicates
}
});