n8n-nodes-mautic-advanced 1.3.0 → 1.3.2

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.
@@ -223,92 +223,105 @@ async function updateCompany(context, itemIndex) {
223
223
  }
224
224
  return result;
225
225
  }
226
+ // Extract owner from a v7 company object. Requests without Accept: application/json
227
+ // return JSON-LD where the owner IRI (/api/v2/users/{id}) lets us resolve the user ID.
228
+ function extractOwnerFromV7(v7Item) {
229
+ if (!v7Item || v7Item.owner === null || v7Item.owner === undefined)
230
+ return null;
231
+ const iri = v7Item.owner?.['@id'];
232
+ if (iri) {
233
+ const match = /\/(\d+)$/.exec(iri);
234
+ if (match)
235
+ return { id: Number(match[1]) };
236
+ }
237
+ // No @id (non-Hydra response) — return partial FormEntity data as-is
238
+ return v7Item.owner;
239
+ }
240
+ // Fetch all companies from v7 and return a map of companyId → owner.
241
+ // Called without Accept: application/json so the response includes JSON-LD @id on the owner.
242
+ async function buildV7OwnerMap(context) {
243
+ const ownerMap = new Map();
244
+ let page = 1;
245
+ const MAX_PAGES = 100;
246
+ while (page <= MAX_PAGES) {
247
+ const response = await (0, ApiHelpers_1.makeApiRequest)(context, 'GET', '/v2/companies', {}, { page });
248
+ const items = Array.isArray(response)
249
+ ? response
250
+ : (response?.['hydra:member'] ?? []);
251
+ if (!items.length)
252
+ break;
253
+ for (const item of items) {
254
+ const id = Number(item.id);
255
+ if (id)
256
+ ownerMap.set(id, extractOwnerFromV7(item));
257
+ }
258
+ const total = response?.['hydra:totalItems'];
259
+ if (typeof total === 'number' && ownerMap.size >= total)
260
+ break;
261
+ page++;
262
+ }
263
+ return ownerMap;
264
+ }
226
265
  async function getCompany(context, itemIndex) {
227
266
  const companyId = (0, ApiHelpers_1.getRequiredParam)(context, 'companyId', itemIndex);
228
267
  const simple = (0, ApiHelpers_1.getOptionalParam)(context, 'simple', itemIndex, false);
229
268
  const mauticVersion = await (0, GenericFunctions_1.getMauticVersion)(context);
230
- let result;
269
+ // v1: custom fields via fields.all
270
+ const v1Response = await (0, ApiHelpers_1.makeApiRequest)(context, 'GET', `/companies/${companyId}`);
271
+ let result = v1Response.company;
231
272
  if (mauticVersion === 'v7') {
232
- result = await (0, ApiHelpers_1.makeApiRequest)(context, 'GET', `/v2/companies/${companyId}`, {}, {}, undefined, {
233
- Accept: 'application/json',
234
- });
235
- }
236
- else {
237
- const response = await (0, ApiHelpers_1.makeApiRequest)(context, 'GET', `/companies/${companyId}`);
238
- result = response.company;
273
+ try {
274
+ // No Accept: application/json — JSON-LD response includes owner @id for user ID extraction
275
+ const v7Company = await (0, ApiHelpers_1.makeApiRequest)(context, 'GET', `/v2/companies/${companyId}`);
276
+ result = { ...result, owner: extractOwnerFromV7(v7Company) };
277
+ }
278
+ catch {
279
+ // enrichment failed — proceed with v1 data (owner: null)
280
+ }
239
281
  }
240
- if (simple) {
282
+ if (simple)
241
283
  result = toSimpleCompany(result);
242
- }
243
- // v7 returns proper JSON types already; convertNumericStrings only needed for v1 string responses
244
- return mauticVersion === 'v7' ? result : (0, DataHelpers_1.convertNumericStrings)(result);
284
+ return (0, DataHelpers_1.convertNumericStrings)(result);
245
285
  }
246
286
  async function getAllCompanies(context, itemIndex) {
247
287
  const returnAll = (0, ApiHelpers_1.getOptionalParam)(context, 'returnAll', itemIndex, false);
248
288
  const simple = (0, ApiHelpers_1.getOptionalParam)(context, 'simple', itemIndex, false);
249
289
  const mauticVersion = await (0, GenericFunctions_1.getMauticVersion)(context);
290
+ const additionalFields = (0, ApiHelpers_1.getOptionalParam)(context, 'additionalFields', itemIndex, {});
291
+ const qs = (0, DataHelpers_1.buildQueryFromOptions)(additionalFields);
292
+ if (!qs.orderBy)
293
+ qs.orderBy = 'id';
294
+ if (!qs.orderByDir)
295
+ qs.orderByDir = 'asc';
296
+ // v1: custom fields via fields.all
250
297
  let responseData;
251
- if (mauticVersion === 'v7') {
252
- // v7: page-based pagination only (no start/limit/orderBy/search)
253
- if (returnAll) {
254
- const limit = (0, ApiHelpers_1.getOptionalParam)(context, 'limit', itemIndex, undefined);
255
- const allItems = [];
256
- let page = 1;
257
- while (true) {
258
- const response = await (0, ApiHelpers_1.makeApiRequest)(context, 'GET', '/v2/companies', {}, { page }, undefined, { Accept: 'application/json' });
259
- const pageItems = Array.isArray(response)
260
- ? response
261
- : (response?.['hydra:member'] ?? []);
262
- if (!pageItems.length)
263
- break;
264
- allItems.push(...pageItems);
265
- if (limit !== undefined && allItems.length >= limit)
266
- break;
267
- page++;
268
- }
269
- responseData = limit !== undefined ? allItems.slice(0, limit) : allItems;
270
- }
271
- else {
272
- // page through until limit satisfied — v7 page size is fixed (~30), limit may exceed it
273
- const limit = (0, ApiHelpers_1.getRequiredParam)(context, 'limit', itemIndex);
274
- const allItems = [];
275
- let page = 1;
276
- while (allItems.length < limit) {
277
- const response = await (0, ApiHelpers_1.makeApiRequest)(context, 'GET', '/v2/companies', {}, { page }, undefined, { Accept: 'application/json' });
278
- const pageItems = Array.isArray(response)
279
- ? response
280
- : (response?.['hydra:member'] ?? []);
281
- if (!pageItems.length)
282
- break;
283
- allItems.push(...pageItems);
284
- page++;
285
- }
286
- responseData = allItems.slice(0, limit);
287
- }
298
+ if (returnAll) {
299
+ const limit = (0, ApiHelpers_1.getOptionalParam)(context, 'limit', itemIndex, undefined);
300
+ responseData = await (0, ApiHelpers_1.makePaginatedRequest)(context, 'companies', 'GET', '/companies', {}, qs, limit);
288
301
  }
289
302
  else {
290
- const additionalFields = (0, ApiHelpers_1.getOptionalParam)(context, 'additionalFields', itemIndex, {});
291
- const qs = (0, DataHelpers_1.buildQueryFromOptions)(additionalFields);
292
- if (!qs.orderBy)
293
- qs.orderBy = 'id';
294
- if (!qs.orderByDir)
295
- qs.orderByDir = 'asc';
296
- if (returnAll) {
297
- const limit = (0, ApiHelpers_1.getOptionalParam)(context, 'limit', itemIndex, undefined);
298
- responseData = await (0, ApiHelpers_1.makePaginatedRequest)(context, 'companies', 'GET', '/companies', {}, qs, limit);
303
+ const limit = (0, ApiHelpers_1.getRequiredParam)(context, 'limit', itemIndex);
304
+ qs.limit = limit;
305
+ const response = await (0, ApiHelpers_1.makeApiRequest)(context, 'GET', '/companies', {}, qs);
306
+ responseData = (response.companies ? Object.values(response.companies) : []);
307
+ }
308
+ if (mauticVersion === 'v7') {
309
+ try {
310
+ // v7 enrichment: overlay owner on each v1 result using a single paginated v7 fetch
311
+ const ownerMap = await buildV7OwnerMap(context);
312
+ responseData = responseData.map((company) => ({
313
+ ...company,
314
+ owner: ownerMap.get(Number(company.id)) ?? null,
315
+ }));
299
316
  }
300
- else {
301
- const limit = (0, ApiHelpers_1.getRequiredParam)(context, 'limit', itemIndex);
302
- qs.limit = limit;
303
- const response = await (0, ApiHelpers_1.makeApiRequest)(context, 'GET', '/companies', {}, qs);
304
- responseData = (response.companies ? Object.values(response.companies) : []);
317
+ catch {
318
+ // enrichment failed proceed with v1 data (owner: null)
305
319
  }
306
320
  }
307
321
  if (simple) {
308
322
  responseData = responseData.map((item) => toSimpleCompany(item));
309
323
  }
310
- // v7 returns proper JSON types already; convertNumericStrings only needed for v1 string responses
311
- return mauticVersion === 'v7' ? responseData : (0, DataHelpers_1.convertNumericStrings)(responseData);
324
+ return (0, DataHelpers_1.convertNumericStrings)(responseData);
312
325
  }
313
326
  async function deleteCompany(context, itemIndex) {
314
327
  const simple = (0, ApiHelpers_1.getOptionalParam)(context, 'simple', itemIndex, false);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "n8n-nodes-mautic-advanced",
3
- "version": "1.3.0",
3
+ "version": "1.3.2",
4
4
  "description": "Enhanced n8n node for Mautic with comprehensive API coverage including tags, campaigns, categories, and advanced contact management",
5
5
  "keywords": [
6
6
  "n8n",