Meteor.methods({
  addMilestone: function(data) {
    check(data, {type: String, period: String});

    let parentIndex = _.indexOf(Milestones.validTypes, data.type) - 1;
    let milestoneParent = Milestones.validTypes[parentIndex];

    if (!this.userId) {
      throw new Meteor.Error('forbidden-action', 'User should be logged in.');
    }

    const bounds = Milestones.boundsFor(data.period, data.type);

    const existingQuery = {
      userId: this.userId,
      type: data.type,
      $or: [
        { startsAt: { $gte: bounds.startsAt, $lte: bounds.endsAt } },
        { endsAt: { $gte: bounds.startsAt, $lte: bounds.endsAt } }
      ]
    };

    const parentQuery = {
      $and: [
        { type: milestoneParent },
        { userId: this.userId },
        { startsAt: { $lte: bounds.startsAt } },
        { endsAt: { $gte: bounds.endsAt } }
      ]
    };

    if (milestoneParent && Milestones.find(parentQuery).count() === 0) {
      throw new Meteor.Error('period-invalid',
        'No parent milestone for this period.');
    }

    if (Milestones.find(existingQuery).count() > 0) {
      throw new Meteor.Error('period-invalid',
        'Milestone intersects existing one.');
    }

    const milestone = _.extend(data, bounds, {userId: this.userId});

    return Milestones.insert(milestone);
  },

  copyGoalsFromParentMilestone: function(milestoneId) {
    check(milestoneId, String);

    this.unblock();

    if (!this.userId) {
      throw new Meteor.Error('forbidden-action', 'User should be logged in.');
    }

    const milestone = Milestones.findOne({
      _id: milestoneId,
      userId: this.userId
    });

    const parentMilestone = milestone && milestone.parent();

    if (!milestone || !parentMilestone) {
      throw new Meteor.Error(
        'forbidden-action',
        'Receiver or source milestone not found.'
      );
    }

    return parentMilestone.goals({completed: { $ne: true }}).map(goal => {
      return Goals.insert({
        title: goal.title,
        parentId: goal._id,
        milestoneId: milestoneId,
        userId: goal.userId,
        completedPct: 0
      });
    });
  }
});

@@ -97,142 +97,8 @@
 );%0A%0A
-    let parentIndex = _.indexOf(Milestones.validTypes, data.type) - 1;%0A    let milestoneParent = Milestones.validTypes%5BparentIndex%5D;%0A%0A
     
@@ -265,24 +265,86 @@
 ata.type);%0A%0A
+    let parentType = Milestones.relativeType(data.type, -1);%0A%0A
     const ex
@@ -589,212 +589,65 @@
 nst 
-parentQuery = %7B%0A      $and: %5B%0A        %7B type: milestoneParent %7D,%0A        %7B userId: this.userId %7D,%0A        %7B startsAt: %7B $lte: bounds.startsAt %7D %7D,%0A        %7B endsAt: %7B $gte: bounds.endsAt %7D %7D%0A      %5D%0A    %7D
+milestone = _.extend(data, bounds, %7BuserId: this.userId%7D)
 ;%0A%0A 
@@ -657,27 +657,23 @@
 if (
-milestoneP
+p
 arent
+Type
  && 
+!
 Mile
@@ -683,39 +683,38 @@
 nes.
-find(parentQuery).count() === 0
+_transform(milestone).parent()
 ) %7B%0A
@@ -975,78 +975,8 @@
  %7D%0A%0A
-    const milestone = _.extend(data, bounds, %7BuserId: this.userId%7D);%0A%0A
     

simplify methods restriction code