n8n-nodes-commercetools 0.1.7 → 0.1.9

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.
@@ -0,0 +1,933 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.executeCustomerOperation = executeCustomerOperation;
4
+ const n8n_workflow_1 = require("n8n-workflow");
5
+ const customer_utils_1 = require("../utils/customer.utils");
6
+ const DEFAULT_LIMIT = 20;
7
+ const MAX_LIMIT_RETURN_ALL = 500;
8
+ const MIN_VERSION = 1;
9
+ function getErrorStatusCode(error) {
10
+ var _a, _b, _c, _d;
11
+ return ((_c = (_a = error.statusCode) !== null && _a !== void 0 ? _a : (_b = error.cause) === null || _b === void 0 ? void 0 : _b.statusCode) !== null && _c !== void 0 ? _c : (_d = error.response) === null || _d === void 0 ? void 0 : _d.statusCode);
12
+ }
13
+ async function getActionsForUpdate(executeFunctions, itemIndex) {
14
+ const actionsUi = executeFunctions.getNodeParameter('customerActionsUi', itemIndex, {});
15
+ const actions = (0, customer_utils_1.buildActionsFromUi)(executeFunctions, actionsUi, itemIndex);
16
+ if (actions.length === 0) {
17
+ throw new n8n_workflow_1.NodeOperationError(executeFunctions.getNode(), 'At least one action must be provided', { itemIndex });
18
+ }
19
+ return actions;
20
+ }
21
+ async function handleHeadOperation(executeFunctions, url) {
22
+ try {
23
+ await executeFunctions.helpers.httpRequestWithAuthentication.call(executeFunctions, 'commerceToolsOAuth2Api', { method: 'HEAD', url });
24
+ return { exists: true };
25
+ }
26
+ catch (error) {
27
+ const errorData = error;
28
+ const statusCode = getErrorStatusCode(errorData);
29
+ if (statusCode === 404 || errorData.httpCode === '404') {
30
+ return { exists: false };
31
+ }
32
+ throw error;
33
+ }
34
+ }
35
+ async function executeCustomerOperation({ operation, itemIndex, baseUrl }) {
36
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
37
+ const results = [];
38
+ if (operation === 'get') {
39
+ const customerId = this.getNodeParameter('customerId', itemIndex);
40
+ const additionalFields = this.getNodeParameter('additionalFieldsGet', itemIndex, {});
41
+ const qs = {};
42
+ (0, customer_utils_1.applyCommonCustomerParameters)(qs, additionalFields);
43
+ const response = (await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
44
+ method: 'GET',
45
+ url: `${baseUrl}/customers/${customerId}`,
46
+ qs,
47
+ }));
48
+ results.push({ json: response });
49
+ return results;
50
+ }
51
+ if (operation === 'getByKey') {
52
+ const customerKey = this.getNodeParameter('customerKey', itemIndex);
53
+ const additionalFields = this.getNodeParameter('additionalFieldsGet', itemIndex, {});
54
+ const qs = {};
55
+ (0, customer_utils_1.applyCommonCustomerParameters)(qs, additionalFields);
56
+ const response = (await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
57
+ method: 'GET',
58
+ url: `${baseUrl}/customers/key=${encodeURIComponent(customerKey)}`,
59
+ qs,
60
+ }));
61
+ results.push({ json: response });
62
+ return results;
63
+ }
64
+ if (operation === 'getInStore') {
65
+ const storeKey = this.getNodeParameter('storeKey', itemIndex);
66
+ const customerId = this.getNodeParameter('customerId', itemIndex);
67
+ const additionalFields = this.getNodeParameter('additionalFieldsGet', itemIndex, {});
68
+ const qs = {};
69
+ (0, customer_utils_1.applyCommonCustomerParameters)(qs, additionalFields);
70
+ const response = (await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
71
+ method: 'GET',
72
+ url: `${baseUrl}/in-store/key=${encodeURIComponent(storeKey)}/customers/${customerId}`,
73
+ qs,
74
+ }));
75
+ results.push({ json: response });
76
+ return results;
77
+ }
78
+ if (operation === 'getInStoreByKey') {
79
+ const storeKey = this.getNodeParameter('storeKey', itemIndex);
80
+ const customerKey = this.getNodeParameter('customerKey', itemIndex);
81
+ const additionalFields = this.getNodeParameter('additionalFieldsGet', itemIndex, {});
82
+ const qs = {};
83
+ (0, customer_utils_1.applyCommonCustomerParameters)(qs, additionalFields);
84
+ const response = (await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
85
+ method: 'GET',
86
+ url: `${baseUrl}/in-store/key=${encodeURIComponent(storeKey)}/customers/key=${encodeURIComponent(customerKey)}`,
87
+ qs,
88
+ }));
89
+ results.push({ json: response });
90
+ return results;
91
+ }
92
+ if (operation === 'query') {
93
+ const returnAll = this.getNodeParameter('returnAll', itemIndex, false);
94
+ const limit = returnAll ? MAX_LIMIT_RETURN_ALL : this.getNodeParameter('limit', itemIndex, DEFAULT_LIMIT);
95
+ const offset = this.getNodeParameter('offset', itemIndex, 0);
96
+ const additionalFields = this.getNodeParameter('additionalFields', itemIndex, {});
97
+ const qs = { limit };
98
+ if (offset) {
99
+ qs.offset = offset;
100
+ }
101
+ (0, customer_utils_1.applyCommonCustomerParameters)(qs, additionalFields, {
102
+ allowSort: true,
103
+ allowWhere: true,
104
+ allowPredicateVariables: true,
105
+ });
106
+ if (Object.prototype.hasOwnProperty.call(additionalFields, 'withTotal')) {
107
+ qs.withTotal = additionalFields.withTotal;
108
+ }
109
+ if (returnAll && qs.withTotal === undefined) {
110
+ qs.withTotal = true;
111
+ }
112
+ const collected = [];
113
+ let requestOffset = offset;
114
+ let hasMore;
115
+ do {
116
+ const response = await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
117
+ method: 'GET',
118
+ url: `${baseUrl}/customers`,
119
+ qs: {
120
+ ...qs,
121
+ offset: requestOffset,
122
+ },
123
+ });
124
+ const resultsPage = ((_a = response.results) !== null && _a !== void 0 ? _a : response);
125
+ if (!Array.isArray(resultsPage)) {
126
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Unexpected response format from Commercetools API', {
127
+ itemIndex,
128
+ });
129
+ }
130
+ collected.push(...resultsPage);
131
+ if (!returnAll) {
132
+ hasMore = false;
133
+ }
134
+ else {
135
+ const received = resultsPage.length;
136
+ if (received === 0) {
137
+ hasMore = false;
138
+ }
139
+ else {
140
+ requestOffset += received;
141
+ const total = response.total;
142
+ hasMore = total !== undefined ? requestOffset < total : received === limit;
143
+ }
144
+ }
145
+ } while (returnAll && hasMore);
146
+ results.push(...this.helpers.returnJsonArray(collected));
147
+ return results;
148
+ }
149
+ if (operation === 'queryInStore') {
150
+ const storeKey = this.getNodeParameter('storeKey', itemIndex);
151
+ if (!(storeKey === null || storeKey === void 0 ? void 0 : storeKey.trim())) {
152
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Store Key cannot be empty', { itemIndex });
153
+ }
154
+ const returnAll = this.getNodeParameter('returnAll', itemIndex, false);
155
+ const limit = returnAll ? MAX_LIMIT_RETURN_ALL : this.getNodeParameter('limit', itemIndex, DEFAULT_LIMIT);
156
+ const offset = this.getNodeParameter('offset', itemIndex, 0);
157
+ const additionalFields = this.getNodeParameter('additionalFields', itemIndex, {});
158
+ const qs = { limit };
159
+ if (offset) {
160
+ qs.offset = offset;
161
+ }
162
+ (0, customer_utils_1.applyCommonCustomerParameters)(qs, additionalFields, {
163
+ allowSort: true,
164
+ allowWhere: true,
165
+ allowPredicateVariables: true,
166
+ });
167
+ if (Object.prototype.hasOwnProperty.call(additionalFields, 'withTotal')) {
168
+ qs.withTotal = additionalFields.withTotal;
169
+ }
170
+ if (returnAll && qs.withTotal === undefined) {
171
+ qs.withTotal = true;
172
+ }
173
+ const collected = [];
174
+ let requestOffset = offset;
175
+ let hasMore;
176
+ do {
177
+ const response = await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
178
+ method: 'GET',
179
+ url: `${baseUrl}/in-store/key=${encodeURIComponent(storeKey)}/customers`,
180
+ qs: {
181
+ ...qs,
182
+ offset: requestOffset,
183
+ },
184
+ });
185
+ const resultsPage = ((_b = response.results) !== null && _b !== void 0 ? _b : response);
186
+ if (!Array.isArray(resultsPage)) {
187
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Unexpected response format from Commercetools API', {
188
+ itemIndex,
189
+ });
190
+ }
191
+ collected.push(...resultsPage);
192
+ if (!returnAll) {
193
+ hasMore = false;
194
+ }
195
+ else {
196
+ const received = resultsPage.length;
197
+ if (received === 0) {
198
+ hasMore = false;
199
+ }
200
+ else {
201
+ requestOffset += received;
202
+ const total = response.total;
203
+ hasMore = total !== undefined ? requestOffset < total : received === limit;
204
+ }
205
+ }
206
+ } while (returnAll && hasMore);
207
+ results.push(...this.helpers.returnJsonArray(collected));
208
+ return results;
209
+ }
210
+ if (operation === 'head') {
211
+ const customerId = this.getNodeParameter('customerId', itemIndex);
212
+ if (!(customerId === null || customerId === void 0 ? void 0 : customerId.trim())) {
213
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Customer ID cannot be empty', { itemIndex });
214
+ }
215
+ const result = await handleHeadOperation(this, `${baseUrl}/customers/${customerId}`);
216
+ results.push({ json: result });
217
+ return results;
218
+ }
219
+ if (operation === 'headByKey') {
220
+ const customerKey = this.getNodeParameter('customerKey', itemIndex);
221
+ if (!(customerKey === null || customerKey === void 0 ? void 0 : customerKey.trim())) {
222
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Customer Key cannot be empty', { itemIndex });
223
+ }
224
+ const result = await handleHeadOperation(this, `${baseUrl}/customers/key=${encodeURIComponent(customerKey)}`);
225
+ results.push({ json: result });
226
+ return results;
227
+ }
228
+ if (operation === 'headByQuery') {
229
+ const additionalFields = this.getNodeParameter('additionalFields', itemIndex, {});
230
+ const qs = {};
231
+ (0, customer_utils_1.applyCommonCustomerParameters)(qs, additionalFields, {
232
+ allowWhere: true,
233
+ allowPredicateVariables: true,
234
+ allowSort: true,
235
+ });
236
+ try {
237
+ await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
238
+ method: 'HEAD',
239
+ url: `${baseUrl}/customers`,
240
+ qs,
241
+ });
242
+ results.push({ json: { exists: true } });
243
+ return results;
244
+ }
245
+ catch (error) {
246
+ const errorData = error;
247
+ const statusCode = getErrorStatusCode(errorData);
248
+ if (statusCode === 404 || errorData.httpCode === '404') {
249
+ results.push({ json: { exists: false } });
250
+ return results;
251
+ }
252
+ throw error;
253
+ }
254
+ }
255
+ if (operation === 'headInStore') {
256
+ const storeKey = this.getNodeParameter('storeKey', itemIndex);
257
+ const customerId = this.getNodeParameter('customerId', itemIndex);
258
+ try {
259
+ await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
260
+ method: 'HEAD',
261
+ url: `${baseUrl}/in-store/key=${encodeURIComponent(storeKey)}/customers/${customerId}`,
262
+ });
263
+ results.push({ json: { exists: true } });
264
+ return results;
265
+ }
266
+ catch (error) {
267
+ const errorData = error;
268
+ const statusCode = (_e = (_c = errorData.statusCode) !== null && _c !== void 0 ? _c : (_d = errorData.cause) === null || _d === void 0 ? void 0 : _d.statusCode) !== null && _e !== void 0 ? _e : (_f = errorData.response) === null || _f === void 0 ? void 0 : _f.statusCode;
269
+ if (statusCode === 404 || errorData.httpCode === '404') {
270
+ results.push({ json: { exists: false } });
271
+ return results;
272
+ }
273
+ throw error;
274
+ }
275
+ }
276
+ if (operation === 'headInStoreByKey') {
277
+ const storeKey = this.getNodeParameter('storeKey', itemIndex);
278
+ const customerKey = this.getNodeParameter('customerKey', itemIndex);
279
+ try {
280
+ await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
281
+ method: 'HEAD',
282
+ url: `${baseUrl}/in-store/key=${encodeURIComponent(storeKey)}/customers/key=${encodeURIComponent(customerKey)}`,
283
+ });
284
+ results.push({ json: { exists: true } });
285
+ return results;
286
+ }
287
+ catch (error) {
288
+ const errorData = error;
289
+ const statusCode = (_j = (_g = errorData.statusCode) !== null && _g !== void 0 ? _g : (_h = errorData.cause) === null || _h === void 0 ? void 0 : _h.statusCode) !== null && _j !== void 0 ? _j : (_k = errorData.response) === null || _k === void 0 ? void 0 : _k.statusCode;
290
+ if (statusCode === 404 || errorData.httpCode === '404') {
291
+ results.push({ json: { exists: false } });
292
+ return results;
293
+ }
294
+ throw error;
295
+ }
296
+ }
297
+ if (operation === 'headInStoreByQuery') {
298
+ const storeKey = this.getNodeParameter('storeKey', itemIndex);
299
+ const additionalFields = this.getNodeParameter('additionalFields', itemIndex, {});
300
+ const qs = {};
301
+ (0, customer_utils_1.applyCommonCustomerParameters)(qs, additionalFields, {
302
+ allowWhere: true,
303
+ allowPredicateVariables: true,
304
+ allowSort: true,
305
+ });
306
+ try {
307
+ await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
308
+ method: 'HEAD',
309
+ url: `${baseUrl}/in-store/key=${encodeURIComponent(storeKey)}/customers`,
310
+ qs,
311
+ });
312
+ results.push({ json: { exists: true } });
313
+ return results;
314
+ }
315
+ catch (error) {
316
+ const errorData = error;
317
+ const statusCode = (_o = (_l = errorData.statusCode) !== null && _l !== void 0 ? _l : (_m = errorData.cause) === null || _m === void 0 ? void 0 : _m.statusCode) !== null && _o !== void 0 ? _o : (_p = errorData.response) === null || _p === void 0 ? void 0 : _p.statusCode;
318
+ if (statusCode === 404 || errorData.httpCode === '404') {
319
+ results.push({ json: { exists: false } });
320
+ return results;
321
+ }
322
+ throw error;
323
+ }
324
+ }
325
+ if (operation === 'create') {
326
+ const additionalFields = this.getNodeParameter('additionalFieldsCreate', itemIndex, {});
327
+ const qs = {};
328
+ (0, customer_utils_1.applyCommonCustomerParameters)(qs, additionalFields);
329
+ const draftRaw = this.getNodeParameter('customerDraft', itemIndex);
330
+ const draft = (0, customer_utils_1.coerceJsonInput)(this, draftRaw, 'Customer draft', itemIndex);
331
+ if (!draft.email || typeof draft.email !== 'string' || !draft.email.trim()) {
332
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Email is required for customer creation. Please provide a valid email address in the customer draft.', { itemIndex });
333
+ }
334
+ try {
335
+ const response = (await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
336
+ method: 'POST',
337
+ url: `${baseUrl}/customers`,
338
+ body: draft,
339
+ qs,
340
+ }));
341
+ results.push({ json: response });
342
+ return results;
343
+ }
344
+ catch (error) {
345
+ const statusCode = getErrorStatusCode(error);
346
+ const errorMessage = error.message || '';
347
+ if (statusCode === 400) {
348
+ if (errorMessage.includes('email') && errorMessage.includes('exists')) {
349
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), `A customer with this email address already exists. Please use a different email address or use the authenticate operation to sign in existing customers.`, { itemIndex });
350
+ }
351
+ }
352
+ if (statusCode === 409 && errorMessage.includes('LockedField')) {
353
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Another customer with the same email is being created simultaneously. Please wait a moment and try again.`, { itemIndex });
354
+ }
355
+ throw error;
356
+ }
357
+ }
358
+ if (operation === 'createInStore') {
359
+ const storeKey = this.getNodeParameter('storeKey', itemIndex);
360
+ const additionalFields = this.getNodeParameter('additionalFieldsCreate', itemIndex, {});
361
+ if (!(storeKey === null || storeKey === void 0 ? void 0 : storeKey.trim())) {
362
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Store Key cannot be empty', { itemIndex });
363
+ }
364
+ const qs = {};
365
+ (0, customer_utils_1.applyCommonCustomerParameters)(qs, additionalFields);
366
+ const draftRaw = this.getNodeParameter('customerDraft', itemIndex);
367
+ const draft = (0, customer_utils_1.coerceJsonInput)(this, draftRaw, 'Customer draft', itemIndex);
368
+ if (!draft.email || typeof draft.email !== 'string' || !draft.email.trim()) {
369
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Email is required for customer creation. Please provide a valid email address in the customer draft.', { itemIndex });
370
+ }
371
+ let response;
372
+ try {
373
+ response = (await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
374
+ method: 'POST',
375
+ url: `${baseUrl}/in-store/key=${encodeURIComponent(storeKey)}/customers`,
376
+ body: draft,
377
+ qs,
378
+ }));
379
+ }
380
+ catch (error) {
381
+ const statusCode = getErrorStatusCode(error);
382
+ const errorMessage = error.message || '';
383
+ if (statusCode === 400) {
384
+ if (errorMessage.includes("referenced object of type 'store'")) {
385
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Store with key '${storeKey}' was not found. Please check that the store exists in your Commercetools project or use a different store key.`, { itemIndex });
386
+ }
387
+ if (errorMessage.includes('email') && errorMessage.includes('exists')) {
388
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), `A customer with this email address already exists. Please use a different email address or use the authenticate operation to sign in existing customers.`, { itemIndex });
389
+ }
390
+ if (errorMessage.includes('cart') && errorMessage.includes('store')) {
391
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), `The cart's store field must reference the same store (${storeKey}) specified in the path parameter.`, { itemIndex });
392
+ }
393
+ if (errorMessage.includes('email') && errorMessage.includes('required')) {
394
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Email is required for customer creation. Please provide a valid email address in the customer draft.`, { itemIndex });
395
+ }
396
+ }
397
+ if (statusCode === 409 && errorMessage.includes('LockedField')) {
398
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Another customer with the same email is being created simultaneously. Please wait a moment and try again.`, { itemIndex });
399
+ }
400
+ throw error;
401
+ }
402
+ results.push({ json: response });
403
+ return results;
404
+ }
405
+ if (operation === 'update') {
406
+ const customerId = this.getNodeParameter('customerId', itemIndex);
407
+ const version = this.getNodeParameter('version', itemIndex);
408
+ const additionalFields = this.getNodeParameter('additionalFieldsUpdate', itemIndex, {});
409
+ if (!(customerId === null || customerId === void 0 ? void 0 : customerId.trim())) {
410
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Customer ID cannot be empty', { itemIndex });
411
+ }
412
+ if (version < MIN_VERSION) {
413
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Version must be ${MIN_VERSION} or greater`, { itemIndex });
414
+ }
415
+ const qs = {};
416
+ (0, customer_utils_1.applyCommonCustomerParameters)(qs, additionalFields);
417
+ const actions = await getActionsForUpdate(this, itemIndex);
418
+ const body = {
419
+ version,
420
+ actions,
421
+ };
422
+ const response = (await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
423
+ method: 'POST',
424
+ url: `${baseUrl}/customers/${customerId}`,
425
+ body,
426
+ qs,
427
+ }));
428
+ results.push({ json: response });
429
+ return results;
430
+ }
431
+ if (operation === 'updateByKey') {
432
+ const customerKey = this.getNodeParameter('customerKey', itemIndex);
433
+ const version = this.getNodeParameter('version', itemIndex);
434
+ const additionalFields = this.getNodeParameter('additionalFieldsUpdate', itemIndex, {});
435
+ if (!(customerKey === null || customerKey === void 0 ? void 0 : customerKey.trim())) {
436
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Customer Key cannot be empty', { itemIndex });
437
+ }
438
+ if (version < MIN_VERSION) {
439
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Version must be ${MIN_VERSION} or greater`, { itemIndex });
440
+ }
441
+ const qs = {};
442
+ (0, customer_utils_1.applyCommonCustomerParameters)(qs, additionalFields);
443
+ const actions = await getActionsForUpdate(this, itemIndex);
444
+ const body = {
445
+ version,
446
+ actions,
447
+ };
448
+ const response = (await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
449
+ method: 'POST',
450
+ url: `${baseUrl}/customers/key=${encodeURIComponent(customerKey)}`,
451
+ body,
452
+ qs,
453
+ }));
454
+ results.push({ json: response });
455
+ return results;
456
+ }
457
+ if (operation === 'updateInStore') {
458
+ const storeKey = this.getNodeParameter('storeKey', itemIndex);
459
+ const customerId = this.getNodeParameter('customerId', itemIndex);
460
+ const version = this.getNodeParameter('version', itemIndex);
461
+ const additionalFields = this.getNodeParameter('additionalFieldsUpdate', itemIndex, {});
462
+ if (!(storeKey === null || storeKey === void 0 ? void 0 : storeKey.trim())) {
463
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Store Key cannot be empty', { itemIndex });
464
+ }
465
+ if (!(customerId === null || customerId === void 0 ? void 0 : customerId.trim())) {
466
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Customer ID cannot be empty', { itemIndex });
467
+ }
468
+ if (version < MIN_VERSION) {
469
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Version must be ${MIN_VERSION} or greater`, { itemIndex });
470
+ }
471
+ const qs = {};
472
+ (0, customer_utils_1.applyCommonCustomerParameters)(qs, additionalFields);
473
+ const actions = await getActionsForUpdate(this, itemIndex);
474
+ const body = {
475
+ version,
476
+ actions,
477
+ };
478
+ const response = (await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
479
+ method: 'POST',
480
+ url: `${baseUrl}/in-store/key=${encodeURIComponent(storeKey)}/customers/${customerId}`,
481
+ body,
482
+ qs,
483
+ }));
484
+ results.push({ json: response });
485
+ return results;
486
+ }
487
+ if (operation === 'updateInStoreByKey') {
488
+ const storeKey = this.getNodeParameter('storeKey', itemIndex);
489
+ const customerKey = this.getNodeParameter('customerKey', itemIndex);
490
+ const version = this.getNodeParameter('version', itemIndex);
491
+ const additionalFields = this.getNodeParameter('additionalFieldsUpdate', itemIndex, {});
492
+ if (!(storeKey === null || storeKey === void 0 ? void 0 : storeKey.trim())) {
493
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Store Key cannot be empty', { itemIndex });
494
+ }
495
+ if (!(customerKey === null || customerKey === void 0 ? void 0 : customerKey.trim())) {
496
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Customer Key cannot be empty', { itemIndex });
497
+ }
498
+ if (version < MIN_VERSION) {
499
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Version must be ${MIN_VERSION} or greater`, { itemIndex });
500
+ }
501
+ const qs = {};
502
+ (0, customer_utils_1.applyCommonCustomerParameters)(qs, additionalFields);
503
+ const actions = await getActionsForUpdate(this, itemIndex);
504
+ const body = {
505
+ version,
506
+ actions,
507
+ };
508
+ const response = (await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
509
+ method: 'POST',
510
+ url: `${baseUrl}/in-store/key=${encodeURIComponent(storeKey)}/customers/key=${encodeURIComponent(customerKey)}`,
511
+ body,
512
+ qs,
513
+ }));
514
+ results.push({ json: response });
515
+ return results;
516
+ }
517
+ if (operation === 'changePassword') {
518
+ const customerId = this.getNodeParameter('customerId', itemIndex);
519
+ const version = this.getNodeParameter('version', itemIndex);
520
+ const currentPassword = this.getNodeParameter('currentPassword', itemIndex);
521
+ const newPassword = this.getNodeParameter('newPassword', itemIndex);
522
+ if (!(customerId === null || customerId === void 0 ? void 0 : customerId.trim())) {
523
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Customer ID cannot be empty', { itemIndex });
524
+ }
525
+ if (version < MIN_VERSION) {
526
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Version must be ${MIN_VERSION} or greater`, { itemIndex });
527
+ }
528
+ if (!(currentPassword === null || currentPassword === void 0 ? void 0 : currentPassword.trim())) {
529
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Current Password cannot be empty', { itemIndex });
530
+ }
531
+ if (!(newPassword === null || newPassword === void 0 ? void 0 : newPassword.trim())) {
532
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'New Password cannot be empty', { itemIndex });
533
+ }
534
+ const additionalFields = this.getNodeParameter('additionalFields', itemIndex, {});
535
+ const qs = {};
536
+ (0, customer_utils_1.applyCommonCustomerParameters)(qs, additionalFields);
537
+ const body = {
538
+ id: customerId,
539
+ version,
540
+ currentPassword,
541
+ newPassword,
542
+ };
543
+ try {
544
+ const response = (await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
545
+ method: 'POST',
546
+ url: `${baseUrl}/customers/password`,
547
+ body,
548
+ qs,
549
+ }));
550
+ results.push({ json: response });
551
+ return results;
552
+ }
553
+ catch (error) {
554
+ const statusCode = getErrorStatusCode(error);
555
+ const errorMessage = error.message || '';
556
+ if (statusCode === 404) {
557
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Customer with ID '${customerId}' was not found. Please verify that the customer exists in your CommerceTools project and that you're using the correct Customer ID.`, { itemIndex });
558
+ }
559
+ if (statusCode === 400) {
560
+ if (errorMessage.includes('password') && errorMessage.includes('invalid')) {
561
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), `The current password provided is incorrect. Please verify the current password and try again.`, { itemIndex });
562
+ }
563
+ if (errorMessage.includes('version')) {
564
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), `The customer version is outdated (provided: ${version}). Please get the latest customer data first to get the current version number.`, { itemIndex });
565
+ }
566
+ }
567
+ throw error;
568
+ }
569
+ }
570
+ if (operation === 'changePasswordInStore') {
571
+ const storeKey = this.getNodeParameter('storeKey', itemIndex);
572
+ const customerId = this.getNodeParameter('customerId', itemIndex);
573
+ const version = this.getNodeParameter('version', itemIndex);
574
+ const currentPassword = this.getNodeParameter('currentPassword', itemIndex);
575
+ const newPassword = this.getNodeParameter('newPassword', itemIndex);
576
+ const additionalFields = this.getNodeParameter('additionalFields', itemIndex, {});
577
+ const qs = {};
578
+ (0, customer_utils_1.applyCommonCustomerParameters)(qs, additionalFields);
579
+ const body = {
580
+ id: customerId,
581
+ version,
582
+ currentPassword,
583
+ newPassword,
584
+ };
585
+ const response = (await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
586
+ method: 'POST',
587
+ url: `${baseUrl}/in-store/key=${encodeURIComponent(storeKey)}/customers/password`,
588
+ body,
589
+ qs,
590
+ }));
591
+ results.push({ json: response });
592
+ return results;
593
+ }
594
+ if (operation === 'authenticate') {
595
+ const email = this.getNodeParameter('email', itemIndex);
596
+ const password = this.getNodeParameter('password', itemIndex);
597
+ const additionalFields = this.getNodeParameter('additionalFields', itemIndex, {});
598
+ const qs = {};
599
+ (0, customer_utils_1.applyCommonCustomerParameters)(qs, additionalFields);
600
+ const body = {
601
+ email,
602
+ password,
603
+ };
604
+ if (additionalFields.anonymousCartId) {
605
+ body.anonymousCart = {
606
+ id: additionalFields.anonymousCartId,
607
+ typeId: 'cart'
608
+ };
609
+ }
610
+ if (additionalFields.anonymousCartSignInMode) {
611
+ body.anonymousCartSignInMode = additionalFields.anonymousCartSignInMode;
612
+ }
613
+ if (additionalFields.anonymousId) {
614
+ body.anonymousId = additionalFields.anonymousId;
615
+ }
616
+ if (additionalFields.updateProductData !== undefined) {
617
+ body.updateProductData = additionalFields.updateProductData;
618
+ }
619
+ const response = (await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
620
+ method: 'POST',
621
+ url: `${baseUrl}/login`,
622
+ body,
623
+ qs,
624
+ }));
625
+ results.push({ json: response });
626
+ return results;
627
+ }
628
+ if (operation === 'authenticateInStore') {
629
+ const storeKey = this.getNodeParameter('storeKey', itemIndex);
630
+ const email = this.getNodeParameter('email', itemIndex);
631
+ const password = this.getNodeParameter('password', itemIndex);
632
+ const additionalFields = this.getNodeParameter('additionalFields', itemIndex, {});
633
+ const qs = {};
634
+ (0, customer_utils_1.applyCommonCustomerParameters)(qs, additionalFields);
635
+ const body = {
636
+ email,
637
+ password,
638
+ };
639
+ if (additionalFields.anonymousCartId) {
640
+ body.anonymousCart = {
641
+ id: additionalFields.anonymousCartId,
642
+ typeId: 'cart'
643
+ };
644
+ }
645
+ if (additionalFields.anonymousCartSignInMode) {
646
+ body.anonymousCartSignInMode = additionalFields.anonymousCartSignInMode;
647
+ }
648
+ if (additionalFields.anonymousId) {
649
+ body.anonymousId = additionalFields.anonymousId;
650
+ }
651
+ if (additionalFields.updateProductData !== undefined) {
652
+ body.updateProductData = additionalFields.updateProductData;
653
+ }
654
+ const response = (await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
655
+ method: 'POST',
656
+ url: `${baseUrl}/in-store/key=${encodeURIComponent(storeKey)}/login`,
657
+ body,
658
+ qs,
659
+ }));
660
+ results.push({ json: response });
661
+ return results;
662
+ }
663
+ if (operation === 'createPasswordResetToken') {
664
+ const email = this.getNodeParameter('email', itemIndex);
665
+ const additionalFields = this.getNodeParameter('additionalFields', itemIndex, {});
666
+ const body = {
667
+ email,
668
+ };
669
+ if (additionalFields.ttlMinutes) {
670
+ body.ttlMinutes = additionalFields.ttlMinutes;
671
+ }
672
+ const response = (await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
673
+ method: 'POST',
674
+ url: `${baseUrl}/customers/password-token`,
675
+ body,
676
+ }));
677
+ results.push({ json: response });
678
+ return results;
679
+ }
680
+ if (operation === 'getByPasswordToken') {
681
+ const passwordToken = this.getNodeParameter('passwordToken', itemIndex);
682
+ const additionalFields = this.getNodeParameter('additionalFieldsGet', itemIndex, {});
683
+ const qs = {};
684
+ (0, customer_utils_1.applyCommonCustomerParameters)(qs, additionalFields);
685
+ const response = (await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
686
+ method: 'GET',
687
+ url: `${baseUrl}/customers/password-token=${encodeURIComponent(passwordToken)}`,
688
+ qs,
689
+ }));
690
+ results.push({ json: response });
691
+ return results;
692
+ }
693
+ if (operation === 'resetPassword') {
694
+ const tokenValue = this.getNodeParameter('tokenValue', itemIndex);
695
+ const newPassword = this.getNodeParameter('newPassword', itemIndex);
696
+ const additionalFields = this.getNodeParameter('additionalFields', itemIndex, {});
697
+ const qs = {};
698
+ (0, customer_utils_1.applyCommonCustomerParameters)(qs, additionalFields);
699
+ const body = {
700
+ tokenValue,
701
+ newPassword,
702
+ };
703
+ const response = (await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
704
+ method: 'POST',
705
+ url: `${baseUrl}/customers/password/reset`,
706
+ body,
707
+ qs,
708
+ }));
709
+ results.push({ json: response });
710
+ return results;
711
+ }
712
+ if (operation === 'createPasswordResetTokenInStore') {
713
+ const storeKey = this.getNodeParameter('storeKey', itemIndex);
714
+ const email = this.getNodeParameter('email', itemIndex);
715
+ const additionalFields = this.getNodeParameter('additionalFields', itemIndex, {});
716
+ const body = {
717
+ email,
718
+ };
719
+ if (additionalFields.ttlMinutes) {
720
+ body.ttlMinutes = additionalFields.ttlMinutes;
721
+ }
722
+ const response = (await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
723
+ method: 'POST',
724
+ url: `${baseUrl}/in-store/key=${encodeURIComponent(storeKey)}/customers/password-token`,
725
+ body,
726
+ }));
727
+ results.push({ json: response });
728
+ return results;
729
+ }
730
+ if (operation === 'getInStoreByPasswordToken') {
731
+ const storeKey = this.getNodeParameter('storeKey', itemIndex);
732
+ const passwordToken = this.getNodeParameter('passwordToken', itemIndex);
733
+ const additionalFields = this.getNodeParameter('additionalFieldsGet', itemIndex, {});
734
+ const qs = {};
735
+ (0, customer_utils_1.applyCommonCustomerParameters)(qs, additionalFields);
736
+ const response = (await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
737
+ method: 'GET',
738
+ url: `${baseUrl}/in-store/key=${encodeURIComponent(storeKey)}/customers/password-token=${encodeURIComponent(passwordToken)}`,
739
+ qs,
740
+ }));
741
+ results.push({ json: response });
742
+ return results;
743
+ }
744
+ if (operation === 'resetPasswordInStore') {
745
+ const storeKey = this.getNodeParameter('storeKey', itemIndex);
746
+ const tokenValue = this.getNodeParameter('tokenValue', itemIndex);
747
+ const newPassword = this.getNodeParameter('newPassword', itemIndex);
748
+ const additionalFields = this.getNodeParameter('additionalFields', itemIndex, {});
749
+ const qs = {};
750
+ (0, customer_utils_1.applyCommonCustomerParameters)(qs, additionalFields);
751
+ const body = {
752
+ tokenValue,
753
+ newPassword,
754
+ };
755
+ const response = (await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
756
+ method: 'POST',
757
+ url: `${baseUrl}/in-store/key=${encodeURIComponent(storeKey)}/customers/password/reset`,
758
+ body,
759
+ qs,
760
+ }));
761
+ results.push({ json: response });
762
+ return results;
763
+ }
764
+ if (operation === 'createEmailToken') {
765
+ const customerId = this.getNodeParameter('customerId', itemIndex);
766
+ const version = this.getNodeParameter('version', itemIndex);
767
+ const ttlMinutes = this.getNodeParameter('ttlMinutes', itemIndex);
768
+ const body = {
769
+ id: customerId,
770
+ version: Number(version),
771
+ ttlMinutes: Number(ttlMinutes),
772
+ };
773
+ const response = (await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
774
+ method: 'POST',
775
+ url: `${baseUrl}/customers/email-token`,
776
+ body,
777
+ }));
778
+ results.push({ json: response });
779
+ return results;
780
+ }
781
+ if (operation === 'getByEmailToken') {
782
+ const emailToken = this.getNodeParameter('emailToken', itemIndex);
783
+ const additionalFields = this.getNodeParameter('additionalFieldsGet', itemIndex, {});
784
+ const qs = {};
785
+ (0, customer_utils_1.applyCommonCustomerParameters)(qs, additionalFields);
786
+ const response = (await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
787
+ method: 'GET',
788
+ url: `${baseUrl}/customers/email-token=${encodeURIComponent(emailToken)}`,
789
+ qs,
790
+ }));
791
+ results.push({ json: response });
792
+ return results;
793
+ }
794
+ if (operation === 'verifyEmail') {
795
+ const tokenValue = this.getNodeParameter('tokenValue', itemIndex);
796
+ const additionalFields = this.getNodeParameter('additionalFields', itemIndex, {});
797
+ const qs = {};
798
+ (0, customer_utils_1.applyCommonCustomerParameters)(qs, additionalFields);
799
+ const body = {
800
+ tokenValue,
801
+ };
802
+ const response = (await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
803
+ method: 'POST',
804
+ url: `${baseUrl}/customers/email/confirm`,
805
+ body,
806
+ qs,
807
+ }));
808
+ results.push({ json: response });
809
+ return results;
810
+ }
811
+ if (operation === 'createEmailTokenInStore') {
812
+ const storeKey = this.getNodeParameter('storeKey', itemIndex);
813
+ const customerId = this.getNodeParameter('customerId', itemIndex);
814
+ const version = this.getNodeParameter('version', itemIndex);
815
+ const ttlMinutes = this.getNodeParameter('ttlMinutes', itemIndex);
816
+ const body = {
817
+ id: customerId,
818
+ version: Number(version),
819
+ ttlMinutes: Number(ttlMinutes),
820
+ };
821
+ const response = (await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
822
+ method: 'POST',
823
+ url: `${baseUrl}/in-store/key=${encodeURIComponent(storeKey)}/customers/email-token`,
824
+ body,
825
+ }));
826
+ results.push({ json: response });
827
+ return results;
828
+ }
829
+ if (operation === 'getInStoreByEmailToken') {
830
+ const storeKey = this.getNodeParameter('storeKey', itemIndex);
831
+ const emailToken = this.getNodeParameter('emailToken', itemIndex);
832
+ const additionalFields = this.getNodeParameter('additionalFieldsGet', itemIndex, {});
833
+ const qs = {};
834
+ (0, customer_utils_1.applyCommonCustomerParameters)(qs, additionalFields);
835
+ const response = (await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
836
+ method: 'GET',
837
+ url: `${baseUrl}/in-store/key=${encodeURIComponent(storeKey)}/customers/email-token=${encodeURIComponent(emailToken)}`,
838
+ qs,
839
+ }));
840
+ results.push({ json: response });
841
+ return results;
842
+ }
843
+ if (operation === 'verifyEmailInStore') {
844
+ const storeKey = this.getNodeParameter('storeKey', itemIndex);
845
+ const tokenValue = this.getNodeParameter('tokenValue', itemIndex);
846
+ const additionalFields = this.getNodeParameter('additionalFields', itemIndex, {});
847
+ const qs = {};
848
+ (0, customer_utils_1.applyCommonCustomerParameters)(qs, additionalFields);
849
+ const body = {
850
+ tokenValue,
851
+ };
852
+ const response = (await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
853
+ method: 'POST',
854
+ url: `${baseUrl}/in-store/key=${encodeURIComponent(storeKey)}/customers/email/confirm`,
855
+ body,
856
+ qs,
857
+ }));
858
+ results.push({ json: response });
859
+ return results;
860
+ }
861
+ if (operation === 'delete') {
862
+ const customerId = this.getNodeParameter('customerId', itemIndex);
863
+ const version = this.getNodeParameter('version', itemIndex);
864
+ if (!(customerId === null || customerId === void 0 ? void 0 : customerId.trim())) {
865
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Customer ID cannot be empty', { itemIndex });
866
+ }
867
+ if (version < MIN_VERSION) {
868
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Version must be ${MIN_VERSION} or greater`, { itemIndex });
869
+ }
870
+ const additionalFields = this.getNodeParameter('additionalFields', itemIndex, {});
871
+ const qs = { version };
872
+ (0, customer_utils_1.applyCommonCustomerParameters)(qs, additionalFields);
873
+ const response = (await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
874
+ method: 'DELETE',
875
+ url: `${baseUrl}/customers/${customerId}`,
876
+ qs,
877
+ }));
878
+ results.push({ json: response });
879
+ return results;
880
+ }
881
+ if (operation === 'deleteByKey') {
882
+ const customerKey = this.getNodeParameter('customerKey', itemIndex);
883
+ const version = this.getNodeParameter('version', itemIndex);
884
+ if (!(customerKey === null || customerKey === void 0 ? void 0 : customerKey.trim())) {
885
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Customer Key cannot be empty', { itemIndex });
886
+ }
887
+ if (version < MIN_VERSION) {
888
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Version must be ${MIN_VERSION} or greater`, { itemIndex });
889
+ }
890
+ const additionalFields = this.getNodeParameter('additionalFields', itemIndex, {});
891
+ const qs = { version };
892
+ (0, customer_utils_1.applyCommonCustomerParameters)(qs, additionalFields);
893
+ const response = (await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
894
+ method: 'DELETE',
895
+ url: `${baseUrl}/customers/key=${encodeURIComponent(customerKey)}`,
896
+ qs,
897
+ }));
898
+ results.push({ json: response });
899
+ return results;
900
+ }
901
+ if (operation === 'deleteInStore') {
902
+ const storeKey = this.getNodeParameter('storeKey', itemIndex);
903
+ const customerId = this.getNodeParameter('customerId', itemIndex);
904
+ const version = this.getNodeParameter('version', itemIndex);
905
+ const additionalFields = this.getNodeParameter('additionalFields', itemIndex, {});
906
+ const qs = { version };
907
+ (0, customer_utils_1.applyCommonCustomerParameters)(qs, additionalFields);
908
+ const response = (await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
909
+ method: 'DELETE',
910
+ url: `${baseUrl}/in-store/key=${encodeURIComponent(storeKey)}/customers/${customerId}`,
911
+ qs,
912
+ }));
913
+ results.push({ json: response });
914
+ return results;
915
+ }
916
+ if (operation === 'deleteInStoreByKey') {
917
+ const storeKey = this.getNodeParameter('storeKey', itemIndex);
918
+ const customerKey = this.getNodeParameter('customerKey', itemIndex);
919
+ const version = this.getNodeParameter('version', itemIndex);
920
+ const additionalFields = this.getNodeParameter('additionalFields', itemIndex, {});
921
+ const qs = { version };
922
+ (0, customer_utils_1.applyCommonCustomerParameters)(qs, additionalFields);
923
+ const response = (await this.helpers.httpRequestWithAuthentication.call(this, 'commerceToolsOAuth2Api', {
924
+ method: 'DELETE',
925
+ url: `${baseUrl}/in-store/key=${encodeURIComponent(storeKey)}/customers/key=${encodeURIComponent(customerKey)}`,
926
+ qs,
927
+ }));
928
+ results.push({ json: response });
929
+ return results;
930
+ }
931
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Unsupported operation: ${operation}`, { itemIndex });
932
+ }
933
+ //# sourceMappingURL=customer.operations.js.map