Files
PetitTetonMeteor/imports/api/Measure.js

130 lines
3.2 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { check } from 'meteor/check';
import {SimpleSchema} from 'meteor/aldeed:simple-schema';
Measures = new Mongo.Collection('Measures');
Measures.attachSchema(new SimpleSchema({
name: {
type: String,
label: "Name",
optional: false,
trim: true,
index: 1,
unique: true
},
postfix: {
type: String,
label: "Postfix",
optional: true, //Note: Each does not have a postfix.
trim: true
},
order: {
type: SimpleSchema.Integer,
label: "Order",
optional: false,
min: 0
},
createdAt: { //Force the value to the current date on the server.
type: Date,
label: "Created On",
// autoValue: function() { //The disadvantage of autoValue is that it sets the date after insertion, causing the UI to update twice - once where the item has no date, and then again where the date is set. Sorted lists will cause the item to bounce around.
// if(this.isInsert) return new Date();
// else if(this.isUpsert) return {$setOnInsert: new Date()};
// else this.unset();
// },
// denyUpdate: true,
optional: false
},
updatedAt: {
type: Date,
label: "Updated On",
// autoValue: function() {
// if(this.isUpdate) return new Date();
// },
// denyInsert: true,
optional: true
},
deletedAt: {
type: Date,
label: "Deleted On",
optional: true
},
deletedBy: {
type: String,
label: "Deleted By",
optional: true
},
restoredAt: {
type: Date,
label: "Restored On",
optional: true
},
restoredBy: {
type: String,
label: "Restored By",
optional: true
}
}));
//https://github.com/zimme/meteor-collection-softremovable
Measures.attachBehaviour("softRemovable", {
removed: 'deleted',
removedAt: 'deletedAt',
removedBy: 'removedBy',
restoredAt: 'restoredAt',
restoredBy: 'restoredBy'
});
if(Meteor.isServer) Meteor.publish('measures', function() {
return Measures.find({});
});
// Requires: meteor add matb33:collection-hooks
Measures.before.insert(function(userId, doc) {
// check(userId, String);
doc.createdAt = new Date();
});
Measures.before.update(function(userId, doc, fieldNames, modifier, options) {
modifier.$set = modifier.$set || {}; //Make sure there is an object.
modifier.$set.updatedAt = new Date();
});
if(Meteor.isServer) {
Meteor.methods({
insertMeasure: function(measure) {
check(measure, {
name: String,
order: Number,
postfix: String
});
measure.createdAt = new Date();
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
Measures.insert(measure);
}
else throw new Meteor.Error(403, "Not authorized.");
},
deleteMeasure: function(id) {
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
Measures.remove(id);
}
else throw new Meteor.Error(403, "Not authorized.");
},
updateMeasure: function(measure) {
check(measure, {
name: String,
order: Number,
postfix: String
});
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
Products.update(id, {$set: {name: measure.name, order: measure.order, postfix: measure.postfix, updateAt: new Date()}});
}
else throw new Meteor.Error(403, "Not authorized.");
}
});
}
export default Measures;