Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. function createUsers(anvilAPI, zendeskAPI, ravenAPI, newAcctInfo, aAcctInfo, aZendeskOrg, owner) {
  2. // 4:
  3. var pendingAnvilUsers = [];
  4. newAcctInfo.listOfUsers.forEach(aUser => {
  5. pendingAnvilUsers.push(anvilAPI.addUserToAccount(aAcctInfo.id, aUser));
  6. });
  7. return Promise.all(pendingAnvilUsers).then(aAnvilUsers => {
  8. // 5
  9. var pendingZendeskUsers = [];
  10. aAnvilUsers.forEach((aAnvilUser, index) => {
  11. if (!aAnvilUser) {
  12. logger.log('Could not create user', index, newAcctInfo.listOfUsers[index].email);
  13. pendingZendeskUsers.push(null);
  14. } else {
  15. pendingZendeskUsers.push(zendeskAPI.createUser(aZendeskOrg, aAnvilUser));
  16. }
  17. });
  18. return Promise.all(pendingZendeskUsers).then(aZendeskUsers =>
  19. // 6. Send mails. We could have done this as we were processing but...
  20. Promise.all(
  21. aAnvilUsers,
  22. aZendeskUsers,
  23. aAnvilUsers.map(aUser => {
  24. // New users have this field, and we should only send the mail for those
  25. if (aUser.email === owner.email) {
  26. aUser.ctPassword = owner.ctPassword;
  27. }
  28. if (aUser.ctPassword) {
  29. return ravenAPI && ravenAPI.sendCustomMail(aUser.email, {
  30. user: aUser,
  31. organization: aAcctInfo
  32. });
  33. } else {
  34. return null;
  35. }
  36. })
  37. )
  38. );
  39. }).catch(e => {
  40. logger.log('Returning error:', e);
  41. });
  42. }
  43.  
  44. // Process:
  45. // 1: Create the administrative user for the account (Anvil)
  46. // 2: Using the credentials for that user, create the account (Anvil)
  47. // 3: Create the organization at Zendesk
  48. // 4: Add the rest of the users to Anvil
  49. // 5: Add the rest of the users to Zendesk
  50. // For this version, if 1, 2, or 3 fails just throw and be done. If 4 or 5 fails, log it
  51. // and continue
  52. // 6: Send mails!
  53. function postAccount(aReq, aRes) {
  54. var authToken = aReq.params.authToken;
  55. getAuthenticatedUser(aReq.path, authToken, aReq.tbConfig.sessionTimeout).then(userInfo => {
  56. if (!userInfo || userInfo.role !== 'full') {
  57. logger.log(aReq.path, ': Unauthorized');
  58. aRes.status(401).send(new ErrorInfo(401, 'Unauthorized'));
  59. return null;
  60. }
  61. var newAcctInfo = aReq.body;
  62. if (!Utils.isA(PARAMS_TEMPLATE['postAccount'], newAcctInfo)) {
  63. aRes.status(400).send(new ErrorInfo(400, 'Incorrect parameters'));
  64. return null;
  65. }
  66.  
  67. var tbConfig = aReq.tbConfig;
  68. var anvilAPI = tbConfig.anvilAPI;
  69. var zendeskAPI = tbConfig.zendeskAPI;
  70. var ravenAPI = tbConfig.ravenAPI;
  71. var supportTiers = tbConfig.supportTiers;
  72. var owner = newAcctInfo.owner;
  73. // 1 and 2
  74. return anvilAPI.createAccount(owner, newAcctInfo.accountName).then(aAcctInfo => {
  75. if (!aAcctInfo) {
  76. throw new ErrorInfo(50001, 'Failed to create anvil account!');
  77. }
  78. // aAcctInfo could have a ctPassword field with the owner's password (in CT) if it was
  79. // created in the previous step.
  80. owner.ctPassword = aAcctInfo.ctPassword;
  81. delete aAcctInfo.ctPassword;
  82. var isNewAcct = !Array.isArray(aAcctInfo);
  83.  
  84. if (!isNewAcct) {
  85. logger.log('The owner already has an organization created. Reusing it!');
  86. aAcctInfo = aAcctInfo[0];
  87. }
  88.  
  89. // 3.
  90. return zendeskAPI.
  91. createOrganization(aAcctInfo, owner, supportTiers[newAcctInfo.supportTier]).
  92. then(aZendeskOrg => {
  93. if (!aZendeskOrg) {
  94. throw new ErrorInfo(5002, 'Failed to create zendesk account');
  95. }
  96.  
  97. // 4, 5 and 6
  98. return createUsers(anvilAPI, zendeskAPI, ravenAPI, newAcctInfo, aAcctInfo,
  99. aZendeskOrg, owner).then(aUserCreationData => {
  100. var aAnvilUsers = aUserCreationData[0];
  101. var aZendeskUsers = aUserCreationData[1];
  102. var aMailsSent = aUserCreationData[2];
  103. aRes.status(200).send({ owner: owner, isNew: isNewAcct, account: aAcctInfo,
  104. anvilUsers: aAnvilUsers, zendeskUsers: aZendeskUsers });
  105. });
  106. });
  107. });
  108. }).catch(e => {
  109. logger.log('Returning error:', e);
  110. aRes.status(500).send(e);
  111. });
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement