Skip to content

Commit fd71a85

Browse files
committed
code refactoring to call the API outside if/else
1 parent fb649cf commit fd71a85

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

src/utils/awsAccess.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,6 @@ import config from "../../config/config";
44
import { discordTextResponse } from "./discordResponse";
55
import { DISCORD_BASE_URL, AWS_IAM_SIGNIN_URL } from "../constants/urls";
66

7-
function sendDiscordMessage(content: string, channelId: number, env: env) {
8-
return fetch(`${DISCORD_BASE_URL}/channels/${channelId}/messages`, {
9-
method: "POST",
10-
headers: {
11-
"Content-Type": "application/json",
12-
Authorization: `Bot ${env.DISCORD_TOKEN}`,
13-
},
14-
body: JSON.stringify({
15-
content: `${content}`,
16-
}),
17-
});
18-
}
19-
207
export async function processAWSAccessRequest(
218
discordUserId: string,
229
awsGroupId: string,
@@ -47,18 +34,36 @@ export async function processAWSAccessRequest(
4734
body: JSON.stringify(requestData),
4835
});
4936

37+
let content = "";
5038
if (!response.ok) {
5139
const responseText = await response.text();
5240
const errorData = JSON.parse(responseText);
53-
const content = `<@${discordUserId}> Error occurred while granting AWS access: ${errorData.error}`;
54-
return sendDiscordMessage(content, channelId, env);
41+
content = `<@${discordUserId}> Error occurred while granting AWS access: ${errorData.error}`;
5542
} else {
56-
const content = `AWS access granted successfully <@${discordUserId}>! Please head over to AWS - ${AWS_IAM_SIGNIN_URL}.`;
57-
return sendDiscordMessage(content, channelId, env);
43+
content = `AWS access granted successfully <@${discordUserId}>! Please head over to AWS - ${AWS_IAM_SIGNIN_URL}.`;
5844
}
45+
return await fetch(`${DISCORD_BASE_URL}/channels/${channelId}/messages`, {
46+
method: "POST",
47+
headers: {
48+
"Content-Type": "application/json",
49+
Authorization: `Bot ${env.DISCORD_TOKEN}`,
50+
},
51+
body: JSON.stringify({
52+
content: content,
53+
}),
54+
});
5955
} catch (err) {
6056
const content = `<@${discordUserId}> Error occurred while granting AWS access.`;
61-
return sendDiscordMessage(content, channelId, env);
57+
return await fetch(`${DISCORD_BASE_URL}/channels/${channelId}/messages`, {
58+
method: "POST",
59+
headers: {
60+
"Content-Type": "application/json",
61+
Authorization: `Bot ${env.DISCORD_TOKEN}`,
62+
},
63+
body: JSON.stringify({
64+
content: content,
65+
}),
66+
});
6267
}
6368
}
6469

0 commit comments

Comments
 (0)