import { Meteor } from 'meteor/meteor';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import SimpleSchema from 'simpl-schema';
import rateLimit from '/imports/lib/rate-limit';
import Documents from './documents';

export const upsertDocument = new ValidatedMethod({
  name: 'documents.upsert',
  validate: new SimpleSchema({
    _id: { type: String, optional: true },
    title: { type: String, optional: true },
    body: { type: String, optional: true },
    owner: { type: String, optional: true },
  }).validator(),
  run(document) {
    if (!this.userId) {
      throw new Meteor.Error('documents.upsert.notLoggedIn',
        'Must be logged in to insertor update a document');
    }

    if (document._id && document.owner !== this.userId) {
      throw new Meteor.Error('documents.upsert.accessDenied',
        'You don\'t have permission to update this document.');
    }

    const data = {
      title: document.title,
      body: document.body,
    };

    if (!document._id) {
      data.createdAt = new Date();
      data.owner = this.userId;
    }

    return Documents.upsert({ _id: document._id }, { $set: data });
  },
});

export const removeDocument = new ValidatedMethod({
  name: 'documents.remove',
  validate: new SimpleSchema({
    _id: { type: String, regEx: SimpleSchema.RegEx.Id },
  }).validator(),
  run({ _id }) {
    if (!this.userId) {
      throw new Meteor.Error('documents.remove.notLoggedIn',
        'Must be logged in to remove a document.');
    }

    const document = Documents.findOne(_id);

    if (document.owner !== this.userId) {
      throw new Meteor.Error('documents.remove.accessDenied',
        'You don\'t have permission to remove this document.');
    }

    Documents.remove(_id);
  },
});

rateLimit({
  methods: [
    upsertDocument,
    removeDocument,
  ],
  limit: 5,
  timeRange: 1000,
});

@@ -1723,16 +1723,35 @@
  %7D%0A%0A    
+try %7B%0A      return 
 Document
@@ -1765,16 +1765,96 @@
 e(_id);%0A
+    %7D catch (exception) %7B%0A      throw new Meteor.Error('500', exception);%0A    %7D%0A
   %7D,%0A%7D);

Update API Documents, removeDocument method, add catch execption