Skip to content

Commit

Permalink
Revert "Dev to main sync"
Browse files Browse the repository at this point in the history
  • Loading branch information
prakashchoudhary07 authored Aug 13, 2024
1 parent bd412b6 commit 7e36798
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 409 deletions.
103 changes: 1 addition & 102 deletions src/handlers/scheduledEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,7 @@ import config from '../config/config';
import { NAMESPACE_NAME } from '../constants';
import { updateUserRoles } from '../services/discordBotServices';
import { getMissedUpdatesUsers } from '../services/rdsBackendService';
import {
DiscordUserRole,
env,
NicknameUpdateResponseType,
OrphanTasksStatusUpdateResponseType,
UserStatusResponse,
} from '../types/global.types';
import { apiCaller } from '../utils/apiCaller';
import { DiscordUserRole, env, NicknameUpdateResponseType } from '../types/global.types';
import { chunks } from '../utils/arrayUtils';
import { generateJwt } from '../utils/generateJwt';

Expand Down Expand Up @@ -101,97 +94,3 @@ export const addMissedUpdatesRole = async (env: env) => {
console.error('Error while adding missed updates roles');
}
};

export const syncUsersStatus = async (env: env): Promise<any | null> => {
await apiCaller(env, 'users/status/update', 'PATCH');

try {
const idleUsersData = (await apiCaller(env, 'users/status?aggregate=true', 'GET')) as UserStatusResponse | undefined;

if (!idleUsersData?.data?.users || idleUsersData.data.users.length === 0) {
console.error('Error: Users data is not in the expected format or no users found');
return null;
}

const response = await apiCaller(env, 'users/status/batch', 'PATCH', {
body: JSON.stringify({ users: idleUsersData.data.users }),
});

return response;
} catch (error) {
console.error('Error during syncUsersStatus:', error);
return null;
}
};

export const syncExternalAccounts = async (env: env) => {
return await apiCaller(env, 'external-accounts/users?action=discord-users-sync', 'POST');
};

export const syncUnverifiedUsers = async (env: env) => {
return await apiCaller(env, 'users', 'POST');
};

export const syncIdleUsers = async (env: env) => {
return await apiCaller(env, 'discord-actions/group-idle', 'PUT');
};

export const syncNickNames = async (env: env) => {
return await apiCaller(env, 'discord-actions/nicknames/sync?dev=true', 'POST');
};

export const syncIdle7dUsers = async (env: env) => {
return await apiCaller(env, 'discord-actions/group-idle-7d', 'PUT');
};

export const syncOnboarding31dPlusUsers = async (env: env) => {
return await apiCaller(env, 'discord-actions/group-onboarding-31d-plus', 'PUT');
};

export async function filterOrphanTasks(env: env) {
const namespace = env[NAMESPACE_NAME] as unknown as KVNamespace;
let lastOrphanTasksFilterationTimestamp: string | null = '0'; // O means it will take the oldest unix timestamp
try {
lastOrphanTasksFilterationTimestamp = await namespace.get('ORPHAN_TASKS_UPDATED_TIME');

if (!lastOrphanTasksFilterationTimestamp) {
console.log(`Empty KV ORPHAN_TASKS_UPDATED_TIME: ${lastOrphanTasksFilterationTimestamp}`);
lastOrphanTasksFilterationTimestamp = '0'; // O means it will take the oldest unix timestamp
}
} catch (err) {
console.error(err, 'Error while fetching the timestamp of last orphan tasks filteration');
throw err;
}

const url = config(env).RDS_BASE_API_URL;
let token;
try {
token = await generateJwt(env);
} catch (err) {
console.error(`Error while generating JWT token: ${err}`);
throw err;
}
const response = await fetch(`${url}/tasks/orphanTasks`, {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
lastOrphanTasksFilterationTimestamp,
}),
});
if (!response.ok) {
throw new Error('Error while trying to update status of orphan tasks to backlog');
}

const data: OrphanTasksStatusUpdateResponseType = await response.json();

try {
await namespace.put('ORPHAN_TASKS_UPDATED_TIME', Date.now().toString());
} catch (err) {
console.error('Error while trying to update the last orphan tasks filteration timestamp');
}

return data;
}
132 changes: 0 additions & 132 deletions src/tests/handlers/scheduledEventHandler.test.ts

This file was deleted.

8 changes: 4 additions & 4 deletions src/tests/services/rdsBackendService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('rdsBackendService', () => {
});

it('should make a successful API call and return the expected data', async () => {
jest.spyOn(globalThis as any, 'fetch').mockResolvedValueOnce({
jest.spyOn(global, 'fetch').mockResolvedValueOnce({
ok: true,
status: 200,
json: jest.fn().mockResolvedValueOnce({ ...missedUpdatesUsersResponse, data: missedUpdatesUsersMock }),
Expand All @@ -32,7 +32,7 @@ describe('rdsBackendService', () => {
expect(result).toEqual({ ...missedUpdatesUsersMock });
});
it('should make a successful API call with cursor', async () => {
jest.spyOn(globalThis as any, 'fetch').mockResolvedValueOnce({
jest.spyOn(global, 'fetch').mockResolvedValueOnce({
ok: true,
status: 200,
json: jest.fn().mockResolvedValueOnce({ ...missedUpdatesUsersResponse, data: missedUpdatesUsersMock }),
Expand All @@ -51,7 +51,7 @@ describe('rdsBackendService', () => {
expect(result).toEqual({ ...missedUpdatesUsersMock });
});
it('should throw error when api call fails', async () => {
jest.spyOn(globalThis as any, 'fetch').mockResolvedValueOnce({
jest.spyOn(global, 'fetch').mockResolvedValueOnce({
ok: false,
status: 400,
} as unknown as Response);
Expand All @@ -60,7 +60,7 @@ describe('rdsBackendService', () => {

it('should handle unknown errors', async () => {
const consoleSpy = jest.spyOn(console, 'error');
jest.spyOn(globalThis as any, 'fetch').mockRejectedValueOnce(new Error('Error occurred'));
jest.spyOn(global, 'fetch').mockRejectedValueOnce(new Error('Error occurred'));
await expect(getMissedUpdatesUsers({}, cursor)).rejects.toThrow('Error occurred');
expect(consoleSpy).toHaveBeenCalledWith('Error occurred while fetching discord user details');
});
Expand Down
78 changes: 0 additions & 78 deletions src/tests/utils/apiCaller.test.ts

This file was deleted.

18 changes: 0 additions & 18 deletions src/types/global.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ export type NicknameUpdateResponseType = {
unsuccessfulNicknameUpdates: number;
};
};

export type OrphanTasksStatusUpdateResponseType = {
message: string;
data: {
orphanTasksUpdatedCount: number;
};
};
export type DiscordUsersResponse = {
message: string;
data: DiscordUserIdList;
Expand All @@ -46,14 +39,3 @@ export type DiscordRoleUpdatedList = {
roleid: string;
success: boolean;
};
export type UserStatusResponse = {
message: string;
data: {
totalUsers: number;
totalIdleUsers: number;
totalActiveUsers: number;
totalUnprocessedUsers: number;
unprocessedUsers: Array<unknown>;
users: Array<unknown>;
};
};
Loading

0 comments on commit 7e36798

Please sign in to comment.