2017-01-15 11:33:37 -08:00
import { Meteor } from 'meteor/meteor' ;
import { Mongo } from 'meteor/mongo' ;
import { check } from 'meteor/check' ;
import { SimpleSchema } from 'meteor/aldeed:simple-schema' ;
2017-12-31 14:06:46 -08:00
const TYPES = [ 'Retail' , "Farmer's Market" , "Restaurant" , "Mail" ] ;
const FREQUENCIES = [ 'Daily' , 'Weekly' ] ;
2020-09-20 20:36:45 -07:00
let Venues = new Mongo . Collection ( 'Venues' ) ;
2017-01-15 11:33:37 -08:00
let VenuesSchema = new SimpleSchema ( {
name : {
type : String ,
label : "Name" ,
optional : false ,
trim : true ,
index : 1 ,
unique : true
} ,
type : {
type : String ,
label : "Type" ,
optional : false ,
2017-12-31 14:06:46 -08:00
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.
2017-01-15 11:33:37 -08:00
} ,
createdAt : {
type : Date ,
label : "Created On" ,
optional : false
} ,
updatedAt : {
type : Date ,
label : "Updated On" ,
optional : true
} ,
2017-02-03 09:20:29 -08:00
deactivated : {
type : Boolean ,
label : "Deactivated" ,
2017-01-15 11:33:37 -08:00
optional : true
} ,
2017-02-03 09:20:29 -08:00
hidden : {
type : Boolean ,
label : "Hidden" ,
2017-01-15 11:33:37 -08:00
optional : true
}
} ) ;
2017-12-31 14:06:46 -08:00
VenuesSchema . constants = { types : TYPES , frequencies : FREQUENCIES } ;
2017-01-15 11:33:37 -08:00
Venues . attachSchema ( VenuesSchema ) ;
if ( Meteor . isServer ) Meteor . publish ( 'venues' , function ( ) {
return Venues . find ( { } ) ;
} ) ;
if ( Meteor . isServer ) {
2017-05-09 13:51:26 -07:00
// //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();
//});
2017-01-15 11:33:37 -08:00
Meteor . methods ( {
2017-12-31 14:06:46 -08:00
createVenue : function ( name , type , frequency ) {
2017-02-03 09:20:29 -08:00
check ( name , String ) ;
check ( type , String ) ;
2017-12-31 14:06:46 -08:00
check ( frequency , String ) ;
2017-01-15 11:33:37 -08:00
if ( Roles . userIsInRole ( this . userId , [ Meteor . UserRoles . ROLE _UPDATE ] ) ) {
2017-12-31 14:06:46 -08:00
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 ( ) } } ) ;
2017-01-15 11:33:37 -08:00
}
else throw new Meteor . Error ( 403 , "Not authorized." ) ;
} ,
2017-02-03 09:20:29 -08:00
deactivateVenue : function ( id ) {
2017-01-15 11:33:37 -08:00
if ( Roles . userIsInRole ( this . userId , [ Meteor . UserRoles . ROLE _UPDATE ] ) ) {
2017-02-03 09:20:29 -08:00
//Venues.remove(id);
Venues . update ( id , { $set : { deactivated : true } } , { bypassCollection2 : true } ) ;
2017-01-15 11:33:37 -08:00
}
else throw new Meteor . Error ( 403 , "Not authorized." ) ;
} ,
2017-02-03 09:20:29 -08:00
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." ) ;
2017-01-15 11:33:37 -08:00
}
} ) ;
}
2019-10-07 15:51:50 -07:00
//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 ; }
} ) ;
2017-01-15 11:33:37 -08:00
export default Venues ;