Advertisement
Guest User

Untitled

a guest
Oct 10th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. // The only available npm module in Zapier Code app is node-fetch.
  2. // Fetch is already available in Zapier.
  3. // Requiring node-fetch when debugging in RunKit.
  4. var fetch = require ('node-fetch')
  5.  
  6. // inputData and ouput are Zapier's built-in global vars so you can include data from previous Zapier step.
  7. // Declare sample inputData for debugging.
  8. var inputData = {
  9. phone: 60124739
  10. }
  11.  
  12. // Declare output format and other vars.
  13. var //store = StoreClient(''),
  14. today = new Date(),
  15. todayMinus7 = today,
  16. output = {
  17. SignupDate: null,
  18. SnapaskUserId: null,
  19. FirstTrialDate: null,
  20. ManyChatSequence: null,
  21. },
  22. metabase = {
  23. url: '' + '/api/', //Metabase url
  24. user: '', //username
  25. pass: '', //password
  26. token: '', //Metabase Auth Token, pending StoreClient implementation
  27. date: null
  28. }
  29.  
  30. // Metabase auth token expires every 1-2 weeks. Create var that shows date 7 days ago for
  31. todayMinus7.setDate(todayMinus7.getDate() - 7)
  32.  
  33. // Accept Metabase query, with startIndex and optional endIndex params to define number of array results wanted
  34. function QueryMetabase(query, startIndex, endIndex) {
  35. var endIndex = endIndex || 1, // Declare optional param
  36. data = { 'database': 2, 'type': 'query', 'query': query }, // Specify database ID
  37. options = {method: 'POST', headers: {'Content-Type': 'application/json', 'X-Metabase-Session': metabase.token}, body: JSON.stringify(data)}
  38. return fetch (metabase.url + 'dataset', options)
  39. .then(res => res.json())
  40. .then(json => {
  41. var rows = json.data.rows[0]
  42. if (rows) {
  43. return rows.slice(startIndex, endIndex + 1)
  44. } else {return null}
  45. })
  46. .catch(err => console.error(err))
  47. }
  48.  
  49. // Specify Metabase query format
  50. // Use 'fields' to specify the result columns wanted
  51. function GetSignupQuery() {
  52. return {
  53. 'source_table': 60,
  54. 'filter': [ 'AND', [ 'CONTAINS', [ 'field-id', 613 ], ''+inputData.phone+'' ] ],
  55. 'fields': [ [ 'field-id', 613 ], [ 'datetime-field', [ 'field-id', 589 ], 'minute' ], [ 'field-id', 603 ] ]
  56. }
  57. }
  58.  
  59. function GetFirstTrialQuery(UserId) {
  60. return {
  61. 'source_table': 33,
  62. 'filter': [ 'AND', [ '=', 353, ''+UserId+'' ] ],
  63. 'order_by': [ [ [ 'datetime-field', [ 'field-id', 336 ], 'minute' ], 'ascending' ] ],
  64. 'fields': [ [ 'datetime-field', [ 'field-id', 336 ], 'minute' ] ]
  65. }
  66. }
  67.  
  68. // Insert Metabase query into fetch promise and handle response
  69. function GetSignup() {
  70. return QueryMetabase(GetSignupQuery(), 1, 2)
  71. .then(res => {
  72. if (res) {
  73. output.SignupDate = res[0]
  74. output.SnapaskUserId = res[1]
  75. }
  76. })
  77. }
  78.  
  79. function GetFirstTrial() {
  80. return QueryMetabase(GetFirstTrialQuery(output.SnapaskUserId), 0)
  81. .then(res => {
  82. if (res) output.FirstTrialDate = res[0]
  83. })
  84. }
  85.  
  86. // Logic for assigning ManyChat sequence ID
  87. function GetManyChatSequence() {
  88. if (output.SignupDate && output.FirstTrialDate) {
  89. output.ManyChatSequence = 198381
  90. return output
  91. }
  92. if (!output.SignupDate && !output.FirstTrialDate) {
  93. output.ManyChatSequence = 198429
  94. return output
  95. }
  96. if (output.SignupDate && !output.FirstTrialDate) {
  97. var d = new Date(inputData.entered_at)
  98. d.setDate(d.getDate() - 10) //10 days before entering funnel
  99. var s = new Date(output.SignupDate)
  100. if (s >= d) {
  101. output.ManyChatSequence = 198431
  102. } else {
  103. output.ManyChatSequence = 198433
  104. }
  105. return output
  106. }
  107. }
  108.  
  109. // Chain promises
  110. GetSignup()
  111. .then(GetFirstTrial)
  112. .then(GetManyChatSequence)
  113. //.then(output => console.log(output))
  114. // Uncomment following 2 lines to enable async callback in Zapier. Otherwise promises won't work.
  115. //.then(function() {callback(null, output)})
  116. //.catch(callback)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement