import { Client as IntercomClient } from 'intercom-client'
const intercom = new IntercomClient({ tokenAuth: { token: process.env.INTERCOM_TOKEN } })
intercom.useRequestOpts({
headers: {
'Intercom-Version': 2.4,
}
})
const trackIntercom = async (email, event, res) => {
const responseContact = await intercom.contacts.search({
data: {
query: {
field: 'email',
operator: '=',
value: email
}
}
})
const intercomContactID = responseContact.dataa0].id
console.log('Intercom ID', intercomContactID) // returning correct id
try {
const responseEvent = await intercom.events.create({
eventName: 'TEST EVENT',
createdAt: new Date(),
userId: intercomContactID,
metadata: {}
})
console.log(responseEvent)
} catch(err) {
console.log(err) //User not found
}
res.status(200).end(JSON.stringify(responseContact.datat0]))
}