|
| 1 | +import common from "../common/common.mjs"; |
| 2 | +import { DEFAULT_LIMIT } from "../../common/constants.mjs"; |
| 3 | + |
| 4 | +export default { |
| 5 | + ...common, |
| 6 | + key: "hubspot-new-custom-object-property-change", |
| 7 | + name: "New Custom Object Property Change", |
| 8 | + description: "Emit new event when a specified property is provided or updated on a custom object.", |
| 9 | + version: "0.0.1", |
| 10 | + dedupe: "unique", |
| 11 | + type: "source", |
| 12 | + props: { |
| 13 | + ...common.props, |
| 14 | + objectSchema: { |
| 15 | + propDefinition: [ |
| 16 | + common.props.hubspot, |
| 17 | + "objectSchema", |
| 18 | + ], |
| 19 | + }, |
| 20 | + property: { |
| 21 | + type: "string", |
| 22 | + label: "Property", |
| 23 | + description: "The custom object property to watch for changes", |
| 24 | + async options() { |
| 25 | + const properties = await this.getWriteOnlyProperties(this.objectSchema); |
| 26 | + return properties.map((property) => property.name); |
| 27 | + }, |
| 28 | + }, |
| 29 | + }, |
| 30 | + methods: { |
| 31 | + ...common.methods, |
| 32 | + getTs(object) { |
| 33 | + const history = object.propertiesWithHistory[this.property]; |
| 34 | + if (!history || !(history.length > 0)) { |
| 35 | + return; |
| 36 | + } |
| 37 | + return Date.parse(history[0].timestamp); |
| 38 | + }, |
| 39 | + generateMeta(object) { |
| 40 | + const { |
| 41 | + id, |
| 42 | + properties, |
| 43 | + } = object; |
| 44 | + const ts = this.getTs(object); |
| 45 | + return { |
| 46 | + id: `${id}${ts}`, |
| 47 | + summary: properties[this.property], |
| 48 | + ts, |
| 49 | + }; |
| 50 | + }, |
| 51 | + isRelevant(object, updatedAfter) { |
| 52 | + return !updatedAfter || this.getTs(object) > updatedAfter; |
| 53 | + }, |
| 54 | + getParams(after) { |
| 55 | + return { |
| 56 | + object: this.objectSchema, |
| 57 | + data: { |
| 58 | + limit: DEFAULT_LIMIT, |
| 59 | + properties: [ |
| 60 | + this.property, |
| 61 | + ], |
| 62 | + sorts: [ |
| 63 | + { |
| 64 | + propertyName: "hs_lastmodifieddate", |
| 65 | + direction: "DESCENDING", |
| 66 | + }, |
| 67 | + ], |
| 68 | + filterGroups: [ |
| 69 | + { |
| 70 | + filters: [ |
| 71 | + { |
| 72 | + propertyName: this.property, |
| 73 | + operator: "HAS_PROPERTY", |
| 74 | + }, |
| 75 | + { |
| 76 | + propertyName: "hs_lastmodifieddate", |
| 77 | + operator: "GTE", |
| 78 | + value: after, |
| 79 | + }, |
| 80 | + ], |
| 81 | + }, |
| 82 | + ], |
| 83 | + }, |
| 84 | + }; |
| 85 | + }, |
| 86 | + batchGetCustomObjects(inputs) { |
| 87 | + return this.hubspot.batchGetObjects({ |
| 88 | + objectType: this.objectSchema, |
| 89 | + data: { |
| 90 | + properties: [ |
| 91 | + this.property, |
| 92 | + ], |
| 93 | + propertiesWithHistory: [ |
| 94 | + this.property, |
| 95 | + ], |
| 96 | + inputs, |
| 97 | + }, |
| 98 | + }); |
| 99 | + }, |
| 100 | + async processResults(after, params) { |
| 101 | + const properties = await this.getWriteOnlyProperties(this.objectSchema); |
| 102 | + const propertyNames = properties.map((property) => property.name); |
| 103 | + |
| 104 | + if (!propertyNames.includes(this.property)) { |
| 105 | + throw new Error(`Property "${this.property}" not supported for custom object ${this.objectSchema}.`); |
| 106 | + } |
| 107 | + |
| 108 | + const updatedObjects = await this.getPaginatedItems(this.hubspot.searchCRM, params); |
| 109 | + |
| 110 | + if (!updatedObjects.length) { |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + const results = await this.processChunks({ |
| 115 | + batchRequestFn: this.batchGetCustomObjects, |
| 116 | + chunks: this.getChunks(updatedObjects), |
| 117 | + }); |
| 118 | + |
| 119 | + this.processEvents(results, after); |
| 120 | + }, |
| 121 | + }, |
| 122 | +}; |
0 commit comments