Skip to content

Commit

Permalink
fixed issues with import members upload
Browse files Browse the repository at this point in the history
  • Loading branch information
thetif committed Apr 20, 2017
1 parent 0fa1fdd commit eb0e8c4
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 65 deletions.
74 changes: 41 additions & 33 deletions modules/forms/client/directives/csv-file-upload.client.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
headersValid: '=',
filename: '='
},
template: '<div class="input-group"><span class="input-group-btn"><span class="btn btn-primary btn-file">Browse&hellip; ' +
'<input type="file" accept="text/comma-separated-values, text/csv, application/csv"></span></span>' +
template: '<div class="input-group"><span class="input-group-btn"><span class="btn btn-primary btn-file">Browse&hellip; ' +
'<input type="file" accept="text/comma-separated-values, text/csv, application/csv"></span></span>' +
'<input type="text" class="form-control" value="{{filename}}" readonly></div>',
link: function(scope, element) {
link: function(scope, element, attrs) {
scope.header = true;
scope.separator = ',';

var validateHeaders = function(content) {
if (scope.header) {
if (scope.headerList) {
Expand Down Expand Up @@ -86,43 +86,51 @@
scope.headersValid = validateHeaders(content);
scope.result = csvToJSON(content);
scope.$apply();
}
}
});

element.on('change', function(onChangeEvent) {
var reader = new FileReader();
scope.filename = onChangeEvent.target.files[0].name;
reader.onload = function(onLoadEvent) {
scope.$apply(function() {
var content = {
csv: onLoadEvent.target.result.replace(/\r\n|\r/g,'\n'),
header: scope.header,
separator: scope.separator
};
console.log('csv change');
if (onChangeEvent && onChangeEvent.target && onChangeEvent.target.files && onChangeEvent.target.files.length &&
onChangeEvent.target.files[0] && onChangeEvent.target.files[0].name) {
console.log('filename', onChangeEvent.target.files[0].name);
var reader = new FileReader();
scope.filename = onChangeEvent.target.files[0].name;
reader.onload = function(onLoadEvent) {
scope.$apply(function() {
var content = {
csv: onLoadEvent.target.result.replace(/\r\n|\r/g,'\n'),
header: scope.header,
separator: scope.separator
};

scope.content = content.csv;
scope.headersValid = validateHeaders(content);
scope.result = csvToJSON(content);
scope.result.filename = scope.filename;
});
};

if ((onChangeEvent.target.type === 'file') && (onChangeEvent.target.files !== null || onChangeEvent.srcElement.files !== null)) {
reader.readAsText((onChangeEvent.srcElement || onChangeEvent.target).files[0]);
} else {
if (scope.content !== null) {
var content = {
csv: scope.content,
header: !scope.header,
separator: scope.separator
};
scope.headersValid = validateHeaders(content);
scope.result = csvToJSON(content);
scope.content = content.csv;
scope.headersValid = validateHeaders(content);
scope.result = csvToJSON(content);
scope.result.filename = scope.filename;
});
};

if ((onChangeEvent.target.type === 'file') && (onChangeEvent.target.files !== null || onChangeEvent.srcElement.files !== null)) {
reader.readAsText((onChangeEvent.srcElement || onChangeEvent.target).files[0]);
} else {
if (scope.content !== null) {
var content = {
csv: scope.content,
header: !scope.header,
separator: scope.separator
};
scope.headersValid = validateHeaders(content);
scope.result = csvToJSON(content);
}
}
} else if (onChangeEvent && onChangeEvent.target && !onChangeEvent.target.files) {
console.log('no files');
scope.filename = null;
angular.element('input[type="file"]').val(null);
}
});
}
};
});
})();
})();
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
headersValid: false,
filename: ''
};
console.log('reset', $scope.csv);

$scope.bulkFileUploaded = false;

Expand All @@ -33,6 +34,7 @@
teamId: null,
newTeamName: null
};
angular.element('#import-team-members-upload').trigger('change');
};

$scope.reset();
Expand All @@ -52,7 +54,7 @@
});
};

$scope.validate = function() {
$scope.validate = function() {
var csvMembers = $scope.csv.result;
if(!csvMembers) {
$scope.headersInvalid = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
scope.$watch('organization', function(newValue, oldValue) {
scope.organization = newValue;
});
element.bind('show.bs.modal', function() {
scope.reset();
});
}
};
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<button type="button" class="close" aria-label="Close" ng-click="cancel()"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title">Import team members</h4>
</div>
<div class="modal-body" ng-hide="bulkFileUploaded">
Expand All @@ -13,7 +13,7 @@ <h4 class="modal-title">Import team members</h4>

<div class="row">
<div class="col-sm-8">
<csv-file-upload class="file-uploader" content="csv.content" result="csv.result" header-list="['First Name *', 'Last Name *', 'Username', 'Email *']" headers-valid="csv.headersValid" filename="csv.filename"></csv-file-upload>
<csv-file-upload class="file-uploader" id="import-team-members-upload" content="csv.content" result="csv.result" header-list="['First Name *', 'Last Name *', 'Username', 'Email *']" headers-valid="csv.headersValid" filename="csv.filename"></csv-file-upload>
</div>
<div class="col-sm-4">
<button class="btn btn-default btn-block" ng-click="validate()">Upload</button>
Expand Down
65 changes: 36 additions & 29 deletions modules/users/server/controllers/admin.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -934,43 +934,50 @@ var convertCsvMember = function(csvMember, successCallback, errorCallback) {
exports.validateMemberCsv = function (req, res) {
convertCsvMember(req.body.member,
function(memberJSON) {
var pattern = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
var email = memberJSON.email;
var username = email.substring(0, email.indexOf('@'));
User.find({ $or: [{ 'email': email },{ 'username': username }] }).exec(function(userErr, users) {
if (userErr) {
return res.status(400).send({
message: userErr
});
} else {
if (users && users.length > 0) {
var nameIndex = _.findIndex(users, function(u) {
return u.username === username;
});

var emailIndex = _.findIndex(users, function(u) {
return u.email === email;
var username = (memberJSON.username) ? memberJSON.username : email.substring(0, email.indexOf('@'));
if(!pattern.test(email)) {
return res.status(400).send({
message: 'Email is invalid'
});
} else {
User.find({ $or: [{ 'email': email },{ 'username': username }] }).exec(function(userErr, users) {
if (userErr) {
return res.status(400).send({
message: userErr
});

if (nameIndex > -1 && emailIndex === -1) {
return res.status(400).send({
message: 'Email address is already in use'
} else {
if (users && users.length > 0) {
var nameIndex = _.findIndex(users, function(u) {
return u.username === memberJSON.username;
});
} else if (nameIndex === -1 && emailIndex > -1) {
return res.status(400).send({
message: 'Username is already in use'

var emailIndex = _.findIndex(users, function(u) {
return u.email === email;
});

if (nameIndex > -1 && emailIndex === -1) {
return res.status(400).send({
message: 'Username is already in use'
});
} else if (nameIndex === -1 && emailIndex > -1) {
return res.status(400).send({
message: 'Email is already in use'
});
} else {
return res.status(400).send({
message: 'Username and email address are already in use'
});
}
} else {
return res.status(400).send({
message: 'Username and email address is already in use'
return res.status(200).send({
message: 'Valid member'
});
}
} else {
return res.status(200).send({
message: 'Valid member'
});
}
}
});
});
}
}, function (err) {
return res.status(400).send({
message: err
Expand Down

0 comments on commit eb0e8c4

Please sign in to comment.