|  | 
|  | 1 | +import { DEFAULT_EMAIL_PROPERTIES } from "../../common/constants.mjs"; | 
|  | 2 | +import hubspot from "../../hubspot.app.mjs"; | 
|  | 3 | + | 
|  | 4 | +export default { | 
|  | 5 | +  key: "hubspot-get-associated-emails", | 
|  | 6 | +  name: "Get Associated Emails", | 
|  | 7 | +  description: "Retrieves emails associated with a specific object (contact, company, or deal). [See the documentation](https://developers.hubspot.com/docs/reference/api/crm/search)", | 
|  | 8 | +  version: "0.0.1", | 
|  | 9 | +  type: "action", | 
|  | 10 | +  props: { | 
|  | 11 | +    hubspot, | 
|  | 12 | +    objectType: { | 
|  | 13 | +      propDefinition: [ | 
|  | 14 | +        hubspot, | 
|  | 15 | +        "objectType", | 
|  | 16 | +        () => ({ | 
|  | 17 | +          includeCustom: true, | 
|  | 18 | +        }), | 
|  | 19 | +      ], | 
|  | 20 | +      label: "Associated Object Type", | 
|  | 21 | +      description: "The type of the object the emails are associated with", | 
|  | 22 | +    }, | 
|  | 23 | +    objectId: { | 
|  | 24 | +      type: "string", | 
|  | 25 | +      label: "Object ID", | 
|  | 26 | +      description: "The ID of the object to get associated emails for", | 
|  | 27 | +      propDefinition: [ | 
|  | 28 | +        hubspot, | 
|  | 29 | +        "objectIds", | 
|  | 30 | +        ({ objectType }) => ({ | 
|  | 31 | +          objectType, | 
|  | 32 | +        }), | 
|  | 33 | +      ], | 
|  | 34 | +    }, | 
|  | 35 | +    additionalProperties: { | 
|  | 36 | +      type: "string[]", | 
|  | 37 | +      label: "Additional Properties", | 
|  | 38 | +      description: "Additional properties to retrieve for the emails", | 
|  | 39 | +      optional: true, | 
|  | 40 | +    }, | 
|  | 41 | +    limit: { | 
|  | 42 | +      type: "integer", | 
|  | 43 | +      label: "Limit", | 
|  | 44 | +      description: "Maximum number of emails to retrieve", | 
|  | 45 | +      optional: true, | 
|  | 46 | +      default: 20, | 
|  | 47 | +      min: 1, | 
|  | 48 | +      max: 100, | 
|  | 49 | +    }, | 
|  | 50 | +  }, | 
|  | 51 | +  async run({ $ }) { | 
|  | 52 | +    const properties = [ | 
|  | 53 | +      ...DEFAULT_EMAIL_PROPERTIES, | 
|  | 54 | +      ...(this.additionalProperties || []), | 
|  | 55 | +    ]; | 
|  | 56 | + | 
|  | 57 | +    const { | 
|  | 58 | +      objectType, objectId, limit, | 
|  | 59 | +    } = this; | 
|  | 60 | + | 
|  | 61 | +    const { results } = await this.hubspot.getAssociations({ | 
|  | 62 | +      $, | 
|  | 63 | +      objectType, | 
|  | 64 | +      objectId, | 
|  | 65 | +      toObjectType: "emails", | 
|  | 66 | +      params: { | 
|  | 67 | +        limit, | 
|  | 68 | +      }, | 
|  | 69 | +    }); | 
|  | 70 | + | 
|  | 71 | +    if (!results?.length > 0) { | 
|  | 72 | +      $.export("$summary", "No emails found with this association"); | 
|  | 73 | +      return []; | 
|  | 74 | +    } | 
|  | 75 | + | 
|  | 76 | +    const emailIds = results.map((association) => ({ | 
|  | 77 | +      id: association.toObjectId, | 
|  | 78 | +    })); | 
|  | 79 | + | 
|  | 80 | +    const { results: emails } = await this.hubspot.batchGetObjects({ | 
|  | 81 | +      $, | 
|  | 82 | +      objectType: "emails", | 
|  | 83 | +      data: { | 
|  | 84 | +        properties, | 
|  | 85 | +        inputs: emailIds, | 
|  | 86 | +      }, | 
|  | 87 | +    }); | 
|  | 88 | + | 
|  | 89 | +    // Sort emails by timestamp in descending order (most recent first) | 
|  | 90 | +    emails?.sort((a, b) => { | 
|  | 91 | +      const timestampA = new Date(a.properties?.hs_timestamp || 0).getTime(); | 
|  | 92 | +      const timestampB = new Date(b.properties?.hs_timestamp || 0).getTime(); | 
|  | 93 | +      return timestampB - timestampA; | 
|  | 94 | +    }) || []; | 
|  | 95 | + | 
|  | 96 | +    const summary = `Successfully retrieved ${emails.length} email(s)`; | 
|  | 97 | + | 
|  | 98 | +    $.export("$summary", summary); | 
|  | 99 | + | 
|  | 100 | +    return emails; | 
|  | 101 | +  }, | 
|  | 102 | +}; | 
0 commit comments