import { Meteor } from 'meteor/meteor';
import { Collections } from '../../collections.js';
import { check, Match } from 'meteor/check';

let Permissions = function() {};

Permissions.prototype.checkIfLogged = function(){
  if(!Meteor.userId())
    throw new Meteor.Error('authentication error');
};

Permissions.prototype.getPrivilege = function(userId, projectName){
  let project = Collections.Projects.findOne({ name: projectName });
  return project.roles[userId];
};

Permissions.prototype.verify = function(userId, projectName, privilege){
  check(privilege, Match.OneOf('pa', 'pm', 'po'));
  let project = Collections.Projects.findOne({ name: projectName });
  if (!(project.roles[userId]===privilege))
    throw new Meteor.Error('authentication error');
};

Permissions.prototype.upsert = function(userId, projectName, privilege){
  Permissions.verify(Meteor.userId(), projectName, 'pa');
  check(privilege, Match.OneOf('pa', 'pm', 'po'));
  let setModifier = { $set: {} };
  setModifier.$set['roles.'+userId] = privilege;
  Collections.Projects.upsert( { name: projectName }, setModifier);
};

Permission.prototype.checkIfOneAdmin = function(projectName){
  let projet = Collections.Projects.findOne({name: projectName});
  for (let key in projet) {
      if (projet[key] === 'pa') {
          return true;
      }
      throw new Meteor.Error('project need at least one admin');
  }
};

Permissions.prototype.delete = function(userId, projectName){
  Permissions.verify(Meteor.userId(), projectName, 'pa');
  if(getPrivilege(userId, projectName)===pa)
    Permissions.checkIfOneAdmin(projectName);
  let unsetModifier = { $unset: {} };
  unsetModifier.$unset['roles.'+userId] = '';
  Collections.Projects.update( { name: projectName }, unsetModifier);
};

Permissions.prototype.addViaEmail = function(userEmail, projectName, privilege){
  Permissions.verify(Meteor.userId(), projectName, 'pa');
  check(privilege, Match.OneOf('pa', 'pm', 'po'));
  let user = Meteor.users.findOne({'emails.0.address':userEmail});
  if(!user)
    throw new Meteor.Error('user not found');
  let setModifier = { $set: {} };
  setModifier.$set['roles.'+user.userId] = privilege;
  Collections.Projects.upsert( { name: projectName }, setModifier);
};

Permissions = new Permissions();
export default Permissions;

@@ -938,24 +938,129 @@
 m', 'po'));%0A
+  if(Permissions.getPrivilege(userId, projectName)==='pa')%0A    Permissions.checkIfOneAdmin(projectName);%0A
   let setMod
@@ -1212,16 +1212,17 @@
 rmission
+s
 .prototy
@@ -1622,16 +1622,28 @@
 );%0A  if(
+Permissions.
 getPrivi
@@ -1670,18 +1670,20 @@
 Name)===
+'
 pa
+'
 )%0A    Pe

fix typos