Skip to content

Commit

Permalink
code refactoring to call the API outside if/else
Browse files Browse the repository at this point in the history
  • Loading branch information
vikhyat187 committed Oct 26, 2024
1 parent fb649cf commit fd71a85
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions src/utils/awsAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,6 @@ import config from "../../config/config";
import { discordTextResponse } from "./discordResponse";
import { DISCORD_BASE_URL, AWS_IAM_SIGNIN_URL } from "../constants/urls";

function sendDiscordMessage(content: string, channelId: number, env: env) {
return fetch(`${DISCORD_BASE_URL}/channels/${channelId}/messages`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bot ${env.DISCORD_TOKEN}`,
},
body: JSON.stringify({
content: `${content}`,
}),
});
}

export async function processAWSAccessRequest(
discordUserId: string,
awsGroupId: string,
Expand Down Expand Up @@ -47,18 +34,36 @@ export async function processAWSAccessRequest(
body: JSON.stringify(requestData),
});

let content = "";
if (!response.ok) {
const responseText = await response.text();
const errorData = JSON.parse(responseText);
const content = `<@${discordUserId}> Error occurred while granting AWS access: ${errorData.error}`;
return sendDiscordMessage(content, channelId, env);
content = `<@${discordUserId}> Error occurred while granting AWS access: ${errorData.error}`;
} else {
const content = `AWS access granted successfully <@${discordUserId}>! Please head over to AWS - ${AWS_IAM_SIGNIN_URL}.`;
return sendDiscordMessage(content, channelId, env);
content = `AWS access granted successfully <@${discordUserId}>! Please head over to AWS - ${AWS_IAM_SIGNIN_URL}.`;
}
return await fetch(`${DISCORD_BASE_URL}/channels/${channelId}/messages`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bot ${env.DISCORD_TOKEN}`,
},
body: JSON.stringify({
content: content,
}),
});
} catch (err) {
const content = `<@${discordUserId}> Error occurred while granting AWS access.`;
return sendDiscordMessage(content, channelId, env);
return await fetch(`${DISCORD_BASE_URL}/channels/${channelId}/messages`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bot ${env.DISCORD_TOKEN}`,
},
body: JSON.stringify({
content: content,
}),
});
}
}

Expand Down

0 comments on commit fd71a85

Please sign in to comment.