115 lines
3.4 KiB
JavaScript
115 lines
3.4 KiB
JavaScript
import { Meteor } from 'meteor/meteor';
|
|
import { Mongo } from 'meteor/mongo';
|
|
import { check } from 'meteor/check';
|
|
import {SimpleSchema} from 'meteor/aldeed:simple-schema';
|
|
|
|
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
|
|
},
|
|
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
|
|
}
|
|
});
|
|
Venues.attachSchema(VenuesSchema);
|
|
|
|
if(Meteor.isServer) Meteor.publish('venues', function() {
|
|
return Venues.find({});
|
|
});
|
|
|
|
// //Requires: meteor add matb33:collection-hooks
|
|
if(Meteor.isServer) {
|
|
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) {
|
|
check(name, String);
|
|
check(type, String);
|
|
|
|
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
|
|
Venues.insert({name, type, createdAt: 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.");
|
|
},
|
|
//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) {
|
|
check(id, String);
|
|
check(name, String);
|
|
check(type, String);
|
|
|
|
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
|
|
Venues.update(id, {$set: {name, type, updatedAt: new Date()}});
|
|
}
|
|
else throw new Meteor.Error(403, "Not authorized.");
|
|
}
|
|
});
|
|
}
|
|
|
|
export default Venues; |