-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor cronjobs for sync APIs (#64)
* fixed and refactored * worte test cases for fireAndForgetApiCaller * renamed functions * refactor * change cron from 11 to 12hrs and 20min to 30min --------- Co-authored-by: Mehul Kiran Chaudhari <[email protected]> Co-authored-by: Yash Raj <[email protected]>
- Loading branch information
1 parent
43dd414
commit 58bb3cb
Showing
7 changed files
with
143 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,132 +1,46 @@ | ||
import { | ||
syncExternalAccounts, | ||
syncIdle7dUsers, | ||
syncIdleUsers, | ||
syncNickNames, | ||
syncOnboarding31dPlusUsers, | ||
syncUnverifiedUsers, | ||
syncUsersStatus, | ||
} from '../../handlers/scheduledEventHandler'; | ||
import { syncApiHandler } from '../../handlers/scheduledEventHandler'; | ||
import { env } from '../../types/global.types'; | ||
import * as apiCallerModule from '../../utils/apiCaller'; | ||
|
||
jest.mock('../../utils/apiCaller', () => ({ | ||
apiCaller: jest.fn(), | ||
})); | ||
|
||
const consoleErrorMock: jest.SpyInstance = jest.spyOn(console, 'error').mockImplementation(); | ||
const apiCallerFunction = apiCallerModule.apiCaller; | ||
const fireAndForgetApiCallMock = jest.fn(); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
jest.spyOn(apiCallerModule, 'fireAndForgetApiCall').mockImplementation(fireAndForgetApiCallMock); | ||
}); | ||
|
||
afterAll(() => { | ||
consoleErrorMock.mockRestore(); | ||
}); | ||
|
||
describe('syncUsersStatus', () => { | ||
const mockEnv: env = { | ||
CURRENT_ENVIRONMENT: { | ||
RDS_BASE_API_URL: 'default', | ||
}, | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should successfully sync users status', async () => { | ||
(apiCallerFunction as jest.Mock).mockResolvedValueOnce(undefined); | ||
(apiCallerFunction as jest.Mock).mockResolvedValueOnce({ | ||
data: { | ||
users: [{ userId: 'asdoiuahow212' }], | ||
}, | ||
}); | ||
(apiCallerFunction as jest.Mock).mockResolvedValueOnce({ success: true }); | ||
|
||
await syncUsersStatus(mockEnv); | ||
|
||
expect(apiCallerFunction).toHaveBeenCalledWith(mockEnv, 'users/status/update', 'PATCH'); | ||
expect(apiCallerFunction).toHaveBeenCalledWith(mockEnv, 'users/status?aggregate=true', 'GET'); | ||
expect(apiCallerFunction).toHaveBeenCalledWith(mockEnv, 'users/status/batch', 'PATCH', { | ||
body: JSON.stringify({ users: [{ userId: 'asdoiuahow212' }] }), | ||
}); | ||
expect(apiCallerFunction).toHaveBeenCalledTimes(3); | ||
}); | ||
|
||
it('should handle error during users data retrieval', async () => { | ||
(apiCallerFunction as jest.Mock).mockResolvedValueOnce(undefined); | ||
(apiCallerFunction as jest.Mock).mockRejectedValueOnce(new Error('Error fetching users data')); | ||
|
||
const result = await syncUsersStatus(mockEnv); | ||
|
||
expect(result).toBeNull(); | ||
|
||
expect(apiCallerFunction).toHaveBeenCalledWith(mockEnv, 'users/status/update', 'PATCH'); | ||
expect(apiCallerFunction).toHaveBeenCalledWith(mockEnv, 'users/status?aggregate=true', 'GET'); | ||
expect(apiCallerFunction).toHaveBeenCalledTimes(2); | ||
|
||
expect(console.error).toHaveBeenCalledWith('Error during syncUsersStatus:', new Error('Error fetching users data')); | ||
expect(console.error).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should log an error when no users are found or data is not in the expected format', async () => { | ||
(apiCallerFunction as jest.Mock).mockResolvedValueOnce(undefined); | ||
(apiCallerFunction as jest.Mock).mockResolvedValueOnce({ | ||
data: { | ||
users: [], | ||
}, | ||
}); | ||
|
||
const result = await syncUsersStatus(mockEnv); | ||
|
||
expect(result).toBeNull(); | ||
|
||
expect(apiCallerFunction).toHaveBeenCalledWith(mockEnv, 'users/status/update', 'PATCH'); | ||
expect(apiCallerFunction).toHaveBeenCalledWith(mockEnv, 'users/status?aggregate=true', 'GET'); | ||
expect(apiCallerFunction).toHaveBeenCalledTimes(2); | ||
|
||
expect(console.error).toHaveBeenCalledWith('Error: Users data is not in the expected format or no users found'); | ||
expect(console.error).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
||
describe('sync apis', () => { | ||
const mockEnv: env = { | ||
CURRENT_ENVIRONMENT: { | ||
RDS_BASE_API_URL: 'staging', | ||
}, | ||
}; | ||
|
||
const testSyncFunction = async (syncFunction: Function, endpoint: string, method: string) => { | ||
await syncFunction(mockEnv); | ||
it('should call all sync functions', async () => { | ||
await syncApiHandler(mockEnv); | ||
|
||
expect(apiCallerFunction).toHaveBeenCalledWith(mockEnv, endpoint, method); | ||
expect(apiCallerFunction).toHaveBeenCalledTimes(1); | ||
}; | ||
|
||
it('should sync unverified users', async () => { | ||
await testSyncFunction(syncUnverifiedUsers, 'users', 'POST'); | ||
}); | ||
|
||
it('should sync idle users', async () => { | ||
await testSyncFunction(syncIdleUsers, 'discord-actions/group-idle', 'PUT'); | ||
expect(apiCallerModule.fireAndForgetApiCall).toHaveBeenCalledWith(mockEnv, 'users/status/sync', 'PATCH'); | ||
expect(apiCallerModule.fireAndForgetApiCall).toHaveBeenCalledWith(mockEnv, 'external-accounts/users?action=discord-users-sync', 'POST'); | ||
expect(apiCallerModule.fireAndForgetApiCall).toHaveBeenCalledWith(mockEnv, 'users', 'POST'); | ||
expect(apiCallerModule.fireAndForgetApiCall).toHaveBeenCalledWith(mockEnv, 'discord-actions/nicknames/sync?dev=true', 'POST'); | ||
expect(apiCallerModule.fireAndForgetApiCall).toHaveBeenCalledWith(mockEnv, 'discord-actions/group-idle-7d', 'PUT'); | ||
expect(apiCallerModule.fireAndForgetApiCall).toHaveBeenCalledWith(mockEnv, 'discord-actions/group-onboarding-31d-plus', 'PUT'); | ||
expect(apiCallerModule.fireAndForgetApiCall).toHaveBeenCalledTimes(6); | ||
}); | ||
|
||
it('should sync external accounts', async () => { | ||
await testSyncFunction(syncExternalAccounts, 'external-accounts/users?action=discord-users-sync', 'POST'); | ||
}); | ||
|
||
it('should sync nicknames', async () => { | ||
await testSyncFunction(syncNickNames, 'discord-actions/nicknames/sync?dev=true', 'POST'); | ||
}); | ||
it('should catch errors during API calls', async () => { | ||
const mockError = new Error('API error'); | ||
(apiCallerModule.fireAndForgetApiCall as jest.MockedFunction<typeof apiCallerModule.fireAndForgetApiCall>).mockRejectedValueOnce( | ||
mockError, | ||
); | ||
|
||
it('should sync idle 7d users', async () => { | ||
await testSyncFunction(syncIdle7dUsers, 'discord-actions/group-idle-7d', 'PUT'); | ||
}); | ||
await syncApiHandler(mockEnv); | ||
|
||
it('should sync onboarding 31d+ users', async () => { | ||
await testSyncFunction(syncOnboarding31dPlusUsers, 'discord-actions/group-onboarding-31d-plus', 'PUT'); | ||
expect(console.error).toHaveBeenCalledWith('Error occurred during Sync API calls:', mockError); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.