@ixiam/n8n-nodes-civicrm 1.1.34 → 1.1.40

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.
@@ -445,6 +445,14 @@ class CiviCrm {
445
445
  ],
446
446
  displayOptions: { show: { resource: ['contact'], operation: ['create', 'update'] } },
447
447
  },
448
+ {
449
+ displayName: 'Mark Email as Primary',
450
+ name: 'isPrimaryEmail',
451
+ type: 'boolean',
452
+ default: true,
453
+ description: 'Whether this email should be the primary email for the contact.',
454
+ displayOptions: { show: { resource: ['contact'], operation: ['create', 'update'] } },
455
+ },
448
456
  {
449
457
  displayName: 'Phone Location Type',
450
458
  name: 'phoneLocation',
@@ -459,6 +467,14 @@ class CiviCrm {
459
467
  ],
460
468
  displayOptions: { show: { resource: ['contact'], operation: ['create', 'update'] } },
461
469
  },
470
+ {
471
+ displayName: 'Mark Phone as Primary',
472
+ name: 'isPrimaryPhone',
473
+ type: 'boolean',
474
+ default: true,
475
+ description: 'Whether this phone should be the primary phone for the contact.',
476
+ displayOptions: { show: { resource: ['contact'], operation: ['create', 'update'] } },
477
+ },
462
478
  {
463
479
  displayName: 'Address Location Type',
464
480
  name: 'addressLocation',
@@ -473,15 +489,12 @@ class CiviCrm {
473
489
  ],
474
490
  displayOptions: { show: { resource: ['contact'], operation: ['create', 'update'] } },
475
491
  },
476
- //
477
- // PRIMARY FLAG
478
- //
479
492
  {
480
- displayName: 'Mark as Primary',
481
- name: 'isPrimary',
493
+ displayName: 'Mark Address as Primary',
494
+ name: 'isPrimaryAddress',
482
495
  type: 'boolean',
483
496
  default: true,
484
- description: 'Whether this should be the primary email/phone/address for the contact.',
497
+ description: 'Whether this address should be the primary address for the contact.',
485
498
  displayOptions: { show: { resource: ['contact'], operation: ['create', 'update'] } },
486
499
  },
487
500
  //
@@ -570,7 +583,7 @@ class CiviCrm {
570
583
  EXECUTE
571
584
  ============================================================================ */
572
585
  async execute() {
573
- var _a, _b, _c, _d, _e, _f, _g, _h;
586
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
574
587
  const items = this.getInputData();
575
588
  const out = [];
576
589
  const resource = this.getNodeParameter('resource', 0);
@@ -599,7 +612,9 @@ class CiviCrm {
599
612
  const emailLocationParam = this.getNodeParameter('emailLocation', i, 'Work');
600
613
  const phoneLocationParam = this.getNodeParameter('phoneLocation', i, 'Work');
601
614
  const addressLocationParam = this.getNodeParameter('addressLocation', i, 'Home');
602
- const isPrimary = this.getNodeParameter('isPrimary', i, true);
615
+ const isPrimaryEmail = this.getNodeParameter('isPrimaryEmail', i, true);
616
+ const isPrimaryPhone = this.getNodeParameter('isPrimaryPhone', i, true);
617
+ const isPrimaryAddress = this.getNodeParameter('isPrimaryAddress', i, true);
603
618
  let emailLocationName = emailLocationParam;
604
619
  let phoneLocationName = phoneLocationParam;
605
620
  let addressLocationName = addressLocationParam;
@@ -750,25 +765,35 @@ class CiviCrm {
750
765
  const val = rawVal;
751
766
  /* Email simple */
752
767
  if (key === 'email') {
753
- emailData.email = val;
768
+ if (val !== '' && val !== null && val !== undefined) {
769
+ emailData.email = val;
770
+ }
754
771
  continue;
755
772
  }
756
773
  if (key.startsWith('email.')) {
757
- emailData[key.replace(/^email\./, '')] = val;
774
+ if (val !== '' && val !== null && val !== undefined) {
775
+ emailData[key.replace(/^email\./, '')] = val;
776
+ }
758
777
  continue;
759
778
  }
760
779
  /* Phone simple */
761
780
  if (key === 'phone') {
762
- phoneData.phone = val;
781
+ if (val !== '' && val !== null && val !== undefined) {
782
+ phoneData.phone = val;
783
+ }
763
784
  continue;
764
785
  }
765
786
  if (key.startsWith('phone.')) {
766
- phoneData[key.replace(/^phone\./, '')] = val;
787
+ if (val !== '' && val !== null && val !== undefined) {
788
+ phoneData[key.replace(/^phone\./, '')] = val;
789
+ }
767
790
  continue;
768
791
  }
769
792
  /* Address */
770
793
  if (key.startsWith('address.')) {
771
- addressData[key.replace(/^address\./, '')] = val;
794
+ if (val !== '' && val !== null && val !== undefined) {
795
+ addressData[key.replace(/^address\./, '')] = val;
796
+ }
772
797
  continue;
773
798
  }
774
799
  /* Prefijo dinámico */
@@ -789,38 +814,50 @@ class CiviCrm {
789
814
  addressLocationName = mappedLocationName;
790
815
  }
791
816
  if (root === 'email') {
792
- if (!subfield)
793
- emailData.email = val;
794
- else
795
- emailData[subfield] = val;
817
+ if (val !== '' && val !== null && val !== undefined) {
818
+ if (!subfield)
819
+ emailData.email = val;
820
+ else
821
+ emailData[subfield] = val;
822
+ }
796
823
  continue;
797
824
  }
798
825
  if (root === 'phone') {
799
- if (!subfield)
800
- phoneData.phone = val;
801
- else
802
- phoneData[subfield] = val;
826
+ if (val !== '' && val !== null && val !== undefined) {
827
+ if (!subfield)
828
+ phoneData.phone = val;
829
+ else
830
+ phoneData[subfield] = val;
831
+ }
803
832
  continue;
804
833
  }
805
834
  if (root === 'address') {
806
- if (subfield)
807
- addressData[subfield] = val;
835
+ if (val !== '' && val !== null && val !== undefined) {
836
+ if (subfield)
837
+ addressData[subfield] = val;
838
+ }
808
839
  continue;
809
840
  }
810
841
  }
811
842
  }
812
843
  /* gender */
813
844
  if (key === 'gender' || key === 'gender_id') {
814
- values.gender_id = val;
845
+ if (val !== '' && val !== null && val !== undefined) {
846
+ values.gender_id = val;
847
+ }
815
848
  continue;
816
849
  }
817
850
  /* birth_date */
818
851
  if (key === 'birth_date' || key === 'birth') {
819
- values.birth_date = normalizeBirthDate(String(val));
852
+ if (val !== '' && val !== null && val !== undefined) {
853
+ values.birth_date = normalizeBirthDate(String(val));
854
+ }
820
855
  continue;
821
856
  }
822
857
  /* default */
823
- values[key] = val;
858
+ if (val !== '' && val !== null && val !== undefined) {
859
+ values[key] = val;
860
+ }
824
861
  }
825
862
  /* Contact type */
826
863
  const contactType = this.getNodeParameter('contactType', i, '');
@@ -843,19 +880,23 @@ class CiviCrm {
843
880
  }
844
881
  /* SUBENTITIES */
845
882
  if (resource === 'contact') {
846
- if (isPrimary) {
883
+ if (isCreate && isPrimaryEmail) {
847
884
  await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Email/delete', {
848
885
  where: [
849
886
  ['contact_id', '=', contactId],
850
887
  ['is_primary', '=', true],
851
888
  ],
852
889
  });
890
+ }
891
+ if (isCreate && isPrimaryPhone) {
853
892
  await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Phone/delete', {
854
893
  where: [
855
894
  ['contact_id', '=', contactId],
856
895
  ['is_primary', '=', true],
857
896
  ],
858
897
  });
898
+ }
899
+ if (isCreate && isPrimaryAddress) {
859
900
  await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Address/delete', {
860
901
  where: [
861
902
  ['contact_id', '=', contactId],
@@ -864,34 +905,133 @@ class CiviCrm {
864
905
  });
865
906
  }
866
907
  if (Object.keys(emailData).length) {
867
- await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Email/create', {
868
- values: {
869
- ...emailData,
870
- contact_id: contactId,
871
- is_primary: isPrimary,
872
- 'location_type_id:name': emailLocationName,
873
- },
874
- });
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
+ },
916
+ });
917
+ }
918
+ else {
919
+ const existingEmail = await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Email/get', {
920
+ where: [
921
+ ['contact_id', '=', contactId],
922
+ ['location_type_id:name', '=', emailLocationName],
923
+ ],
924
+ limit: 1,
925
+ select: ['id'],
926
+ });
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', {
930
+ values: {
931
+ id: existingEmailId,
932
+ ...emailData,
933
+ contact_id: contactId,
934
+ is_primary: isPrimaryEmail,
935
+ },
936
+ });
937
+ }
938
+ 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
+ },
946
+ });
947
+ }
948
+ }
875
949
  }
876
950
  if (Object.keys(phoneData).length) {
877
- await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Phone/create', {
878
- values: {
879
- ...phoneData,
880
- contact_id: contactId,
881
- is_primary: isPrimary,
882
- 'location_type_id:name': phoneLocationName,
883
- },
884
- });
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', {
973
+ values: {
974
+ id: existingPhoneId,
975
+ ...phoneData,
976
+ contact_id: contactId,
977
+ is_primary: isPrimaryPhone,
978
+ },
979
+ });
980
+ }
981
+ 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
+ },
989
+ });
990
+ }
991
+ }
885
992
  }
886
993
  if (Object.keys(addressData).length) {
887
- await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Address/create', {
888
- values: {
889
- ...addressData,
890
- contact_id: contactId,
891
- is_primary: isPrimary,
892
- 'location_type_id:name': addressLocationName,
893
- },
894
- });
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', {
1016
+ values: {
1017
+ id: existingAddressId,
1018
+ ...addressData,
1019
+ contact_id: contactId,
1020
+ is_primary: isPrimaryAddress,
1021
+ },
1022
+ });
1023
+ }
1024
+ 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
+ },
1032
+ });
1033
+ }
1034
+ }
895
1035
  }
896
1036
  }
897
1037
  /* FINAL GET */
@@ -919,7 +1059,7 @@ class CiviCrm {
919
1059
  }
920
1060
  : {}),
921
1061
  });
922
- out.push({ json: (_h = (_g = res === null || res === void 0 ? void 0 : res.values) === null || _g === void 0 ? void 0 : _g[0]) !== null && _h !== void 0 ? _h : {} });
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 : {} });
923
1063
  }
924
1064
  return [out];
925
1065
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ixiam/n8n-nodes-civicrm",
3
- "version": "1.1.34",
3
+ "version": "1.1.40",
4
4
  "description": "Full-featured CiviCRM API v4 integration for n8n with support for Contacts, Memberships, Groups, Relationships, Activities and structured sub-entities like Email, Phone, and Address.",
5
5
  "license": "MIT",
6
6
  "keywords": [
@@ -482,6 +482,14 @@ export class CiviCrm implements INodeType {
482
482
  ],
483
483
  displayOptions: { show: { resource: ['contact'], operation: ['create', 'update'] } },
484
484
  },
485
+ {
486
+ displayName: 'Mark Email as Primary',
487
+ name: 'isPrimaryEmail',
488
+ type: 'boolean',
489
+ default: true,
490
+ description: 'Whether this email should be the primary email for the contact.',
491
+ displayOptions: { show: { resource: ['contact'], operation: ['create', 'update'] } },
492
+ },
485
493
  {
486
494
  displayName: 'Phone Location Type',
487
495
  name: 'phoneLocation',
@@ -496,6 +504,14 @@ export class CiviCrm implements INodeType {
496
504
  ],
497
505
  displayOptions: { show: { resource: ['contact'], operation: ['create', 'update'] } },
498
506
  },
507
+ {
508
+ displayName: 'Mark Phone as Primary',
509
+ name: 'isPrimaryPhone',
510
+ type: 'boolean',
511
+ default: true,
512
+ description: 'Whether this phone should be the primary phone for the contact.',
513
+ displayOptions: { show: { resource: ['contact'], operation: ['create', 'update'] } },
514
+ },
499
515
  {
500
516
  displayName: 'Address Location Type',
501
517
  name: 'addressLocation',
@@ -510,16 +526,12 @@ export class CiviCrm implements INodeType {
510
526
  ],
511
527
  displayOptions: { show: { resource: ['contact'], operation: ['create', 'update'] } },
512
528
  },
513
-
514
- //
515
- // PRIMARY FLAG
516
- //
517
529
  {
518
- displayName: 'Mark as Primary',
519
- name: 'isPrimary',
530
+ displayName: 'Mark Address as Primary',
531
+ name: 'isPrimaryAddress',
520
532
  type: 'boolean',
521
533
  default: true,
522
- description: 'Whether this should be the primary email/phone/address for the contact.',
534
+ description: 'Whether this address should be the primary address for the contact.',
523
535
  displayOptions: { show: { resource: ['contact'], operation: ['create', 'update'] } },
524
536
  },
525
537
 
@@ -662,7 +674,9 @@ export class CiviCrm implements INodeType {
662
674
  const emailLocationParam = this.getNodeParameter('emailLocation', i, 'Work') as string;
663
675
  const phoneLocationParam = this.getNodeParameter('phoneLocation', i, 'Work') as string;
664
676
  const addressLocationParam = this.getNodeParameter('addressLocation', i, 'Home') as string;
665
- const isPrimary = this.getNodeParameter('isPrimary', i, true) as boolean;
677
+ const isPrimaryEmail = this.getNodeParameter('isPrimaryEmail', i, true) as boolean;
678
+ const isPrimaryPhone = this.getNodeParameter('isPrimaryPhone', i, true) as boolean;
679
+ const isPrimaryAddress = this.getNodeParameter('isPrimaryAddress', i, true) as boolean;
666
680
 
667
681
  let emailLocationName = emailLocationParam;
668
682
  let phoneLocationName = phoneLocationParam;
@@ -858,27 +872,37 @@ export class CiviCrm implements INodeType {
858
872
 
859
873
  /* Email simple */
860
874
  if (key === 'email') {
861
- emailData.email = val;
875
+ if (val !== '' && val !== null && val !== undefined) {
876
+ emailData.email = val;
877
+ }
862
878
  continue;
863
879
  }
864
880
  if (key.startsWith('email.')) {
865
- emailData[key.replace(/^email\./, '')] = val;
881
+ if (val !== '' && val !== null && val !== undefined) {
882
+ emailData[key.replace(/^email\./, '')] = val;
883
+ }
866
884
  continue;
867
885
  }
868
886
 
869
887
  /* Phone simple */
870
888
  if (key === 'phone') {
871
- phoneData.phone = val;
889
+ if (val !== '' && val !== null && val !== undefined) {
890
+ phoneData.phone = val;
891
+ }
872
892
  continue;
873
893
  }
874
894
  if (key.startsWith('phone.')) {
875
- phoneData[key.replace(/^phone\./, '')] = val;
895
+ if (val !== '' && val !== null && val !== undefined) {
896
+ phoneData[key.replace(/^phone\./, '')] = val;
897
+ }
876
898
  continue;
877
899
  }
878
900
 
879
901
  /* Address */
880
902
  if (key.startsWith('address.')) {
881
- addressData[key.replace(/^address\./, '')] = val;
903
+ if (val !== '' && val !== null && val !== undefined) {
904
+ addressData[key.replace(/^address\./, '')] = val;
905
+ }
882
906
  continue;
883
907
  }
884
908
 
@@ -900,19 +924,25 @@ export class CiviCrm implements INodeType {
900
924
  }
901
925
 
902
926
  if (root === 'email') {
903
- if (!subfield) emailData.email = val;
904
- else emailData[subfield] = val;
927
+ if (val !== '' && val !== null && val !== undefined) {
928
+ if (!subfield) emailData.email = val;
929
+ else emailData[subfield] = val;
930
+ }
905
931
  continue;
906
932
  }
907
933
 
908
934
  if (root === 'phone') {
909
- if (!subfield) phoneData.phone = val;
910
- else phoneData[subfield] = val;
935
+ if (val !== '' && val !== null && val !== undefined) {
936
+ if (!subfield) phoneData.phone = val;
937
+ else phoneData[subfield] = val;
938
+ }
911
939
  continue;
912
940
  }
913
941
 
914
942
  if (root === 'address') {
915
- if (subfield) addressData[subfield] = val;
943
+ if (val !== '' && val !== null && val !== undefined) {
944
+ if (subfield) addressData[subfield] = val;
945
+ }
916
946
  continue;
917
947
  }
918
948
  }
@@ -920,18 +950,24 @@ export class CiviCrm implements INodeType {
920
950
 
921
951
  /* gender */
922
952
  if (key === 'gender' || key === 'gender_id') {
923
- values.gender_id = val;
953
+ if (val !== '' && val !== null && val !== undefined) {
954
+ values.gender_id = val;
955
+ }
924
956
  continue;
925
957
  }
926
958
 
927
959
  /* birth_date */
928
960
  if (key === 'birth_date' || key === 'birth') {
929
- values.birth_date = normalizeBirthDate(String(val));
961
+ if (val !== '' && val !== null && val !== undefined) {
962
+ values.birth_date = normalizeBirthDate(String(val));
963
+ }
930
964
  continue;
931
965
  }
932
966
 
933
967
  /* default */
934
- values[key] = val;
968
+ if (val !== '' && val !== null && val !== undefined) {
969
+ values[key] = val;
970
+ }
935
971
  }
936
972
 
937
973
  /* Contact type */
@@ -965,19 +1001,23 @@ export class CiviCrm implements INodeType {
965
1001
 
966
1002
  /* SUBENTITIES */
967
1003
  if (resource === 'contact') {
968
- if (isPrimary) {
1004
+ if (isCreate && isPrimaryEmail) {
969
1005
  await civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Email/delete', {
970
1006
  where: [
971
1007
  ['contact_id', '=', contactId],
972
1008
  ['is_primary', '=', true],
973
1009
  ],
974
1010
  });
1011
+ }
1012
+ if (isCreate && isPrimaryPhone) {
975
1013
  await civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Phone/delete', {
976
1014
  where: [
977
1015
  ['contact_id', '=', contactId],
978
1016
  ['is_primary', '=', true],
979
1017
  ],
980
1018
  });
1019
+ }
1020
+ if (isCreate && isPrimaryAddress) {
981
1021
  await civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Address/delete', {
982
1022
  where: [
983
1023
  ['contact_id', '=', contactId],
@@ -987,36 +1027,130 @@ export class CiviCrm implements INodeType {
987
1027
  }
988
1028
 
989
1029
  if (Object.keys(emailData).length) {
990
- await civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Email/create', {
991
- values: {
992
- ...emailData,
993
- contact_id: contactId,
994
- is_primary: isPrimary,
995
- 'location_type_id:name': emailLocationName,
996
- },
997
- });
1030
+ if (isCreate) {
1031
+ await civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Email/create', {
1032
+ values: {
1033
+ ...emailData,
1034
+ contact_id: contactId,
1035
+ is_primary: isPrimaryEmail,
1036
+ 'location_type_id:name': emailLocationName,
1037
+ },
1038
+ });
1039
+ } else {
1040
+ const existingEmail = await civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Email/get', {
1041
+ where: [
1042
+ ['contact_id', '=', contactId],
1043
+ ['location_type_id:name', '=', emailLocationName],
1044
+ ],
1045
+ limit: 1,
1046
+ select: ['id'],
1047
+ });
1048
+ const existingEmailId = existingEmail?.values?.[0]?.id as number | undefined;
1049
+ if (existingEmailId) {
1050
+ await civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Email/update', {
1051
+ values: {
1052
+ id: existingEmailId,
1053
+ ...emailData,
1054
+ contact_id: contactId,
1055
+ is_primary: isPrimaryEmail,
1056
+ },
1057
+ });
1058
+ } else {
1059
+ await civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Email/create', {
1060
+ values: {
1061
+ ...emailData,
1062
+ contact_id: contactId,
1063
+ is_primary: isPrimaryEmail,
1064
+ 'location_type_id:name': emailLocationName,
1065
+ },
1066
+ });
1067
+ }
1068
+ }
998
1069
  }
999
1070
 
1000
- if (Object.keys(phoneData).length) {
1071
+ if (Object.keys(phoneData).length) {
1072
+ if (isCreate) {
1001
1073
  await civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Phone/create', {
1002
1074
  values: {
1003
1075
  ...phoneData,
1004
1076
  contact_id: contactId,
1005
- is_primary: isPrimary,
1077
+ is_primary: isPrimaryPhone,
1006
1078
  'location_type_id:name': phoneLocationName,
1007
1079
  },
1008
1080
  });
1081
+ } else {
1082
+ const existingPhone = await civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Phone/get', {
1083
+ where: [
1084
+ ['contact_id', '=', contactId],
1085
+ ['location_type_id:name', '=', phoneLocationName],
1086
+ ],
1087
+ limit: 1,
1088
+ select: ['id'],
1089
+ });
1090
+ const existingPhoneId = existingPhone?.values?.[0]?.id as number | undefined;
1091
+ if (existingPhoneId) {
1092
+ await civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Phone/update', {
1093
+ values: {
1094
+ id: existingPhoneId,
1095
+ ...phoneData,
1096
+ contact_id: contactId,
1097
+ is_primary: isPrimaryPhone,
1098
+ },
1099
+ });
1100
+ } else {
1101
+ await civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Phone/create', {
1102
+ values: {
1103
+ ...phoneData,
1104
+ contact_id: contactId,
1105
+ is_primary: isPrimaryPhone,
1106
+ 'location_type_id:name': phoneLocationName,
1107
+ },
1108
+ });
1109
+ }
1009
1110
  }
1111
+ }
1112
+
1010
1113
 
1011
1114
  if (Object.keys(addressData).length) {
1012
- await civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Address/create', {
1013
- values: {
1014
- ...addressData,
1015
- contact_id: contactId,
1016
- is_primary: isPrimary,
1017
- 'location_type_id:name': addressLocationName,
1018
- },
1019
- });
1115
+ if (isCreate) {
1116
+ await civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Address/create', {
1117
+ values: {
1118
+ ...addressData,
1119
+ contact_id: contactId,
1120
+ is_primary: isPrimaryAddress,
1121
+ 'location_type_id:name': addressLocationName,
1122
+ },
1123
+ });
1124
+ } else {
1125
+ const existingAddress = await civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Address/get', {
1126
+ where: [
1127
+ ['contact_id', '=', contactId],
1128
+ ['location_type_id:name', '=', addressLocationName],
1129
+ ],
1130
+ limit: 1,
1131
+ select: ['id'],
1132
+ });
1133
+ const existingAddressId = existingAddress?.values?.[0]?.id as number | undefined;
1134
+ if (existingAddressId) {
1135
+ await civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Address/update', {
1136
+ values: {
1137
+ id: existingAddressId,
1138
+ ...addressData,
1139
+ contact_id: contactId,
1140
+ is_primary: isPrimaryAddress,
1141
+ },
1142
+ });
1143
+ } else {
1144
+ await civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Address/create', {
1145
+ values: {
1146
+ ...addressData,
1147
+ contact_id: contactId,
1148
+ is_primary: isPrimaryAddress,
1149
+ 'location_type_id:name': addressLocationName,
1150
+ },
1151
+ });
1152
+ }
1153
+ }
1020
1154
  }
1021
1155
  }
1022
1156