|
| 1 | +import common from "../common/common-create.mjs"; |
| 2 | +import appProp from "./common-app-prop.mjs"; |
| 3 | +import { ConfigurationError } from "@pipedream/platform"; |
| 4 | +import { ASSOCIATION_CATEGORY } from "../../common/constants.mjs"; |
| 5 | + |
| 6 | +export default { |
| 7 | + ...common, |
| 8 | + key: "hubspot-create-communication", |
| 9 | + name: "Create Communication", |
| 10 | + description: "Create a WhatsApp, LinkedIn, or SMS message. [See the documentation](https://developers.hubspot.com/beta-docs/reference/api/crm/engagements/communications/v3#post-%2Fcrm%2Fv3%2Fobjects%2Fcommunications)", |
| 11 | + version: "0.0.1", |
| 12 | + type: "action", |
| 13 | + props: { |
| 14 | + ...appProp.props, |
| 15 | + toObjectType: { |
| 16 | + propDefinition: [ |
| 17 | + appProp.props.hubspot, |
| 18 | + "objectType", |
| 19 | + ], |
| 20 | + label: "Associated Object Type", |
| 21 | + description: "Type of object the communication is being associated with", |
| 22 | + optional: true, |
| 23 | + }, |
| 24 | + toObjectId: { |
| 25 | + propDefinition: [ |
| 26 | + appProp.props.hubspot, |
| 27 | + "objectId", |
| 28 | + (c) => ({ |
| 29 | + objectType: c.toObjectType, |
| 30 | + }), |
| 31 | + ], |
| 32 | + label: "Associated Object", |
| 33 | + description: "ID of object the communication is being associated with", |
| 34 | + optional: true, |
| 35 | + }, |
| 36 | + associationType: { |
| 37 | + propDefinition: [ |
| 38 | + appProp.props.hubspot, |
| 39 | + "associationType", |
| 40 | + (c) => ({ |
| 41 | + fromObjectType: "communications", |
| 42 | + toObjectType: c.toObjectType, |
| 43 | + }), |
| 44 | + ], |
| 45 | + description: "A unique identifier to indicate the association type between the communication and the other object", |
| 46 | + optional: true, |
| 47 | + }, |
| 48 | + }, |
| 49 | + methods: { |
| 50 | + ...common.methods, |
| 51 | + getObjectType() { |
| 52 | + return "communications"; |
| 53 | + }, |
| 54 | + }, |
| 55 | + async run({ $ }) { |
| 56 | + const { |
| 57 | + hubspot, |
| 58 | + /* eslint-disable no-unused-vars */ |
| 59 | + toObjectType, |
| 60 | + toObjectId, |
| 61 | + associationType, |
| 62 | + getObjectType, |
| 63 | + ...properties |
| 64 | + } = this; |
| 65 | + |
| 66 | + if ((toObjectId && !associationType) || (!toObjectId && associationType)) { |
| 67 | + throw new ConfigurationError("Both `toObjectId` and `associationType` must be entered"); |
| 68 | + } |
| 69 | + |
| 70 | + const response = await hubspot.createObject({ |
| 71 | + $, |
| 72 | + objectType: getObjectType(), |
| 73 | + data: { |
| 74 | + associations: toObjectId |
| 75 | + ? [ |
| 76 | + { |
| 77 | + types: [ |
| 78 | + { |
| 79 | + associationTypeId: associationType, |
| 80 | + associationCategory: ASSOCIATION_CATEGORY.HUBSPOT_DEFINED, |
| 81 | + }, |
| 82 | + ], |
| 83 | + to: { |
| 84 | + id: toObjectId, |
| 85 | + }, |
| 86 | + }, |
| 87 | + ] |
| 88 | + : undefined, |
| 89 | + properties: { |
| 90 | + ...properties, |
| 91 | + hs_communication_logged_from: "CRM", |
| 92 | + }, |
| 93 | + }, |
| 94 | + }); |
| 95 | + |
| 96 | + $.export("$summary", `Successfully created communication with ID ${response.id}`); |
| 97 | + return response; |
| 98 | + }, |
| 99 | +}; |
0 commit comments