generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { env } from "../typeDefinitions/default.types"; | ||
import { | ||
messageRequestDataOptions, | ||
messageRequestMember, | ||
} from "../typeDefinitions/discordMessage.types"; | ||
import { DevFlag } from "../typeDefinitions/filterUsersByRole"; | ||
import { discordTextResponse } from "../utils/discordResponse"; | ||
import { | ||
createOnboardingExtension, | ||
CreateOnboardingExtensionArgs, | ||
} from "../utils/onboardingExtension"; | ||
|
||
export async function onboardingExtensionCommand( | ||
transformedArgument: { | ||
member: messageRequestMember; | ||
userId: messageRequestDataOptions; | ||
numberOfDays: messageRequestDataOptions; | ||
reason: messageRequestDataOptions; | ||
channelId: number; | ||
dev?: DevFlag; | ||
}, | ||
env: env, | ||
ctx: ExecutionContext | ||
) { | ||
const dev = transformedArgument.dev?.value || false; | ||
const discordId = transformedArgument.member.user.id.toString(); | ||
|
||
if (!dev) { | ||
return discordTextResponse(`<@${discordId}> Feature not implemented`); | ||
} | ||
|
||
const args: CreateOnboardingExtensionArgs = { | ||
channelId: transformedArgument.channelId, | ||
userId: transformedArgument.userId.value, | ||
numberOfDays: Number(transformedArgument.numberOfDays.value), | ||
reason: transformedArgument.reason.value, | ||
discordId: discordId, | ||
}; | ||
|
||
const initialResponse = `<@${discordId}> Processing your request for onboarding extension`; | ||
|
||
ctx.waitUntil(createOnboardingExtension(args, env)); | ||
|
||
return discordTextResponse(initialResponse); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import config from "../../config/config"; | ||
import { DISCORD_BASE_URL } from "../constants/urls"; | ||
import { env } from "../typeDefinitions/default.types"; | ||
import { generateDiscordAuthToken } from "./authTokenGenerator"; | ||
import { sendReplyInDiscordChannel } from "./sendReplyInDiscordChannel"; | ||
|
||
export type CreateOnboardingExtensionArgs = { | ||
userId: string; | ||
channelId: number; | ||
reason: string; | ||
numberOfDays: number; | ||
discordId: string; | ||
}; | ||
|
||
export const createOnboardingExtension = async ( | ||
args: CreateOnboardingExtensionArgs, | ||
env: env | ||
) => { | ||
const { channelId } = args; | ||
|
||
const authToken = await generateDiscordAuthToken( | ||
"Cloudflare Worker", | ||
Math.floor(Date.now() / 1000) + 2, | ||
env.BOT_PRIVATE_KEY, | ||
"RS256" | ||
); | ||
|
||
const discordReplyUrl = `${DISCORD_BASE_URL}/channels/${channelId}/messages`; | ||
const base_url = config(env).RDS_BASE_API_URL; | ||
const createOnboardingExtensionUrl = `${base_url}/requests?dev=true`; | ||
const requestBody = { | ||
userId: args.userId, | ||
type: "ONBOARDING", | ||
numberOfDays: args.numberOfDays, | ||
requestedBy: args.discordId, | ||
reason: args.reason, | ||
}; | ||
|
||
let content: string; | ||
|
||
try { | ||
const response = await fetch(createOnboardingExtensionUrl, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${authToken}`, | ||
}, | ||
body: JSON.stringify(requestBody), | ||
}); | ||
const jsonResponse = (await response.json()) as unknown as { | ||
message: string; | ||
}; | ||
content = `<@${args.discordId}> ${jsonResponse.message}`; | ||
await sendReplyInDiscordChannel(discordReplyUrl, content, env); | ||
} catch (err) { | ||
content = `<@${args.discordId}> Error occurred while creating onboarding extension request.`; | ||
await sendReplyInDiscordChannel(discordReplyUrl, content, env); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { env } from "../typeDefinitions/default.types"; | ||
|
||
export const sendReplyInDiscordChannel = async ( | ||
discordReplyUrl: string, | ||
body: any, | ||
env: env | ||
) => { | ||
await fetch(discordReplyUrl, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bot ${env.DISCORD_TOKEN}`, | ||
}, | ||
body: JSON.stringify({ | ||
content: body, | ||
}), | ||
}); | ||
}; |