Files
AVEF/imports/api/Internship.js

75 lines
1.7 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { check } from 'meteor/check';
import SimpleSchema from 'simpl-schema';
let Internship = new Mongo.Collection('Internship');
Internship.attachSchema(new SimpleSchema({
name: {
type: String,
label: "Name",
optional: false,
trim: true,
index: 1,
unique: true
},
content: {
type: String,
label: "Content",
optional: false,
trim: false
},
createdAt: {
type: Date,
label: "Created On",
optional: true
},
updatedAt: {
type: Date,
label: "Updated On",
optional: true
}
}));
if(Meteor.isServer) Meteor.publish('Internship', function() {
return Internship.find({});
});
if(Meteor.isServer) {
Meteor.methods({
addInternship: function(name, content) {
check(name, String);
check(content, String);
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
const createdAt = new Date();
const updatedAt = createdAt;
//Returns the id of the object created.
return Internship.insert({name, content, createdAt, updatedAt});
}
else throw new Meteor.Error(403, "Not authorized.");
},
removeInternship: function(_id) {
check(_id, String);
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
Internship.remove(_id);
}
else throw new Meteor.Error(403, "Not authorized.");
},
updateInternship: function(_id, content) {
check(_id, String);
check(content, String);
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
const updatedAt = new Date();
return Internship.update(_id, {$set: {content, updatedAt}});
}
else throw new Meteor.Error(403, "Not authorized.");
}
});
}
export default Internship;