11import config from "../../config/config" ;
2+ import { UNAUTHORIZED_TO_CREATE_ONBOARDING_EXTENSION_REQUEST } from "../constants/responses" ;
23import { DISCORD_BASE_URL } from "../constants/urls" ;
34import { env } from "../typeDefinitions/default.types" ;
45import { generateDiscordAuthToken } from "./authTokenGenerator" ;
6+ import { getUserDetails } from "./getUserDetails" ;
57import { sendReplyInDiscordChannel } from "./sendReplyInDiscordChannel" ;
68
79export type CreateOnboardingExtensionArgs = {
8- userId : string ;
10+ userId ? : string ;
911 channelId : number ;
1012 reason : string ;
1113 numberOfDays : number ;
@@ -25,19 +27,28 @@ export const createOnboardingExtension = async (
2527 "RS256"
2628 ) ;
2729
30+ let content : string ;
2831 const discordReplyUrl = `${ DISCORD_BASE_URL } /channels/${ channelId } /messages` ;
32+
33+ if ( args . userId && args . discordId !== args . userId ) {
34+ const userResponse = await getUserDetails ( args . discordId ) ;
35+ if ( ! userResponse . user ?. roles ?. super_user ) {
36+ content = `<@${ args . discordId } > ${ UNAUTHORIZED_TO_CREATE_ONBOARDING_EXTENSION_REQUEST } ` ;
37+ return await sendReplyInDiscordChannel ( discordReplyUrl , content , env ) ;
38+ }
39+ }
40+
41+ const userDiscordId = args . userId ? args . userId : args . discordId ;
2942 const base_url = config ( env ) . RDS_BASE_API_URL ;
3043 const createOnboardingExtensionUrl = `${ base_url } /requests?dev=true` ;
44+
3145 const requestBody = {
32- userId : args . userId ,
46+ userId : userDiscordId ,
3347 type : "ONBOARDING" ,
3448 numberOfDays : args . numberOfDays ,
35- requestedBy : args . discordId ,
3649 reason : args . reason ,
3750 } ;
3851
39- let content : string ;
40-
4152 try {
4253 const response = await fetch ( createOnboardingExtensionUrl , {
4354 method : "POST" ,
@@ -51,9 +62,9 @@ export const createOnboardingExtension = async (
5162 message : string ;
5263 } ;
5364 content = `<@${ args . discordId } > ${ jsonResponse . message } ` ;
54- await sendReplyInDiscordChannel ( discordReplyUrl , content , env ) ;
65+ return await sendReplyInDiscordChannel ( discordReplyUrl , content , env ) ;
5566 } catch ( err ) {
5667 content = `<@${ args . discordId } > Error occurred while creating onboarding extension request.` ;
57- await sendReplyInDiscordChannel ( discordReplyUrl , content , env ) ;
68+ return await sendReplyInDiscordChannel ( discordReplyUrl , content , env ) ;
5869 }
5970} ;
0 commit comments