@or-sdk/contacts 3.5.7-beta.2204.0 → 3.5.7
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.
- package/CHANGELOG.md +9 -0
- package/README.md +41 -2
- package/dist/cjs/Contacts.js +5 -7
- package/dist/cjs/Contacts.js.map +1 -1
- package/dist/cjs/api/batchProcessApi.js +20 -0
- package/dist/cjs/api/batchProcessApi.js.map +1 -1
- package/dist/cjs/api/bulkContactsCreateApi.js +45 -136
- package/dist/cjs/api/bulkContactsCreateApi.js.map +1 -1
- package/dist/cjs/api/contactApi.js +19 -15
- package/dist/cjs/api/contactApi.js.map +1 -1
- package/dist/cjs/constants.js +1 -1
- package/dist/cjs/constants.js.map +1 -1
- package/dist/esm/Contacts.js +5 -7
- package/dist/esm/Contacts.js.map +1 -1
- package/dist/esm/api/batchProcessApi.js +16 -0
- package/dist/esm/api/batchProcessApi.js.map +1 -1
- package/dist/esm/api/bulkContactsCreateApi.js +21 -83
- package/dist/esm/api/bulkContactsCreateApi.js.map +1 -1
- package/dist/esm/api/contactApi.js +6 -4
- package/dist/esm/api/contactApi.js.map +1 -1
- package/dist/esm/constants.js +1 -1
- package/dist/esm/constants.js.map +1 -1
- package/dist/types/Contacts.d.ts.map +1 -1
- package/dist/types/api/batchProcessApi.d.ts +2 -0
- package/dist/types/api/batchProcessApi.d.ts.map +1 -1
- package/dist/types/api/bulkContactsCreateApi.d.ts +3 -10
- package/dist/types/api/bulkContactsCreateApi.d.ts.map +1 -1
- package/dist/types/api/contactApi.d.ts +3 -3
- package/dist/types/api/contactApi.d.ts.map +1 -1
- package/dist/types/constants.d.ts +1 -1
- package/dist/types/constants.d.ts.map +1 -1
- package/dist/types/types.d.ts +7 -0
- package/dist/types/types.d.ts.map +1 -1
- package/package.json +4 -3
- package/src/Contacts.ts +5 -6
- package/src/api/batchProcessApi.ts +15 -0
- package/src/api/bulkContactsCreateApi.ts +40 -103
- package/src/api/contactApi.ts +13 -5
- package/src/constants.ts +1 -1
- package/src/types.ts +14 -0
|
@@ -4,13 +4,8 @@ import BatchProcessApi from './batchProcessApi';
|
|
|
4
4
|
import { REQUEST_PAYLOAD_MAX_BYTES } from '../constants';
|
|
5
5
|
import { chunkArrByMaxSize, debouncePromise, getObjectSizeInBytes } from '../utils';
|
|
6
6
|
import { v4 } from 'uuid';
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
BulkCreateResultsDto,
|
|
10
|
-
BulkProgressDto,
|
|
11
|
-
CreateBulkResponseDto,
|
|
12
|
-
} from '@onereach/types-contacts-api';
|
|
13
|
-
import { BatchBulkCreateData, BulkCreateData } from '../types';
|
|
7
|
+
import { BulkContactDto } from '@onereach/types-contacts-api';
|
|
8
|
+
import { BatchBulkCreateData, BulkCreateData, BulkOptions, CreateBulkResults } from '../types';
|
|
14
9
|
|
|
15
10
|
export default class BulkContactsCreateApi extends BaseWithPoling {
|
|
16
11
|
constructor(
|
|
@@ -48,65 +43,37 @@ export default class BulkContactsCreateApi extends BaseWithPoling {
|
|
|
48
43
|
* In the case if contactKey is not defined, it it will be automatically set to the index (as string)
|
|
49
44
|
* of contact.
|
|
50
45
|
*/
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
): Promise<
|
|
46
|
+
async bulkCreateContacts(
|
|
47
|
+
data: BulkCreateData,
|
|
48
|
+
options: BulkOptions = {},
|
|
49
|
+
): Promise<CreateBulkResults> {
|
|
55
50
|
const dataWithContactKeys = this.updateContactsWithContactKeys(data);
|
|
56
|
-
|
|
57
|
-
await this.indicesOff();
|
|
58
|
-
|
|
51
|
+
const { batchSize = REQUEST_PAYLOAD_MAX_BYTES } = options;
|
|
52
|
+
await this.batchProcessApi.indicesOff();
|
|
53
|
+
let results: CreateBulkResults = {
|
|
59
54
|
created: {},
|
|
60
55
|
failed: {},
|
|
61
56
|
};
|
|
62
|
-
await this.setBulkProgress(bulkName);
|
|
63
57
|
try {
|
|
64
58
|
const { contacts, ...rest } = dataWithContactKeys;
|
|
65
|
-
const contactsMaxSize =
|
|
59
|
+
const contactsMaxSize = batchSize - getObjectSizeInBytes({ ...rest });
|
|
66
60
|
const contactsChunks = chunkArrByMaxSize(contacts, contactsMaxSize);
|
|
67
|
-
|
|
68
|
-
totalBatches: contactsChunks.length,
|
|
69
|
-
totalContacts: contacts.length,
|
|
70
|
-
completedBatches: 0,
|
|
71
|
-
completedContacts: 0,
|
|
72
|
-
});
|
|
61
|
+
const batchGroupId = v4();
|
|
73
62
|
|
|
74
63
|
if (contactsChunks.length === 1) {
|
|
75
|
-
await this.createContactsInSingleBatch(dataWithContactKeys as BatchBulkCreateData,
|
|
64
|
+
await this.createContactsInSingleBatch(dataWithContactKeys as BatchBulkCreateData, batchGroupId);
|
|
76
65
|
} else {
|
|
77
|
-
await this.createContactsInMultiBatches(contactsChunks as BulkContactDto[][], rest,
|
|
66
|
+
await this.createContactsInMultiBatches(contactsChunks as BulkContactDto[][], rest, batchGroupId);
|
|
78
67
|
}
|
|
68
|
+
|
|
69
|
+
results = await this.getBulkCreateResults(batchGroupId);
|
|
79
70
|
} finally {
|
|
80
|
-
|
|
81
|
-
this.indicesOn(indicesOnBatchId);
|
|
82
|
-
await debouncePromise(() => this.polling(indicesOnBatchId), 3000);
|
|
71
|
+
await this.batchProcessApi.indicesOn();
|
|
83
72
|
}
|
|
84
73
|
|
|
85
74
|
return results;
|
|
86
75
|
}
|
|
87
76
|
|
|
88
|
-
async getBulkProgress(bulkName: string): Promise<BulkProgressDto> {
|
|
89
|
-
return this.apiCall({
|
|
90
|
-
method: 'GET',
|
|
91
|
-
route: `${this.apiBasePath}/bulk-progress/${bulkName}`,
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
async setBulkProgress(bulkName: string): Promise<void> {
|
|
96
|
-
return this.apiCall({
|
|
97
|
-
method: 'POST',
|
|
98
|
-
route: `${this.apiBasePath}/bulk-progress/${bulkName}`,
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
async updateBulkProgress(bulkName: string, progress: BulkProgressDto): Promise<void> {
|
|
103
|
-
return this.apiCall({
|
|
104
|
-
method: 'PATCH',
|
|
105
|
-
route: `${this.apiBasePath}/bulk-progress/${bulkName}`,
|
|
106
|
-
data: progress,
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
|
|
110
77
|
private updateContactsWithContactKeys(data: BulkCreateData): BatchBulkCreateData {
|
|
111
78
|
const dataWithContactKeys = {
|
|
112
79
|
...data,
|
|
@@ -126,20 +93,6 @@ export default class BulkContactsCreateApi extends BaseWithPoling {
|
|
|
126
93
|
return dataWithContactKeys;
|
|
127
94
|
}
|
|
128
95
|
|
|
129
|
-
private async indicesOn(batchId: string): Promise<void> {
|
|
130
|
-
return this.apiCall({
|
|
131
|
-
method: 'POST',
|
|
132
|
-
route: `${this.apiBasePath}/indices-on/${batchId}`,
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
private async indicesOff(): Promise<void> {
|
|
137
|
-
return this.apiCall({
|
|
138
|
-
method: 'POST',
|
|
139
|
-
route: `${this.apiBasePath}/indices-off`,
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
|
|
143
96
|
private checkContactKeysDuplications(contacts: BulkContactDto[]): void {
|
|
144
97
|
contacts.map(({ contactKey }) => contactKey).forEach((key, index, array) => {
|
|
145
98
|
if (index !== array.lastIndexOf(key)) {
|
|
@@ -148,32 +101,30 @@ export default class BulkContactsCreateApi extends BaseWithPoling {
|
|
|
148
101
|
});
|
|
149
102
|
}
|
|
150
103
|
|
|
151
|
-
|
|
152
|
-
const
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
method: 'GET',
|
|
176
|
-
route: `${this.apiBasePath}/bulk-results/${batchGroupId}/${skip}`,
|
|
104
|
+
private async getBulkCreateResults(batchGroupId: string): Promise<CreateBulkResults> {
|
|
105
|
+
const batchProcessesGroup = await this.batchProcessApi.getBatchProcessesGroup(batchGroupId);
|
|
106
|
+
return batchProcessesGroup.reduce<CreateBulkResults>((acc, batch) => {
|
|
107
|
+
const createdContacts = batch.results.reduce<Record<string, string>>((acc, result) => ({
|
|
108
|
+
...acc,
|
|
109
|
+
...JSON.parse(result),
|
|
110
|
+
}), {});
|
|
111
|
+
const failedContacts = batch.messages.reduce<Record<string, string>>((acc, message) => ({
|
|
112
|
+
...acc,
|
|
113
|
+
...JSON.parse(message),
|
|
114
|
+
}), {});
|
|
115
|
+
return {
|
|
116
|
+
created: {
|
|
117
|
+
...acc.created,
|
|
118
|
+
...createdContacts,
|
|
119
|
+
},
|
|
120
|
+
failed: {
|
|
121
|
+
...acc.failed,
|
|
122
|
+
...failedContacts,
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
}, {
|
|
126
|
+
created: {},
|
|
127
|
+
failed: {},
|
|
177
128
|
});
|
|
178
129
|
}
|
|
179
130
|
|
|
@@ -193,20 +144,6 @@ export default class BulkContactsCreateApi extends BaseWithPoling {
|
|
|
193
144
|
});
|
|
194
145
|
// giving time to 2 lambdas to wake up (1 - create Batch and 2 - ping batch status)
|
|
195
146
|
await debouncePromise(() => this.polling(batchId), 3000);
|
|
196
|
-
|
|
197
|
-
const {
|
|
198
|
-
totalBatches,
|
|
199
|
-
totalContacts,
|
|
200
|
-
completedBatches,
|
|
201
|
-
completedContacts,
|
|
202
|
-
} = await this.getBulkProgress(batchGroupId);
|
|
203
|
-
|
|
204
|
-
await this.updateBulkProgress(batchGroupId, {
|
|
205
|
-
totalBatches,
|
|
206
|
-
totalContacts,
|
|
207
|
-
completedBatches: completedBatches + 1,
|
|
208
|
-
completedContacts: completedContacts + data.contacts.length,
|
|
209
|
-
});
|
|
210
147
|
}
|
|
211
148
|
|
|
212
149
|
private async createContactsInMultiBatches(
|
package/src/api/contactApi.ts
CHANGED
|
@@ -20,7 +20,7 @@ import {
|
|
|
20
20
|
import { CalApiParams, List } from '@or-sdk/base';
|
|
21
21
|
import BatchProcessApi from './batchProcessApi';
|
|
22
22
|
import { getObjectSizeInBytes, chunkArrByMaxSize, debouncePromise } from '../utils';
|
|
23
|
-
import { InitCreateBatchResponse, CreateContactsBatchResults } from '../types';
|
|
23
|
+
import { InitCreateBatchResponse, CreateContactsBatchResults, BulkOptions } from '../types';
|
|
24
24
|
import ContactBookApi from './contactBookApi';
|
|
25
25
|
import { REQUEST_PAYLOAD_MAX_BYTES, CONTACTS_DELETE_MAX_AMOUNT } from '../constants';
|
|
26
26
|
import { ApiError, CreateContactsBatchError } from '../apiError';
|
|
@@ -173,9 +173,13 @@ export default class ContactApi extends BaseWithPoling {
|
|
|
173
173
|
*
|
|
174
174
|
* @see constructor of packages/contacts/src/Contacts
|
|
175
175
|
*/
|
|
176
|
-
async bulkCreateContacts(
|
|
176
|
+
async bulkCreateContacts(
|
|
177
|
+
data: CreateMultipleContactsDto,
|
|
178
|
+
options: BulkOptions = {}
|
|
179
|
+
): Promise<void> {
|
|
180
|
+
const { batchSize = REQUEST_PAYLOAD_MAX_BYTES } = options;
|
|
177
181
|
const { contacts, ...rest } = data;
|
|
178
|
-
const contactsMaxSize =
|
|
182
|
+
const contactsMaxSize = batchSize - getObjectSizeInBytes({ ...rest });
|
|
179
183
|
const contactsChunks = chunkArrByMaxSize(contacts, contactsMaxSize);
|
|
180
184
|
|
|
181
185
|
contactsChunks.length === 1
|
|
@@ -190,9 +194,13 @@ export default class ContactApi extends BaseWithPoling {
|
|
|
190
194
|
*
|
|
191
195
|
* @see batchProcessApi.trackBatchProcess
|
|
192
196
|
*/
|
|
193
|
-
async initCreateBatch(
|
|
197
|
+
async initCreateBatch(
|
|
198
|
+
data: CreateMultipleContactsDto,
|
|
199
|
+
options: BulkOptions = {}
|
|
200
|
+
): Promise<InitCreateBatchResponse> {
|
|
201
|
+
const { batchSize = REQUEST_PAYLOAD_MAX_BYTES } = options;
|
|
194
202
|
const { contacts, ...rest } = data;
|
|
195
|
-
const contactsMaxSize =
|
|
203
|
+
const contactsMaxSize = batchSize - getObjectSizeInBytes({ ...rest });
|
|
196
204
|
const contactsChunks = chunkArrByMaxSize(contacts, contactsMaxSize);
|
|
197
205
|
|
|
198
206
|
const batchProcess = await this.apiCall<BatchProcessResponseDto>({
|
package/src/constants.ts
CHANGED
package/src/types.ts
CHANGED
|
@@ -81,3 +81,17 @@ export type BulkCreateData = Omit<BulkCreateRequestDto, 'batchId' | 'batchGroupI
|
|
|
81
81
|
& { contacts: OptionalBy<BulkContactDto, 'contactKey'>[]; };
|
|
82
82
|
|
|
83
83
|
export type BatchBulkCreateData = Omit<BulkCreateRequestDto, 'batchId' | 'batchGroupId'>;
|
|
84
|
+
|
|
85
|
+
export type CreateBulkResults = {created: Record<string, string>; failed: Record<string, string>;};
|
|
86
|
+
|
|
87
|
+
export type BulkOptions = {
|
|
88
|
+
/**
|
|
89
|
+
* Sets the limit in bytes for the JSON body size to be processed by a single bulk batch.
|
|
90
|
+
* Can be used for decreasing DB server load.
|
|
91
|
+
* Default is 4000000 bytes, or approximately 4mb.
|
|
92
|
+
*
|
|
93
|
+
* @see REQUEST_PAYLOAD_MAX_BYTES in packages/contacts/src/constants.ts
|
|
94
|
+
*/
|
|
95
|
+
batchSize?: number;
|
|
96
|
+
};
|
|
97
|
+
|