Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function createUsers(anvilAPI, zendeskAPI, ravenAPI, newAcctInfo, aAcctInfo, aZendeskOrg, owner) {
- // 4:
- var pendingAnvilUsers = [];
- newAcctInfo.listOfUsers.forEach(aUser => {
- pendingAnvilUsers.push(anvilAPI.addUserToAccount(aAcctInfo.id, aUser));
- });
- return Promise.all(pendingAnvilUsers).then(aAnvilUsers => {
- // 5
- var pendingZendeskUsers = [];
- aAnvilUsers.forEach((aAnvilUser, index) => {
- if (!aAnvilUser) {
- logger.log('Could not create user', index, newAcctInfo.listOfUsers[index].email);
- pendingZendeskUsers.push(null);
- } else {
- pendingZendeskUsers.push(zendeskAPI.createUser(aZendeskOrg, aAnvilUser));
- }
- });
- return Promise.all(pendingZendeskUsers).then(aZendeskUsers =>
- // 6. Send mails. We could have done this as we were processing but...
- Promise.all(
- aAnvilUsers,
- aZendeskUsers,
- aAnvilUsers.map(aUser => {
- // New users have this field, and we should only send the mail for those
- if (aUser.email === owner.email) {
- aUser.ctPassword = owner.ctPassword;
- }
- if (aUser.ctPassword) {
- return ravenAPI && ravenAPI.sendCustomMail(aUser.email, {
- user: aUser,
- organization: aAcctInfo
- });
- } else {
- return null;
- }
- })
- )
- );
- }).catch(e => {
- logger.log('Returning error:', e);
- });
- }
- // Process:
- // 1: Create the administrative user for the account (Anvil)
- // 2: Using the credentials for that user, create the account (Anvil)
- // 3: Create the organization at Zendesk
- // 4: Add the rest of the users to Anvil
- // 5: Add the rest of the users to Zendesk
- // For this version, if 1, 2, or 3 fails just throw and be done. If 4 or 5 fails, log it
- // and continue
- // 6: Send mails!
- function postAccount(aReq, aRes) {
- var authToken = aReq.params.authToken;
- getAuthenticatedUser(aReq.path, authToken, aReq.tbConfig.sessionTimeout).then(userInfo => {
- if (!userInfo || userInfo.role !== 'full') {
- logger.log(aReq.path, ': Unauthorized');
- aRes.status(401).send(new ErrorInfo(401, 'Unauthorized'));
- return null;
- }
- var newAcctInfo = aReq.body;
- if (!Utils.isA(PARAMS_TEMPLATE['postAccount'], newAcctInfo)) {
- aRes.status(400).send(new ErrorInfo(400, 'Incorrect parameters'));
- return null;
- }
- var tbConfig = aReq.tbConfig;
- var anvilAPI = tbConfig.anvilAPI;
- var zendeskAPI = tbConfig.zendeskAPI;
- var ravenAPI = tbConfig.ravenAPI;
- var supportTiers = tbConfig.supportTiers;
- var owner = newAcctInfo.owner;
- // 1 and 2
- return anvilAPI.createAccount(owner, newAcctInfo.accountName).then(aAcctInfo => {
- if (!aAcctInfo) {
- throw new ErrorInfo(50001, 'Failed to create anvil account!');
- }
- // aAcctInfo could have a ctPassword field with the owner's password (in CT) if it was
- // created in the previous step.
- owner.ctPassword = aAcctInfo.ctPassword;
- delete aAcctInfo.ctPassword;
- var isNewAcct = !Array.isArray(aAcctInfo);
- if (!isNewAcct) {
- logger.log('The owner already has an organization created. Reusing it!');
- aAcctInfo = aAcctInfo[0];
- }
- // 3.
- return zendeskAPI.
- createOrganization(aAcctInfo, owner, supportTiers[newAcctInfo.supportTier]).
- then(aZendeskOrg => {
- if (!aZendeskOrg) {
- throw new ErrorInfo(5002, 'Failed to create zendesk account');
- }
- // 4, 5 and 6
- return createUsers(anvilAPI, zendeskAPI, ravenAPI, newAcctInfo, aAcctInfo,
- aZendeskOrg, owner).then(aUserCreationData => {
- var aAnvilUsers = aUserCreationData[0];
- var aZendeskUsers = aUserCreationData[1];
- var aMailsSent = aUserCreationData[2];
- aRes.status(200).send({ owner: owner, isNew: isNewAcct, account: aAcctInfo,
- anvilUsers: aAnvilUsers, zendeskUsers: aZendeskUsers });
- });
- });
- });
- }).catch(e => {
- logger.log('Returning error:', e);
- aRes.status(500).send(e);
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement