@app-connect/core 1.7.15 → 1.7.17

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.
@@ -29,6 +29,7 @@ const logHandler = require('../../handlers/log');
29
29
  const { CallLogModel } = require('../../models/callLogModel');
30
30
  const { MessageLogModel } = require('../../models/messageLogModel');
31
31
  const { UserModel } = require('../../models/userModel');
32
+ const { AccountDataModel } = require('../../models/accountDataModel');
32
33
  const connectorRegistry = require('../../connector/registry');
33
34
  const oauth = require('../../lib/oauth');
34
35
  const { composeCallLog } = require('../../lib/callLogComposer');
@@ -40,12 +41,14 @@ describe('Log Handler', () => {
40
41
  await CallLogModel.sync({ force: true });
41
42
  await MessageLogModel.sync({ force: true });
42
43
  await UserModel.sync({ force: true });
44
+ await AccountDataModel.sync({ force: true });
43
45
  });
44
46
 
45
47
  afterEach(async () => {
46
48
  await CallLogModel.destroy({ where: {} });
47
49
  await MessageLogModel.destroy({ where: {} });
48
50
  await UserModel.destroy({ where: {} });
51
+ await AccountDataModel.destroy({ where: {} });
49
52
  jest.clearAllMocks();
50
53
  });
51
54
 
@@ -742,6 +745,228 @@ describe('Log Handler', () => {
742
745
  expect(mockConnector.createMessageLog).toHaveBeenCalledTimes(0);
743
746
  expect(mockConnector.updateMessageLog).toHaveBeenCalledTimes(1);
744
747
  });
748
+
749
+ test('should handle group SMS with contactId suffix for message IDs', async () => {
750
+ // Arrange - group SMS has multiple correspondents
751
+ await UserModel.create({
752
+ id: 'test-user-id',
753
+ platform: 'testCRM',
754
+ accessToken: 'test-token',
755
+ rcAccountId: 'rc-account-123',
756
+ platformAdditionalInfo: {}
757
+ });
758
+
759
+ const mockConnector = {
760
+ getAuthType: jest.fn().mockResolvedValue('apiKey'),
761
+ getBasicAuth: jest.fn().mockReturnValue('base64-encoded'),
762
+ createMessageLog: jest.fn().mockResolvedValue({
763
+ logId: 'msg-log-group-123',
764
+ returnMessage: { message: 'Message logged', messageType: 'success', ttl: 2000 }
765
+ }),
766
+ updateMessageLog: jest.fn()
767
+ };
768
+ connectorRegistry.getConnector.mockReturnValue(mockConnector);
769
+
770
+ const incomingData = {
771
+ logInfo: {
772
+ messages: [{ id: 'msg-group-1', subject: 'Group SMS', direction: 'Outbound', creationTime: new Date() }],
773
+ correspondents: [
774
+ { phoneNumber: '+1234567890' },
775
+ { phoneNumber: '+0987654321' }
776
+ ],
777
+ conversationId: 'conv-group-123',
778
+ conversationLogId: 'conv-log-group-123'
779
+ },
780
+ contactId: 'contact-456',
781
+ contactType: 'Contact',
782
+ contactName: 'Primary Contact',
783
+ additionalSubmission: {}
784
+ };
785
+
786
+ // Act
787
+ const result = await logHandler.createMessageLog({
788
+ platform: 'testCRM',
789
+ userId: 'test-user-id',
790
+ incomingData
791
+ });
792
+
793
+ // Assert
794
+ expect(result.successful).toBe(true);
795
+ expect(result.logIds).toContain('msg-group-1-contact-456');
796
+ const savedLog = await MessageLogModel.findOne({ where: { id: 'msg-group-1-contact-456' } });
797
+ expect(savedLog).not.toBeNull();
798
+ expect(savedLog.thirdPartyLogId).toBe('msg-log-group-123');
799
+ });
800
+
801
+ test('should pass correspondents to createMessageLog when group SMS has different contact names', async () => {
802
+ // Arrange - correspondent in cache with different name
803
+ await UserModel.create({
804
+ id: 'test-user-id',
805
+ platform: 'testCRM',
806
+ accessToken: 'test-token',
807
+ rcAccountId: 'rc-account-123',
808
+ platformAdditionalInfo: {}
809
+ });
810
+
811
+ await AccountDataModel.create({
812
+ rcAccountId: 'rc-account-123',
813
+ platformName: 'testCRM',
814
+ dataKey: 'contact-+0987654321',
815
+ data: [{ name: 'Other Contact', id: 'contact-789' }]
816
+ });
817
+
818
+ const mockConnector = {
819
+ getAuthType: jest.fn().mockResolvedValue('apiKey'),
820
+ getBasicAuth: jest.fn().mockReturnValue('base64-encoded'),
821
+ createMessageLog: jest.fn().mockResolvedValue({
822
+ logId: 'msg-log-correspondents',
823
+ returnMessage: { message: 'Message logged', messageType: 'success', ttl: 2000 }
824
+ }),
825
+ updateMessageLog: jest.fn()
826
+ };
827
+ connectorRegistry.getConnector.mockReturnValue(mockConnector);
828
+
829
+ const incomingData = {
830
+ logInfo: {
831
+ messages: [{ id: 'msg-correspondents', subject: 'Group SMS', direction: 'Outbound', creationTime: new Date() }],
832
+ correspondents: [
833
+ { phoneNumber: '+1234567890' },
834
+ { phoneNumber: '+0987654321' }
835
+ ],
836
+ conversationId: 'conv-correspondents',
837
+ conversationLogId: 'conv-log-correspondents'
838
+ },
839
+ contactId: 'contact-456',
840
+ contactType: 'Contact',
841
+ contactName: 'Primary Contact',
842
+ additionalSubmission: {}
843
+ };
844
+
845
+ // Act
846
+ await logHandler.createMessageLog({
847
+ platform: 'testCRM',
848
+ userId: 'test-user-id',
849
+ incomingData
850
+ });
851
+
852
+ // Assert - createMessageLog should receive correspondents with different name
853
+ expect(mockConnector.createMessageLog).toHaveBeenCalledWith(
854
+ expect.objectContaining({
855
+ correspondents: [[{ name: 'Other Contact', id: 'contact-789' }]]
856
+ })
857
+ );
858
+ });
859
+
860
+ test('should not add correspondent when name matches contactName in group SMS', async () => {
861
+ // Arrange - correspondent in cache with same name as contactName
862
+ await UserModel.create({
863
+ id: 'test-user-id',
864
+ platform: 'testCRM',
865
+ accessToken: 'test-token',
866
+ rcAccountId: 'rc-account-123',
867
+ platformAdditionalInfo: {}
868
+ });
869
+
870
+ await AccountDataModel.create({
871
+ rcAccountId: 'rc-account-123',
872
+ platformName: 'testCRM',
873
+ dataKey: 'contact-+0987654321',
874
+ data: [{ name: 'Primary Contact', id: 'contact-789' }]
875
+ });
876
+
877
+ const mockConnector = {
878
+ getAuthType: jest.fn().mockResolvedValue('apiKey'),
879
+ getBasicAuth: jest.fn().mockReturnValue('base64-encoded'),
880
+ createMessageLog: jest.fn().mockResolvedValue({
881
+ logId: 'msg-log-same-name',
882
+ returnMessage: { message: 'Message logged', messageType: 'success', ttl: 2000 }
883
+ }),
884
+ updateMessageLog: jest.fn()
885
+ };
886
+ connectorRegistry.getConnector.mockReturnValue(mockConnector);
887
+
888
+ const incomingData = {
889
+ logInfo: {
890
+ messages: [{ id: 'msg-same-name', subject: 'Group SMS', direction: 'Outbound', creationTime: new Date() }],
891
+ correspondents: [
892
+ { phoneNumber: '+1234567890' },
893
+ { phoneNumber: '+0987654321' }
894
+ ],
895
+ conversationId: 'conv-same-name',
896
+ conversationLogId: 'conv-log-same-name'
897
+ },
898
+ contactId: 'contact-456',
899
+ contactType: 'Contact',
900
+ contactName: 'Primary Contact',
901
+ additionalSubmission: {}
902
+ };
903
+
904
+ // Act
905
+ await logHandler.createMessageLog({
906
+ platform: 'testCRM',
907
+ userId: 'test-user-id',
908
+ incomingData
909
+ });
910
+
911
+ // Assert - correspondents should be empty when names match
912
+ expect(mockConnector.createMessageLog).toHaveBeenCalledWith(
913
+ expect.objectContaining({
914
+ correspondents: []
915
+ })
916
+ );
917
+ });
918
+
919
+ test('should use suffixed conversationLogId and conversationId for group SMS', async () => {
920
+ // Arrange
921
+ await UserModel.create({
922
+ id: 'test-user-id',
923
+ platform: 'testCRM',
924
+ accessToken: 'test-token',
925
+ rcAccountId: 'rc-account-123',
926
+ platformAdditionalInfo: {}
927
+ });
928
+
929
+ const mockConnector = {
930
+ getAuthType: jest.fn().mockResolvedValue('apiKey'),
931
+ getBasicAuth: jest.fn().mockReturnValue('base64-encoded'),
932
+ createMessageLog: jest.fn().mockResolvedValue({
933
+ logId: 'msg-log-suffix',
934
+ returnMessage: { message: 'Message logged', messageType: 'success', ttl: 2000 }
935
+ }),
936
+ updateMessageLog: jest.fn()
937
+ };
938
+ connectorRegistry.getConnector.mockReturnValue(mockConnector);
939
+
940
+ const incomingData = {
941
+ logInfo: {
942
+ messages: [{ id: 'msg-suffix', subject: 'Group SMS', direction: 'Outbound', creationTime: new Date() }],
943
+ correspondents: [
944
+ { phoneNumber: '+1234567890' },
945
+ { phoneNumber: '+0987654321' }
946
+ ],
947
+ conversationId: 'conv-original',
948
+ conversationLogId: 'conv-log-original'
949
+ },
950
+ contactId: 'contact-999',
951
+ contactType: 'Contact',
952
+ contactName: 'Test Contact',
953
+ additionalSubmission: {}
954
+ };
955
+
956
+ // Act
957
+ const result = await logHandler.createMessageLog({
958
+ platform: 'testCRM',
959
+ userId: 'test-user-id',
960
+ incomingData
961
+ });
962
+
963
+ // Assert - message log saved with suffixed conversationLogId
964
+ expect(result.successful).toBe(true);
965
+ const savedLog = await MessageLogModel.findOne({ where: { id: 'msg-suffix-contact-999' } });
966
+ expect(savedLog).not.toBeNull();
967
+ expect(savedLog.conversationLogId).toBe('conv-log-original-contact-999');
968
+ expect(savedLog.conversationId).toBe('conv-original-contact-999');
969
+ });
745
970
  });
746
971
 
747
972
  describe('saveNoteCache', () => {
package/test/setup.js CHANGED
@@ -1,176 +1,176 @@
1
- // Test setup for @app-connect/core package
2
- const path = require('path');
3
- require('dotenv').config({ path: path.resolve(__dirname, '../.env.test') });
4
-
5
- // Set test timeout
6
- jest.setTimeout(30000);
7
-
8
- // Mock console methods to reduce noise in tests
9
- global.console = {
10
- ...console,
11
- log: jest.fn(),
12
- debug: jest.fn(),
13
- info: jest.fn(),
14
- warn: jest.fn(),
15
- error: jest.fn(),
16
- };
17
-
18
- // Setup database models for testing
19
- beforeAll(async () => {
20
- try {
21
- // Set up test database URL if not provided
22
- if (!process.env.DATABASE_URL) {
23
- process.env.DATABASE_URL = 'sqlite::memory:';
24
- }
25
-
26
- // Import models
27
- const { CallLogModel } = require('../models/callLogModel');
28
- const { MessageLogModel } = require('../models/messageLogModel');
29
- const { UserModel } = require('../models/userModel');
30
- const { CacheModel } = require('../models/cacheModel');
31
- const { AdminConfigModel } = require('../models/adminConfigModel');
32
-
33
- // Sync database models
34
- await CallLogModel.sync({ force: true });
35
- await MessageLogModel.sync({ force: true });
36
- await UserModel.sync({ force: true });
37
- await CacheModel.sync({ force: true });
38
- await AdminConfigModel.sync({ force: true });
39
-
40
- console.log('Database models synced for testing');
41
- } catch (error) {
42
- console.error('Error setting up test database:', error);
43
- // Don't fail the setup, some tests might not need database
44
- }
45
- });
46
-
47
- // Clean up after all tests
48
- afterAll(async () => {
49
- try {
50
- // Close database connections
51
- const { sequelize } = require('../models/sequelize');
52
- if (sequelize) {
53
- await sequelize.close();
54
- }
55
- } catch (error) {
56
- console.error('Error closing database connection:', error);
57
- }
58
- });
59
-
60
- // Global test utilities
61
- global.testUtils = {
62
- // Helper to create mock user
63
- createMockUser: (overrides = {}) => ({
64
- id: 'test-user-id',
65
- platform: 'testCRM',
66
- accessToken: 'test-access-token',
67
- refreshToken: 'test-refresh-token',
68
- tokenExpiry: new Date(Date.now() + 3600000), // 1 hour from now
69
- platformUserInfo: {
70
- id: 'test-platform-user-id',
71
- name: 'Test User',
72
- timezoneName: 'America/Los_Angeles',
73
- timezoneOffset: 0,
74
- platformAdditionalInfo: {}
75
- },
76
- ...overrides
77
- }),
78
-
79
- // Helper to create mock call log
80
- createMockCallLog: (overrides = {}) => ({
81
- id: 'test-call-log-id',
82
- userId: 'test-user-id',
83
- platform: 'testCRM',
84
- thirdPartyLogId: 'test-third-party-id',
85
- contactId: 'test-contact-id',
86
- contactType: 'Contact',
87
- phoneNumber: '+1234567890',
88
- callDirection: 'Inbound',
89
- callResult: 'Answered',
90
- callDuration: 120,
91
- callStartTime: new Date(),
92
- callEndTime: new Date(Date.now() + 120000),
93
- recordingLink: 'https://example.com/recording.mp3',
94
- subject: 'Test Call',
95
- note: 'Test call note',
96
- ...overrides
97
- }),
98
-
99
- // Helper to create mock contact
100
- createMockContact: (overrides = {}) => ({
101
- id: 'test-contact-id',
102
- name: 'Test Contact',
103
- type: 'Contact',
104
- phone: '+1234567890',
105
- additionalInfo: null,
106
- ...overrides
107
- }),
108
-
109
- // Helper to reset connector registry
110
- resetConnectorRegistry: () => {
111
- const connectorRegistry = require('../connector/registry');
112
- connectorRegistry.connectors.clear();
113
- connectorRegistry.manifests.clear();
114
- connectorRegistry.platformInterfaces.clear();
115
- connectorRegistry.releaseNotes = {};
116
- },
117
-
118
- // Helper to create mock connector
119
- createMockConnector: (overrides = {}) => ({
120
- getAuthType: jest.fn().mockReturnValue('apiKey'),
121
- getUserInfo: jest.fn().mockResolvedValue({
122
- successful: true,
123
- platformUserInfo: {
124
- id: 'test-user-id',
125
- name: 'Test User',
126
- timezoneName: 'America/Los_Angeles',
127
- timezoneOffset: 0,
128
- platformAdditionalInfo: {}
129
- }
130
- }),
131
- createCallLog: jest.fn().mockResolvedValue({
132
- logId: 'test-log-id',
133
- returnMessage: {
134
- message: 'Call logged successfully',
135
- messageType: 'success',
136
- ttl: 2000
137
- }
138
- }),
139
- updateCallLog: jest.fn().mockResolvedValue({
140
- updatedNote: 'Call log updated',
141
- returnMessage: {
142
- message: 'Call log updated successfully',
143
- messageType: 'success',
144
- ttl: 2000
145
- }
146
- }),
147
- unAuthorize: jest.fn().mockResolvedValue({
148
- returnMessage: {
149
- messageType: 'success',
150
- message: 'Logged out successfully',
151
- ttl: 1000
152
- }
153
- }),
154
- findContact: jest.fn().mockResolvedValue([
155
- {
156
- id: 'test-contact-id',
157
- name: 'Test Contact',
158
- type: 'Contact',
159
- phone: '+1234567890',
160
- additionalInfo: null
161
- }
162
- ]),
163
- createContact: jest.fn().mockResolvedValue({
164
- contactInfo: {
165
- id: 'new-contact-id',
166
- name: 'New Contact'
167
- },
168
- returnMessage: {
169
- message: 'Contact created successfully',
170
- messageType: 'success',
171
- ttl: 2000
172
- }
173
- }),
174
- ...overrides
175
- })
176
- };
1
+ // Test setup for @app-connect/core package
2
+ const path = require('path');
3
+ require('dotenv').config({ path: path.resolve(__dirname, '../.env.test') });
4
+
5
+ // Set test timeout
6
+ jest.setTimeout(30000);
7
+
8
+ // Mock console methods to reduce noise in tests
9
+ global.console = {
10
+ ...console,
11
+ log: jest.fn(),
12
+ debug: jest.fn(),
13
+ info: jest.fn(),
14
+ warn: jest.fn(),
15
+ error: jest.fn(),
16
+ };
17
+
18
+ // Setup database models for testing
19
+ beforeAll(async () => {
20
+ try {
21
+ // Set up test database URL if not provided
22
+ if (!process.env.DATABASE_URL) {
23
+ process.env.DATABASE_URL = 'sqlite::memory:';
24
+ }
25
+
26
+ // Import models
27
+ const { CallLogModel } = require('../models/callLogModel');
28
+ const { MessageLogModel } = require('../models/messageLogModel');
29
+ const { UserModel } = require('../models/userModel');
30
+ const { CacheModel } = require('../models/cacheModel');
31
+ const { AdminConfigModel } = require('../models/adminConfigModel');
32
+
33
+ // Sync database models
34
+ await CallLogModel.sync({ force: true });
35
+ await MessageLogModel.sync({ force: true });
36
+ await UserModel.sync({ force: true });
37
+ await CacheModel.sync({ force: true });
38
+ await AdminConfigModel.sync({ force: true });
39
+
40
+ console.log('Database models synced for testing');
41
+ } catch (error) {
42
+ console.error('Error setting up test database:', error);
43
+ // Don't fail the setup, some tests might not need database
44
+ }
45
+ });
46
+
47
+ // Clean up after all tests
48
+ afterAll(async () => {
49
+ try {
50
+ // Close database connections
51
+ const { sequelize } = require('../models/sequelize');
52
+ if (sequelize) {
53
+ await sequelize.close();
54
+ }
55
+ } catch (error) {
56
+ console.error('Error closing database connection:', error);
57
+ }
58
+ });
59
+
60
+ // Global test utilities
61
+ global.testUtils = {
62
+ // Helper to create mock user
63
+ createMockUser: (overrides = {}) => ({
64
+ id: 'test-user-id',
65
+ platform: 'testCRM',
66
+ accessToken: 'test-access-token',
67
+ refreshToken: 'test-refresh-token',
68
+ tokenExpiry: new Date(Date.now() + 3600000), // 1 hour from now
69
+ platformUserInfo: {
70
+ id: 'test-platform-user-id',
71
+ name: 'Test User',
72
+ timezoneName: 'America/Los_Angeles',
73
+ timezoneOffset: 0,
74
+ platformAdditionalInfo: {}
75
+ },
76
+ ...overrides
77
+ }),
78
+
79
+ // Helper to create mock call log
80
+ createMockCallLog: (overrides = {}) => ({
81
+ id: 'test-call-log-id',
82
+ userId: 'test-user-id',
83
+ platform: 'testCRM',
84
+ thirdPartyLogId: 'test-third-party-id',
85
+ contactId: 'test-contact-id',
86
+ contactType: 'Contact',
87
+ phoneNumber: '+1234567890',
88
+ callDirection: 'Inbound',
89
+ callResult: 'Answered',
90
+ callDuration: 120,
91
+ callStartTime: new Date(),
92
+ callEndTime: new Date(Date.now() + 120000),
93
+ recordingLink: 'https://example.com/recording.mp3',
94
+ subject: 'Test Call',
95
+ note: 'Test call note',
96
+ ...overrides
97
+ }),
98
+
99
+ // Helper to create mock contact
100
+ createMockContact: (overrides = {}) => ({
101
+ id: 'test-contact-id',
102
+ name: 'Test Contact',
103
+ type: 'Contact',
104
+ phone: '+1234567890',
105
+ additionalInfo: null,
106
+ ...overrides
107
+ }),
108
+
109
+ // Helper to reset connector registry
110
+ resetConnectorRegistry: () => {
111
+ const connectorRegistry = require('../connector/registry');
112
+ connectorRegistry.connectors.clear();
113
+ connectorRegistry.manifests.clear();
114
+ connectorRegistry.platformInterfaces.clear();
115
+ connectorRegistry.releaseNotes = {};
116
+ },
117
+
118
+ // Helper to create mock connector
119
+ createMockConnector: (overrides = {}) => ({
120
+ getAuthType: jest.fn().mockReturnValue('apiKey'),
121
+ getUserInfo: jest.fn().mockResolvedValue({
122
+ successful: true,
123
+ platformUserInfo: {
124
+ id: 'test-user-id',
125
+ name: 'Test User',
126
+ timezoneName: 'America/Los_Angeles',
127
+ timezoneOffset: 0,
128
+ platformAdditionalInfo: {}
129
+ }
130
+ }),
131
+ createCallLog: jest.fn().mockResolvedValue({
132
+ logId: 'test-log-id',
133
+ returnMessage: {
134
+ message: 'Call logged successfully',
135
+ messageType: 'success',
136
+ ttl: 2000
137
+ }
138
+ }),
139
+ updateCallLog: jest.fn().mockResolvedValue({
140
+ updatedNote: 'Call log updated',
141
+ returnMessage: {
142
+ message: 'Call log updated successfully',
143
+ messageType: 'success',
144
+ ttl: 2000
145
+ }
146
+ }),
147
+ unAuthorize: jest.fn().mockResolvedValue({
148
+ returnMessage: {
149
+ messageType: 'success',
150
+ message: 'Logged out successfully',
151
+ ttl: 1000
152
+ }
153
+ }),
154
+ findContact: jest.fn().mockResolvedValue([
155
+ {
156
+ id: 'test-contact-id',
157
+ name: 'Test Contact',
158
+ type: 'Contact',
159
+ phone: '+1234567890',
160
+ additionalInfo: null
161
+ }
162
+ ]),
163
+ createContact: jest.fn().mockResolvedValue({
164
+ contactInfo: {
165
+ id: 'new-contact-id',
166
+ name: 'New Contact'
167
+ },
168
+ returnMessage: {
169
+ message: 'Contact created successfully',
170
+ messageType: 'success',
171
+ ttl: 2000
172
+ }
173
+ }),
174
+ ...overrides
175
+ })
176
+ };