Last active
August 29, 2015 14:10
-
-
Save jakeisonline/80cabe7d180c86098682 to your computer and use it in GitHub Desktop.
CREATE MANY ORGANIZATIONS & ORGANIZATION MEMBERSHIPS, THEN REMOVE THEM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
# CREATE MANY ORGANIZATIONS & ORGANIZATION MEMBERSHIPS, THEN REMOVE THEM | |
Copy and paste this entire snippet into your browser's JS console, while within | |
/agent and authenticated as an admin. Account must have multi-org feature. | |
jQuery is required, because lazy. | |
USAGE: createManyOrgsAndMemberships(userId, numOrganizations, orgNamePrefix) | |
EXAMPLE: createManyOrgsAndMemberships(12, 2, "Example Org") | |
RESULT: 2 Organizations by the names of "Example Org #1" and "Example Org #2" | |
are created and assigned to the user by the ID of 12 | |
## REALLY IMPORTANT | |
When using createManyOrgsAndMemberships() the console will be outputting plenty | |
of noise. If you want to also delete the organizations at a later time, you should | |
copy and paste the last "Created so far:" message somewhere safe. | |
deleteManyOrganizations([123, 456, 789]); | |
OR if you are still on the same console page as where you executed the creation | |
command, running deleteManyOrganizations(createdOrganizationIDs) as-is will remove | |
all organizations created during that console session. | |
Use is at your own risk. See something to improve? Fork it: | |
https://gist.github.com/jakeisonline/80cabe7d180c86098682 | |
*/ | |
var createdOrganizationIDs = []; | |
function createManyOrgsAndMemberships(userId, numOrganizations, orgNamePrefix) { | |
for (i = 0; i < numOrganizations; i++) { | |
var orgName = orgNamePrefix + " #" + i; | |
createOrganizationAndMembership(orgName, userId); | |
} | |
} | |
function deleteManyOrganizations(orgIdArray) { | |
orgIdArray.forEach(function(i) { | |
$.ajax({ | |
type: 'DELETE', | |
url: '/api/v2/organizations/' + i + '.json', | |
success: function() { | |
console.log('Deleted organization', i); | |
} | |
}) | |
}); | |
} | |
/* Private */ | |
function createOrganizationAndMembership(orgName, userId) { | |
var data = { organization: { name: orgName } }; | |
$.ajax({ | |
type: 'POST', | |
contentType: 'application/json', | |
url: '/api/v2/organizations.json', | |
data: JSON.stringify(data), | |
success: function(response) { | |
var organizationId = response.organization.id; | |
createOrganizationMembership(userId, organizationId); | |
createdOrganizationIDs.push(organizationId); | |
console.log("Created so far:", JSON.stringify(createdOrganizationIDs)); | |
}, | |
error: function() { | |
return; | |
} | |
}); | |
} | |
function createOrganizationMembership(userId, organizationId) { | |
var data = { organization_membership: { organization_id: organizationId } }; | |
$.ajax({ | |
type: 'POST', | |
contentType: 'application/json', | |
url: '/api/v2/users/' + userId + '/organization_memberships.json', | |
data: JSON.stringify(data), | |
success: function(response) { | |
console.log(userId + ' added to organization ' + organizationId); | |
}, | |
error: function() { | |
return; | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment