|
6 | 6 | addGroupRole,
|
7 | 7 | createGuildRole,
|
8 | 8 | removeGuildRole,
|
| 9 | + getGuildRoleByName, |
| 10 | + getGuildRoles, |
9 | 11 | } from "../utils/guildRole";
|
10 | 12 | import {
|
11 | 13 | createNewRole,
|
@@ -68,3 +70,74 @@ export async function removeGuildRoleHandler(request: IRequest, env: env) {
|
68 | 70 | });
|
69 | 71 | }
|
70 | 72 | }
|
| 73 | +export async function getGuildRolesHandler(request: IRequest, env: env) { |
| 74 | + const authHeader = request.headers.get("Authorization"); |
| 75 | + |
| 76 | + if (!authHeader) { |
| 77 | + return new JSONResponse(response.BAD_SIGNATURE, { status: 401 }); |
| 78 | + } |
| 79 | + try { |
| 80 | + await verifyAuthToken(authHeader, env); |
| 81 | + const roles = await getGuildRoles(env); |
| 82 | + return new JSONResponse({ roles }); |
| 83 | + } catch (err: any) { |
| 84 | + const error = |
| 85 | + err?.message === response.ROLE_FETCH_FAILED |
| 86 | + ? response.ROLE_FETCH_FAILED |
| 87 | + : response.INTERNAL_SERVER_ERROR; |
| 88 | + return new JSONResponse( |
| 89 | + { |
| 90 | + error, |
| 91 | + }, |
| 92 | + { |
| 93 | + status: 500, |
| 94 | + headers: { |
| 95 | + "content-type": "application/json;charset=UTF-8", |
| 96 | + }, |
| 97 | + } |
| 98 | + ); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +export async function getGuildRoleByRoleNameHandler( |
| 103 | + request: IRequest, |
| 104 | + env: env |
| 105 | +) { |
| 106 | + const authHeader = request.headers.get("Authorization"); |
| 107 | + const roleName = decodeURI(request.params?.roleName ?? ""); |
| 108 | + |
| 109 | + if (!authHeader) { |
| 110 | + return new JSONResponse(response.BAD_SIGNATURE, { status: 401 }); |
| 111 | + } |
| 112 | + |
| 113 | + if (!roleName) { |
| 114 | + return new JSONResponse(response.BAD_REQUEST, { status: 404 }); |
| 115 | + } |
| 116 | + try { |
| 117 | + await verifyAuthToken(authHeader, env); |
| 118 | + const role = await getGuildRoleByName(roleName, env); |
| 119 | + if (!role) { |
| 120 | + return new JSONResponse(response.NOT_FOUND, { |
| 121 | + status: 404, |
| 122 | + headers: { |
| 123 | + "content-type": "application/json;charset=UTF-8", |
| 124 | + }, |
| 125 | + }); |
| 126 | + } |
| 127 | + return new JSONResponse(role); |
| 128 | + } catch (err: any) { |
| 129 | + const error = |
| 130 | + err?.message === response.ROLE_FETCH_FAILED |
| 131 | + ? response.ROLE_FETCH_FAILED |
| 132 | + : response.INTERNAL_SERVER_ERROR; |
| 133 | + return new JSONResponse( |
| 134 | + { error }, |
| 135 | + { |
| 136 | + status: 500, |
| 137 | + headers: { |
| 138 | + "content-type": "application/json;charset=UTF-8", |
| 139 | + }, |
| 140 | + } |
| 141 | + ); |
| 142 | + } |
| 143 | +} |
0 commit comments