@ixiam/n8n-nodes-civicrm 1.1.40 → 2.1.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.
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CiviCrm = void 0;
4
+ const n8n_workflow_1 = require("n8n-workflow");
4
5
  const GenericFunctions_1 = require("../transport/GenericFunctions");
5
6
  const resources_1 = require("./descriptions/resources");
6
7
  const generic_1 = require("./descriptions/generic");
@@ -59,8 +60,9 @@ class CiviCrm {
59
60
  'Includes dynamic mapping of email, phone, address and location types.\n' +
60
61
  'Includes birth_date validation and JSON filters for GET MANY.\n',
61
62
  defaults: { name: 'CiviCRM' },
62
- inputs: ['main'],
63
- outputs: ['main'],
63
+ subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
64
+ inputs: [n8n_workflow_1.NodeConnectionTypes.Main],
65
+ outputs: [n8n_workflow_1.NodeConnectionTypes.Main],
64
66
  // @ts-ignore
65
67
  usableAsTool: true,
66
68
  // @ts-ignore
@@ -559,12 +561,11 @@ class CiviCrm {
559
561
  this.methods = {
560
562
  loadOptions: {
561
563
  async loadOptionValues() {
562
- const { url, apiKey } = (await this.getCredentials('civiCrmApi'));
563
- const res = await this.helpers.httpRequest({
564
+ const { baseUrl } = (await this.getCredentials('civiCrmApi'));
565
+ const res = await this.helpers.httpRequestWithAuthentication.call(this, 'civiCrmApi', {
564
566
  method: 'POST',
565
- url: `${url.replace(/\/$/, '')}/civicrm/ajax/api4/OptionValue/get`,
567
+ url: `${baseUrl.replace(/\/$/, '')}/civicrm/ajax/api4/OptionValue/get`,
566
568
  headers: {
567
- Authorization: `Bearer ${apiKey}`,
568
569
  'Content-Type': 'application/x-www-form-urlencoded',
569
570
  },
570
571
  body: { params: JSON.stringify({ limit: 50, select: ['id', 'label'] }) },
@@ -588,478 +589,505 @@ class CiviCrm {
588
589
  const out = [];
589
590
  const resource = this.getNodeParameter('resource', 0);
590
591
  const operation = this.getNodeParameter('operation', 0);
591
- // Custom API call: generic passthrough to any API4 entity/action
592
- if (resource === 'customApi') {
593
- const entity = this.getNodeParameter('customEntity', 0);
594
- const action = this.getNodeParameter('customAction', 0);
595
- const paramsJson = this.getNodeParameter('customParamsJson', 0, '');
596
- let params = {};
597
- if (paramsJson) {
598
- try {
599
- params = JSON.parse(paramsJson);
600
- }
601
- catch (error) {
602
- throw new Error('Invalid JSON in "Params (JSON)"');
603
- }
604
- }
605
- const res = await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', `/civicrm/ajax/api4/${entity}/${action}`, params);
606
- // Return the raw API4 response so advanced users can work with values and metadata
607
- out.push({ json: res });
608
- return [out];
609
- }
610
- const entity = ENTITY_MAP[resource];
592
+ const entity = resource !== 'customApi' ? ENTITY_MAP[resource] : '';
611
593
  for (let i = 0; i < items.length; i++) {
612
- const emailLocationParam = this.getNodeParameter('emailLocation', i, 'Work');
613
- const phoneLocationParam = this.getNodeParameter('phoneLocation', i, 'Work');
614
- const addressLocationParam = this.getNodeParameter('addressLocation', i, 'Home');
615
- const isPrimaryEmail = this.getNodeParameter('isPrimaryEmail', i, true);
616
- const isPrimaryPhone = this.getNodeParameter('isPrimaryPhone', i, true);
617
- const isPrimaryAddress = this.getNodeParameter('isPrimaryAddress', i, true);
618
- let emailLocationName = emailLocationParam;
619
- let phoneLocationName = phoneLocationParam;
620
- let addressLocationName = addressLocationParam;
621
- /* ======================================================
622
- GET
623
- ====================================================== */
624
- if (operation === 'get') {
625
- const id = this.getNodeParameter('id', i);
626
- const params = resource === 'contact'
627
- ? {
628
- where: [['id', '=', id]],
629
- limit: 1,
630
- select: [
631
- 'id',
632
- 'display_name',
633
- 'first_name',
634
- 'last_name',
635
- 'organization_name',
636
- 'legal_name',
637
- 'contact_type',
638
- 'gender_id',
639
- 'gender_id:name',
640
- 'birth_date',
641
- ],
642
- chain: {
643
- emails: ['Email', 'get', { where: [['contact_id', '=', '$id']] }],
644
- phones: ['Phone', 'get', { where: [['contact_id', '=', '$id']] }],
645
- addresses: ['Address', 'get', { where: [['contact_id', '=', '$id']] }],
646
- },
647
- }
648
- : {
649
- where: [['id', '=', id]],
650
- limit: 1,
651
- select: ['id', 'name', 'title', 'subject', 'display_name'],
652
- };
653
- const res = await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', `/civicrm/ajax/api4/${entity}/get`, params);
654
- out.push({ json: (_b = (_a = res === null || res === void 0 ? void 0 : res.values) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : {} });
655
- continue;
656
- }
657
- /* ======================================================
658
- GET MANY
659
- ====================================================== */
660
- if (operation === 'getMany') {
661
- const limit = this.getNodeParameter('limit', i, 100);
662
- const returnAll = this.getNodeParameter('returnAll', i, false);
663
- const whereJson = this.getNodeParameter('whereJson', i, '');
664
- let where = [];
665
- if (whereJson) {
666
- try {
667
- where = JSON.parse(whereJson);
668
- }
669
- catch (error) {
670
- throw new Error('Invalid JSON in whereJson');
594
+ try {
595
+ // Custom API call: generic passthrough to any API4 entity/action
596
+ if (resource === 'customApi') {
597
+ const customEntity = this.getNodeParameter('customEntity', i);
598
+ const action = this.getNodeParameter('customAction', i);
599
+ const paramsJson = this.getNodeParameter('customParamsJson', i, '');
600
+ let params = {};
601
+ if (paramsJson) {
602
+ try {
603
+ params = JSON.parse(paramsJson);
604
+ }
605
+ catch (error) {
606
+ throw new Error('Invalid JSON in "Params (JSON)"');
607
+ }
671
608
  }
609
+ const res = await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', `/civicrm/ajax/api4/${customEntity}/${action}`, params);
610
+ // Return the raw API4 response so advanced users can work with values and metadata
611
+ out.push({
612
+ json: res,
613
+ pairedItem: { item: i },
614
+ });
615
+ continue;
672
616
  }
673
- if (resource === 'contact') {
674
- const contactType = this.getNodeParameter('contactType', i, '');
675
- if (contactType)
676
- where.push(['contact_type', '=', contactType]);
617
+ const emailLocationParam = this.getNodeParameter('emailLocation', i, 'Work');
618
+ const phoneLocationParam = this.getNodeParameter('phoneLocation', i, 'Work');
619
+ const addressLocationParam = this.getNodeParameter('addressLocation', i, 'Home');
620
+ const isPrimaryEmail = this.getNodeParameter('isPrimaryEmail', i, true);
621
+ const isPrimaryPhone = this.getNodeParameter('isPrimaryPhone', i, true);
622
+ const isPrimaryAddress = this.getNodeParameter('isPrimaryAddress', i, true);
623
+ let emailLocationName = emailLocationParam;
624
+ let phoneLocationName = phoneLocationParam;
625
+ let addressLocationName = addressLocationParam;
626
+ /* ======================================================
627
+ GET
628
+ ====================================================== */
629
+ if (operation === 'get') {
630
+ const id = this.getNodeParameter('id', i);
631
+ const params = resource === 'contact'
632
+ ? {
633
+ where: [['id', '=', id]],
634
+ limit: 1,
635
+ select: [
636
+ 'id',
637
+ 'display_name',
638
+ 'first_name',
639
+ 'last_name',
640
+ 'organization_name',
641
+ 'legal_name',
642
+ 'contact_type',
643
+ 'gender_id',
644
+ 'gender_id:name',
645
+ 'birth_date',
646
+ ],
647
+ chain: {
648
+ emails: ['Email', 'get', { where: [['contact_id', '=', '$id']] }],
649
+ phones: ['Phone', 'get', { where: [['contact_id', '=', '$id']] }],
650
+ addresses: ['Address', 'get', { where: [['contact_id', '=', '$id']] }],
651
+ },
652
+ }
653
+ : {
654
+ where: [['id', '=', id]],
655
+ limit: 1,
656
+ select: ['id', 'name', 'title', 'subject', 'display_name'],
657
+ };
658
+ const res = await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', `/civicrm/ajax/api4/${entity}/get`, params);
659
+ out.push({
660
+ json: (_b = (_a = res === null || res === void 0 ? void 0 : res.values) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : {},
661
+ pairedItem: { item: i },
662
+ });
663
+ continue;
677
664
  }
678
- const params = resource === 'contact'
679
- ? {
680
- where,
681
- select: [
682
- 'id',
683
- 'display_name',
684
- 'first_name',
685
- 'last_name',
686
- 'organization_name',
687
- 'legal_name',
688
- 'contact_type',
689
- 'gender_id',
690
- 'gender_id:name',
691
- 'birth_date',
692
- ],
693
- chain: {
694
- emails: ['Email', 'get', { where: [['contact_id', '=', '$id']] }],
695
- phones: ['Phone', 'get', { where: [['contact_id', '=', '$id']] }],
696
- addresses: ['Address', 'get', { where: [['contact_id', '=', '$id']] }],
697
- },
665
+ /* ======================================================
666
+ GET MANY
667
+ ====================================================== */
668
+ if (operation === 'getMany') {
669
+ const limit = this.getNodeParameter('limit', i, 100);
670
+ const returnAll = this.getNodeParameter('returnAll', i, false);
671
+ const whereJson = this.getNodeParameter('whereJson', i, '');
672
+ let where = [];
673
+ if (whereJson) {
674
+ try {
675
+ where = JSON.parse(whereJson);
676
+ }
677
+ catch (error) {
678
+ throw new Error('Invalid JSON in whereJson');
679
+ }
698
680
  }
699
- : {
700
- where,
701
- select: ['id', 'name', 'title', 'subject', 'display_name'],
702
- };
703
- if (returnAll) {
704
- let offset = 0;
705
- const page = 500;
706
- let hasMore = true;
707
- while (hasMore) {
708
- const r = await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', `/civicrm/ajax/api4/${entity}/get`, { ...params, limit: page, offset });
709
- const vals = (_c = r === null || r === void 0 ? void 0 : r.values) !== null && _c !== void 0 ? _c : [];
710
- for (const v of vals)
711
- out.push({ json: v });
712
- if (vals.length < page) {
713
- hasMore = false;
681
+ if (resource === 'contact') {
682
+ const contactType = this.getNodeParameter('contactType', i, '');
683
+ if (contactType)
684
+ where.push(['contact_type', '=', contactType]);
685
+ }
686
+ const params = resource === 'contact'
687
+ ? {
688
+ where,
689
+ select: [
690
+ 'id',
691
+ 'display_name',
692
+ 'first_name',
693
+ 'last_name',
694
+ 'organization_name',
695
+ 'legal_name',
696
+ 'contact_type',
697
+ 'gender_id',
698
+ 'gender_id:name',
699
+ 'birth_date',
700
+ ],
701
+ chain: {
702
+ emails: ['Email', 'get', { where: [['contact_id', '=', '$id']] }],
703
+ phones: ['Phone', 'get', { where: [['contact_id', '=', '$id']] }],
704
+ addresses: ['Address', 'get', { where: [['contact_id', '=', '$id']] }],
705
+ },
714
706
  }
715
- else {
716
- offset += page;
707
+ : {
708
+ where,
709
+ select: ['id', 'name', 'title', 'subject', 'display_name'],
710
+ };
711
+ if (returnAll) {
712
+ let offset = 0;
713
+ const page = 500;
714
+ let hasMore = true;
715
+ while (hasMore) {
716
+ const r = await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', `/civicrm/ajax/api4/${entity}/get`, { ...params, limit: page, offset });
717
+ const vals = (_c = r === null || r === void 0 ? void 0 : r.values) !== null && _c !== void 0 ? _c : [];
718
+ for (const v of vals) {
719
+ out.push({
720
+ json: v,
721
+ pairedItem: { item: i },
722
+ });
723
+ }
724
+ if (vals.length < page) {
725
+ hasMore = false;
726
+ }
727
+ else {
728
+ offset += page;
729
+ }
717
730
  }
718
731
  }
719
- }
720
- else {
721
- const r = await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', `/civicrm/ajax/api4/${entity}/get`, { ...params, limit });
722
- const vals = (_d = r === null || r === void 0 ? void 0 : r.values) !== null && _d !== void 0 ? _d : [];
723
- for (const v of vals)
724
- out.push({ json: v });
725
- }
726
- continue;
727
- }
728
- /* ======================================================
729
- DELETE
730
- ====================================================== */
731
- if (operation === 'delete') {
732
- const id = this.getNodeParameter('id', i);
733
- const r = await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', `/civicrm/ajax/api4/${entity}/delete`, {
734
- where: [['id', '=', id]],
735
- });
736
- out.push({
737
- json: {
738
- success: true,
739
- message: `${entity} ${id} deleted`,
740
- deleted_id: id,
741
- api_response: r,
742
- },
743
- });
744
- continue;
745
- }
746
- /* ======================================================
747
- CREATE / UPDATE
748
- ====================================================== */
749
- const isCreate = operation === 'create';
750
- const id = !isCreate ? this.getNodeParameter('id', i) : undefined;
751
- const pairs = this.getNodeParameter('fields.field', i, []);
752
- const values = {};
753
- const emailData = {};
754
- const phoneData = {};
755
- const addressData = {};
756
- let locationTypeMap = {};
757
- if (resource === 'contact') {
758
- locationTypeMap = await getLocationTypeMap.call(this);
759
- }
760
- for (const p of pairs) {
761
- if (!p.fieldName)
762
- continue;
763
- const key = p.fieldName.trim();
764
- const rawVal = convertValue(p.fieldValue);
765
- const val = rawVal;
766
- /* Email simple */
767
- if (key === 'email') {
768
- if (val !== '' && val !== null && val !== undefined) {
769
- emailData.email = val;
732
+ else {
733
+ const r = await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', `/civicrm/ajax/api4/${entity}/get`, { ...params, limit });
734
+ const vals = (_d = r === null || r === void 0 ? void 0 : r.values) !== null && _d !== void 0 ? _d : [];
735
+ for (const v of vals) {
736
+ out.push({
737
+ json: v,
738
+ pairedItem: { item: i },
739
+ });
740
+ }
770
741
  }
771
742
  continue;
772
743
  }
773
- if (key.startsWith('email.')) {
774
- if (val !== '' && val !== null && val !== undefined) {
775
- emailData[key.replace(/^email\./, '')] = val;
776
- }
744
+ /* ======================================================
745
+ DELETE
746
+ ====================================================== */
747
+ if (operation === 'delete') {
748
+ const id = this.getNodeParameter('id', i);
749
+ const r = await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', `/civicrm/ajax/api4/${entity}/delete`, {
750
+ where: [['id', '=', id]],
751
+ });
752
+ out.push({
753
+ json: {
754
+ success: true,
755
+ message: `${entity} ${id} deleted`,
756
+ deleted_id: id,
757
+ api_response: r,
758
+ },
759
+ pairedItem: { item: i },
760
+ });
777
761
  continue;
778
762
  }
779
- /* Phone simple */
780
- if (key === 'phone') {
781
- if (val !== '' && val !== null && val !== undefined) {
782
- phoneData.phone = val;
783
- }
784
- continue;
763
+ /* ======================================================
764
+ CREATE / UPDATE
765
+ ====================================================== */
766
+ const isCreate = operation === 'create';
767
+ const id = !isCreate ? this.getNodeParameter('id', i) : undefined;
768
+ const pairs = this.getNodeParameter('fields.field', i, []);
769
+ const values = {};
770
+ const emailData = {};
771
+ const phoneData = {};
772
+ const addressData = {};
773
+ let locationTypeMap = {};
774
+ if (resource === 'contact') {
775
+ locationTypeMap = await getLocationTypeMap.call(this);
785
776
  }
786
- if (key.startsWith('phone.')) {
787
- if (val !== '' && val !== null && val !== undefined) {
788
- phoneData[key.replace(/^phone\./, '')] = val;
777
+ for (const p of pairs) {
778
+ if (!p.fieldName)
779
+ continue;
780
+ const key = p.fieldName.trim();
781
+ const rawVal = convertValue(p.fieldValue);
782
+ const val = rawVal;
783
+ /* Email simple */
784
+ if (key === 'email') {
785
+ if (val !== '' && val !== null && val !== undefined) {
786
+ emailData.email = val;
787
+ }
788
+ continue;
789
789
  }
790
- continue;
791
- }
792
- /* Address */
793
- if (key.startsWith('address.')) {
794
- if (val !== '' && val !== null && val !== undefined) {
795
- addressData[key.replace(/^address\./, '')] = val;
790
+ if (key.startsWith('email.')) {
791
+ if (val !== '' && val !== null && val !== undefined) {
792
+ emailData[key.replace(/^email\./, '')] = val;
793
+ }
794
+ continue;
796
795
  }
797
- continue;
798
- }
799
- /* Prefijo dinámico */
800
- const segments = key.split('.');
801
- if (segments.length >= 2 && resource === 'contact') {
802
- const prefixRaw = segments[0];
803
- const root = segments[1];
804
- const subfield = segments.slice(2).join('.') || '';
805
- const normalizedPrefix = normalizeLocationKey(prefixRaw);
806
- const mappedLocationName = locationTypeMap[normalizedPrefix];
807
- if (root === 'email' || root === 'phone' || root === 'address') {
808
- if (mappedLocationName) {
809
- if (root === 'email')
810
- emailLocationName = mappedLocationName;
811
- if (root === 'phone')
812
- phoneLocationName = mappedLocationName;
813
- if (root === 'address')
814
- addressLocationName = mappedLocationName;
796
+ /* Phone simple */
797
+ if (key === 'phone') {
798
+ if (val !== '' && val !== null && val !== undefined) {
799
+ phoneData.phone = val;
815
800
  }
816
- if (root === 'email') {
817
- if (val !== '' && val !== null && val !== undefined) {
818
- if (!subfield)
819
- emailData.email = val;
820
- else
821
- emailData[subfield] = val;
822
- }
823
- continue;
801
+ continue;
802
+ }
803
+ if (key.startsWith('phone.')) {
804
+ if (val !== '' && val !== null && val !== undefined) {
805
+ phoneData[key.replace(/^phone\./, '')] = val;
824
806
  }
825
- if (root === 'phone') {
826
- if (val !== '' && val !== null && val !== undefined) {
827
- if (!subfield)
828
- phoneData.phone = val;
829
- else
830
- phoneData[subfield] = val;
831
- }
832
- continue;
807
+ continue;
808
+ }
809
+ /* Address */
810
+ if (key.startsWith('address.')) {
811
+ if (val !== '' && val !== null && val !== undefined) {
812
+ addressData[key.replace(/^address\./, '')] = val;
833
813
  }
834
- if (root === 'address') {
835
- if (val !== '' && val !== null && val !== undefined) {
836
- if (subfield)
837
- addressData[subfield] = val;
814
+ continue;
815
+ }
816
+ /* Dynamic prefix: resolve location type from field name */
817
+ const segments = key.split('.');
818
+ if (segments.length >= 2 && resource === 'contact') {
819
+ const prefixRaw = segments[0];
820
+ const root = segments[1];
821
+ const subfield = segments.slice(2).join('.') || '';
822
+ const normalizedPrefix = normalizeLocationKey(prefixRaw);
823
+ const mappedLocationName = locationTypeMap[normalizedPrefix];
824
+ if (root === 'email' || root === 'phone' || root === 'address') {
825
+ if (mappedLocationName) {
826
+ if (root === 'email')
827
+ emailLocationName = mappedLocationName;
828
+ if (root === 'phone')
829
+ phoneLocationName = mappedLocationName;
830
+ if (root === 'address')
831
+ addressLocationName = mappedLocationName;
832
+ }
833
+ if (root === 'email') {
834
+ if (val !== '' && val !== null && val !== undefined) {
835
+ if (!subfield)
836
+ emailData.email = val;
837
+ else
838
+ emailData[subfield] = val;
839
+ }
840
+ continue;
841
+ }
842
+ if (root === 'phone') {
843
+ if (val !== '' && val !== null && val !== undefined) {
844
+ if (!subfield)
845
+ phoneData.phone = val;
846
+ else
847
+ phoneData[subfield] = val;
848
+ }
849
+ continue;
850
+ }
851
+ if (root === 'address') {
852
+ if (val !== '' && val !== null && val !== undefined) {
853
+ if (subfield)
854
+ addressData[subfield] = val;
855
+ }
856
+ continue;
838
857
  }
839
- continue;
840
858
  }
841
859
  }
842
- }
843
- /* gender */
844
- if (key === 'gender' || key === 'gender_id') {
845
- if (val !== '' && val !== null && val !== undefined) {
846
- values.gender_id = val;
860
+ /* gender */
861
+ if (key === 'gender' || key === 'gender_id') {
862
+ if (val !== '' && val !== null && val !== undefined) {
863
+ values.gender_id = val;
864
+ }
865
+ continue;
847
866
  }
848
- continue;
849
- }
850
- /* birth_date */
851
- if (key === 'birth_date' || key === 'birth') {
867
+ /* birth_date */
868
+ if (key === 'birth_date' || key === 'birth') {
869
+ if (val !== '' && val !== null && val !== undefined) {
870
+ values.birth_date = normalizeBirthDate(String(val));
871
+ }
872
+ continue;
873
+ }
874
+ /* default */
852
875
  if (val !== '' && val !== null && val !== undefined) {
853
- values.birth_date = normalizeBirthDate(String(val));
876
+ values[key] = val;
854
877
  }
855
- continue;
856
878
  }
857
- /* default */
858
- if (val !== '' && val !== null && val !== undefined) {
859
- values[key] = val;
879
+ /* Contact type */
880
+ const contactType = this.getNodeParameter('contactType', i, '');
881
+ if (resource === 'contact' && contactType) {
882
+ values.contact_type = contactType;
860
883
  }
861
- }
862
- /* Contact type */
863
- const contactType = this.getNodeParameter('contactType', i, '');
864
- if (resource === 'contact' && contactType) {
865
- values.contact_type = contactType;
866
- }
867
- /* CREATE/UPDATE contact */
868
- let contactId = id;
869
- if (isCreate) {
870
- const r = await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Contact/create', { values });
871
- contactId = (_f = (_e = r === null || r === void 0 ? void 0 : r.values) === null || _e === void 0 ? void 0 : _e[0]) === null || _f === void 0 ? void 0 : _f.id;
872
- if (!contactId)
873
- throw new Error('Failed to create contact.');
874
- }
875
- else {
876
- await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', `/civicrm/ajax/api4/${entity}/update`, {
877
- values,
878
- where: [['id', '=', contactId]],
879
- });
880
- }
881
- /* SUBENTITIES */
882
- if (resource === 'contact') {
883
- if (isCreate && isPrimaryEmail) {
884
- await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Email/delete', {
885
- where: [
886
- ['contact_id', '=', contactId],
887
- ['is_primary', '=', true],
888
- ],
889
- });
884
+ /* CREATE/UPDATE contact */
885
+ let contactId = id;
886
+ if (isCreate) {
887
+ const r = await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Contact/create', { values });
888
+ contactId = (_f = (_e = r === null || r === void 0 ? void 0 : r.values) === null || _e === void 0 ? void 0 : _e[0]) === null || _f === void 0 ? void 0 : _f.id;
889
+ if (!contactId)
890
+ throw new Error('Failed to create contact.');
890
891
  }
891
- if (isCreate && isPrimaryPhone) {
892
- await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Phone/delete', {
893
- where: [
894
- ['contact_id', '=', contactId],
895
- ['is_primary', '=', true],
896
- ],
897
- });
898
- }
899
- if (isCreate && isPrimaryAddress) {
900
- await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Address/delete', {
901
- where: [
902
- ['contact_id', '=', contactId],
903
- ['is_primary', '=', true],
904
- ],
892
+ else {
893
+ await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', `/civicrm/ajax/api4/${entity}/update`, {
894
+ values,
895
+ where: [['id', '=', contactId]],
905
896
  });
906
897
  }
907
- if (Object.keys(emailData).length) {
908
- if (isCreate) {
909
- await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Email/create', {
910
- values: {
911
- ...emailData,
912
- contact_id: contactId,
913
- is_primary: isPrimaryEmail,
914
- 'location_type_id:name': emailLocationName,
915
- },
898
+ /* SUBENTITIES */
899
+ if (resource === 'contact') {
900
+ if (isCreate && isPrimaryEmail) {
901
+ await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Email/delete', {
902
+ where: [
903
+ ['contact_id', '=', contactId],
904
+ ['is_primary', '=', true],
905
+ ],
916
906
  });
917
907
  }
918
- else {
919
- const existingEmail = await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Email/get', {
908
+ if (isCreate && isPrimaryPhone) {
909
+ await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Phone/delete', {
920
910
  where: [
921
911
  ['contact_id', '=', contactId],
922
- ['location_type_id:name', '=', emailLocationName],
912
+ ['is_primary', '=', true],
913
+ ],
914
+ });
915
+ }
916
+ if (isCreate && isPrimaryAddress) {
917
+ await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Address/delete', {
918
+ where: [
919
+ ['contact_id', '=', contactId],
920
+ ['is_primary', '=', true],
923
921
  ],
924
- limit: 1,
925
- select: ['id'],
926
922
  });
927
- const existingEmailId = (_h = (_g = existingEmail === null || existingEmail === void 0 ? void 0 : existingEmail.values) === null || _g === void 0 ? void 0 : _g[0]) === null || _h === void 0 ? void 0 : _h.id;
928
- if (existingEmailId) {
929
- await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Email/update', {
923
+ }
924
+ if (Object.keys(emailData).length) {
925
+ if (isCreate) {
926
+ await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Email/create', {
930
927
  values: {
931
- id: existingEmailId,
932
928
  ...emailData,
933
929
  contact_id: contactId,
934
930
  is_primary: isPrimaryEmail,
931
+ 'location_type_id:name': emailLocationName,
935
932
  },
936
933
  });
937
934
  }
938
935
  else {
939
- await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Email/create', {
940
- values: {
941
- ...emailData,
942
- contact_id: contactId,
943
- is_primary: isPrimaryEmail,
944
- 'location_type_id:name': emailLocationName,
945
- },
936
+ const existingEmail = await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Email/get', {
937
+ where: [
938
+ ['contact_id', '=', contactId],
939
+ ['location_type_id:name', '=', emailLocationName],
940
+ ],
941
+ limit: 1,
942
+ select: ['id'],
946
943
  });
944
+ const existingEmailId = (_h = (_g = existingEmail === null || existingEmail === void 0 ? void 0 : existingEmail.values) === null || _g === void 0 ? void 0 : _g[0]) === null || _h === void 0 ? void 0 : _h.id;
945
+ if (existingEmailId) {
946
+ await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Email/update', {
947
+ values: {
948
+ id: existingEmailId,
949
+ ...emailData,
950
+ contact_id: contactId,
951
+ is_primary: isPrimaryEmail,
952
+ },
953
+ });
954
+ }
955
+ else {
956
+ await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Email/create', {
957
+ values: {
958
+ ...emailData,
959
+ contact_id: contactId,
960
+ is_primary: isPrimaryEmail,
961
+ 'location_type_id:name': emailLocationName,
962
+ },
963
+ });
964
+ }
947
965
  }
948
966
  }
949
- }
950
- if (Object.keys(phoneData).length) {
951
- if (isCreate) {
952
- await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Phone/create', {
953
- values: {
954
- ...phoneData,
955
- contact_id: contactId,
956
- is_primary: isPrimaryPhone,
957
- 'location_type_id:name': phoneLocationName,
958
- },
959
- });
960
- }
961
- else {
962
- const existingPhone = await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Phone/get', {
963
- where: [
964
- ['contact_id', '=', contactId],
965
- ['location_type_id:name', '=', phoneLocationName],
966
- ],
967
- limit: 1,
968
- select: ['id'],
969
- });
970
- const existingPhoneId = (_k = (_j = existingPhone === null || existingPhone === void 0 ? void 0 : existingPhone.values) === null || _j === void 0 ? void 0 : _j[0]) === null || _k === void 0 ? void 0 : _k.id;
971
- if (existingPhoneId) {
972
- await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Phone/update', {
967
+ if (Object.keys(phoneData).length) {
968
+ if (isCreate) {
969
+ await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Phone/create', {
973
970
  values: {
974
- id: existingPhoneId,
975
971
  ...phoneData,
976
972
  contact_id: contactId,
977
973
  is_primary: isPrimaryPhone,
974
+ 'location_type_id:name': phoneLocationName,
978
975
  },
979
976
  });
980
977
  }
981
978
  else {
982
- await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Phone/create', {
983
- values: {
984
- ...phoneData,
985
- contact_id: contactId,
986
- is_primary: isPrimaryPhone,
987
- 'location_type_id:name': phoneLocationName,
988
- },
979
+ const existingPhone = await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Phone/get', {
980
+ where: [
981
+ ['contact_id', '=', contactId],
982
+ ['location_type_id:name', '=', phoneLocationName],
983
+ ],
984
+ limit: 1,
985
+ select: ['id'],
989
986
  });
987
+ const existingPhoneId = (_k = (_j = existingPhone === null || existingPhone === void 0 ? void 0 : existingPhone.values) === null || _j === void 0 ? void 0 : _j[0]) === null || _k === void 0 ? void 0 : _k.id;
988
+ if (existingPhoneId) {
989
+ await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Phone/update', {
990
+ values: {
991
+ id: existingPhoneId,
992
+ ...phoneData,
993
+ contact_id: contactId,
994
+ is_primary: isPrimaryPhone,
995
+ },
996
+ });
997
+ }
998
+ else {
999
+ await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Phone/create', {
1000
+ values: {
1001
+ ...phoneData,
1002
+ contact_id: contactId,
1003
+ is_primary: isPrimaryPhone,
1004
+ 'location_type_id:name': phoneLocationName,
1005
+ },
1006
+ });
1007
+ }
990
1008
  }
991
1009
  }
992
- }
993
- if (Object.keys(addressData).length) {
994
- if (isCreate) {
995
- await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Address/create', {
996
- values: {
997
- ...addressData,
998
- contact_id: contactId,
999
- is_primary: isPrimaryAddress,
1000
- 'location_type_id:name': addressLocationName,
1001
- },
1002
- });
1003
- }
1004
- else {
1005
- const existingAddress = await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Address/get', {
1006
- where: [
1007
- ['contact_id', '=', contactId],
1008
- ['location_type_id:name', '=', addressLocationName],
1009
- ],
1010
- limit: 1,
1011
- select: ['id'],
1012
- });
1013
- const existingAddressId = (_m = (_l = existingAddress === null || existingAddress === void 0 ? void 0 : existingAddress.values) === null || _l === void 0 ? void 0 : _l[0]) === null || _m === void 0 ? void 0 : _m.id;
1014
- if (existingAddressId) {
1015
- await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Address/update', {
1010
+ if (Object.keys(addressData).length) {
1011
+ if (isCreate) {
1012
+ await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Address/create', {
1016
1013
  values: {
1017
- id: existingAddressId,
1018
1014
  ...addressData,
1019
1015
  contact_id: contactId,
1020
1016
  is_primary: isPrimaryAddress,
1017
+ 'location_type_id:name': addressLocationName,
1021
1018
  },
1022
1019
  });
1023
1020
  }
1024
1021
  else {
1025
- await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Address/create', {
1026
- values: {
1027
- ...addressData,
1028
- contact_id: contactId,
1029
- is_primary: isPrimaryAddress,
1030
- 'location_type_id:name': addressLocationName,
1031
- },
1022
+ const existingAddress = await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Address/get', {
1023
+ where: [
1024
+ ['contact_id', '=', contactId],
1025
+ ['location_type_id:name', '=', addressLocationName],
1026
+ ],
1027
+ limit: 1,
1028
+ select: ['id'],
1032
1029
  });
1030
+ const existingAddressId = (_m = (_l = existingAddress === null || existingAddress === void 0 ? void 0 : existingAddress.values) === null || _l === void 0 ? void 0 : _l[0]) === null || _m === void 0 ? void 0 : _m.id;
1031
+ if (existingAddressId) {
1032
+ await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Address/update', {
1033
+ values: {
1034
+ id: existingAddressId,
1035
+ ...addressData,
1036
+ contact_id: contactId,
1037
+ is_primary: isPrimaryAddress,
1038
+ },
1039
+ });
1040
+ }
1041
+ else {
1042
+ await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Address/create', {
1043
+ values: {
1044
+ ...addressData,
1045
+ contact_id: contactId,
1046
+ is_primary: isPrimaryAddress,
1047
+ 'location_type_id:name': addressLocationName,
1048
+ },
1049
+ });
1050
+ }
1033
1051
  }
1034
1052
  }
1035
1053
  }
1054
+ /* FINAL GET */
1055
+ const res = await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', `/civicrm/ajax/api4/${entity}/get`, {
1056
+ where: [['id', '=', contactId]],
1057
+ select: [
1058
+ 'id',
1059
+ 'display_name',
1060
+ 'first_name',
1061
+ 'last_name',
1062
+ 'organization_name',
1063
+ 'legal_name',
1064
+ 'contact_type',
1065
+ 'gender_id',
1066
+ 'gender_id:name',
1067
+ 'birth_date',
1068
+ ],
1069
+ ...(resource === 'contact'
1070
+ ? {
1071
+ chain: {
1072
+ emails: ['Email', 'get', { where: [['contact_id', '=', '$id']] }],
1073
+ phones: ['Phone', 'get', { where: [['contact_id', '=', '$id']] }],
1074
+ addresses: ['Address', 'get', { where: [['contact_id', '=', '$id']] }],
1075
+ },
1076
+ }
1077
+ : {}),
1078
+ });
1079
+ out.push({
1080
+ json: (_p = (_o = res === null || res === void 0 ? void 0 : res.values) === null || _o === void 0 ? void 0 : _o[0]) !== null && _p !== void 0 ? _p : {},
1081
+ pairedItem: { item: i },
1082
+ });
1083
+ }
1084
+ catch (error) {
1085
+ if (this.continueOnFail()) {
1086
+ out.push({ json: { error: error instanceof Error ? error.message : String(error) }, pairedItem: { item: i } });
1087
+ continue;
1088
+ }
1089
+ throw error;
1036
1090
  }
1037
- /* FINAL GET */
1038
- const res = await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', `/civicrm/ajax/api4/${entity}/get`, {
1039
- where: [['id', '=', contactId]],
1040
- select: [
1041
- 'id',
1042
- 'display_name',
1043
- 'first_name',
1044
- 'last_name',
1045
- 'organization_name',
1046
- 'legal_name',
1047
- 'contact_type',
1048
- 'gender_id',
1049
- 'gender_id:name',
1050
- 'birth_date',
1051
- ],
1052
- ...(resource === 'contact'
1053
- ? {
1054
- chain: {
1055
- emails: ['Email', 'get', { where: [['contact_id', '=', '$id']] }],
1056
- phones: ['Phone', 'get', { where: [['contact_id', '=', '$id']] }],
1057
- addresses: ['Address', 'get', { where: [['contact_id', '=', '$id']] }],
1058
- },
1059
- }
1060
- : {}),
1061
- });
1062
- out.push({ json: (_p = (_o = res === null || res === void 0 ? void 0 : res.values) === null || _o === void 0 ? void 0 : _o[0]) !== null && _p !== void 0 ? _p : {} });
1063
1091
  }
1064
1092
  return [out];
1065
1093
  }