125 lines
3.9 KiB
JavaScript
125 lines
3.9 KiB
JavaScript
|
|
import { Meteor } from 'meteor/meteor';
|
||
|
|
import { Mongo } from 'meteor/mongo';
|
||
|
|
import { check } from 'meteor/check';
|
||
|
|
import {SimpleSchema} from 'meteor/aldeed:simple-schema';
|
||
|
|
|
||
|
|
Workers = new Mongo.Collection('Workers');
|
||
|
|
let WORKER_ACTIVITIES = ['sales', 'prep', 'canning', 'farming'];
|
||
|
|
let workersSchema = new SimpleSchema({
|
||
|
|
name: {
|
||
|
|
type: String,
|
||
|
|
label: "Name",
|
||
|
|
optional: false,
|
||
|
|
trim: true,
|
||
|
|
index: 1,
|
||
|
|
unique: true
|
||
|
|
},
|
||
|
|
activities: {
|
||
|
|
type: [String],
|
||
|
|
label: "Activities",
|
||
|
|
optional: false,
|
||
|
|
trim: true
|
||
|
|
},
|
||
|
|
hourlyRate: {
|
||
|
|
type: SimpleSchema.Integer,
|
||
|
|
label: "HourlyRate",
|
||
|
|
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
|
||
|
|
},
|
||
|
|
deactivated: {
|
||
|
|
type: Boolean,
|
||
|
|
label: "Deactivated",
|
||
|
|
optional: true
|
||
|
|
},
|
||
|
|
hidden: {
|
||
|
|
type: Boolean,
|
||
|
|
label: "Hidden",
|
||
|
|
optional: true
|
||
|
|
}
|
||
|
|
});
|
||
|
|
workersSchema.constants = {activities: WORKER_ACTIVITIES};
|
||
|
|
Workers.attachSchema(workersSchema);
|
||
|
|
|
||
|
|
if(Meteor.isServer) Meteor.publish('Workers', function() {
|
||
|
|
return Workers.find({});
|
||
|
|
});
|
||
|
|
|
||
|
|
if(Meteor.isServer) {
|
||
|
|
Meteor.methods({
|
||
|
|
createWorker: function(name, activities, hourlyRate) {
|
||
|
|
check(name, String);
|
||
|
|
check(activities, [String]);
|
||
|
|
check(hourlyRate, Number);
|
||
|
|
|
||
|
|
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
|
||
|
|
Workers.insert({name, activities, hourlyRate, createdAt: new Date()});
|
||
|
|
}
|
||
|
|
else throw new Meteor.Error(403, "Not authorized.");
|
||
|
|
},
|
||
|
|
//deleteWorker: function(id) {
|
||
|
|
// if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
|
||
|
|
// //TODO: Should troll the database looking for references to remove or replace. This is currently not used.
|
||
|
|
// Workers.remove(id);
|
||
|
|
// }
|
||
|
|
// else throw new Meteor.Error(403, "Not authorized.");
|
||
|
|
//},
|
||
|
|
updateWorker: function(id, name, activities, hourlyRate) {
|
||
|
|
check(id, String);
|
||
|
|
check(name, String);
|
||
|
|
check(activities, [String]);
|
||
|
|
check(hourlyRate, Number);
|
||
|
|
|
||
|
|
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
|
||
|
|
Workers.update(id, {$set: {name, activities, hourlyRate, updatedAt: new Date()}});
|
||
|
|
}
|
||
|
|
else throw new Meteor.Error(403, "Not authorized.");
|
||
|
|
},
|
||
|
|
deactivateWorker: function(id) {
|
||
|
|
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
|
||
|
|
//Workers.remove(id);
|
||
|
|
Workers.update(id, {$set: {deactivated: true}}, {bypassCollection2: true});
|
||
|
|
}
|
||
|
|
else throw new Meteor.Error(403, "Not authorized.");
|
||
|
|
},
|
||
|
|
reactivateWorker: function(id) {
|
||
|
|
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
|
||
|
|
Workers.update(id, {$set: {deactivated: false}}, {bypassCollection2: true});
|
||
|
|
}
|
||
|
|
else throw new Meteor.Error(403, "Not authorized.");
|
||
|
|
},
|
||
|
|
hideWorker: function(id) { //One step past deactivated - will only show in the Workers list if hidden Workers are enabled.
|
||
|
|
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
|
||
|
|
Workers.update(id, {$set: {hidden: true}}, {bypassCollection2: true});
|
||
|
|
}
|
||
|
|
else throw new Meteor.Error(403, "Not authorized.");
|
||
|
|
},
|
||
|
|
showWorker: function(id) { //Returns the measure to being simply deactivated. Will again show in lists.
|
||
|
|
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
|
||
|
|
Workers.update(id, {$set: {hidden: false}}, {bypassCollection2: true});
|
||
|
|
}
|
||
|
|
else throw new Meteor.Error(403, "Not authorized.");
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
export default Workers;
|