get_my/imports/api/menuProdLinks.js

64 lines
2.2 KiB
JavaScript
Raw Normal View History

2024-07-28 19:42:23 -05:00
import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { check } from 'meteor/check';
import { Products } from './products';
export const MenuProdLinks = new Mongo.Collection('menuProdLinks');
MenuProdLinks.allow({
insert: function(userId, doc){
// if use id exists, allow insert
return !!userId;
},
});
Meteor.methods({
2025-07-22 13:50:49 -05:00
async 'add.menuProdLinks' (menuItemId, menuItemName, prodNameArray) {
2024-07-28 19:42:23 -05:00
check(menuItemId, String);
check(menuItemName, String);
check(prodNameArray, [Object]);
2024-07-28 19:42:23 -05:00
if (!this.userId) {
throw new Meteor.Error('You are not allowed to add menu and product links. Make sure you are logged in with valid user credentials.');
};
2024-07-28 19:42:23 -05:00
let linkExists = MenuProdLinks.findOne({ menuItemId: menuItemId });
if (linkExists) {
2025-07-23 19:44:24 -05:00
// console.log("sending to update method instead.");
Meteor.call('update.menuPordLInks', menuItemId, menuItemName, prodNameArray, function(err, result) {
if (err) {
2025-07-23 19:44:24 -05:00
// console.log(" ERROR moving to the update method: " + err);
} else {
2025-07-23 19:44:24 -05:00
// console.log("Successfully updated the menu prod links.")
}
});
} else {
2025-07-22 13:50:49 -05:00
return await MenuProdLinks.insertAsync({
menuItemId: menuItemId,
menuItemName: menuItemName,
products: prodNameArray,
dateCreated: Date(),
createdBy: this.userId
});
}
},
2025-07-22 13:50:49 -05:00
async 'update.menuPordLInks' (menuItemId, menuItemName, prodNameArray) {
check(menuItemId, String);
check(menuItemName, String);
check(prodNameArray, [Object]);
if (!this.userId) {
throw new Meteor.Error('You are not allowed to add menu and product links. Make sure you are logged in with valid user credentials.');
};
2025-07-22 13:50:49 -05:00
return await MenuProdLinks.updateAsync({ menuItemId: menuItemId }, {
$set: {
products: prodNameArray,
dateUpdated: Date(),
updatedBy: this.userId
}
});
},
});