Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // The only available npm module in Zapier Code app is node-fetch.
- // Fetch is already available in Zapier.
- // Requiring node-fetch when debugging in RunKit.
- var fetch = require ('node-fetch')
- // inputData and ouput are Zapier's built-in global vars so you can include data from previous Zapier step.
- // Declare sample inputData for debugging.
- var inputData = {
- phone: 60124739
- }
- // Declare output format and other vars.
- var //store = StoreClient(''),
- today = new Date(),
- todayMinus7 = today,
- output = {
- SignupDate: null,
- SnapaskUserId: null,
- FirstTrialDate: null,
- ManyChatSequence: null,
- },
- metabase = {
- url: '' + '/api/', //Metabase url
- user: '', //username
- pass: '', //password
- token: '', //Metabase Auth Token, pending StoreClient implementation
- date: null
- }
- // Metabase auth token expires every 1-2 weeks. Create var that shows date 7 days ago for
- todayMinus7.setDate(todayMinus7.getDate() - 7)
- // Accept Metabase query, with startIndex and optional endIndex params to define number of array results wanted
- function QueryMetabase(query, startIndex, endIndex) {
- var endIndex = endIndex || 1, // Declare optional param
- data = { 'database': 2, 'type': 'query', 'query': query }, // Specify database ID
- options = {method: 'POST', headers: {'Content-Type': 'application/json', 'X-Metabase-Session': metabase.token}, body: JSON.stringify(data)}
- return fetch (metabase.url + 'dataset', options)
- .then(res => res.json())
- .then(json => {
- var rows = json.data.rows[0]
- if (rows) {
- return rows.slice(startIndex, endIndex + 1)
- } else {return null}
- })
- .catch(err => console.error(err))
- }
- // Specify Metabase query format
- // Use 'fields' to specify the result columns wanted
- function GetSignupQuery() {
- return {
- 'source_table': 60,
- 'filter': [ 'AND', [ 'CONTAINS', [ 'field-id', 613 ], ''+inputData.phone+'' ] ],
- 'fields': [ [ 'field-id', 613 ], [ 'datetime-field', [ 'field-id', 589 ], 'minute' ], [ 'field-id', 603 ] ]
- }
- }
- function GetFirstTrialQuery(UserId) {
- return {
- 'source_table': 33,
- 'filter': [ 'AND', [ '=', 353, ''+UserId+'' ] ],
- 'order_by': [ [ [ 'datetime-field', [ 'field-id', 336 ], 'minute' ], 'ascending' ] ],
- 'fields': [ [ 'datetime-field', [ 'field-id', 336 ], 'minute' ] ]
- }
- }
- // Insert Metabase query into fetch promise and handle response
- function GetSignup() {
- return QueryMetabase(GetSignupQuery(), 1, 2)
- .then(res => {
- if (res) {
- output.SignupDate = res[0]
- output.SnapaskUserId = res[1]
- }
- })
- }
- function GetFirstTrial() {
- return QueryMetabase(GetFirstTrialQuery(output.SnapaskUserId), 0)
- .then(res => {
- if (res) output.FirstTrialDate = res[0]
- })
- }
- // Logic for assigning ManyChat sequence ID
- function GetManyChatSequence() {
- if (output.SignupDate && output.FirstTrialDate) {
- output.ManyChatSequence = 198381
- return output
- }
- if (!output.SignupDate && !output.FirstTrialDate) {
- output.ManyChatSequence = 198429
- return output
- }
- if (output.SignupDate && !output.FirstTrialDate) {
- var d = new Date(inputData.entered_at)
- d.setDate(d.getDate() - 10) //10 days before entering funnel
- var s = new Date(output.SignupDate)
- if (s >= d) {
- output.ManyChatSequence = 198431
- } else {
- output.ManyChatSequence = 198433
- }
- return output
- }
- }
- // Chain promises
- GetSignup()
- .then(GetFirstTrial)
- .then(GetManyChatSequence)
- //.then(output => console.log(output))
- // Uncomment following 2 lines to enable async callback in Zapier. Otherwise promises won't work.
- //.then(function() {callback(null, output)})
- //.catch(callback)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement