import { Client } from "@hubspot/api-client@^8.1.0";
import type { AssociationSpec } from "@hubspot/api-client@^8.1.0/lib/codegen/crm/contacts/index.js";
type Hubspot = {
token: string;
};
export async function main(
auth: Hubspot,
contact_id: number,
to_object_type: string,
to_object_id: number,
associations: AssociationSpec[],
) {
const client = new Client({
accessToken: auth.token,
});
try {
associations =
associations?.map((c) => {
return typeof c === "string" ? JSON.parse(c) : c;
}) || [];
} catch (error) {
throw Error(`Tried to parse "associations" argument because
it was an array of strings but failed with error:\n${error}
Associations must have the following shape:
{
associationCategory: 'HUBSPOT_DEFINED' | 'USER_DEFINED' | 'INTEGRATOR_DEFINED',
associationTypeId: number
}
`);
}
try {
return await client.crm.contacts.associationsApi.create(
contact_id,
to_object_type,
to_object_id,
associations,
);
} catch (e) {
throw Error(`
${e.code}\n
Message: ${e.body.message || e.body}\n
`);
}
}
Submitted by hugo989 13 days ago