35 lines
1.0 KiB
JavaScript
35 lines
1.0 KiB
JavaScript
const getHubspotPipelines = require("./getHubspotPipelines")
|
|
const getHubspotTicketStatusByContact = require("./getHubspotTicketStatusByContact")
|
|
|
|
|
|
async function findTicketOpenHubspotByContact(authentication, contact) {
|
|
const { token } = authentication
|
|
|
|
console.log('contact: ', contact)
|
|
|
|
const pipelines = await getHubspotPipelines(token)
|
|
const tickets = await getHubspotTicketStatusByContact(token, contact.contactId)
|
|
|
|
let open_tickets = []
|
|
|
|
for (let pipeline of pipelines.results) {
|
|
const open = pipeline.stages.filter((s) => s.metadata.ticketState.toLowerCase() == "open")
|
|
|
|
if (open && open.length > 0) {
|
|
open_tickets = [...open_tickets, ...open];
|
|
}
|
|
}
|
|
|
|
const ticket_ids = open_tickets.map((t) => t.id)
|
|
|
|
if (ticket_ids && ticket_ids.length > 0 && tickets?.results?.length > 0) {
|
|
|
|
const ticket = tickets.results.find(t => ticket_ids.includes(t.properties.hs_pipeline_stage))
|
|
|
|
return ticket
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = findTicketOpenHubspotByContact |