11import jwt from "@tsndr/cloudflare-worker-jwt" ;
2- import { v4 as uuidv4 } from "uuid" ;
32import { env } from "../typeDefinitions/default.types" ;
43import config from "../../config/config" ;
54import { discordTextResponse } from "./discordResponse" ;
65import { DISCORD_BASE_URL , AWS_IAM_SIGNIN_URL } from "../constants/urls" ;
76
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+
820export async function processAWSAccessRequest (
921 discordUserId : string ,
1022 awsGroupId : string ,
1123 env : env ,
12- TraceId : string ,
1324 channelId : number
1425) {
1526 const authToken = await jwt . sign (
@@ -25,7 +36,7 @@ export async function processAWSAccessRequest(
2536 userId : discordUserId ,
2637 } ;
2738
28- const url = `${ base_url } /aws-access/ ` ;
39+ const url = `${ base_url } /aws-access` ;
2940
3041 const response = await fetch ( url , {
3142 method : "POST" ,
@@ -37,39 +48,17 @@ export async function processAWSAccessRequest(
3748 } ) ;
3849
3950 if ( ! response . ok ) {
40- return fetch ( `${ DISCORD_BASE_URL } /channels/${ channelId } /messages` , {
41- method : "POST" ,
42- headers : {
43- "Content-Type" : "application/json" ,
44- Authorization : `Bot ${ env . DISCORD_TOKEN } ` ,
45- } ,
46- body : JSON . stringify ( {
47- content : `<@${ discordUserId } > Error occurred while granting AWS access: ${ response . status } ${ response . statusText } ` ,
48- } ) ,
49- } ) ;
51+ const responseText = await response . text ( ) ;
52+ const errorData = JSON . parse ( responseText ) ;
53+ const content = `<@${ discordUserId } > Error occurred while granting AWS access: ${ errorData . error } ` ;
54+ return sendDiscordMessage ( content , channelId , env ) ;
5055 } else {
51- return fetch ( `${ DISCORD_BASE_URL } /channels/${ channelId } /messages` , {
52- method : "POST" ,
53- headers : {
54- "Content-Type" : "application/json" ,
55- Authorization : `Bot ${ env . DISCORD_TOKEN } ` ,
56- } ,
57- body : JSON . stringify ( {
58- content : `AWS access granted successfully <@${ discordUserId } >! Please head over to AWS - ${ AWS_IAM_SIGNIN_URL } .` ,
59- } ) ,
60- } ) ;
56+ const content = `AWS access granted successfully <@${ discordUserId } >! Please head over to AWS - ${ AWS_IAM_SIGNIN_URL } .` ;
57+ return sendDiscordMessage ( content , channelId , env ) ;
6158 }
6259 } catch ( err ) {
63- return fetch ( `${ DISCORD_BASE_URL } /channels/${ channelId } /messages` , {
64- method : "POST" ,
65- headers : {
66- "Content-Type" : "application/json" ,
67- Authorization : `Bot ${ env . DISCORD_TOKEN } ` ,
68- } ,
69- body : JSON . stringify ( {
70- content : `[TraceId: ${ TraceId } ] <@${ discordUserId } > Error occurred while granting AWS access.` ,
71- } ) ,
72- } ) ;
60+ const content = `<@${ discordUserId } > Error occurred while granting AWS access.` ;
61+ return sendDiscordMessage ( content , channelId , env ) ;
7362 }
7463}
7564
@@ -80,15 +69,14 @@ export async function grantAWSAccess(
8069 ctx : ExecutionContext ,
8170 channelId : number
8271) {
83- const TraceId = uuidv4 ( ) ;
8472 // Immediately send a Discord response to acknowledge the command
8573 const initialResponse = discordTextResponse (
86- `[TraceId: ${ TraceId } ] <@${ discordUserId } > Processing your request to grant AWS access.`
74+ `<@${ discordUserId } > Processing your request to grant AWS access.`
8775 ) ;
8876
8977 ctx . waitUntil (
9078 // Asynchronously call the function to grant AWS access
91- processAWSAccessRequest ( discordUserId , awsGroupId , env , TraceId , channelId )
79+ processAWSAccessRequest ( discordUserId , awsGroupId , env , channelId )
9280 ) ;
9381
9482 // Return the immediate response within 3 seconds
0 commit comments