Skip to content

Commit

Permalink
Commit refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
verschoren committed Mar 20, 2023
1 parent df284a1 commit a191707
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 117 deletions.
2 changes: 1 addition & 1 deletion app/assets/iframe.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</head>
<body class="p-2">
<div class="flex gap-2">
<div id="remove_domains" class="flex w-full items-center justify-center rounded-md bg-white px-3 py-2 text-sm font-semibold text-red-600 shadow-sm ring-1 ring-inset ring-red-300 hover:bg-red-50 focus-visible:outline-offset-0">Remove Bad Users</div>
<div id="remove_users" class="flex w-full items-center justify-center rounded-md bg-white px-3 py-2 text-sm font-semibold text-red-600 shadow-sm ring-1 ring-inset ring-red-300 hover:bg-red-50 focus-visible:outline-offset-0">Remove Bad Users</div>
<div id="add_users" class="flex w-full items-center justify-center rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50 focus-visible:outline-offset-0">Add Matching Users</div>
</div>
<div class="ml-4 mt-4 relative flex items-start">
Expand Down
220 changes: 105 additions & 115 deletions app/assets/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,98 +11,75 @@ $('#test_mode').change(function() {
}
});

$('#remove_domains').click(function() {
removeUsers();
$('#remove_users').click(function() {
handleFlow('remove');
});

$('#add_users').click(function() {
addUsers();
handleFlow('add');
});

function removeUsers() {
function handleFlow(type){
client.get('organization').then(function(organization) {
var domains = organization.organization.domains.split(' ');
var organization_id = organization.organization.id;

$('#summary').html('');
$('#results').empty();

// check all users in organization and remove those that don't match the domain
let options = {
contentType: "application/json",
url: `/api/v2/organizations/${organization_id}/users`,
type: "GET",
};

client.request(options).then((users) => {
$('#results').empty();
var users = users.users;
var count = 0;
// loop through users and remove those that don't match the domain

jQuery.each( users, function( i, val ) {
if (val.email != null){
var domain = val.email.split('@')[1];

//check if val.email matches any of the domains in domains
if (domains.indexOf(domain) == -1) {
count++;
var output = `<li class="py-4">
<div class="flex items-center space-x-4">
<div class="flex-shrink-0">
<svg class="h-5 w-5 flex-shrink-0 text-red-400" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.28 7.22a.75.75 0 00-1.06 1.06L8.94 10l-1.72 1.72a.75.75 0 101.06 1.06L10 11.06l1.72 1.72a.75.75 0 101.06-1.06L11.06 10l1.72-1.72a.75.75 0 00-1.06-1.06L10 8.94 8.28 7.22z" clip-rule="evenodd" />
</svg>
</div>
<div class="min-w-0 flex-1">
<p class="truncate text-sm font-medium text-gray-900">${val.name}</p>
<p class="truncate text-sm text-gray-500">${val.email}</p>
</div>
<div>
<div data-target="${val.id}" class="show_user inline-flex items-center rounded-full bg-white px-2.5 py-1 text-xs font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50">View</div>
</div>
</div>
</li>`

if (test_mode == false){
let options_nomatch = {
contentType: "application/json",
url: `/api/v2/users/${val.id}`,
type: "PUT",
data: JSON.stringify({
"user": {
"organization_id": null
}
})
};
client.request(options_nomatch).then((data) => {
$('#results').append(output);
client.invoke('notify', 'Users removed!');
});
} else {
$('#results').append(output);
}
}
}
if (domains == null) {
$('#summary').html(`No domains set.`);
return;
}
if (type == 'add'){
domains.forEach(function(domain) {
addUsertoDomain(domain,organization_id);
});
$('#summary').html(`Removed ${count} users.`);
});
});
}
if (type == 'remove'){
removeUsers(domains,organization_id);
}
});
}

function addUsers() {
client.get('organization').then(function(data) {
var domains = data.organization.domains;
var organization_id = data.organization.id;
function removeUsers(domains,organization_id){
// check all users in organization and remove those that don't match the domain
let options = {
contentType: "application/json",
url: `/api/v2/organizations/${organization_id}/users`,
type: "GET",
};

$('#results').empty();
$('#summary').html('');
client.request(options).then((results) => {
console.log(results);
var users = results.users;
var count = 0;
// loop through users and remove those that don't match the domain

var unmatchedUsers = {"users":[]};

if (domains == null) {
$('#summary').html(`No domains set.`);
jQuery.each( users, function( i, user ) {
if (user.email != null){
var domain = user.email.split('@')[1];

//check if val.email matches any of the domains in domains
if (domains.indexOf(domain) == -1) {
count++;
printOutput(user,'remove');
unmatchedUsers.users.push({"id":user.id, "organization_id": null});
}
}
});

if (count == 0) {
$('#summary').html(`No users found.`);
return;
}
domains.split(' ').forEach(function(domain) {
addUsertoDomain(domain,organization_id)
});
});

if (test_mode == false){
createOrUpdateMany(unmatchedUsers, `Removed ${count} users.`);
}
});
}

function addUsertoDomain(domain,organization_id){
Expand All @@ -112,55 +89,68 @@ function addUsertoDomain(domain,organization_id){
type: "GET",
};

client.request(options).then((data) => {
var users = data.results;
client.request(options).then((results) => {
console.log(results);
var users = results.results;
var unmatchedUsers = {"users":[]};

// loop through users and add them to the organization
if (data.results.length == 0) {
if (results.results.length == 0) {
$('#summary').html(`No users found.`);
return;
} else {
jQuery.each( users, function( i, user ) {
var output = `<li class="py-4">
<div class="flex items-center space-x-4">
<div class="flex-shrink-0">
<svg class="h-5 w-5 flex-shrink-0 text-blue-400" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm.75-11.25a.75.75 0 00-1.5 0v2.5h-2.5a.75.75 0 000 1.5h2.5v2.5a.75.75 0 001.5 0v-2.5h2.5a.75.75 0 000-1.5h-2.5v-2.5z" clip-rule="evenodd" />
</svg>
</div>
<div class="min-w-0 flex-1">
<p class="truncate text-sm font-medium text-gray-900">${user.name}</p>
<p class="truncate text-sm text-gray-500">${user.email}</p>
</div>
<div>
<div data-target="${user.id}" class="show_user inline-flex items-center rounded-full bg-white px-2.5 py-1 text-xs font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50">View</div>
</div>
</div>
</li>`

if (test_mode == false){
let options_add = {
contentType: "application/json",
url: `/api/v2/users/${user.id}`,
type: "PUT",
data: JSON.stringify({
"user": {
"organization_id": organization_id
}
})
};
client.request(options_add).then((data) => {
$('#results').append(output);
client.invoke('notify', 'Users added!');
});
} else {
$('#results').append(output);
}
printOutput(user,'add');
unmatchedUsers.users.push({"id":user.id, "organization_id": organization_id});
});
$('#summary').html(`Added ${users.length} users for ${domain}.`);

if (test_mode == false){
createOrUpdateMany(unmatchedUsers, `Added ${users.length} users for ${domain}.`);
}
}
});
}

function createOrUpdateMany(unmatchedUsers,message){
let options_nomatch = {
contentType: "application/json",
url: `/api/v2/users/create_or_update_many`,
type: "POST",
data: JSON.stringify(unmatchedUsers)
};
client.request(options_nomatch).then((update_many) => {
client.invoke('notify', message);
});
}

function printOutput(user,type){
var icon = {
"color": "blue",
"path":'<path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm.75-11.25a.75.75 0 00-1.5 0v2.5h-2.5a.75.75 0 000 1.5h2.5v2.5a.75.75 0 001.5 0v-2.5h2.5a.75.75 0 000-1.5h-2.5v-2.5z" clip-rule="evenodd" />'
}
if (type == 'remove'){
icon.color = "red";
icon.path = '<path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.28 7.22a.75.75 0 00-1.06 1.06L8.94 10l-1.72 1.72a.75.75 0 101.06 1.06L10 11.06l1.72 1.72a.75.75 0 101.06-1.06L11.06 10l1.72-1.72a.75.75 0 00-1.06-1.06L10 8.94 8.28 7.22z" clip-rule="evenodd" />'
}

var output = `<li class="py-4">
<div class="flex items-center space-x-4">
<div class="flex-shrink-0">
<svg class="h-5 w-5 flex-shrink-0 text-${icon.color}-400" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">${icon.path}</svg>
</div>
<div class="min-w-0 flex-1">
<p class="truncate text-sm font-medium text-gray-900">${user.name}</p>
<p class="truncate text-sm text-gray-500">${user.email}</p>
</div>
<div>
<div data-target="${user.id}" class="show_user inline-flex items-center rounded-full bg-white px-2.5 py-1 text-xs font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50">View</div>
</div>
</div>
</li>`

$('#results').append(output);
}

// Navigate to user upon click on div with class show_user
$('body').on('click', '.show_user', function() {
client.invoke('routeTo', 'user', $(this).data('target'));
Expand Down
2 changes: 1 addition & 1 deletion app/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
}
},
"version": "1.0.0",
"version": "1.0.1",
"frameworkVersion": "2.0",
"termsConditionsURL": "https://internalnote.com/terms-and-conditions/"
}
Binary file added app/tmp/app-20230320193113564.zip
Binary file not shown.

0 comments on commit a191707

Please sign in to comment.