|
| 1 | +import common from "../common/common-create.mjs"; |
| 2 | +import { ConfigurationError } from "@pipedream/platform"; |
| 3 | +import { ASSOCIATION_CATEGORY } from "../../common/constants.mjs"; |
| 4 | + |
| 5 | +export default { |
| 6 | + ...common, |
| 7 | + key: "hubspot-create-task", |
| 8 | + name: "Create Task", |
| 9 | + description: "Create a new task. [See the documentation](https://developers.hubspot.com/docs/api/crm/engagements)", |
| 10 | + version: "0.0.1", |
| 11 | + type: "action", |
| 12 | + props: { |
| 13 | + ...common.props, |
| 14 | + toObjectType: { |
| 15 | + propDefinition: [ |
| 16 | + common.props.hubspot, |
| 17 | + "objectType", |
| 18 | + ], |
| 19 | + label: "Associated Object Type", |
| 20 | + description: "Type of object the engagement is being associated with", |
| 21 | + optional: true, |
| 22 | + }, |
| 23 | + toObjectId: { |
| 24 | + propDefinition: [ |
| 25 | + common.props.hubspot, |
| 26 | + "objectId", |
| 27 | + (c) => ({ |
| 28 | + objectType: c.toObjectType, |
| 29 | + }), |
| 30 | + ], |
| 31 | + label: "Associated Object", |
| 32 | + description: "ID of object the engagement is being associated with", |
| 33 | + optional: true, |
| 34 | + }, |
| 35 | + associationType: { |
| 36 | + propDefinition: [ |
| 37 | + common.props.hubspot, |
| 38 | + "associationType", |
| 39 | + (c) => ({ |
| 40 | + fromObjectType: "tasks", |
| 41 | + toObjectType: c.toObjectType, |
| 42 | + }), |
| 43 | + ], |
| 44 | + description: "A unique identifier to indicate the association type between the task and the other object", |
| 45 | + optional: true, |
| 46 | + }, |
| 47 | + objectProperties: { |
| 48 | + type: "object", |
| 49 | + label: "Object Properties", |
| 50 | + description: "Enter the `engagement` properties as a JSON object", |
| 51 | + }, |
| 52 | + }, |
| 53 | + methods: { |
| 54 | + ...common.methods, |
| 55 | + getObjectType() { |
| 56 | + return "tasks"; |
| 57 | + }, |
| 58 | + isRelevantProperty(property) { |
| 59 | + return common.methods.isRelevantProperty(property) && !property.name.includes("hs_pipeline"); |
| 60 | + }, |
| 61 | + createEngagement(objectType, properties, associations, $) { |
| 62 | + return this.hubspot.createObject({ |
| 63 | + objectType, |
| 64 | + data: { |
| 65 | + properties, |
| 66 | + associations, |
| 67 | + }, |
| 68 | + $, |
| 69 | + }); |
| 70 | + }, |
| 71 | + }, |
| 72 | + async run({ $ }) { |
| 73 | + const { |
| 74 | + hubspot, |
| 75 | + /* eslint-disable no-unused-vars */ |
| 76 | + toObjectType, |
| 77 | + toObjectId, |
| 78 | + associationType, |
| 79 | + $db, |
| 80 | + objectProperties, |
| 81 | + ...otherProperties |
| 82 | + } = this; |
| 83 | + |
| 84 | + if ((toObjectId && !associationType) || (!toObjectId && associationType)) { |
| 85 | + throw new ConfigurationError("Both `toObjectId` and `associationType` must be entered"); |
| 86 | + } |
| 87 | + |
| 88 | + const properties = objectProperties |
| 89 | + ? typeof objectProperties === "string" |
| 90 | + ? JSON.parse(objectProperties) |
| 91 | + : objectProperties |
| 92 | + : otherProperties; |
| 93 | + |
| 94 | + const objectType = this.getObjectType(); |
| 95 | + |
| 96 | + const associations = toObjectId |
| 97 | + ? [ |
| 98 | + { |
| 99 | + to: { |
| 100 | + id: toObjectId, |
| 101 | + }, |
| 102 | + types: [ |
| 103 | + { |
| 104 | + associationTypeId: associationType, |
| 105 | + associationCategory: ASSOCIATION_CATEGORY.HUBSPOT_DEFINED, |
| 106 | + }, |
| 107 | + ], |
| 108 | + }, |
| 109 | + ] |
| 110 | + : undefined; |
| 111 | + |
| 112 | + if (properties.hs_task_reminders) { |
| 113 | + properties.hs_task_reminders = Date.parse(properties.hs_task_reminders); |
| 114 | + } |
| 115 | + |
| 116 | + const engagement = await this.createEngagement(objectType, properties, associations, $); |
| 117 | + |
| 118 | + const objectName = hubspot.getObjectTypeName(objectType); |
| 119 | + $.export("$summary", `Successfully created ${objectName}`); |
| 120 | + |
| 121 | + return engagement; |
| 122 | + }, |
| 123 | +}; |
0 commit comments