diff --git a/app/assets/iframe.html b/app/assets/iframe.html index 0b3f8ab..d89a300 100644 --- a/app/assets/iframe.html +++ b/app/assets/iframe.html @@ -8,7 +8,7 @@
-
Remove Bad Users
+
Remove Bad Users
Add Matching Users
diff --git a/app/assets/main.js b/app/assets/main.js index 1194dd9..34bb025 100644 --- a/app/assets/main.js +++ b/app/assets/main.js @@ -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 = `
  • -
    -
    - -
    -
    -

    ${val.name}

    -

    ${val.email}

    -
    -
    -
    View
    -
    -
    -
  • ` - - 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){ @@ -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 = `
  • -
    -
    - -
    -
    -

    ${user.name}

    -

    ${user.email}

    -
    -
    -
    View
    -
    -
    -
  • ` - - 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":'' + } + if (type == 'remove'){ + icon.color = "red"; + icon.path = '' + } + + var output = `
  • +
    +
    + +
    +
    +

    ${user.name}

    +

    ${user.email}

    +
    +
    +
    View
    +
    +
    +
  • ` + + $('#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')); diff --git a/app/manifest.json b/app/manifest.json index 7a53566..f6e3b4f 100644 --- a/app/manifest.json +++ b/app/manifest.json @@ -15,7 +15,7 @@ } } }, - "version": "1.0.0", + "version": "1.0.1", "frameworkVersion": "2.0", "termsConditionsURL": "https://internalnote.com/terms-and-conditions/" } \ No newline at end of file diff --git a/app/tmp/app-20230320193113564.zip b/app/tmp/app-20230320193113564.zip new file mode 100644 index 0000000..adfd8d6 Binary files /dev/null and b/app/tmp/app-20230320193113564.zip differ