import { Meteor } from 'meteor/meteor';
import { Accounts } from 'meteor/accounts-base';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import SimpleSchema from 'simpl-schema';
import rateLimit from '../../../lib/rate-limit';

export const insertUser = new ValidatedMethod({
  name: 'users.insert',
  validate: new SimpleSchema({
    givenName: { type: String },
    familyName: { type: String },
    identifiant: { type: String, regEx: SimpleSchema.RegEx.EmailWithTLD, optional: true },
    password: { type: String },
  }).validator(),
  run(user) {
    if (!this.userId) {
      throw new Meteor.Error('users.insert.notLoggedIn',
        'Must be logged in to insert an user');
    }

    const userData = {
      email: user.identifiant,
      password: user.password,
      profile: {
        givenName: user.givenName,
        familyName: user.familyName,
      },
    };

    return Accounts.createUser(userData);
  },
});

export const updateUser = new ValidatedMethod({
  name: 'users.update',
  validate: new SimpleSchema({
    _id: { type: String, regEx: SimpleSchema.RegEx.Id },
    givenName: { type: String, optional: true },
    familyName: { type: String, optional: true },
    identifiant: { type: String, regEx: SimpleSchema.RegEx.EmailWithTLD, optional: true },
  }).validator(),
  run(user) {
    if (!this.userId) {
      throw new Meteor.Error('users.update.notLoggedIn',
        'Must be logged in to insert update an user');
    }

    try {
      const userId = user._id;
      Meteor.users.update({ _id: user._id }, {
        $set: {
          profile: {
            givenName: user.givenName,
            familyName: user.familyName,
          },
        },
      });
      return userId; // Return _id so we can redirect to user after update.
    } catch (exception) {
      throw new Meteor.Error('500', exception);
    }
  },
});

rateLimit({
  methods: [
    insertUser,
    updateUser,
  ],
  limit: 5,
  timeRange: 1000,
});

@@ -234,16 +234,56 @@
 -limit';
+%0Aimport userSchema from '../userSchema';
 %0A%0Aexport
@@ -364,220 +364,76 @@
 te: 
-new SimpleSchema(%7B%0A    givenName: %7B type: String %7D,%0A    familyName: %7B type: String %7D,%0A    identifiant: %7B type: String, regEx: SimpleSchema.RegEx.EmailWithTLD, optional: true %7D,%0A    password: %7B type: String %7D,%0A  %7D
+userSchema.pick('givenName', 'familyName', 'identifiant', 'password'
 ).va
@@ -924,438 +924,392 @@
 te: 
-new SimpleSchema(%7B%0A    _id: %7B type: String, regEx: SimpleSchema.RegEx.Id %7D,%0A    givenName: %7B type: String, optional: true %7D,%0A    familyName: %7B type: String, optional: true %7D,%0A    identifiant: %7B type: String, regEx: SimpleSchema.RegEx.EmailWithTLD, optional: true %7D,%0A  %7D).validator(),%0A  run(user) %7B%0A    if (!this.userId) %7B%0A      throw new Meteor.Error('users.update.notLoggedIn',%0A        'Must be logged in to insert update an user
+userSchema.pick('_id','givenName', 'familyName', 'identifiant').validator(),%0A  run(user) %7B%0A    if (!this.userId) %7B%0A      throw new Meteor.Error('users.update.notLoggedIn',%0A        'Must be logged in to insert update an user');%0A    %7D%0A%0A    if (user._id !== this.userId) %7B%0A      throw new Meteor.Error('users.update.accessDenied',%0A        'You don%5C't have permission to update this user.
 ');%0A

Update API Users, replace new SimplSchema validator by the Documents.schema directly and only user can update his profil