Skip to content

Commit

Permalink
Merge pull request #58 from Real-Dev-Squad/develop
Browse files Browse the repository at this point in the history
Dev to main sync
  • Loading branch information
iamitprakash authored Feb 2, 2024
2 parents d546470 + 478bce1 commit 2aa3551
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 37 deletions.
33 changes: 0 additions & 33 deletions .github/pull_request_template.md

This file was deleted.

4 changes: 4 additions & 0 deletions .github/workflows/production.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ jobs:
CRON_JOB_PRIVATE_KEY
DISCORD_BOT_PRIVATE_KEY
DISCORD_BOT_API_URL
DISCORD_SERVICE_PRIVATE_KEY
FF_CRON_DISCORD_KEY_PAIR_FLOW
env:
CLOUDFLARE_API_TOKEN: ${{secrets.CLOUDFLARE_API_TOKEN}}
CLOUDFLARE_ACCOUNT_ID: ${{secrets.CLOUDFLARE_ACCOUNT_ID}}
CURRENT_ENVIRONMENT: ${{vars.CURRENT_ENVIRONMENT}}
CRON_JOB_PRIVATE_KEY: ${{secrets.CRON_JOB_PRIVATE_KEY}}
DISCORD_BOT_PRIVATE_KEY: ${{secrets.DISCORD_BOT_PRIVATE_KEY}}
DISCORD_BOT_API_URL: ${{secrets.DISCORD_BOT_API_URL}}
DISCORD_SERVICE_PRIVATE_KEY: ${{secrets.DISCORD_SERVICE_PRIVATE_KEY}}
FF_CRON_DISCORD_KEY_PAIR_FLOW: ${{vars.FF_CRON_DISCORD_KEY_PAIR_FLOW}}
4 changes: 4 additions & 0 deletions .github/workflows/staging.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ jobs:
CRON_JOB_PRIVATE_KEY
DISCORD_BOT_PRIVATE_KEY
DISCORD_BOT_API_URL
DISCORD_SERVICE_PRIVATE_KEY
FF_CRON_DISCORD_KEY_PAIR_FLOW
env:
CLOUDFLARE_API_TOKEN: ${{secrets.CLOUDFLARE_API_TOKEN}}
CLOUDFLARE_ACCOUNT_ID: ${{secrets.CLOUDFLARE_ACCOUNT_ID}}
CURRENT_ENVIRONMENT: ${{vars.CURRENT_ENVIRONMENT}}
CRON_JOB_PRIVATE_KEY: ${{secrets.CRON_JOB_PRIVATE_KEY}}
DISCORD_BOT_PRIVATE_KEY: ${{secrets.DISCORD_BOT_PRIVATE_KEY}}
DISCORD_BOT_API_URL: ${{secrets.DISCORD_BOT_API_URL}}
DISCORD_SERVICE_PRIVATE_KEY: ${{secrets.DISCORD_SERVICE_PRIVATE_KEY}}
FF_CRON_DISCORD_KEY_PAIR_FLOW: ${{vars.FF_CRON_DISCORD_KEY_PAIR_FLOW}}
2 changes: 1 addition & 1 deletion src/handlers/scheduledEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const addMissedUpdatesRole = async (env: env) => {

const missedUpdatesUsers = await getMissedUpdatesUsers(env, cursor);

if (!!missedUpdatesUsers && missedUpdatesUsers.usersToAddRole?.length > 1) {
if (!!missedUpdatesUsers && missedUpdatesUsers.usersToAddRole?.length >= 1) {
const discordUserIdRoleIdList: DiscordUserRole[] = missedUpdatesUsers.usersToAddRole.map((userId) => ({
userid: userId,
roleid: config(env).MISSED_UPDATES_ROLE_ID,
Expand Down
5 changes: 3 additions & 2 deletions src/services/discordBotServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ export const updateUserRoles = async (env: env, payload: DiscordUserRole[]): Pro
try {
const url = config(env).DISCORD_BOT_API_URL;
const token = await generateDiscordBotJwt(env);

const response = await env.DISCORD_BOT.fetch(`${url}/roles?action=add-role`, {
//TODO(@Ajeyakrishna-k): remove dev flag https://github.com/Real-Dev-Squad/discord-slash-commands/issues/193
const devQuery = env.FF_CRON_DISCORD_KEY_PAIR_FLOW === 'true' ? '&dev=true' : '';
const response = await env.DISCORD_BOT.fetch(`${url}/roles?action=add-role${devQuery}`, {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
Expand Down
9 changes: 9 additions & 0 deletions src/tests/handlers/missedRoleHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ describe('addMissedUpdatesRole', () => {
expect(updateUserRoles).toHaveBeenCalledTimes(2);
});

it('should call getMissedUpdatesUsers and updateUserRoles when theres only one user', async () => {
const usersMockData = { ...missedUpdatesUsersMockWithoutCursor };
usersMockData.usersToAddRole = usersMockData.usersToAddRole.slice(0, 1);
(getMissedUpdatesUsers as jest.Mock).mockResolvedValueOnce(usersMockData);
await addMissedUpdatesRole({});
expect(getMissedUpdatesUsers).toHaveBeenCalledTimes(1);
expect(updateUserRoles).toHaveBeenCalledTimes(1);
});

it('should not call updateUserRoles when there are no users to add role', async () => {
(getMissedUpdatesUsers as jest.Mock).mockResolvedValueOnce(missedUpdatesUsersMockWithNoUsers);

Expand Down
4 changes: 3 additions & 1 deletion src/utils/generateJwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ export const generateJwt = async (env: env) => {

export const generateDiscordBotJwt = async (env: env) => {
try {
//TODO(@Ajeyakrishna-k): remove dev flag https://github.com/Real-Dev-Squad/discord-slash-commands/issues/193
const privateKey = env.FF_CRON_DISCORD_KEY_PAIR_FLOW === 'true' ? env.DISCORD_SERVICE_PRIVATE_KEY : env.DISCORD_BOT_PRIVATE_KEY;
const authToken = await jwt.sign(
{
exp: Math.floor(Date.now() / 1000) + 60,
},
env.DISCORD_BOT_PRIVATE_KEY,
privateKey,
{ algorithm: 'RS256' },
);
return authToken;
Expand Down

0 comments on commit 2aa3551

Please sign in to comment.