179 lines
5.3 KiB
JavaScript
179 lines
5.3 KiB
JavaScript
import { Meteor } from 'meteor/meteor';
|
|
import { Mongo } from 'meteor/mongo';
|
|
import { check } from 'meteor/check';
|
|
import 'meteor/aldeed:collection2/static'
|
|
import SimpleSchema from 'meteor/aldeed:simple-schema';
|
|
import Measures from "./Measure";
|
|
|
|
let SalesSheets = new Mongo.Collection('SalesSheets');
|
|
|
|
if(Meteor.isServer) {
|
|
//Set MongoDB indexes (or remove them) here.
|
|
try {
|
|
SalesSheets.rawCollection().createIndex({name: -1}, {unique: false})
|
|
} catch(e) {console.log("Caught exception while setting indexes in MongoDB"); console.error(e)}
|
|
}
|
|
|
|
const SalesSheetSchema = new SimpleSchema({
|
|
name: {
|
|
type: String,
|
|
label: "Name",
|
|
optional: false,
|
|
trim: true,
|
|
//index: 1,
|
|
//unique: false
|
|
},
|
|
products: { //An ordered array of product id's included on the sheet.
|
|
type: Array,
|
|
label: "products",
|
|
optional: false,
|
|
defaultValue: []
|
|
},
|
|
'products.$': {
|
|
type: new SimpleSchema({
|
|
name: {
|
|
type: String,
|
|
label: "Name",
|
|
optional: false,
|
|
trim: true,
|
|
//unique: false
|
|
},
|
|
productId: { //Note: Will be non-existent for headings.
|
|
type: String,
|
|
label: "Product ID",
|
|
trim: false,
|
|
regEx: SimpleSchema.RegEx.Id,
|
|
optional: true
|
|
},
|
|
measureIds: { //Note: Will be non-existent for headings.
|
|
type: Array, //[String],
|
|
label: "Measure IDs",
|
|
optional: true
|
|
},
|
|
'measureIds.$': {
|
|
type: String,
|
|
},
|
|
//measureIds: {
|
|
// type: Array,
|
|
// label: "Measure IDs",
|
|
// optional: true
|
|
//},
|
|
//'measureIds.$': {
|
|
// type: String,
|
|
// label: "Measure ID",
|
|
// trim: false,
|
|
// regEx: SimpleSchema.RegEx.Id,
|
|
// optional: false
|
|
//}
|
|
})
|
|
},
|
|
createdAt: {
|
|
type: Date,
|
|
label: "Created On",
|
|
optional: false
|
|
},
|
|
updatedAt: {
|
|
type: Date,
|
|
label: "Updated On",
|
|
optional: true
|
|
}
|
|
});
|
|
SalesSheets.attachSchema(SalesSheetSchema);
|
|
|
|
if(Meteor.isServer) {
|
|
Meteor.publish('salesSheets', function() {
|
|
return SalesSheets.find({});
|
|
});
|
|
|
|
Meteor.methods({
|
|
createSalesSheet: async function(name) {
|
|
check(name, String);
|
|
|
|
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
|
|
return await SalesSheets.insertAsync({name, products: [], createdAt: new Date()});
|
|
}
|
|
else throw new Meteor.Error(403, "Not authorized.");
|
|
},
|
|
// This gets ridiculous. What would be required, along with a ton of code to micro manage each change.
|
|
//updateSalesSheet_addProduct: function(id, productId, productName, productMeasures) {
|
|
//
|
|
//},
|
|
//updateSalesSheet_removeProduct: function(id, productId) {
|
|
//
|
|
//},
|
|
//updateSalesSheet_updateProduct: function(id, productId, productName) {
|
|
//
|
|
//},
|
|
//updateSalesSheet_updateProduct_addMeasure: function(id, productId, productName, productMeasures) {
|
|
//
|
|
//},
|
|
//updateSalesSheet_updateProduct_removeMeasure: function(id, productId, productName, productMeasures) {
|
|
//
|
|
//},
|
|
updateSalesSheet: async function(id, name, products) {
|
|
check(id, String);
|
|
check(name, String);
|
|
check(products, [{
|
|
productId: Match.Maybe(String),
|
|
name: String,
|
|
measureIds: Match.Maybe([String])
|
|
}]);
|
|
|
|
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
|
|
try {
|
|
// Generates some queries for testing.
|
|
//console.log("db.SalesSheet.update({_id: " + id + "}, {{name: " + name + ", products: " + products + ", updatedAt: " + new Date() + "}})");
|
|
//let productList = "";
|
|
//let firstProduct = true;
|
|
//for(next of products) {
|
|
// if(firstProduct) firstProduct = false;
|
|
// else productList += ',';
|
|
// productList += '{id:"' + next.id + '",name:"' + next.name + '",measureIds:[';
|
|
// let firstMeasure = true;
|
|
// for(measureId of next.measureIds) {
|
|
// if(firstMeasure) firstMeasure = false;
|
|
// else productList += ',';
|
|
// productList += '"' + measureId + '"';
|
|
// }
|
|
// productList += ']}';
|
|
//}
|
|
//console.log("db.SalesSheet.update({_id: '" + id + "'}, {$set: {name: '" + name + "', updatedAt: " + new Date() + "}, $pull: {$exists: true}, $pushAll: [" + productList + "]})");
|
|
|
|
// Forces the object to be re-written, versus piecemeal updated.
|
|
await SalesSheets.updateAsync({_id: id}, {$set: {name: name, products: products, updatedAt: new Date()}}, {validate: false}, function(err, count) {
|
|
if(err) console.log(err);
|
|
});
|
|
|
|
// Attempts to remove all products and re-add them. Note: Does not work!
|
|
//SalesSheet.update({_id: id}, {$set: {name: name, updatedAt: new Date()}, $pull: {products: {$exists: true}}}, {bypassCollection2: true}, function(err, count) {
|
|
// if(err) console.log(err);
|
|
//});
|
|
//SalesSheet.update({_id: id}, {$push: {products: {$each: [products]}}}, {bypassCollection2: true}, function(err, count) {
|
|
// if(err) console.log(err);
|
|
//});
|
|
}
|
|
catch(err) {
|
|
console.log(err);
|
|
}
|
|
}
|
|
else throw new Meteor.Error(403, "Not authorized.");
|
|
},
|
|
removeSalesSheet: async function(id) {
|
|
check(id, String);
|
|
|
|
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
|
|
await SalesSheets.removeAsync(id);
|
|
}
|
|
else throw new Meteor.Error(403, "Not authorized.");
|
|
}
|
|
});
|
|
}
|
|
|
|
//Allows the client to do DB interaction without calling server side methods, while still retaining control over whether the user can make changes.
|
|
SalesSheets.allow({
|
|
insert: function() {return false;},
|
|
update: function() {return false;},
|
|
remove: function() {return false;}
|
|
});
|
|
|
|
export default SalesSheets; |