import { Meteor } from 'meteor/meteor'; import { Mongo } from 'meteor/mongo'; import { check } from 'meteor/check'; import {SimpleSchema} from 'meteor/aldeed:simple-schema'; const TYPES = ['Retail', "Farmer's Market", "Restaurant", "Mail"]; const FREQUENCIES = ['Daily', 'Weekly']; let Venues = new Mongo.Collection('Venues'); let VenuesSchema = new SimpleSchema({ name: { type: String, label: "Name", optional: false, trim: true, index: 1, unique: true }, type: { type: String, label: "Type", optional: false, trim: true, allowedValues: TYPES //If you change these values, also change Venues.js for the editor to include the new list when setting up the combo. }, frequency: { // How often the market is run. The exact day or week is not important, just that it is daily, weekly, bi-weekly, monthly, etc... Currently only supporting Daily and Weekly since all markets fit into these categories. type: String, label: "Frequency", optional: false, defaultValue: 'Daily', allowedValues: FREQUENCIES //If you change these values, also change Venues.js for the editor to include the new list when setting up the combo. }, createdAt: { type: Date, label: "Created On", optional: false }, updatedAt: { type: Date, label: "Updated On", optional: true }, deactivated: { type: Boolean, label: "Deactivated", optional: true }, hidden: { type: Boolean, label: "Hidden", optional: true } }); VenuesSchema.constants = {types: TYPES, frequencies: FREQUENCIES}; Venues.attachSchema(VenuesSchema); if(Meteor.isServer) Meteor.publish('venues', function() { return Venues.find({}); }); if(Meteor.isServer) { // //Requires: meteor add matb33:collection-hooks //Venues.before.insert(function(userId, doc) { // // check(userId, String); // doc.createdAt = new Date(); //}); //Venues.before.update(function(userId, doc, fieldNames, modifier, options) { // modifier.$set = modifier.$set || {}; //Make sure there is an object. // modifier.$set.updatedAt = new Date(); //}); Meteor.methods({ createVenue: function(name, type, frequency) { check(name, String); check(type, String); check(frequency, String); if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) { Venues.insert({name, type, frequency, createdAt: new Date()}); } else throw new Meteor.Error(403, "Not authorized."); }, //deleteVenue: function(id) { // if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) { // Venues.remove(id); //TODO: If this is ever allowed, we should either remove or replace references to the deleted venue in the rest of the database. // } // else throw new Meteor.Error(403, "Not authorized."); //}, updateVenue: function(id, name, type, frequency) { check(id, String); check(name, String); check(type, String); check(frequency, String); if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) { Venues.update(id, {$set: {name, type, frequency, updatedAt: new Date()}}); } else throw new Meteor.Error(403, "Not authorized."); }, deactivateVenue: function(id) { if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) { //Venues.remove(id); Venues.update(id, {$set: {deactivated: true}}, {bypassCollection2: true}); } else throw new Meteor.Error(403, "Not authorized."); }, reactivateVenue: function(id) { if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) { Venues.update(id, {$set: {deactivated: false}}, {bypassCollection2: true}); } else throw new Meteor.Error(403, "Not authorized."); }, hideVenue: function(id) { //One step past deactivated - will only show in the venues list if hidden venues are enabled. if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) { //Venues.remove(id); Venues.update(id, {$set: {hidden: true}}, {bypassCollection2: true}); } else throw new Meteor.Error(403, "Not authorized."); }, showVenue: function(id) { //Returns the venue to being simply deactivated. Will again show in lists. if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) { Venues.update(id, {$set: {hidden: false}}, {bypassCollection2: true}); } 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. Venues.allow({ insert: function() {return false;}, update: function() {return false;}, remove: function() {return false;} }); export default Venues;