1
1
import jwt from "@tsndr/cloudflare-worker-jwt" ;
2
- import { v4 as uuidv4 } from "uuid" ;
3
2
import { env } from "../typeDefinitions/default.types" ;
4
3
import config from "../../config/config" ;
5
4
import { discordTextResponse } from "./discordResponse" ;
6
5
import { DISCORD_BASE_URL , AWS_IAM_SIGNIN_URL } from "../constants/urls" ;
7
6
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
+
8
20
export async function processAWSAccessRequest (
9
21
discordUserId : string ,
10
22
awsGroupId : string ,
11
23
env : env ,
12
- TraceId : string ,
13
24
channelId : number
14
25
) {
15
26
const authToken = await jwt . sign (
@@ -25,7 +36,7 @@ export async function processAWSAccessRequest(
25
36
userId : discordUserId ,
26
37
} ;
27
38
28
- const url = `${ base_url } /aws-access/ ` ;
39
+ const url = `${ base_url } /aws-access` ;
29
40
30
41
const response = await fetch ( url , {
31
42
method : "POST" ,
@@ -37,39 +48,17 @@ export async function processAWSAccessRequest(
37
48
} ) ;
38
49
39
50
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 ) ;
50
55
} 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 ) ;
61
58
}
62
59
} 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 ) ;
73
62
}
74
63
}
75
64
@@ -80,15 +69,14 @@ export async function grantAWSAccess(
80
69
ctx : ExecutionContext ,
81
70
channelId : number
82
71
) {
83
- const TraceId = uuidv4 ( ) ;
84
72
// Immediately send a Discord response to acknowledge the command
85
73
const initialResponse = discordTextResponse (
86
- `[TraceId: ${ TraceId } ] <@${ discordUserId } > Processing your request to grant AWS access.`
74
+ `<@${ discordUserId } > Processing your request to grant AWS access.`
87
75
) ;
88
76
89
77
ctx . waitUntil (
90
78
// Asynchronously call the function to grant AWS access
91
- processAWSAccessRequest ( discordUserId , awsGroupId , env , TraceId , channelId )
79
+ processAWSAccessRequest ( discordUserId , awsGroupId , env , channelId )
92
80
) ;
93
81
94
82
// Return the immediate response within 3 seconds
0 commit comments