@app-connect/core 1.7.34 → 1.7.36
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/connector/mock.js +9 -4
- package/connector/proxy/engine.js +3 -2
- package/connector/proxy/index.js +2 -2
- package/docs/architecture.md +1 -0
- package/docs/handlers.md +1 -1
- package/docs/models.md +2 -0
- package/docs/routes.md +1 -1
- package/handlers/disposition.js +3 -2
- package/handlers/log.js +18 -6
- package/handlers/plugin.js +13 -6
- package/index.js +22 -11
- package/lib/callLogLookup.js +82 -9
- package/lib/migrateCallLogsSchema.js +128 -7
- package/models/callLogModel.js +6 -0
- package/package.json +1 -1
- package/releaseNotes.json +44 -0
- package/test/connector/developerPortal.test.js +166 -0
- package/test/connector/mock.test.js +131 -0
- package/test/connector/proxy/engine.test.js +85 -0
- package/test/connector/proxy/index.test.js +246 -0
- package/test/connector/proxy/sample.json +1 -0
- package/test/handlers/admin.test.js +344 -0
- package/test/handlers/appointment.test.js +260 -0
- package/test/handlers/auth.test.js +6 -2
- package/test/handlers/calldown.test.js +310 -0
- package/test/handlers/disposition.test.js +396 -0
- package/test/handlers/log.test.js +327 -3
- package/test/handlers/managedOAuth.test.js +262 -0
- package/test/handlers/plugin.test.js +305 -0
- package/test/handlers/user.test.js +381 -0
- package/test/index.test.js +166 -1
- package/test/lib/analytics.test.js +146 -0
- package/test/lib/authSession.test.js +173 -0
- package/test/lib/encode.test.js +59 -0
- package/test/lib/errorHandler.test.js +246 -0
- package/test/lib/generalErrorMessage.test.js +82 -0
- package/test/lib/s3ErrorLogReport.test.js +187 -0
- package/test/mcp/mcpHandlerMore.test.js +384 -0
- package/test/mcp/tools/appointmentTools.test.js +362 -0
- package/test/models/callDownListModel.test.js +125 -0
- package/test/models/dynamo/lockSchema.test.js +37 -0
- package/test/models/dynamo/noteCacheSchema.test.js +45 -0
- package/test/models/llmSessionModel.test.js +91 -0
- package/test/models/models.test.js +92 -0
- package/test/routes/calldownRoutes.test.js +224 -0
- package/test/routes/coreRouterBroadRoutes.test.js +855 -0
- package/test/routes/dispositionRoutes.test.js +192 -0
- package/test/routes/managedAuthRoutes.test.js +151 -0
- package/test/routes/pluginRoutes.test.js +262 -0
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
jest.mock('../../models/userModel', () => ({
|
|
2
|
+
UserModel: {
|
|
3
|
+
findByPk: jest.fn()
|
|
4
|
+
}
|
|
5
|
+
}));
|
|
6
|
+
jest.mock('../../connector/registry', () => ({
|
|
7
|
+
getConnector: jest.fn()
|
|
8
|
+
}));
|
|
9
|
+
jest.mock('../../lib/oauth', () => ({
|
|
10
|
+
getOAuthApp: jest.fn(() => ({ app: true })),
|
|
11
|
+
checkAndRefreshAccessToken: jest.fn()
|
|
12
|
+
}));
|
|
13
|
+
jest.mock('../../models/dynamo/connectorSchema', () => ({
|
|
14
|
+
Connector: {
|
|
15
|
+
getProxyConfig: jest.fn()
|
|
16
|
+
}
|
|
17
|
+
}));
|
|
18
|
+
|
|
19
|
+
const appointmentHandler = require('../../handlers/appointment');
|
|
20
|
+
const { UserModel } = require('../../models/userModel');
|
|
21
|
+
const connectorRegistry = require('../../connector/registry');
|
|
22
|
+
const oauth = require('../../lib/oauth');
|
|
23
|
+
const { Connector } = require('../../models/dynamo/connectorSchema');
|
|
24
|
+
|
|
25
|
+
describe('Appointment Handler', () => {
|
|
26
|
+
const baseUser = {
|
|
27
|
+
id: 'user-1',
|
|
28
|
+
hostname: 'crm.example.com',
|
|
29
|
+
accessToken: 'access-token',
|
|
30
|
+
platformAdditionalInfo: {}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
beforeEach(() => {
|
|
34
|
+
jest.clearAllMocks();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
function mockApiKeyConnector(overrides = {}) {
|
|
38
|
+
const connector = {
|
|
39
|
+
getAuthType: jest.fn().mockResolvedValue('apiKey'),
|
|
40
|
+
getBasicAuth: jest.fn(() => 'encoded-key'),
|
|
41
|
+
listAppointments: jest.fn().mockResolvedValue({ appointments: [{ id: 'appt-1' }] }),
|
|
42
|
+
createAppointment: jest.fn().mockResolvedValue({ appointmentId: 'appt-2', appointment: { id: 'appt-2' } }),
|
|
43
|
+
updateAppointment: jest.fn().mockResolvedValue({ appointment: { id: 'appt-3' } }),
|
|
44
|
+
refreshAppointment: jest.fn().mockResolvedValue({ appointment: { id: 'appt-4' } }),
|
|
45
|
+
confirmAppointment: jest.fn().mockResolvedValue({ appointment: { id: 'appt-5' } }),
|
|
46
|
+
cancelAppointment: jest.fn().mockResolvedValue({ appointment: { id: 'appt-6' } }),
|
|
47
|
+
...overrides
|
|
48
|
+
};
|
|
49
|
+
connectorRegistry.getConnector.mockReturnValue(connector);
|
|
50
|
+
return connector;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
test('returns database warning when user lookup fails', async () => {
|
|
54
|
+
UserModel.findByPk.mockRejectedValueOnce(new Error('db unavailable'));
|
|
55
|
+
|
|
56
|
+
const result = await appointmentHandler.listAppointments({
|
|
57
|
+
platform: 'testCRM',
|
|
58
|
+
userId: 'user-1'
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
expect(result.successful).toBe(false);
|
|
62
|
+
expect(result.returnMessage.messageType).toBe('warning');
|
|
63
|
+
expect(result.returnMessage.message).toBe('Database operation failed');
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test('returns warning when user is missing or session has no token', async () => {
|
|
67
|
+
UserModel.findByPk.mockResolvedValueOnce(null);
|
|
68
|
+
|
|
69
|
+
await expect(appointmentHandler.listAppointments({
|
|
70
|
+
platform: 'testCRM',
|
|
71
|
+
userId: 'missing-user'
|
|
72
|
+
})).resolves.toMatchObject({
|
|
73
|
+
successful: false,
|
|
74
|
+
returnMessage: {
|
|
75
|
+
message: 'User not found'
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
UserModel.findByPk.mockResolvedValueOnce({ id: 'user-1' });
|
|
80
|
+
const noTokenResult = await appointmentHandler.createAppointment({
|
|
81
|
+
platform: 'testCRM',
|
|
82
|
+
userId: 'user-1',
|
|
83
|
+
payload: { title: 'Meeting' }
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
expect(noTokenResult.successful).toBe(false);
|
|
87
|
+
expect(noTokenResult.returnMessage.message).toBe('User not found');
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test('listAppointments resolves oauth auth with proxy config and returns connector result', async () => {
|
|
91
|
+
const user = {
|
|
92
|
+
...baseUser,
|
|
93
|
+
platformAdditionalInfo: {
|
|
94
|
+
proxyId: 'proxy-1',
|
|
95
|
+
tokenUrl: 'https://token.example.com'
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
const refreshedUser = {
|
|
99
|
+
...user,
|
|
100
|
+
accessToken: 'fresh-token'
|
|
101
|
+
};
|
|
102
|
+
const connector = {
|
|
103
|
+
getAuthType: jest.fn().mockResolvedValue('oauth'),
|
|
104
|
+
getOauthInfo: jest.fn().mockResolvedValue({ tokenUrl: 'https://token.example.com' }),
|
|
105
|
+
listAppointments: jest.fn().mockResolvedValue({
|
|
106
|
+
appointments: [{ id: 'appt-1' }],
|
|
107
|
+
returnMessage: {
|
|
108
|
+
messageType: 'success',
|
|
109
|
+
message: 'Listed'
|
|
110
|
+
}
|
|
111
|
+
})
|
|
112
|
+
};
|
|
113
|
+
UserModel.findByPk.mockResolvedValue(user);
|
|
114
|
+
Connector.getProxyConfig.mockResolvedValue({ id: 'proxy-1' });
|
|
115
|
+
oauth.checkAndRefreshAccessToken.mockResolvedValue(refreshedUser);
|
|
116
|
+
connectorRegistry.getConnector.mockReturnValue(connector);
|
|
117
|
+
|
|
118
|
+
const result = await appointmentHandler.listAppointments({
|
|
119
|
+
platform: 'testCRM',
|
|
120
|
+
userId: 'user-1',
|
|
121
|
+
range: { startDate: '2026-07-01' },
|
|
122
|
+
mineOnly: true,
|
|
123
|
+
forceSync: true
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
expect(connector.getOauthInfo).toHaveBeenCalledWith({
|
|
127
|
+
tokenUrl: 'https://token.example.com',
|
|
128
|
+
hostname: 'crm.example.com',
|
|
129
|
+
proxyId: 'proxy-1',
|
|
130
|
+
proxyConfig: { id: 'proxy-1' }
|
|
131
|
+
});
|
|
132
|
+
expect(connector.listAppointments).toHaveBeenCalledWith({
|
|
133
|
+
user: refreshedUser,
|
|
134
|
+
authHeader: 'Bearer fresh-token',
|
|
135
|
+
range: { startDate: '2026-07-01' },
|
|
136
|
+
mineOnly: true,
|
|
137
|
+
forceSync: true,
|
|
138
|
+
proxyConfig: { id: 'proxy-1' }
|
|
139
|
+
});
|
|
140
|
+
expect(result).toMatchObject({
|
|
141
|
+
successful: true,
|
|
142
|
+
appointments: [{ id: 'appt-1' }]
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test('oauth auth returns revoke response when token refresh fails', async () => {
|
|
147
|
+
const connector = {
|
|
148
|
+
getAuthType: jest.fn().mockResolvedValue('oauth'),
|
|
149
|
+
getOauthInfo: jest.fn().mockResolvedValue({})
|
|
150
|
+
};
|
|
151
|
+
UserModel.findByPk.mockResolvedValue(baseUser);
|
|
152
|
+
connectorRegistry.getConnector.mockReturnValue(connector);
|
|
153
|
+
oauth.checkAndRefreshAccessToken.mockResolvedValue(null);
|
|
154
|
+
|
|
155
|
+
const result = await appointmentHandler.createAppointment({
|
|
156
|
+
platform: 'testCRM',
|
|
157
|
+
userId: 'user-1',
|
|
158
|
+
payload: { title: 'Meeting' }
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
expect(result).toMatchObject({
|
|
162
|
+
successful: false,
|
|
163
|
+
isRevokeUserSession: true,
|
|
164
|
+
returnMessage: {
|
|
165
|
+
message: 'User session expired. Please connect again.'
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test('apiKey auth is used by create, update, refresh, confirm, and cancel operations', async () => {
|
|
171
|
+
UserModel.findByPk.mockResolvedValue(baseUser);
|
|
172
|
+
const connector = mockApiKeyConnector();
|
|
173
|
+
|
|
174
|
+
const createResult = await appointmentHandler.createAppointment({
|
|
175
|
+
platform: 'testCRM',
|
|
176
|
+
userId: 'user-1',
|
|
177
|
+
payload: { title: 'Create' }
|
|
178
|
+
});
|
|
179
|
+
const updateResult = await appointmentHandler.updateAppointment({
|
|
180
|
+
platform: 'testCRM',
|
|
181
|
+
userId: 'user-1',
|
|
182
|
+
appointmentId: 'appt-2',
|
|
183
|
+
patchBody: { title: 'Update' }
|
|
184
|
+
});
|
|
185
|
+
const refreshResult = await appointmentHandler.refreshAppointment({
|
|
186
|
+
platform: 'testCRM',
|
|
187
|
+
userId: 'user-1',
|
|
188
|
+
appointmentId: 'appt-3'
|
|
189
|
+
});
|
|
190
|
+
const confirmResult = await appointmentHandler.confirmAppointment({
|
|
191
|
+
platform: 'testCRM',
|
|
192
|
+
userId: 'user-1',
|
|
193
|
+
appointmentId: 'appt-4'
|
|
194
|
+
});
|
|
195
|
+
const cancelResult = await appointmentHandler.cancelAppointment({
|
|
196
|
+
platform: 'testCRM',
|
|
197
|
+
userId: 'user-1',
|
|
198
|
+
appointmentId: 'appt-5'
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
expect(createResult.appointmentId).toBe('appt-2');
|
|
202
|
+
expect(updateResult.appointment).toEqual({ id: 'appt-3' });
|
|
203
|
+
expect(refreshResult.appointment).toEqual({ id: 'appt-4' });
|
|
204
|
+
expect(confirmResult.appointment).toEqual({ id: 'appt-5' });
|
|
205
|
+
expect(cancelResult.appointment).toEqual({ id: 'appt-6' });
|
|
206
|
+
expect(connector.getBasicAuth).toHaveBeenCalledWith({ apiKey: 'access-token' });
|
|
207
|
+
expect(connector.createAppointment).toHaveBeenCalledWith({
|
|
208
|
+
user: baseUser,
|
|
209
|
+
authHeader: 'Basic encoded-key',
|
|
210
|
+
payload: { title: 'Create' },
|
|
211
|
+
proxyConfig: null
|
|
212
|
+
});
|
|
213
|
+
expect(connector.updateAppointment).toHaveBeenCalledWith({
|
|
214
|
+
user: baseUser,
|
|
215
|
+
authHeader: 'Basic encoded-key',
|
|
216
|
+
appointmentId: 'appt-2',
|
|
217
|
+
patchBody: { title: 'Update' },
|
|
218
|
+
proxyConfig: null
|
|
219
|
+
});
|
|
220
|
+
expect(connector.refreshAppointment).toHaveBeenCalledWith({
|
|
221
|
+
user: baseUser,
|
|
222
|
+
authHeader: 'Basic encoded-key',
|
|
223
|
+
appointmentId: 'appt-3',
|
|
224
|
+
proxyConfig: null
|
|
225
|
+
});
|
|
226
|
+
expect(connector.confirmAppointment).toHaveBeenCalledWith({
|
|
227
|
+
user: baseUser,
|
|
228
|
+
authHeader: 'Basic encoded-key',
|
|
229
|
+
appointmentId: 'appt-4',
|
|
230
|
+
proxyConfig: null
|
|
231
|
+
});
|
|
232
|
+
expect(connector.cancelAppointment).toHaveBeenCalledWith({
|
|
233
|
+
user: baseUser,
|
|
234
|
+
authHeader: 'Basic encoded-key',
|
|
235
|
+
appointmentId: 'appt-5',
|
|
236
|
+
proxyConfig: null
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
test('operation errors are mapped through API error handler', async () => {
|
|
241
|
+
UserModel.findByPk.mockResolvedValue(baseUser);
|
|
242
|
+
mockApiKeyConnector({
|
|
243
|
+
updateAppointment: jest.fn().mockRejectedValue({
|
|
244
|
+
response: {
|
|
245
|
+
status: 429
|
|
246
|
+
}
|
|
247
|
+
})
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
const result = await appointmentHandler.updateAppointment({
|
|
251
|
+
platform: 'testCRM',
|
|
252
|
+
userId: 'user-1',
|
|
253
|
+
appointmentId: 'appt-2',
|
|
254
|
+
patchBody: { title: 'Update' }
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
expect(result.successful).toBe(false);
|
|
258
|
+
expect(result.returnMessage.messageType).toBe('warning');
|
|
259
|
+
});
|
|
260
|
+
});
|
|
@@ -863,8 +863,9 @@ describe('Auth Handler', () => {
|
|
|
863
863
|
test('should use overriding OAuth option if provided', async () => {
|
|
864
864
|
// Arrange
|
|
865
865
|
const overridingOption = { redirect_uri: 'custom-redirect' };
|
|
866
|
+
const oauthInfo = { clientId: 'id', clientSecret: 'secret' };
|
|
866
867
|
const mockConnector = global.testUtils.createMockConnector({
|
|
867
|
-
getOauthInfo: jest.fn().mockResolvedValue(
|
|
868
|
+
getOauthInfo: jest.fn().mockResolvedValue(oauthInfo),
|
|
868
869
|
getOverridingOAuthOption: jest.fn().mockReturnValue(overridingOption),
|
|
869
870
|
getUserInfo: jest.fn().mockResolvedValue({
|
|
870
871
|
successful: true,
|
|
@@ -892,7 +893,10 @@ describe('Auth Handler', () => {
|
|
|
892
893
|
await authHandler.onOAuthCallback(requestData);
|
|
893
894
|
|
|
894
895
|
// Assert
|
|
895
|
-
expect(mockConnector.getOverridingOAuthOption).toHaveBeenCalledWith({
|
|
896
|
+
expect(mockConnector.getOverridingOAuthOption).toHaveBeenCalledWith({
|
|
897
|
+
code: 'code123',
|
|
898
|
+
oauthInfo: { clientId: 'id', clientSecret: 'secret' }
|
|
899
|
+
});
|
|
896
900
|
expect(mockOAuthApp.code.getToken).toHaveBeenCalledWith(
|
|
897
901
|
expect.any(String),
|
|
898
902
|
overridingOption
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
const calldown = require('../../handlers/calldown');
|
|
2
|
+
const { generateJwt } = require('../../lib/jwt');
|
|
3
|
+
const { CallDownListModel } = require('../../models/callDownListModel');
|
|
4
|
+
const { UserModel } = require('../../models/userModel');
|
|
5
|
+
|
|
6
|
+
describe('Calldown Handler', () => {
|
|
7
|
+
const originalSecret = process.env.APP_SERVER_SECRET_KEY;
|
|
8
|
+
|
|
9
|
+
beforeAll(async () => {
|
|
10
|
+
process.env.APP_SERVER_SECRET_KEY = 'test-app-server-secret-key-123456';
|
|
11
|
+
await UserModel.sync({ force: true });
|
|
12
|
+
await CallDownListModel.sync({ force: true });
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
afterEach(async () => {
|
|
16
|
+
await CallDownListModel.destroy({ where: {} });
|
|
17
|
+
await UserModel.destroy({ where: {} });
|
|
18
|
+
jest.restoreAllMocks();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
afterAll(() => {
|
|
22
|
+
if (originalSecret === undefined) {
|
|
23
|
+
delete process.env.APP_SERVER_SECRET_KEY;
|
|
24
|
+
} else {
|
|
25
|
+
process.env.APP_SERVER_SECRET_KEY = originalSecret;
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
function tokenFor(userId) {
|
|
30
|
+
return generateJwt({
|
|
31
|
+
id: userId,
|
|
32
|
+
platform: 'testCRM',
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function createUser(id = 'user-1') {
|
|
37
|
+
return UserModel.create({
|
|
38
|
+
id,
|
|
39
|
+
platform: 'testCRM',
|
|
40
|
+
accessToken: 'token',
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
test('schedule creates a scheduled record for the JWT user', async () => {
|
|
45
|
+
await createUser('user-1');
|
|
46
|
+
|
|
47
|
+
const result = await calldown.schedule({
|
|
48
|
+
jwtToken: tokenFor('user-1'),
|
|
49
|
+
body: {
|
|
50
|
+
contactId: 12345,
|
|
51
|
+
contactType: 'Lead',
|
|
52
|
+
contactName: 'Ignored By Current Schema',
|
|
53
|
+
phoneNumber: '+15551234567',
|
|
54
|
+
scheduledAt: '2026-07-02T13:00:00.000Z',
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
expect(result.id).toMatch(/^[0-9a-f]{32}$/);
|
|
59
|
+
const record = await CallDownListModel.findByPk(result.id);
|
|
60
|
+
expect(record).toMatchObject({
|
|
61
|
+
id: result.id,
|
|
62
|
+
userId: 'user-1',
|
|
63
|
+
contactId: '12345',
|
|
64
|
+
contactType: 'Lead',
|
|
65
|
+
status: 'scheduled',
|
|
66
|
+
});
|
|
67
|
+
expect(record.scheduledAt.toISOString()).toBe('2026-07-02T13:00:00.000Z');
|
|
68
|
+
expect(record.lastCallAt).toBeNull();
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test('schedule uses current defaults for optional body fields', async () => {
|
|
72
|
+
await createUser('user-1');
|
|
73
|
+
|
|
74
|
+
const result = await calldown.schedule({
|
|
75
|
+
jwtToken: tokenFor('user-1'),
|
|
76
|
+
body: {},
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
const record = await CallDownListModel.findByPk(result.id);
|
|
80
|
+
expect(record.contactId).toBeNull();
|
|
81
|
+
expect(record.contactType).toBe('contact');
|
|
82
|
+
expect(record.status).toBe('scheduled');
|
|
83
|
+
expect(record.scheduledAt).toBeNull();
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test('schedule rejects invalid jwt and missing users', async () => {
|
|
87
|
+
await expect(calldown.schedule({
|
|
88
|
+
jwtToken: 'not-a-valid-token',
|
|
89
|
+
body: {},
|
|
90
|
+
})).rejects.toThrow('Unauthorized');
|
|
91
|
+
|
|
92
|
+
await expect(calldown.schedule({
|
|
93
|
+
jwtToken: tokenFor('missing-user'),
|
|
94
|
+
body: {},
|
|
95
|
+
})).rejects.toThrow('User not found');
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test('list returns all records for the JWT user ordered by scheduledAt', async () => {
|
|
99
|
+
await createUser('user-1');
|
|
100
|
+
await createUser('user-2');
|
|
101
|
+
await CallDownListModel.bulkCreate([
|
|
102
|
+
{
|
|
103
|
+
id: 'later',
|
|
104
|
+
userId: 'user-1',
|
|
105
|
+
contactId: 'contact-2',
|
|
106
|
+
status: 'called',
|
|
107
|
+
scheduledAt: new Date('2026-07-02T14:00:00.000Z'),
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
id: 'earlier',
|
|
111
|
+
userId: 'user-1',
|
|
112
|
+
contactId: 'contact-1',
|
|
113
|
+
status: 'scheduled',
|
|
114
|
+
scheduledAt: new Date('2026-07-02T13:00:00.000Z'),
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
id: 'other-user',
|
|
118
|
+
userId: 'user-2',
|
|
119
|
+
contactId: 'contact-3',
|
|
120
|
+
status: 'scheduled',
|
|
121
|
+
scheduledAt: new Date('2026-07-02T12:00:00.000Z'),
|
|
122
|
+
},
|
|
123
|
+
]);
|
|
124
|
+
|
|
125
|
+
const { items } = await calldown.list({
|
|
126
|
+
jwtToken: tokenFor('user-1'),
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
expect(items.map((item) => item.id)).toEqual(['earlier', 'later']);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test('list filters called and not-called statuses according to current implementation', async () => {
|
|
133
|
+
await createUser('user-1');
|
|
134
|
+
await CallDownListModel.bulkCreate([
|
|
135
|
+
{
|
|
136
|
+
id: 'called',
|
|
137
|
+
userId: 'user-1',
|
|
138
|
+
status: 'called',
|
|
139
|
+
scheduledAt: new Date('2026-07-02T13:00:00.000Z'),
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
id: 'scheduled',
|
|
143
|
+
userId: 'user-1',
|
|
144
|
+
status: 'scheduled',
|
|
145
|
+
scheduledAt: new Date('2026-07-02T14:00:00.000Z'),
|
|
146
|
+
},
|
|
147
|
+
{
|
|
148
|
+
id: 'removed-status',
|
|
149
|
+
userId: 'user-1',
|
|
150
|
+
status: 'removed',
|
|
151
|
+
scheduledAt: new Date('2026-07-02T15:00:00.000Z'),
|
|
152
|
+
},
|
|
153
|
+
]);
|
|
154
|
+
|
|
155
|
+
const called = await calldown.list({
|
|
156
|
+
jwtToken: tokenFor('user-1'),
|
|
157
|
+
status: 'called',
|
|
158
|
+
});
|
|
159
|
+
const notCalled = await calldown.list({
|
|
160
|
+
jwtToken: tokenFor('user-1'),
|
|
161
|
+
status: 'not_called',
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
expect(called.items.map((item) => item.id)).toEqual(['called']);
|
|
165
|
+
expect(notCalled.items.map((item) => item.id)).toEqual(['scheduled', 'removed-status']);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
test('remove physically deletes only records owned by the JWT user', async () => {
|
|
169
|
+
await createUser('user-1');
|
|
170
|
+
await createUser('user-2');
|
|
171
|
+
await CallDownListModel.bulkCreate([
|
|
172
|
+
{
|
|
173
|
+
id: 'owned-record',
|
|
174
|
+
userId: 'user-1',
|
|
175
|
+
status: 'scheduled',
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
id: 'other-record',
|
|
179
|
+
userId: 'user-2',
|
|
180
|
+
status: 'scheduled',
|
|
181
|
+
},
|
|
182
|
+
]);
|
|
183
|
+
|
|
184
|
+
await expect(calldown.remove({
|
|
185
|
+
jwtToken: tokenFor('user-1'),
|
|
186
|
+
id: 'other-record',
|
|
187
|
+
})).rejects.toThrow('Not found');
|
|
188
|
+
|
|
189
|
+
await expect(calldown.remove({
|
|
190
|
+
jwtToken: tokenFor('user-1'),
|
|
191
|
+
id: 'owned-record',
|
|
192
|
+
})).resolves.toEqual({ successful: true });
|
|
193
|
+
|
|
194
|
+
await expect(CallDownListModel.findByPk('owned-record')).resolves.toBeNull();
|
|
195
|
+
await expect(CallDownListModel.findByPk('other-record')).resolves.not.toBeNull();
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
test('markCalled updates status and lastCallAt for owned records', async () => {
|
|
199
|
+
await createUser('user-1');
|
|
200
|
+
await CallDownListModel.create({
|
|
201
|
+
id: 'call-to-mark',
|
|
202
|
+
userId: 'user-1',
|
|
203
|
+
status: 'scheduled',
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
const result = await calldown.markCalled({
|
|
207
|
+
jwtToken: tokenFor('user-1'),
|
|
208
|
+
id: 'call-to-mark',
|
|
209
|
+
lastCallAt: '2026-07-02T14:30:00.000Z',
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
expect(result).toEqual({ successful: true });
|
|
213
|
+
const record = await CallDownListModel.findByPk('call-to-mark');
|
|
214
|
+
expect(record.status).toBe('called');
|
|
215
|
+
expect(record.lastCallAt.toISOString()).toBe('2026-07-02T14:30:00.000Z');
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
test('markCalled preserves owner isolation and returns the current database error shape', async () => {
|
|
219
|
+
await createUser('user-1');
|
|
220
|
+
await createUser('user-2');
|
|
221
|
+
await CallDownListModel.create({
|
|
222
|
+
id: 'other-user-call',
|
|
223
|
+
userId: 'user-2',
|
|
224
|
+
status: 'scheduled',
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
const result = await calldown.markCalled({
|
|
228
|
+
jwtToken: tokenFor('user-1'),
|
|
229
|
+
id: 'other-user-call',
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
expect(result).toEqual({
|
|
233
|
+
successful: false,
|
|
234
|
+
returnMessage: {
|
|
235
|
+
message: 'Database operation failed',
|
|
236
|
+
messageType: 'warning',
|
|
237
|
+
ttl: 5000,
|
|
238
|
+
},
|
|
239
|
+
});
|
|
240
|
+
const record = await CallDownListModel.findByPk('other-user-call');
|
|
241
|
+
expect(record.status).toBe('scheduled');
|
|
242
|
+
expect(record.lastCallAt).toBeNull();
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
test('update applies allowed fields and ignores disallowed fields', async () => {
|
|
246
|
+
await createUser('user-1');
|
|
247
|
+
await CallDownListModel.create({
|
|
248
|
+
id: 'call-to-update',
|
|
249
|
+
userId: 'user-1',
|
|
250
|
+
contactId: 'old-contact',
|
|
251
|
+
contactType: 'Lead',
|
|
252
|
+
status: 'scheduled',
|
|
253
|
+
scheduledAt: new Date('2026-07-02T13:00:00.000Z'),
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
const result = await calldown.update({
|
|
257
|
+
jwtToken: tokenFor('user-1'),
|
|
258
|
+
id: 'call-to-update',
|
|
259
|
+
updateData: {
|
|
260
|
+
contactId: 'new-contact',
|
|
261
|
+
contactType: 'Contact',
|
|
262
|
+
status: 'called',
|
|
263
|
+
scheduledAt: '2026-07-02T15:00:00.000Z',
|
|
264
|
+
lastCallAt: '2026-07-02T15:10:00.000Z',
|
|
265
|
+
unexpectedField: 'ignored',
|
|
266
|
+
},
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
expect(result).toEqual({ successful: true });
|
|
270
|
+
const record = await CallDownListModel.findByPk('call-to-update');
|
|
271
|
+
expect(record).toMatchObject({
|
|
272
|
+
contactId: 'new-contact',
|
|
273
|
+
contactType: 'Contact',
|
|
274
|
+
status: 'called',
|
|
275
|
+
});
|
|
276
|
+
expect(record.scheduledAt.toISOString()).toBe('2026-07-02T15:00:00.000Z');
|
|
277
|
+
expect(record.lastCallAt.toISOString()).toBe('2026-07-02T15:10:00.000Z');
|
|
278
|
+
expect(record.unexpectedField).toBeUndefined();
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
test('update rejects no-op and cross-owner updates', async () => {
|
|
282
|
+
await createUser('user-1');
|
|
283
|
+
await createUser('user-2');
|
|
284
|
+
await CallDownListModel.create({
|
|
285
|
+
id: 'other-user-update',
|
|
286
|
+
userId: 'user-2',
|
|
287
|
+
status: 'scheduled',
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
await expect(calldown.update({
|
|
291
|
+
jwtToken: tokenFor('user-1'),
|
|
292
|
+
id: 'other-user-update',
|
|
293
|
+
updateData: {
|
|
294
|
+
status: 'called',
|
|
295
|
+
},
|
|
296
|
+
})).rejects.toThrow('Not found');
|
|
297
|
+
|
|
298
|
+
await expect(calldown.update({
|
|
299
|
+
jwtToken: tokenFor('user-1'),
|
|
300
|
+
id: 'other-user-update',
|
|
301
|
+
updateData: {
|
|
302
|
+
unexpectedField: 'ignored',
|
|
303
|
+
},
|
|
304
|
+
})).rejects.toThrow('No valid fields to update');
|
|
305
|
+
|
|
306
|
+
const record = await CallDownListModel.findByPk('other-user-update');
|
|
307
|
+
expect(record.status).toBe('scheduled');
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
|