@dma-sdk/hubspot 1.0.18 → 1.0.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,4 +3,5 @@ export declare const ApiMethods: {
3
3
  refreshToken: string;
4
4
  createObjectProperties: (objectType: string) => string;
5
5
  subscriptions: (appId: string) => string;
6
+ associateObjectBatch: (fromObjectType: string, toObjectType: string) => string;
6
7
  };
@@ -4,6 +4,7 @@ exports.ApiMethods = void 0;
4
4
  exports.ApiMethods = {
5
5
  getUserInfoByToken: '/oauth/v1/access-tokens',
6
6
  refreshToken: '/oauth/v1/token',
7
- createObjectProperties: (objectType) => '/crm/v3/properties/{objectType}/batch/create'.replace('{objectType}', objectType),
8
- subscriptions: (appId) => '/webhooks/v3/{appId}/subscriptions'.replace('{appId}', appId),
7
+ createObjectProperties: (objectType) => `/crm/v3/properties/${objectType}/batch/create`,
8
+ subscriptions: (appId) => `/webhooks/v3/${appId}/subscriptions`,
9
+ associateObjectBatch: (fromObjectType, toObjectType) => `/crm/v4/associations/${fromObjectType}/${toObjectType}/batch/create`
9
10
  };
@@ -0,0 +1,3 @@
1
+ export declare class SystemError extends Error {
2
+ constructor(message: string);
3
+ }
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SystemError = void 0;
4
+ class SystemError extends Error {
5
+ constructor(message) {
6
+ super(message);
7
+ this.name = "SystemError";
8
+ }
9
+ }
10
+ exports.SystemError = SystemError;
@@ -6,3 +6,17 @@ export interface AssociateObjectInput {
6
6
  objectIdSecond: string;
7
7
  options?: AssociationSpec[];
8
8
  }
9
+ export interface BatchAssociateObjectInput {
10
+ objectTypeFirst: string;
11
+ objectTypeSecond: string;
12
+ associations: AssociationsObject[];
13
+ }
14
+ export interface AssociationsObject {
15
+ from: {
16
+ id: string;
17
+ };
18
+ to: {
19
+ id: string;
20
+ };
21
+ types: Array<AssociationSpec>;
22
+ }
@@ -9,8 +9,8 @@ import { CollectionResponsePropertyNoPaging, PropertyCreate, PropertyGroup, Prop
9
9
  import { HubspotSignatureData } from '../types/auth/hs-signature-data';
10
10
  import { CreateSubscriptionInput } from '../interfaces/input/subscriptions';
11
11
  import { CreateSubscriptionOutput } from '../interfaces/output/subscriptions';
12
- import { PublicObjectSearchRequest } from '@hubspot/api-client/lib/codegen/crm/companies';
13
- import { AssociateObjectInput } from '../interfaces/input/associations';
12
+ import { PublicObjectSearchRequest, SimplePublicObjectWithAssociations } from '@hubspot/api-client/lib/codegen/crm/companies';
13
+ import { AssociateObjectInput, BatchAssociateObjectInput } from '../interfaces/input/associations';
14
14
  export declare class HubspotService {
15
15
  static HS_BASE_URL: string;
16
16
  static readonly hubspotObjectType: {
@@ -80,7 +80,7 @@ export declare class HubspotService {
80
80
  props?: string[];
81
81
  propsWithHistory?: string[];
82
82
  accessToken: string;
83
- }): Promise<SimplePublicObject>;
83
+ }): Promise<SimplePublicObjectWithAssociations>;
84
84
  getObjectWithPropsType(objectType: string, objectId: string, accessToken: string, types?: string[]): Promise<SimplePublicObject & {
85
85
  propertiesWithTypes: {
86
86
  [key: string]: {
@@ -94,6 +94,9 @@ export declare class HubspotService {
94
94
  [key: string]: string;
95
95
  }, accessToken: string, options?: Configuration): Promise<SimplePublicObject>;
96
96
  associateObjects(commandInput: AssociateObjectInput, accessToken: string): Promise<LabelsBetweenObjectPair>;
97
+ associateObjectsBatch(commandInput: BatchAssociateObjectInput, accessToken: string): Promise<Array<{
98
+ errors: string;
99
+ }>>;
97
100
  createObjectProperties(objectType: string, properties: PropertyCreate[], accessToken: string): Promise<any>;
98
101
  createPropertyGroup(objectType: string, group: PropertyGroupCreate, accessToken: string): Promise<PropertyGroup>;
99
102
  convertPropertyValues(properties: {
@@ -105,5 +108,6 @@ export declare class HubspotService {
105
108
  commandInput: CreateSubscriptionInput;
106
109
  devApiKey: string;
107
110
  }): Promise<CreateSubscriptionOutput>;
111
+ setAccessToken(accessToken: string): void;
108
112
  private fetchApi;
109
113
  }
@@ -7,6 +7,7 @@ const data_1 = require("../constants/data");
7
7
  const api_client_2 = require("@hubspot/api-client");
8
8
  const utils_1 = require("@dma-sdk/utils");
9
9
  const api_1 = require("../constants/api");
10
+ const system_error_1 = require("../exceptions/system-error");
10
11
  // Per ogni metodo che viene creato, è fondamentale che
11
12
  // il client venga inizializzato con il token di accesso o l'api key
12
13
  // altrimenti non funzionerà correttamente.
@@ -132,6 +133,32 @@ class HubspotService {
132
133
  const response = await this.client.crm.associations.v4.basicApi.create(objectTypeFirst, objectIdFirst, objectTypeSecond, objectIdSecond, options || []);
133
134
  return response;
134
135
  }
136
+ async associateObjectsBatch(commandInput, accessToken) {
137
+ const errors = [];
138
+ const chunkSize = 20;
139
+ for (let i = 0; i < commandInput.associations.length; i += chunkSize) {
140
+ const associationChunk = commandInput.associations.slice(i, i + chunkSize);
141
+ const options = {
142
+ method: 'POST',
143
+ headers: {
144
+ Authorization: `Bearer ${accessToken}`,
145
+ 'Content-Type': 'application/json'
146
+ },
147
+ body: JSON.stringify({
148
+ inputs: associationChunk
149
+ })
150
+ };
151
+ try {
152
+ await this.fetchApi(api_1.ApiMethods.associateObjectBatch(commandInput.objectTypeFirst, commandInput.objectTypeSecond), options);
153
+ }
154
+ catch (error) {
155
+ errors.push({
156
+ errors: error instanceof Error ? error.message : String(error)
157
+ });
158
+ }
159
+ }
160
+ return errors;
161
+ }
135
162
  async createObjectProperties(objectType, properties, accessToken) {
136
163
  const options = {
137
164
  method: 'POST',
@@ -181,6 +208,12 @@ class HubspotService {
181
208
  const response = await this.fetchApi(api_1.ApiMethods.subscriptions(appId), options, { hapikey: devApiKey });
182
209
  return await response.json();
183
210
  }
211
+ setAccessToken(accessToken) {
212
+ if (this.client === undefined) {
213
+ throw new system_error_1.SystemError('Hubspot client not initialized');
214
+ }
215
+ this.client.setAccessToken(accessToken);
216
+ }
184
217
  async fetchApi(endpoint, options, queryParams) {
185
218
  return await (0, utils_1.fetchApi)(HubspotService.HS_BASE_URL, endpoint, options, queryParams);
186
219
  }
@@ -1,6 +1,7 @@
1
1
  export const ApiMethods = {
2
2
  getUserInfoByToken: '/oauth/v1/access-tokens',
3
3
  refreshToken: '/oauth/v1/token',
4
- createObjectProperties: (objectType: string) => '/crm/v3/properties/{objectType}/batch/create'.replace('{objectType}', objectType),
5
- subscriptions: (appId: string) => '/webhooks/v3/{appId}/subscriptions'.replace('{appId}', appId),
6
- }
4
+ createObjectProperties: (objectType: string) => `/crm/v3/properties/${objectType}/batch/create`,
5
+ subscriptions: (appId: string) => `/webhooks/v3/${appId}/subscriptions`,
6
+ associateObjectBatch: (fromObjectType: string, toObjectType: string) => `/crm/v4/associations/${fromObjectType}/${toObjectType}/batch/create`
7
+ }
@@ -0,0 +1,6 @@
1
+ export class SystemError extends Error {
2
+ constructor(message: string) {
3
+ super(message);
4
+ this.name = "SystemError";
5
+ }
6
+ }
@@ -6,4 +6,21 @@ export interface AssociateObjectInput {
6
6
  objectIdFirst: string,
7
7
  objectIdSecond: string,
8
8
  options?: AssociationSpec[]
9
+ }
10
+
11
+ export interface BatchAssociateObjectInput {
12
+ objectTypeFirst: string,
13
+ objectTypeSecond: string,
14
+ associations: AssociationsObject[]
15
+ }
16
+
17
+
18
+ export interface AssociationsObject {
19
+ from: {
20
+ id: string
21
+ },
22
+ to: {
23
+ id: string
24
+ },
25
+ types: Array<AssociationSpec>
9
26
  }
@@ -16,8 +16,9 @@ import { fetchApi } from '@dma-sdk/utils'
16
16
  import { ApiMethods } from '../constants/api'
17
17
  import { CreateSubscriptionInput } from '../interfaces/input/subscriptions'
18
18
  import { CreateSubscriptionOutput } from '../interfaces/output/subscriptions'
19
- import { AssociationSpec, FilterGroup, PublicObjectSearchRequest } from '@hubspot/api-client/lib/codegen/crm/companies'
20
- import { AssociateObjectInput } from '../interfaces/input/associations'
19
+ import { PublicObjectSearchRequest, SimplePublicObjectWithAssociations } from '@hubspot/api-client/lib/codegen/crm/companies'
20
+ import { AssociateObjectInput, AssociationsObject, BatchAssociateObjectInput } from '../interfaces/input/associations'
21
+ import { SystemError } from '../exceptions/system-error'
21
22
 
22
23
  // Per ogni metodo che viene creato, è fondamentale che
23
24
  // il client venga inizializzato con il token di accesso o l'api key
@@ -167,7 +168,7 @@ export class HubspotService {
167
168
  props?: string[], // nome delle props da restituire
168
169
  propsWithHistory?: string[], // nome delle props da restituire
169
170
  accessToken: string
170
- }): Promise<SimplePublicObject> {
171
+ }): Promise<SimplePublicObjectWithAssociations> {
171
172
  const { objectType, objectId, associations, props, propsWithHistory, accessToken } = params
172
173
  this.client.setAccessToken(accessToken)
173
174
  return await this.client.crm.objects.basicApi.getById(
@@ -250,6 +251,42 @@ export class HubspotService {
250
251
  return response
251
252
  }
252
253
 
254
+ async associateObjectsBatch(
255
+ commandInput: BatchAssociateObjectInput,
256
+ accessToken: string
257
+ ): Promise<Array<{ errors: string }>> {
258
+ const errors: Array<{ errors: string }> = []
259
+ const chunkSize = 20
260
+
261
+ for (let i = 0; i < commandInput.associations.length; i += chunkSize) {
262
+ const associationChunk = commandInput.associations.slice(i, i + chunkSize)
263
+
264
+ const options = {
265
+ method: 'POST',
266
+ headers: {
267
+ Authorization: `Bearer ${accessToken}`,
268
+ 'Content-Type': 'application/json'
269
+ },
270
+ body: JSON.stringify({
271
+ inputs: associationChunk
272
+ })
273
+ };
274
+
275
+ try {
276
+ await this.fetchApi(
277
+ ApiMethods.associateObjectBatch(commandInput.objectTypeFirst, commandInput.objectTypeSecond),
278
+ options
279
+ )
280
+ } catch (error) {
281
+ errors.push({
282
+ errors: error instanceof Error ? error.message : String(error)
283
+ })
284
+ }
285
+ }
286
+
287
+ return errors
288
+ }
289
+
253
290
  async createObjectProperties(
254
291
  objectType: string,
255
292
  properties: PropertyCreate[],
@@ -322,6 +359,13 @@ export class HubspotService {
322
359
 
323
360
  return await response.json()
324
361
  }
362
+
363
+ setAccessToken(accessToken: string) {
364
+ if(this.client === undefined) {
365
+ throw new SystemError('Hubspot client not initialized')
366
+ }
367
+ this.client.setAccessToken(accessToken)
368
+ }
325
369
 
326
370
  private async fetchApi(endpoint: string, options: {}, queryParams?: {}): Promise<any> {
327
371
  return await fetchApi(HubspotService.HS_BASE_URL, endpoint, options, queryParams)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dma-sdk/hubspot",
3
- "version": "1.0.18",
3
+ "version": "1.0.20",
4
4
  "description": "DMA SDK – Hubspot service",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",