Skip to content

Commit 9eb00b8

Browse files
Merge pull request #64 from Real-Dev-Squad/develop
Sync dev to main
2 parents 24ac1de + 67342e1 commit 9eb00b8

11 files changed

+118
-23
lines changed

src/constants/responses.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ export const RETRY_COMMAND =
2323
"Oops, we didn't catch that! Please use the command again.";
2424

2525
export const ROLE_ADDED = "Role added successfully";
26+
27+
export const NAME_CHANGED = "User nickname changed successfully";

src/controllers/changeNickname.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { IRequest } from "itty-router";
2+
import { env } from "../typeDefinitions/default.types";
3+
import JSONResponse from "../utils/JsonResponse";
4+
import * as response from "../constants/responses";
5+
import { verifyAuthToken } from "../utils/verifyAuthToken";
6+
import { updateNickName } from "../utils/updateNickname";
7+
8+
export async function changeNickname(request: IRequest, env: env) {
9+
const authHeader = await request.headers.get("Authorization");
10+
11+
if (!authHeader) {
12+
return new JSONResponse(response.BAD_SIGNATURE);
13+
}
14+
15+
try {
16+
await verifyAuthToken(authHeader, env);
17+
const { discordId, userName } = await request.json();
18+
const res = await updateNickName(discordId, userName, env);
19+
return new JSONResponse(res);
20+
} catch {
21+
return new JSONResponse(response.BAD_SIGNATURE);
22+
}
23+
}

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
createGuildRoleHandler,
1212
} from "./controllers/guildRoleHandler";
1313
import { getMembersInServerHandler } from "./controllers/getMembersInServer";
14+
import { changeNickname } from "./controllers/changeNickname";
1415

1516
const router = Router();
1617

@@ -20,6 +21,8 @@ router.get("/", async () => {
2021
});
2122
});
2223

24+
router.patch("/guild/member", changeNickname);
25+
2326
router.put("/roles/create", createGuildRoleHandler);
2427

2528
router.put("/roles/add", addGroupRoleHandler);

src/utils/updateNickname.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { INTERNAL_SERVER_ERROR } from "../constants/responses";
1+
import { INTERNAL_SERVER_ERROR, NAME_CHANGED } from "../constants/responses";
22
import { DISCORD_BASE_URL } from "../constants/urls";
33
import { env } from "../typeDefinitions/default.types";
44

@@ -19,13 +19,9 @@ export async function updateNickName(
1919
body: JSON.stringify(data),
2020
});
2121
if (nameChangeResponse.ok) {
22-
return await nameChangeResponse.json();
23-
} else {
24-
console.log(nameChangeResponse.json());
25-
return INTERNAL_SERVER_ERROR;
22+
return NAME_CHANGED;
2623
}
2724
} catch (error) {
28-
console.log(error);
2925
return INTERNAL_SERVER_ERROR;
3026
}
3127
}

tests/unit/discordEphemeralResponse.test.ts renamed to tests/unit/utils/discordEphemeralResponse.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { InteractionResponseType } from "discord-interactions";
2-
import { responseJson } from "../../src/typeDefinitions/default.types";
3-
import JSONResponse from "../../src/utils/JsonResponse";
4-
import { discordEphemeralResponse } from "../../src/utils/discordEphemeralResponse";
2+
import { responseJson } from "../../../src/typeDefinitions/default.types";
3+
import JSONResponse from "../../../src/utils/JsonResponse";
4+
import { discordEphemeralResponse } from "../../../src/utils/discordEphemeralResponse";
55

66
describe("Test discordEphemeralResponse function", () => {
77
it("should return a JSONResponse", () => {

tests/unit/discordResponse.test.ts renamed to tests/unit/utils/discordResponse.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { InteractionResponseType } from "discord-interactions";
2-
import { responseJson } from "../../src/typeDefinitions/default.types";
3-
import { discordTextResponse } from "../../src/utils/discordResponse";
4-
import JSONResponse from "../../src/utils/JsonResponse";
2+
import { responseJson } from "../../../src/typeDefinitions/default.types";
3+
import { discordTextResponse } from "../../../src/utils/discordResponse";
4+
import JSONResponse from "../../../src/utils/JsonResponse";
55

66
describe("Test discordResponse function", () => {
77
it("should return a JSONResponse", () => {

tests/unit/getCommandName.test.ts renamed to tests/unit/utils/getCommandName.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { HELLO, VERIFY } from "../../src/constants/commands";
2-
import { getCommandName } from "../../src/utils/getCommandName";
1+
import { HELLO, VERIFY } from "../../../src/constants/commands";
2+
import { getCommandName } from "../../../src/utils/getCommandName";
33

44
describe("Test getCommandName function", () => {
55
it("Returns hello command name in lower case", () => {

tests/unit/getMembersInServer.test.ts renamed to tests/unit/utils/getMembersInServer.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { DISCORD_BASE_URL } from "../../src/constants/urls";
2-
import JSONResponse from "../../src/utils/JsonResponse";
3-
import { getMembersInServer } from "../../src/utils/getMembersInServer";
1+
import { DISCORD_BASE_URL } from "../../../src/constants/urls";
2+
import JSONResponse from "../../../src/utils/JsonResponse";
3+
import { getMembersInServer } from "../../../src/utils/getMembersInServer";
44

55
describe("getMembersInServer", () => {
66
const mockEnv = {

tests/unit/guildRole.test.ts renamed to tests/unit/utils/guildRole.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import JSONResponse from "../../src/utils/JsonResponse";
2-
import * as response from "../../src/constants/responses";
3-
import { createGuildRole, addGroupRole } from "../../src/utils/guildRole";
1+
import JSONResponse from "../../../src/utils/JsonResponse";
2+
import * as response from "../../../src/constants/responses";
3+
import { createGuildRole, addGroupRole } from "../../../src/utils/guildRole";
44
import {
55
dummyAddRoleBody,
66
dummyCreateBody,
77
guildEnv,
8-
} from "../fixtures/fixture";
8+
} from "../../fixtures/fixture";
99

1010
describe("createGuildRole", () => {
1111
test("should return INTERNAL_SERVER_ERROR when response is not ok", async () => {

tests/unit/lowerCaseMessageCommands.test.ts renamed to tests/unit/utils/lowerCaseMessageCommands.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { lowerCaseMessageCommands } from "../../src/utils/lowerCaseMessageCommand";
2-
import { dummyHelloMessage, dummyVerifyMessage } from "../fixtures/fixture";
1+
import { lowerCaseMessageCommands } from "../../../src/utils/lowerCaseMessageCommand";
2+
import { dummyHelloMessage, dummyVerifyMessage } from "../../fixtures/fixture";
33

44
describe("Test LowerCaseMessageCommand function", () => {
55
it("Should return the `HELLO` message in lower case", () => {

0 commit comments

Comments
 (0)