130 lines
2.9 KiB
JavaScript
130 lines
2.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';
|
||
|
|
|
||
|
|
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
|
||
|
|
},
|
||
|
|
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
|
||
|
|
}
|
||
|
|
});
|
||
|
|
Venues.attachSchema(VenuesSchema);
|
||
|
|
|
||
|
|
//https://github.com/zimme/meteor-collection-softremovable
|
||
|
|
Venues.attachBehaviour("softRemovable", {
|
||
|
|
removed: 'deleted',
|
||
|
|
removedAt: 'deletedAt',
|
||
|
|
removedBy: 'removedBy',
|
||
|
|
restoredAt: 'restoredAt',
|
||
|
|
restoredBy: 'restoredBy'
|
||
|
|
});
|
||
|
|
|
||
|
|
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({
|
||
|
|
insertVenue: function(venue) {
|
||
|
|
check(venue, {
|
||
|
|
name: String,
|
||
|
|
type: String
|
||
|
|
});
|
||
|
|
|
||
|
|
venue.createdAt = new Date();
|
||
|
|
|
||
|
|
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
|
||
|
|
Venues.insert(venue);
|
||
|
|
}
|
||
|
|
else throw new Meteor.Error(403, "Not authorized.");
|
||
|
|
},
|
||
|
|
deleteVenue: function(id) {
|
||
|
|
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
|
||
|
|
Venues.remove(id);
|
||
|
|
}
|
||
|
|
else throw new Meteor.Error(403, "Not authorized.");
|
||
|
|
},
|
||
|
|
updateVenue: function(venue) {
|
||
|
|
check(venue, {
|
||
|
|
name: String,
|
||
|
|
type: String
|
||
|
|
});
|
||
|
|
|
||
|
|
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
|
||
|
|
Venues.update(id, {$set: {name: venue.name, type: venue.type, updateAt: new Date()}});
|
||
|
|
}
|
||
|
|
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.
|
||
|
|
// Meteor.allow({
|
||
|
|
// insert: true,
|
||
|
|
// update: ()->{return true},
|
||
|
|
// remove: checkUser
|
||
|
|
// };
|
||
|
|
// checkUser = function(userId, doc) {
|
||
|
|
// return doc && doc.userId === userId;
|
||
|
|
// };
|
||
|
|
|
||
|
|
//Allows the client to interact with the db through the server via custom methods.
|
||
|
|
// Meteor.methods({
|
||
|
|
// deleteMeasure: function(id) {
|
||
|
|
// Measures.remove(id);
|
||
|
|
// }
|
||
|
|
// });
|
||
|
|
|
||
|
|
export default Venues;
|