|
| 1 | +import common from "../common/common.mjs"; |
| 2 | +import { |
| 3 | + DEFAULT_LIMIT, OBJECT_TYPES, |
| 4 | +} from "../../common/constants.mjs"; |
| 5 | + |
| 6 | +export default { |
| 7 | + ...common, |
| 8 | + key: "hubspot-new-note", |
| 9 | + name: "New Note Created", |
| 10 | + description: "Emit new event for each new note created. [See the documentation](https://developers.hubspot.com/docs/reference/api/crm/engagements/notes#get-%2Fcrm%2Fv3%2Fobjects%2Fnotes)", |
| 11 | + version: "0.0.1", |
| 12 | + type: "source", |
| 13 | + dedupe: "unique", |
| 14 | + methods: { |
| 15 | + ...common.methods, |
| 16 | + getTs(note) { |
| 17 | + return Date.parse(note.createdAt); |
| 18 | + }, |
| 19 | + generateMeta(note) { |
| 20 | + return { |
| 21 | + id: note.id, |
| 22 | + summary: `New Note: ${note.properties.hs_body_preview || note.id}`, |
| 23 | + ts: this.getTs(note), |
| 24 | + }; |
| 25 | + }, |
| 26 | + isRelevant(note, createdAfter) { |
| 27 | + return this.getTs(note) > createdAfter; |
| 28 | + }, |
| 29 | + async getParams() { |
| 30 | + const { results: allProperties } = await this.hubspot.getProperties({ |
| 31 | + objectType: "notes", |
| 32 | + }); |
| 33 | + const properties = allProperties.map(({ name }) => name); |
| 34 | + |
| 35 | + const objectTypes = OBJECT_TYPES.map(({ value }) => value); |
| 36 | + const { results: custom } = await this.hubspot.listSchemas(); |
| 37 | + const customObjects = custom?.map(({ name }) => name); |
| 38 | + const associations = [ |
| 39 | + ...objectTypes, |
| 40 | + ...customObjects, |
| 41 | + ]; |
| 42 | + |
| 43 | + return { |
| 44 | + params: { |
| 45 | + limit: DEFAULT_LIMIT, |
| 46 | + properties: properties.join(","), |
| 47 | + associations: associations.join(","), |
| 48 | + }, |
| 49 | + }; |
| 50 | + }, |
| 51 | + async processResults(after, params) { |
| 52 | + const notes = await this.getPaginatedItems(this.hubspot.listNotes.bind(this), params); |
| 53 | + await this.processEvents(notes, after); |
| 54 | + }, |
| 55 | + }, |
| 56 | +}; |
0 commit comments