Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added moderator addition to db #133

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
24 changes: 17 additions & 7 deletions client/views/add/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ Template.add.events({
'click #modsdonebutton': function (event, template) {
const modBoxes = document.getElementsByClassName('modbox');
const modBoxesArray = Array.from(modBoxes);
const allEmails = template.data.moderators;
const modEmails = modBoxesArray.map(b => b.value);
const newMods = modEmails.filter(value => !allEmails.includes(value));
const occurrences = modEmails.filter(val => val !== "").length;
if (checkPrevMod(modBoxes) === false) {
showModsError('Email ID was already added as a moderator.');
Expand All @@ -87,27 +89,35 @@ Template.add.events({
mods.push(modsInput[m].value);
}
}
Meteor.call('addMods', mods, template.data._id, (error, result) => {
// If the result is an object, there was an error
if (typeof result === 'object' && result.length > 0) {
Meteor.call('addMods', mods, template.data._id, (error, response) => {
console.log(response);
if (typeof response === 'object' && response['status_code'] === false && response['result'].length > 0) {
// Alert the error
for (let i = 0; i < result.length; i++) {
for (let i = 0; i < response['result'].length; i++) {
// Check is the server returned error corresponding to the addition of owner as moderator
if(result[i].name === 'owner') {
if(response['result'][i].name === 'owner') {
// Display the error message
showModsError(`${result[i].value} is already an owner of the instance and has the privileges of a moderator.`);
showModsError(`${response['result'][i].value} is already an owner of the instance and has the privileges of a moderator.`);
return false;
}
}
showModsError('Please enter valid email addresses.');
return false;
} else if (typeof response === 'object' && response['status_code'] === true && response['result'].length > 0) {
for(let k = 0; k < response['result'].length; k++) {
Accounts.forgotPassword({email: response['result'][k]}, function(err) {
if (err) {
console.log(err);
}
})
}
}
for (let m = 0; m < mods.length; m++) {
Meteor.call('sendEmail',
mods[m],
Meteor.user().emails[0].address,
'You have been added as a moderator on Question Tool',
Meteor.user().profile.name + ' added you as a moderator of ' + template.data.tablename + ' at ' + Iron.Location.get().originalUrl + ' on Question Tool. You are able to modify, combine, and hide questions. You must use this email address when registering to be considered a moderator.');
Meteor.user().profile.name + ' added you as a moderator of ' + template.data.tablename + ' at ' + Iron.Location.get().originalUrl + ' on Question Tool. You are able to modify, combine, and hide questions.');
}
let boxes = document.getElementsByClassName('newmod');
boxes = boxes[boxes.length - 1];
Expand Down
31 changes: 27 additions & 4 deletions server/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import { Answers, Questions, Instances, Votes } from '../lib/common.js';

var fs = Npm.require('fs');

function checkValidEmail(email) {
let filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return filter.test(email);
}

Meteor.methods({
// A method that returns the current connection's IP address
getIP() {
Expand Down Expand Up @@ -267,8 +272,22 @@ Meteor.methods({
},
addMods(mods, instanceid) {
if (this.userId) {
let keys;
var keys = {};
const email = Meteor.users.findOne({ _id: this.userId }).emails[0].address;
const existingAccounts = Meteor.users.find({'emails.address': {'$in': mods}}).fetch().map(el => el.emails[0].address);
const newAccounts = mods.filter(email => !existingAccounts.includes(email));
newAccounts.forEach((item) => {
if (checkValidEmail(item) === false) {
return;
}
Accounts.createUser({
email: item,
password: Math.random().toString(36).substr(2, 7),
profile: {
name: item.slice(0, item.indexOf('@'))
}
});
});
const instance = Instances.findOne({
_id: instanceid,
});
Expand All @@ -289,16 +308,20 @@ Meteor.methods({
},
}, (error, count, status) => {
if (error) {
keys = error.invalidKeys;
keys['status_code'] = false;
keys['result'] = error.invalidKeys;
}
});
} else {
return false;
}
if (keys) {
if (keys && Object.keys(keys).length > 0) {
return keys;
}
return true;
keys = {};
keys['status_code'] = true;
keys['result'] = newAccounts;
return keys;
}
return false;
},
Expand Down