@ixiam/n8n-nodes-civicrm 1.1.35 → 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;
@@ -865,19 +880,23 @@ class CiviCrm {
865
880
  }
866
881
  /* SUBENTITIES */
867
882
  if (resource === 'contact') {
868
- if (isCreate && isPrimary) {
883
+ if (isCreate && isPrimaryEmail) {
869
884
  await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Email/delete', {
870
885
  where: [
871
886
  ['contact_id', '=', contactId],
872
887
  ['is_primary', '=', true],
873
888
  ],
874
889
  });
890
+ }
891
+ if (isCreate && isPrimaryPhone) {
875
892
  await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Phone/delete', {
876
893
  where: [
877
894
  ['contact_id', '=', contactId],
878
895
  ['is_primary', '=', true],
879
896
  ],
880
897
  });
898
+ }
899
+ if (isCreate && isPrimaryAddress) {
881
900
  await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Address/delete', {
882
901
  where: [
883
902
  ['contact_id', '=', contactId],
@@ -886,34 +905,133 @@ class CiviCrm {
886
905
  });
887
906
  }
888
907
  if (Object.keys(emailData).length) {
889
- await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Email/create', {
890
- values: {
891
- ...emailData,
892
- contact_id: contactId,
893
- is_primary: isCreate ? isPrimary : false,
894
- 'location_type_id:name': emailLocationName,
895
- },
896
- });
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
+ }
897
949
  }
898
950
  if (Object.keys(phoneData).length) {
899
- await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Phone/create', {
900
- values: {
901
- ...phoneData,
902
- contact_id: contactId,
903
- is_primary: isCreate ? isPrimary : false,
904
- 'location_type_id:name': phoneLocationName,
905
- },
906
- });
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
+ }
907
992
  }
908
993
  if (Object.keys(addressData).length) {
909
- await GenericFunctions_1.civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Address/create', {
910
- values: {
911
- ...addressData,
912
- contact_id: contactId,
913
- is_primary: isCreate ? isPrimary : false,
914
- 'location_type_id:name': addressLocationName,
915
- },
916
- });
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
+ }
917
1035
  }
918
1036
  }
919
1037
  /* FINAL GET */
@@ -941,7 +1059,7 @@ class CiviCrm {
941
1059
  }
942
1060
  : {}),
943
1061
  });
944
- 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 : {} });
945
1063
  }
946
1064
  return [out];
947
1065
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ixiam/n8n-nodes-civicrm",
3
- "version": "1.1.35",
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;
@@ -987,19 +1001,23 @@ export class CiviCrm implements INodeType {
987
1001
 
988
1002
  /* SUBENTITIES */
989
1003
  if (resource === 'contact') {
990
- if (isCreate && isPrimary) {
1004
+ if (isCreate && isPrimaryEmail) {
991
1005
  await civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Email/delete', {
992
1006
  where: [
993
1007
  ['contact_id', '=', contactId],
994
1008
  ['is_primary', '=', true],
995
1009
  ],
996
1010
  });
1011
+ }
1012
+ if (isCreate && isPrimaryPhone) {
997
1013
  await civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Phone/delete', {
998
1014
  where: [
999
1015
  ['contact_id', '=', contactId],
1000
1016
  ['is_primary', '=', true],
1001
1017
  ],
1002
1018
  });
1019
+ }
1020
+ if (isCreate && isPrimaryAddress) {
1003
1021
  await civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Address/delete', {
1004
1022
  where: [
1005
1023
  ['contact_id', '=', contactId],
@@ -1009,36 +1027,130 @@ export class CiviCrm implements INodeType {
1009
1027
  }
1010
1028
 
1011
1029
  if (Object.keys(emailData).length) {
1012
- await civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Email/create', {
1013
- values: {
1014
- ...emailData,
1015
- contact_id: contactId,
1016
- is_primary: isCreate ? isPrimary : false,
1017
- 'location_type_id:name': emailLocationName,
1018
- },
1019
- });
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
+ }
1020
1069
  }
1021
1070
 
1022
- if (Object.keys(phoneData).length) {
1071
+ if (Object.keys(phoneData).length) {
1072
+ if (isCreate) {
1023
1073
  await civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Phone/create', {
1024
1074
  values: {
1025
1075
  ...phoneData,
1026
1076
  contact_id: contactId,
1027
- is_primary: isCreate ? isPrimary : false,
1077
+ is_primary: isPrimaryPhone,
1028
1078
  'location_type_id:name': phoneLocationName,
1029
1079
  },
1030
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
+ }
1031
1110
  }
1111
+ }
1112
+
1032
1113
 
1033
1114
  if (Object.keys(addressData).length) {
1034
- await civicrmApiRequest.call(this, 'POST', '/civicrm/ajax/api4/Address/create', {
1035
- values: {
1036
- ...addressData,
1037
- contact_id: contactId,
1038
- is_primary: isCreate ? isPrimary : false,
1039
- 'location_type_id:name': addressLocationName,
1040
- },
1041
- });
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
+ }
1042
1154
  }
1043
1155
  }
1044
1156