get_my/imports/api/location.js
2022-08-05 16:55:56 -05:00

61 lines
No EOL
1.8 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { check } from 'meteor/check';
export const Locations = new Mongo.Collection('locations');
Locations.allow({
insert: function(userId, doc){
// if use id exists, allow insert
return !!userId;
},
});
Meteor.methods({
'add.location' (name, type) {
check(name, String);
check(type, String);
if (!this.userId) {
throw new Meteor.Error('You are not allowed to add locations. Make sure you are logged in with valid user credentials.');
}
return Locations.insert({
locationName: name,
locationType: type,
owner: this.userid,
});
},
'edit.location' (locationId, locationName, locationType) {
check(locationId, String);
check(locationName, String);
check(locationType, String);
if (!this.userId) {
throw new Meteor.Error('You are not allowed to edit locations. Make sure you are logged in with valid user credentials.');
}
return Locations.update({ _id: locationId }, {
$set: {
locationName: locationName,
locationType: locationType,
}
});
},
'delete.location' (locationId) {
check(locationId, String);
if (!this.userId) {
throw new Meteor.Error('You are not allowed to delete locations. Make sure you are logged in with valid user credentials.');
}
let locationInfo = Locations.findOne({ _id: locationId });
let myId = this.userId;
if (myId == locationInfo.owner) {
return Locations.remove({ _id: locationId });
} else {
console.log("User not allowed to delete this location. Not the owner!");
return("Not Allowed!");
}
},
});