generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 54
feat: Add onboarding extension command to create an onboarding extension request #294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Achintya-Chatterjee
merged 1 commit into
Real-Dev-Squad:develop
from
pankajjs:feat/create-onboarding-extension-command
Jan 14, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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: { | ||
memberObj: messageRequestMember; | ||
userIdObj?: messageRequestDataOptions; | ||
numberOfDaysObj: messageRequestDataOptions; | ||
reasonObj: messageRequestDataOptions; | ||
channelId: number; | ||
devObj?: DevFlag; | ||
}, | ||
env: env, | ||
ctx: ExecutionContext | ||
) { | ||
const dev = transformedArgument.devObj?.value || false; | ||
const discordId = transformedArgument.memberObj.user.id.toString(); | ||
|
||
if (!dev) { | ||
return discordTextResponse(`<@${discordId}> Feature not implemented`); | ||
} | ||
|
||
const args: CreateOnboardingExtensionArgs = { | ||
channelId: transformedArgument.channelId, | ||
userId: transformedArgument.userIdObj?.value, | ||
numberOfDays: Number(transformedArgument.numberOfDaysObj.value), | ||
reason: transformedArgument.reasonObj.value, | ||
discordId: discordId, | ||
}; | ||
|
||
const initialResponse = `<@${discordId}> Processing your request for onboarding extension`; | ||
|
||
ctx.waitUntil(createOnboardingExtension(args, env)); | ||
|
||
return discordTextResponse(initialResponse); | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,70 @@ | ||
import config from "../../config/config"; | ||
import { UNAUTHORIZED_TO_CREATE_ONBOARDING_EXTENSION_REQUEST } from "../constants/responses"; | ||
import { DISCORD_BASE_URL } from "../constants/urls"; | ||
import { env } from "../typeDefinitions/default.types"; | ||
import { generateDiscordAuthToken } from "./authTokenGenerator"; | ||
import { getUserDetails } from "./getUserDetails"; | ||
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" | ||
); | ||
|
||
let content: string; | ||
const discordReplyUrl = `${DISCORD_BASE_URL}/channels/${channelId}/messages`; | ||
|
||
if (args.userId && args.discordId !== args.userId) { | ||
const userResponse = await getUserDetails(args.discordId); | ||
if (!userResponse?.user?.roles?.super_user) { | ||
content = `<@${args.discordId}> ${UNAUTHORIZED_TO_CREATE_ONBOARDING_EXTENSION_REQUEST}`; | ||
return await sendReplyInDiscordChannel(discordReplyUrl, content, env); | ||
} | ||
} | ||
|
||
const userDiscordId = args.userId ? args.userId : args.discordId; | ||
const base_url = config(env).RDS_BASE_API_URL; | ||
const createOnboardingExtensionUrl = `${base_url}/requests?dev=true`; | ||
|
||
const requestBody = { | ||
userId: userDiscordId, | ||
pankajjs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
type: "ONBOARDING", | ||
numberOfDays: args.numberOfDays, | ||
reason: args.reason, | ||
}; | ||
|
||
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}`; | ||
return await sendReplyInDiscordChannel(discordReplyUrl, content, env); | ||
} catch (err) { | ||
content = `<@${args.discordId}> Error occurred while creating onboarding extension request.`; | ||
return await sendReplyInDiscordChannel(discordReplyUrl, content, env); | ||
} | ||
}; |
This file contains hidden or 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, | ||
}), | ||
}); | ||
}; |
This file contains hidden or 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 hidden or 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,71 @@ | ||
import { onboardingExtensionCommand } from "../../../src/controllers/onboardingExtensionCommand"; | ||
import { discordTextResponse } from "../../../src/utils/discordResponse"; | ||
import { | ||
ctx, | ||
guildEnv, | ||
transformedArgsForOnboardingExtension, | ||
} from "../../fixtures/fixture"; | ||
import * as utils from "../../../src/utils/onboardingExtension"; | ||
|
||
describe("onboardingExtensionCommand", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
const discordId = | ||
transformedArgsForOnboardingExtension.memberObj.user.id.toString(); | ||
|
||
it("should return Feature not implemented", async () => { | ||
const expectedRes = await onboardingExtensionCommand( | ||
transformedArgsForOnboardingExtension, | ||
guildEnv, | ||
ctx | ||
); | ||
const jsonResponse = await expectedRes.json(); | ||
const mockResponse = discordTextResponse( | ||
`<@${discordId}> Feature not implemented` | ||
); | ||
const mockJsonResponse = await mockResponse.json(); | ||
expect(jsonResponse).toStrictEqual(mockJsonResponse); | ||
}); | ||
|
||
it("should return initial response", async () => { | ||
transformedArgsForOnboardingExtension.devObj.value = true; | ||
const expectedRes = await onboardingExtensionCommand( | ||
transformedArgsForOnboardingExtension, | ||
guildEnv, | ||
ctx | ||
); | ||
const jsonResponse = await expectedRes.json(); | ||
const mockResponse = discordTextResponse( | ||
`<@${discordId}> Processing your request for onboarding extension` | ||
); | ||
const mockJsonResponse = await mockResponse.json(); | ||
expect(jsonResponse).toStrictEqual(mockJsonResponse); | ||
}); | ||
|
||
it("should call createOnboardingExtension", async () => { | ||
jest.spyOn(utils, "createOnboardingExtension"); | ||
transformedArgsForOnboardingExtension.devObj.value = true; | ||
await onboardingExtensionCommand( | ||
transformedArgsForOnboardingExtension, | ||
guildEnv, | ||
ctx | ||
); | ||
expect(utils.createOnboardingExtension).toHaveBeenCalledTimes(1); | ||
expect(utils.createOnboardingExtension).toHaveBeenCalledWith( | ||
{ | ||
channelId: transformedArgsForOnboardingExtension.channelId, | ||
userId: transformedArgsForOnboardingExtension.userIdObj?.value, | ||
numberOfDays: Number( | ||
transformedArgsForOnboardingExtension.numberOfDaysObj.value | ||
), | ||
reason: transformedArgsForOnboardingExtension.reasonObj.value, | ||
discordId: discordId, | ||
}, | ||
guildEnv | ||
); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.