@crmcom/self-service-sdk 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.
package/contacts.js ADDED
@@ -0,0 +1,1109 @@
1
+ import { httpUtil } from "./httpUtil";
2
+ import { ErrorCodes, createResult, createCommonResult } from "./resultUtil";
3
+ import { account } from "./account";
4
+ import { logger } from './logger';
5
+
6
+ export const contacts = {
7
+ getContact,
8
+ updateContact,
9
+ setMarketingPreferences,
10
+ submitReferralCode,
11
+ getContactAddresses,
12
+ addContactAddress,
13
+ updateContactAddress,
14
+ updateContactOnboardingCards,
15
+ deleteContactAddress,
16
+ reclaimPurchase,
17
+ referFriend,
18
+ redeemPass,
19
+ getContactPreferredOrganisation,
20
+ addContactPreferredOrganisation,
21
+ unregisterContact,
22
+ signOutContact,
23
+ getContactPurchases,
24
+ getPurchase,
25
+ requestOTPSpend,
26
+ getOTPSpend,
27
+ onChangePassword,
28
+ getNameDayRules,
29
+ addApplicationUsage,
30
+ requestContactToken,
31
+ getContactTokens,
32
+ addPNDeviceToken,
33
+ deleteDeviceToken,
34
+ uploadProfileImage,
35
+ deleteProfileImage,
36
+ verifyContactExist,
37
+ getAndroidWalletPass,
38
+ getApplePass,
39
+ getGooglePass,
40
+ exportStatements,
41
+ getContactDonationsOffer,
42
+ getDonationHistory,
43
+ donationActions,
44
+ updateDonation,
45
+ removeDonation,
46
+ getDonationOffers,
47
+ getDonationOrganisations
48
+ };
49
+ async function getContact() {
50
+ try {
51
+ let id = httpUtil.getSession().sub;
52
+ let response = await httpUtil.get({
53
+ resourcePath: "/v2/contacts/" + id,
54
+ });
55
+ return createCommonResult(response);
56
+ } catch (e) {
57
+ logger.error("Exception getContact:", e);
58
+ return createResult(ErrorCodes.UNKNOWN, e);
59
+ }
60
+ }
61
+
62
+ async function verifyContactExist({ email_address, phone } = {}) {
63
+ try {
64
+ let response = await httpUtil.get({
65
+ logOutIfSessionInvalid: false,
66
+ resourcePath: "/v2/contacts/",
67
+ queryParams: {
68
+ email_address,
69
+ phone
70
+ }
71
+ });
72
+ return createCommonResult(response);
73
+ } catch (e) {
74
+ logger.error("Exception getContact:", e);
75
+ return createResult(ErrorCodes.UNKNOWN, e);
76
+ }
77
+ }
78
+
79
+ async function getContactAddresses() {
80
+ try {
81
+ let id = httpUtil.getSession().sub;
82
+ let response = await httpUtil.get({
83
+ resourcePath: "/v2/contacts/" + id + "/addresses",
84
+ });
85
+ return createCommonResult(response);
86
+ } catch (e) {
87
+ logger.error("Exception getContactAddresses:", e);
88
+ return createResult(ErrorCodes.UNKNOWN, e);
89
+ }
90
+ }
91
+
92
+ async function addContactAddress({
93
+ type,
94
+ name,
95
+ is_primary,
96
+ address_line_1,
97
+ address_line_2,
98
+ state_province_county,
99
+ town_city,
100
+ postal_code,
101
+ country_code,
102
+ lat,
103
+ lon,
104
+ google_place_id
105
+ } = {}) {
106
+ try {
107
+ let id = httpUtil.getSession().sub;
108
+ let response = await httpUtil.post({
109
+ resourcePath: "/v2/contacts/" + id + "/addresses",
110
+ body: {
111
+ type,
112
+ name,
113
+ is_primary,
114
+ address_line_1,
115
+ address_line_2,
116
+ state_province_county,
117
+ town_city,
118
+ postal_code,
119
+ country_code,
120
+ lat,
121
+ lon,
122
+ google_place_id
123
+ },
124
+ withAccessToken: true,
125
+ });
126
+ if (response.code == "OK")
127
+ return createResult(ErrorCodes.OK, response.data);
128
+ else {
129
+ if (
130
+ response.error && (
131
+ response.error.error == "COM.CRM.EXCEPTIONS.ALREADYEXISTSEXCEPTION" || response.error.error == "CRM.EXCEPTIONS.ALREADYEXISTSEXCEPTION")
132
+ ) {
133
+ return createResult(
134
+ ErrorCodes.ADD_ADDRESS_ALREADY_TYPE,
135
+ response.error
136
+ );
137
+ } else return createResult(ErrorCodes.UNCLASSIFIED_ERROR, response.error);
138
+ }
139
+ } catch (e) {
140
+ logger.error("Exception addContactAddress:", e);
141
+ return createResult(ErrorCodes.UNKNOWN, e);
142
+ }
143
+ }
144
+
145
+ async function updateContactAddress({
146
+ type,
147
+ name,
148
+ is_primary,
149
+ address_line_1,
150
+ address_line_2,
151
+ state_province_county,
152
+ town_city,
153
+ postal_code,
154
+ country_code,
155
+ lat,
156
+ lon,
157
+ google_place_id
158
+ } = {}, addId) {
159
+ try {
160
+ let id = httpUtil.getSession().sub;
161
+ let response = await httpUtil.put({
162
+ resourcePath: "/v2/contacts/" + id + "/addresses/" + addId,
163
+ body: {
164
+ type,
165
+ name,
166
+ is_primary,
167
+ address_line_1,
168
+ address_line_2,
169
+ state_province_county,
170
+ town_city,
171
+ postal_code,
172
+ country_code,
173
+ lat,
174
+ lon,
175
+ google_place_id
176
+ },
177
+ withAccessToken: true,
178
+ });
179
+ if (response.code == "OK")
180
+ return createResult(ErrorCodes.OK, response.data);
181
+ else {
182
+ if (
183
+ response.error && (
184
+ response.error.error == "COM.CRM.EXCEPTIONS.ALREADYEXISTSEXCEPTION" || response.error.error == "CRM.EXCEPTIONS.ALREADYEXISTSEXCEPTION")
185
+ ) {
186
+ return createResult(
187
+ ErrorCodes.ADD_ADDRESS_ALREADY_TYPE,
188
+ response.error
189
+ );
190
+ } else return createResult(ErrorCodes.UNCLASSIFIED_ERROR, response.error);
191
+ }
192
+ } catch (e) {
193
+ logger.error("Exception updateContactAddress:", e);
194
+ return createResult(ErrorCodes.UNKNOWN, e);
195
+ }
196
+ }
197
+
198
+ async function updateContactOnboardingCards({
199
+ viewed
200
+ } = {}, addId) {
201
+ try {
202
+ let id = httpUtil.getSession().sub;
203
+ let response = await httpUtil.put({
204
+ resourcePath: "/v2/contacts/" + id + "/onboarding" ,
205
+ body: {
206
+ viewed
207
+ },
208
+ withAccessToken: true,
209
+ });
210
+ if (response.code == "OK")
211
+ return createResult(ErrorCodes.OK, response.data);
212
+ else return createResult(ErrorCodes.UNCLASSIFIED_ERROR, response.error);
213
+ } catch (e) {
214
+ logger.error("Exception updateContactOnboardingCards:", e);
215
+ return createResult(ErrorCodes.UNKNOWN, e);
216
+ }
217
+ }
218
+
219
+
220
+ async function deleteContactAddress(addId) {
221
+ try {
222
+ let id = httpUtil.getSession().sub;
223
+ let response = await httpUtil.sendDelete({
224
+ resourcePath: "/v2/contacts/" + id + "/addresses/" + addId,
225
+ withAccessToken: true,
226
+ });
227
+ return createCommonResult(response);
228
+ } catch (e) {
229
+ logger.error("Exception deleteContactAddress:", e);
230
+ return createResult(ErrorCodes.UNKNOWN, e);
231
+ }
232
+ }
233
+
234
+ async function reclaimPurchase({
235
+ purchase_id,
236
+ merchant_tap_code,
237
+ venue_tap_code,
238
+ total_amount,
239
+ transaction_code
240
+ }, { }) {
241
+ try {
242
+ let body = {}
243
+ if (transaction_code) {
244
+ body.transaction_code = transaction_code;
245
+ } else {
246
+ body.id = purchase_id
247
+ body.organisation = {
248
+ merchant_tap: {
249
+ code: merchant_tap_code
250
+ },
251
+ venue_tap: {
252
+ code: venue_tap_code
253
+ },
254
+ }
255
+ body.transaction_amounts = {
256
+ total: total_amount
257
+ }
258
+ }
259
+ let response = await httpUtil.post({
260
+ resourcePath: "/v2/purchases/reclaim",
261
+ body: body,
262
+ withAccessToken: true,
263
+ });
264
+ return createCommonResult(response);
265
+ } catch (e) {
266
+ logger.error("Exception reclaimPurchase:", e);
267
+ return createResult(ErrorCodes.UNKNOWN, e);
268
+ }
269
+ }
270
+
271
+ async function referFriend({
272
+ recipients
273
+ }, { }) {
274
+ try {
275
+ let response = await httpUtil.post({
276
+ resourcePath: "/v2/referrals/actions ",
277
+ body: {
278
+ action: "SEND",
279
+ recipients
280
+ },
281
+ withAccessToken: true,
282
+ });
283
+ return createCommonResult(response);
284
+ } catch (e) {
285
+ logger.error("Exception referFriend:", e);
286
+ return createResult(ErrorCodes.UNKNOWN, e);
287
+ }
288
+ }
289
+
290
+ async function redeemPass({
291
+ code,
292
+ contact_id,
293
+ wallet_id,
294
+ pin
295
+ }, { }) {
296
+ try {
297
+ let response = await httpUtil.post({
298
+ resourcePath: "/v2/passes/redeem ",
299
+ body: {
300
+ code,
301
+ contact_id,
302
+ wallet_id,
303
+ pin
304
+ },
305
+ withAccessToken: true,
306
+ });
307
+ return createCommonResult(response);
308
+ } catch (e) {
309
+ logger.error("Exception redeemPass:", e);
310
+ return createResult(ErrorCodes.UNKNOWN, e);
311
+ }
312
+ }
313
+
314
+ async function getContactPreferredOrganisation() {
315
+ try {
316
+ let id = httpUtil.getSession().sub;
317
+ let response = await httpUtil.get({
318
+ resourcePath: "/v2/contacts/" + id + "/preferences",
319
+ });
320
+ return createCommonResult(response);
321
+ } catch (e) {
322
+ logger.error("Exception getContactPreferredOrganisation:", e);
323
+ return createResult(ErrorCodes.UNKNOWN, e);
324
+ }
325
+ }
326
+
327
+ async function addContactPreferredOrganisation({
328
+ organisation_id,
329
+ type = 'ORDERS'
330
+ } = {}) {
331
+ try {
332
+ let id = httpUtil.getSession().sub;
333
+ let response = await httpUtil.post({
334
+ resourcePath: "/v2/contacts/" + id + "/preferences",
335
+ body: {
336
+ organisation_id,
337
+ type
338
+ }
339
+ });
340
+ return createCommonResult(response);
341
+ } catch (e) {
342
+ logger.error("Exception addContactPreferredOrganisation:", e);
343
+ return createResult(ErrorCodes.UNKNOWN, e);
344
+ }
345
+ }
346
+
347
+ async function updateContact({
348
+ first_name,
349
+ middle_name,
350
+ last_name,
351
+ language_code,
352
+ email,
353
+ phone,
354
+ gender,
355
+ date_of_birth,
356
+ name_day,
357
+ custom_fields,
358
+ statutory_number,
359
+ id_number,
360
+ id_issuing_country_code,
361
+ id_expiration_date,
362
+ passport_number,
363
+ passport_issuing_country_code,
364
+ passport_expiration_date,
365
+ } = {}) {
366
+ try {
367
+ let body = {
368
+ first_name,
369
+ middle_name,
370
+ last_name,
371
+ language_code,
372
+ email,
373
+ phone,
374
+ custom_fields
375
+ }
376
+ let demographics = {};
377
+ let id_details = {};
378
+ let passport={};
379
+ if (gender) {
380
+ demographics.gender = gender;
381
+ }
382
+ if (date_of_birth) {
383
+ demographics.date_of_birth = date_of_birth;
384
+ }
385
+ if (name_day) {
386
+ demographics.name_day = name_day;
387
+ }
388
+ if (statutory_number) {
389
+ demographics.statutory_number = statutory_number;
390
+ }
391
+ if (id_number) {
392
+ id_details.number = id_number;
393
+ }
394
+ if (id_issuing_country_code) {
395
+ id_details.issuing_country_code = id_issuing_country_code;
396
+ }
397
+ if (id_expiration_date) {
398
+ id_details.expiration_date = id_expiration_date;
399
+ }
400
+ if (Object.keys(id_details).length > 0) {
401
+ demographics.id_details = id_details;
402
+ }
403
+ if (passport_number) {
404
+ passport.number = passport_number;
405
+ }
406
+ if (passport_issuing_country_code) {
407
+ passport.issuing_country_code = passport_issuing_country_code;
408
+ }
409
+ if (passport_expiration_date) {
410
+ passport.expiration_date = passport_expiration_date;
411
+ }
412
+ if (Object.keys(passport).length > 0) {
413
+ demographics.passport = passport;
414
+ }
415
+
416
+ body.demographics = demographics;
417
+ let id = httpUtil.getSession().sub;
418
+ let response = await httpUtil.put({
419
+ resourcePath: "/v2/contacts/" + id,
420
+ body: body
421
+ });
422
+ return createCommonResult(response);
423
+ } catch (e) {
424
+ logger.error("Exception updateContact:", e);
425
+ return createResult(ErrorCodes.UNKNOWN, e);
426
+ }
427
+ }
428
+
429
+ async function setMarketingPreferences({
430
+ sms_opt_out,
431
+ email_opt_out,
432
+ contact_id,
433
+ access_token
434
+ } = {}) {
435
+ try {
436
+ let id = contact_id ? contact_id : httpUtil.getSession().sub;
437
+ let response = await httpUtil.put({
438
+ resourcePath: "/v2/contacts/" + id,
439
+ body: {
440
+ sms_opt_out,
441
+ email_opt_out
442
+ },
443
+ withAccessToken: true,
444
+ accessToken: access_token
445
+ });
446
+ return createCommonResult(response);
447
+ } catch (e) {
448
+ logger.error("Exception setMarketingPreferences:", e);
449
+ return createResult(ErrorCodes.UNKNOWN, e);
450
+ }
451
+ }
452
+
453
+ async function submitReferralCode({
454
+ referral_code,
455
+ } = {}) {
456
+ try {
457
+ let id = httpUtil.getSession().sub;
458
+ let response = await httpUtil.post({
459
+ resourcePath: "/v2/contacts/" + id + "/referral_code",
460
+ body: {
461
+ referral_code,
462
+ },
463
+ });
464
+ return createCommonResult(response);
465
+ } catch (e) {
466
+ logger.error("Exception submitReferralCode:", e);
467
+ return createResult(ErrorCodes.UNKNOWN, e);
468
+ }
469
+ }
470
+
471
+ async function unregisterContact() {
472
+ try {
473
+ let id = httpUtil.getSession().sub;
474
+ let response = await httpUtil.sendDelete({
475
+ resourcePath: "/v2/contacts/" + id
476
+ });
477
+ return createCommonResult(response);
478
+ } catch (e) {
479
+ logger.error("Exception unregisterContact:", e);
480
+ return createResult(ErrorCodes.UNKNOWN, e);
481
+ }
482
+ }
483
+
484
+ async function signOutContact({
485
+ redirect_url,
486
+ } = {}) {
487
+ try {
488
+ let id = httpUtil.getSession().sub;
489
+ let response = await httpUtil.post({
490
+ resourcePath: "/v2/contacts/" + id + "/sign_out",
491
+ body:{
492
+ redirect_url
493
+ }
494
+ });
495
+ return createCommonResult(response);
496
+ } catch (e) {
497
+ logger.error("Exception signOutContact:", e);
498
+ return createResult(ErrorCodes.UNKNOWN, e);
499
+ }
500
+ }
501
+
502
+
503
+ async function getContactPurchases({
504
+ date,
505
+ date_gt,
506
+ date_gte,
507
+ date_lt,
508
+ date_lte,
509
+ include_total,
510
+ is_ad_hoc_returned,
511
+ page,
512
+ size = 20,
513
+ sort,
514
+ order,
515
+ }={}) {
516
+ try {
517
+ let id = httpUtil.getSession().sub;
518
+ let response = await httpUtil.get({
519
+ resourcePath: "/v2/contacts/" + id +"/purchases/",
520
+ queryParams:{
521
+ date,
522
+ "date[gt]": date_gt,
523
+ "date[gte]": date_gte,
524
+ "date[lt]": date_lt,
525
+ "date[lte]": date_lte,
526
+ include_total,
527
+ is_ad_hoc_returned,
528
+ page,
529
+ size,
530
+ sort,
531
+ order,
532
+ }
533
+ });
534
+ return createCommonResult(response);
535
+ } catch (e) {
536
+ logger.error("Exception getContactPurchases:", e);
537
+ return createResult(ErrorCodes.UNKNOWN, e);
538
+ }
539
+ }
540
+
541
+ async function getPurchase(purchaseId) {
542
+ try {
543
+ let response = await httpUtil.get({
544
+ resourcePath: "/v2/purchases/" + purchaseId,
545
+ });
546
+ return createCommonResult(response);
547
+ } catch (e) {
548
+ logger.error("Exception getPurchase:", e);
549
+ return createResult(ErrorCodes.UNKNOWN, e);
550
+ }
551
+ }
552
+
553
+ async function requestOTPSpend({
554
+ intent = 'SPEND',
555
+ spend_amount
556
+ }={}) {
557
+ try {
558
+ let id = httpUtil.getSession().sub;
559
+ let response = await httpUtil.post({
560
+ resourcePath: "/v2/contacts/" + id + "/token",
561
+ body:{
562
+ intent,
563
+ spend_amount
564
+ },
565
+ withAccessToken: true,
566
+ });
567
+ return createCommonResult(response);
568
+ } catch (e) {
569
+ logger.error("Exception requestOTPSpend:", e);
570
+ return createResult(ErrorCodes.UNKNOWN, e);
571
+ }
572
+ }
573
+
574
+ async function getOTPSpend({
575
+ intent = 'SPEND',
576
+ community_id
577
+ } = {}) {
578
+ try {
579
+ let id = httpUtil.getSession().sub;
580
+ let response = await httpUtil.get({
581
+ resourcePath: "/v2/contacts/" + id + "/tokens",
582
+ queryParams: {
583
+ intent,
584
+ community_id
585
+ },
586
+ withAccessToken: true,
587
+ });
588
+ //check return code here instead of put as there would be different intepretation for different API
589
+ return createCommonResult(response);
590
+ } catch (e) {
591
+ logger.error("Exception getOTPSpend:", e);
592
+ return createResult(ErrorCodes.UNKNOWN, e);
593
+ }
594
+ }
595
+
596
+ async function onChangePassword({
597
+ password
598
+ }={}) {
599
+ try {
600
+ let id = httpUtil.getSession().sub;
601
+ let response = await httpUtil.post({
602
+ resourcePath: "/v2/contacts/" + id + "/change_password",
603
+ body:{
604
+ password
605
+ },
606
+ withAccessToken: true,
607
+ });
608
+ return createCommonResult(response);
609
+ } catch (e) {
610
+ logger.error("Exception onChangePassword:", e);
611
+ return createResult(ErrorCodes.UNKNOWN, e);
612
+ }
613
+ }
614
+
615
+ async function getNameDayRules({
616
+ first_name
617
+ } = {}) {
618
+ try {
619
+ let id = httpUtil.getSession().sub;
620
+ let response = await httpUtil.get({
621
+ resourcePath: "/v2/name_day_rules",
622
+ queryParams: {
623
+ first_name
624
+ },
625
+ withAccessToken: true,
626
+ });
627
+ //check return code here instead of put as there would be different intepretation for different API
628
+ return createCommonResult(response);
629
+ } catch (e) {
630
+ logger.error("Exception getNameDayRules:", e);
631
+ return createResult(ErrorCodes.UNKNOWN, e);
632
+ }
633
+ }
634
+
635
+ async function addApplicationUsage(applicationID, platform) {
636
+ try {
637
+ let id = httpUtil.getSession().sub;
638
+ let response = await httpUtil.post({
639
+ resourcePath: "/v1/contacts/" + id + "/applications",
640
+ body: {
641
+ application_id: applicationID,
642
+ platform: platform
643
+ },
644
+ withAccessToken: true,
645
+ });
646
+ return createCommonResult(response);
647
+ } catch (e) {
648
+ logger.error("Exception addApplicationUsage:", e);
649
+ return createResult(ErrorCodes.UNKNOWN, e);
650
+ }
651
+ }
652
+
653
+ async function requestContactToken({
654
+ intent = 'SPEND',
655
+ spend_amount,
656
+ community_id
657
+ }) {
658
+ try {
659
+ let id = httpUtil.getSession().sub;
660
+ let response = await httpUtil.post({
661
+ resourcePath: "/v2/contacts/" + id + "/tokens",
662
+ body: {
663
+ intent,
664
+ spend_amount,
665
+ community_id
666
+ },
667
+ withAccessToken: true,
668
+ });
669
+ //check return code here instead of put as there would be different intepretation for different API
670
+ if (response.code == "OK")
671
+ return createResult(ErrorCodes.OK, response.data);
672
+ else {
673
+ return createCommonResult(response);
674
+ }
675
+ } catch (e) {
676
+ logger.error("Exception requestContactToken:", e);
677
+ return createResult(ErrorCodes.UNKNOWN, e);
678
+ }
679
+ }
680
+
681
+
682
+ async function getContactTokens({
683
+ intent = 'SPEND',
684
+ community_id
685
+ } = {}) {
686
+ try {
687
+ let id = httpUtil.getSession().sub;
688
+ let response = await httpUtil.get({
689
+ resourcePath: "/v2/contacts/" + id + "/tokens",
690
+ queryParams: {
691
+ intent,
692
+ community_id
693
+ },
694
+ withAccessToken: true,
695
+ });
696
+ //check return code here instead of put as there would be different intepretation for different API
697
+ return createCommonResult(response);
698
+ } catch (e) {
699
+ logger.error("Exception getContactTokens:", e);
700
+ return createResult(ErrorCodes.UNKNOWN, e);
701
+ }
702
+ }
703
+
704
+ async function addPNDeviceToken({
705
+ serial_number,
706
+ registration_token,
707
+ mac_address,
708
+ platform="MOBILE",
709
+ product_id,
710
+ application_id,
711
+ electronic_id
712
+ }={}) {
713
+ try {
714
+ let id = httpUtil.getSession().sub;
715
+ let response = await httpUtil.post({
716
+ resourcePath: "/v2/contacts/" + id + "/devices",
717
+ body: {
718
+ serial_number,
719
+ platform,
720
+ registration_token,
721
+ mac_address,
722
+ product_id,
723
+ application_id,
724
+ electronic_id
725
+ },
726
+ withAccessToken: true,
727
+ logOutIfSessionInvalid: false
728
+ });
729
+ return createCommonResult(response);
730
+ } catch (e) {
731
+ logger.error("Exception addDevice:", e);
732
+ return createResult(ErrorCodes.UNKNOWN, e);
733
+ }
734
+ }
735
+
736
+ async function deleteDeviceToken({
737
+ registration_token,
738
+ }={}) {
739
+ try {
740
+ let id = httpUtil.getSession().sub;
741
+ let response = await httpUtil.sendDelete({
742
+ resourcePath: "/v2/contacts/" + id + "/devices/"+registration_token,
743
+ withAccessToken: true,
744
+ logOutIfSessionInvalid: false
745
+ });
746
+ return createCommonResult(response);
747
+ } catch (e) {
748
+ logger.error("Exception addDevice:", e);
749
+ return createResult(ErrorCodes.UNKNOWN, e);
750
+ }
751
+ }
752
+
753
+
754
+ async function uploadProfileImage({
755
+ file,
756
+ disalbedContentType = false,
757
+ } = {}) {
758
+ try {
759
+ let id = httpUtil.getSession().sub;
760
+ let response = await httpUtil.uploadFile({
761
+ resourcePath: "/v2/contacts/" + id + "/image",
762
+ fileData: file,
763
+ withAccessToken: true,
764
+ method:"PUT",
765
+ keyParam:'image',
766
+ disalbedContentType: disalbedContentType,
767
+ });
768
+ return createCommonResult(response);
769
+ } catch (e) {
770
+ logger.error("Exception uploadProfileImage:", e);
771
+ return createResult(ErrorCodes.UNKNOWN, e);
772
+ }
773
+ }
774
+
775
+ async function deleteProfileImage() {
776
+ try {
777
+ let id = httpUtil.getSession().sub;
778
+ let response = await httpUtil.sendDelete({
779
+ resourcePath: "/v2/contacts/" + id + "/image",
780
+ withAccessToken: true,
781
+ });
782
+ return createCommonResult(response);
783
+ } catch (e) {
784
+ logger.error("Exception deleteProfileImage:", e);
785
+ return createResult(ErrorCodes.UNKNOWN, e);
786
+ }
787
+ }
788
+
789
+ async function getApplePass(org_id) {
790
+ try {
791
+ let id = httpUtil.getSession().sub;
792
+ if(!org_id){
793
+ org_id = httpUtil.getSession().current_organisation_id;
794
+ }
795
+ let url = httpUtil.getURI(false, "/v2/apple_pass/" + org_id + '/contacts/' + id);
796
+ return { code: 'OK', data: url }
797
+ } catch (e) {
798
+ logger.error("Exception getApplePass:", e);
799
+ return createResult(ErrorCodes.UNKNOWN, e);
800
+ }
801
+ }
802
+
803
+ async function getAndroidWalletPass(org_id) {
804
+ try {
805
+ let id = httpUtil.getSession().sub;
806
+ if(!org_id){
807
+ org_id = httpUtil.getSession().current_organisation_id;
808
+ }
809
+ let url = httpUtil.getURI(false, "/v2/android_wallet_pass/" + org_id + '/contacts/' + id);
810
+ return { code: 'OK', data: url }
811
+ } catch (e) {
812
+ logger.error("Exception getAndroidWalletPass:", e);
813
+ return createResult(ErrorCodes.UNKNOWN, e);
814
+ }
815
+ }
816
+
817
+ async function getGooglePass(org_id) {
818
+ try {
819
+ let id = httpUtil.getSession().sub;
820
+ if(!org_id){
821
+ org_id = httpUtil.getSession().current_organisation_id;
822
+ }
823
+ let url = httpUtil.getURI(false, "/v2/google_pass/" + org_id + '/contacts/' + id);
824
+ return { code: 'OK', data: url }
825
+ } catch (e) {
826
+ logger.error("Exception getGooglePass:", e);
827
+ return createResult(ErrorCodes.UNKNOWN, e);
828
+ }
829
+ }
830
+
831
+ async function exportStatements({
832
+ account_id,
833
+ from_month,
834
+ to_month,
835
+ from_year,
836
+ to_year,
837
+ format,
838
+ }) {
839
+ try {
840
+ let id = httpUtil.getSession().sub;
841
+ if (!account_id) {
842
+ let primeAccRes = await account.getPrimaryAccount();
843
+ if (primeAccRes.code != ErrorCodes.OK)
844
+ return primeAccRes;
845
+ if (!primeAccRes.data.content && primeAccRes.data.content.size() == 0)
846
+ return createResult(ErrorCodes.ACCOUNT_NOT_FOUND, response.error);
847
+ account_id = primeAccRes.data.content[0].id;
848
+ }
849
+ let response = await httpUtil.post({
850
+ resourcePath: "/v2/contacts/" + id + "/export_statement",
851
+ body: {
852
+ account_id,
853
+ from_month,
854
+ to_month,
855
+ from_year,
856
+ to_year,
857
+ format,
858
+ },
859
+ withAccessToken: true,
860
+ });
861
+ //check return code here instead of put as there would be different intepretation for different API
862
+ if (response.code == "OK")
863
+ return createResult(ErrorCodes.OK, response.data);
864
+ else {
865
+ return createCommonResult(response);
866
+ }
867
+ } catch (e) {
868
+ logger.error("Exception exportStatements:", e);
869
+ return createResult(ErrorCodes.UNKNOWN, e);
870
+ }
871
+ }
872
+
873
+ async function getContactDonationsOffer({
874
+ contact_id,
875
+ page = 1,
876
+ size = 20,
877
+ include_opt_out,
878
+ include_total,
879
+ order,
880
+ sort,
881
+ donation_offer_id
882
+ }) {
883
+ try {
884
+ let id = httpUtil.getSession().sub;
885
+ if (!contact_id) {
886
+ contact_id = id;
887
+ }
888
+ let response = await httpUtil.get({
889
+ resourcePath: "/v2/contacts/" + id + "/donations",
890
+ withAccessToken: true,
891
+ queryParams: {
892
+ page,
893
+ size,
894
+ include_opt_out,
895
+ include_total,
896
+ order,
897
+ sort,
898
+ donation_offer_id
899
+ }
900
+ });
901
+ //check return code here instead of put as there would be different intepretation for different API
902
+ if (response.code == "OK")
903
+ return createResult(ErrorCodes.OK, response.data);
904
+ else {
905
+ return createCommonResult(response);
906
+ }
907
+ } catch (e) {
908
+ logger.error("Exception getContactDonationsOffer:", e);
909
+ return createResult(ErrorCodes.UNKNOWN, e);
910
+ }
911
+ }
912
+
913
+ async function getDonationHistory({
914
+ contact_id,
915
+ include_total,
916
+ order,
917
+ organisation_id,
918
+ page = 1,
919
+ size = 20,
920
+ sort,
921
+ }) {
922
+ try {
923
+ let id = httpUtil.getSession().sub;
924
+ if (!contact_id) {
925
+ contact_id = id;
926
+ }
927
+ let response = await httpUtil.get({
928
+ resourcePath: "/v2/contacts/" + id + "/donations/history",
929
+ withAccessToken: true,
930
+ queryParams: {
931
+ include_total,
932
+ order,
933
+ organisation_id,
934
+ page,
935
+ size,
936
+ sort,
937
+ }
938
+ });
939
+ //check return code here instead of put as there would be different intepretation for different API
940
+ if (response.code == "OK")
941
+ return createResult(ErrorCodes.OK, response.data);
942
+ else {
943
+ return createCommonResult(response);
944
+ }
945
+ } catch (e) {
946
+ logger.error("Exception getDonationHistory:", e);
947
+ return createResult(ErrorCodes.UNKNOWN, e);
948
+ }
949
+ }
950
+
951
+ async function donationActions({
952
+ contact_id,
953
+ donation_offer_id,
954
+ multiplier,
955
+ amount
956
+ }) {
957
+ try {
958
+ let id = httpUtil.getSession().sub;
959
+ if (!contact_id) {
960
+ contact_id = id;
961
+ }
962
+ let response = await httpUtil.post({
963
+ resourcePath: "/v2/contacts/" + id + "/donations",
964
+ withAccessToken: true,
965
+ body: {
966
+ donation_offer_id,
967
+ multiplier,
968
+ amount
969
+ }
970
+ });
971
+ //check return code here instead of put as there would be different intepretation for different API
972
+ if (response.code == "OK")
973
+ return createResult(ErrorCodes.OK, response.data);
974
+ else {
975
+ return createCommonResult(response);
976
+ }
977
+ } catch (e) {
978
+ logger.error("Exception donationActions:", e);
979
+ return createResult(ErrorCodes.UNKNOWN, e);
980
+ }
981
+ }
982
+
983
+ async function updateDonation({
984
+ contact_id,
985
+ donation_id,
986
+ donation_offer_id,
987
+ multiplier,
988
+ amount
989
+ }) {
990
+ try {
991
+ let id = httpUtil.getSession().sub;
992
+ if (!contact_id) {
993
+ contact_id = id;
994
+ }
995
+ let response = await httpUtil.put({
996
+ resourcePath: "/v2/contacts/" + id + "/donations/" + donation_id,
997
+ withAccessToken: true,
998
+ body: {
999
+ donation_offer_id,
1000
+ multiplier,
1001
+ amount
1002
+ }
1003
+ });
1004
+ //check return code here instead of put as there would be different intepretation for different API
1005
+ if (response.code == "OK")
1006
+ return createResult(ErrorCodes.OK, response.data);
1007
+ else {
1008
+ return createCommonResult(response);
1009
+ }
1010
+ } catch (e) {
1011
+ logger.error("Exception updateDonation:", e);
1012
+ return createResult(ErrorCodes.UNKNOWN, e);
1013
+ }
1014
+ }
1015
+
1016
+ async function removeDonation({
1017
+ contact_id,
1018
+ donation_id,
1019
+ }) {
1020
+ try {
1021
+ let id = httpUtil.getSession().sub;
1022
+ if (!contact_id) {
1023
+ contact_id = id;
1024
+ }
1025
+ let response = await httpUtil.sendDelete({
1026
+ resourcePath: "/v2/contacts/" + id + "/donations/" + donation_id,
1027
+ withAccessToken: true,
1028
+ });
1029
+ //check return code here instead of put as there would be different intepretation for different API
1030
+ if (response.code == "OK")
1031
+ return createResult(ErrorCodes.OK, response.data);
1032
+ else {
1033
+ return createCommonResult(response);
1034
+ }
1035
+ } catch (e) {
1036
+ logger.error("Exception removeDonation:", e);
1037
+ return createResult(ErrorCodes.UNKNOWN, e);
1038
+ }
1039
+ }
1040
+
1041
+ async function getDonationOffers({
1042
+ page = 1,
1043
+ size = 20,
1044
+ organisations,
1045
+ order,
1046
+ include_total,
1047
+ owner,
1048
+ search_value,
1049
+ sort,
1050
+ types,
1051
+ donated_to,
1052
+ donation_offer_id,
1053
+ donation_types,
1054
+ }) {
1055
+ try {
1056
+ let response = await httpUtil.get({
1057
+ resourcePath: "/v2/donation_offers",
1058
+ withAccessToken: true,
1059
+ queryParams: {
1060
+ page,
1061
+ size,
1062
+ organisations,
1063
+ order,
1064
+ include_total,
1065
+ owner,
1066
+ search_value,
1067
+ sort,
1068
+ types,
1069
+ donated_to,
1070
+ donation_offer_id,
1071
+ donation_types,
1072
+ }
1073
+ });
1074
+ //check return code here instead of put as there would be different intepretation for different API
1075
+ if (response.code == "OK")
1076
+ return createResult(ErrorCodes.OK, response.data);
1077
+ else {
1078
+ return createCommonResult(response);
1079
+ }
1080
+ } catch (e) {
1081
+ logger.error("Exception getDonationOffers:", e);
1082
+ return createResult(ErrorCodes.UNKNOWN, e);
1083
+ }
1084
+ }
1085
+
1086
+ async function getDonationOrganisations({
1087
+ include_creatives,
1088
+ type,
1089
+ }) {
1090
+ try {
1091
+ let response = await httpUtil.get({
1092
+ resourcePath: "/v2/donation_offers/organisations",
1093
+ withAccessToken: true,
1094
+ queryParams: {
1095
+ include_creatives,
1096
+ type,
1097
+ }
1098
+ });
1099
+ //check return code here instead of put as there would be different intepretation for different API
1100
+ if (response.code == "OK")
1101
+ return createResult(ErrorCodes.OK, response.data);
1102
+ else {
1103
+ return createCommonResult(response);
1104
+ }
1105
+ } catch (e) {
1106
+ logger.error("Exception getDonationOffers:", e);
1107
+ return createResult(ErrorCodes.UNKNOWN, e);
1108
+ }
1109
+ }