Skip to content

Commit

Permalink
refactor: 💡 unify thwow error func
Browse files Browse the repository at this point in the history
  • Loading branch information
s-hirano-ist committed Oct 14, 2024
1 parent f8edb4f commit ea89589
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 20 deletions.
6 changes: 0 additions & 6 deletions src/app/admin/@users/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Unauthorized } from "@/components/unauthorized";
import { NotAllowedError } from "@/error-classes";
import { UserCard } from "@/features/auth/components/user-card";
import { getSelfRole } from "@/features/auth/utils/get-session";
import { checkAdminPermission } from "@/features/auth/utils/role";
import prisma from "@/prisma";

Expand All @@ -10,10 +8,6 @@ export const dynamic = "force-dynamic";
export default async function Page() {
const hasAdminPermission = await checkAdminPermission();

// FIXME:
const role = await getSelfRole();
if (role !== "ADMIN") throw new NotAllowedError();

const users = await prisma.users.findMany({
select: {
id: true,
Expand Down
6 changes: 2 additions & 4 deletions src/features/auth/actions/change-role.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
"use server";
import "server-only";
import { SUCCESS_MESSAGES } from "@/constants";
import { NotAllowedError } from "@/error-classes";
import { wrapServerSideErrorForClient } from "@/error-wrapper";
import { getSelfRole } from "@/features/auth/utils/get-session";
import { checkSelfAdminRoleOrThrow } from "@/features/auth/utils/get-session";
import prisma from "@/prisma";
import type { ServerAction } from "@/types";
import { sendLineNotifyMessage } from "@/utils/fetch-message";
Expand All @@ -15,8 +14,7 @@ export async function changeRole(
role: Role,
): Promise<ServerAction<undefined>> {
try {
const selfRole = await getSelfRole();
if (selfRole !== "ADMIN") throw new NotAllowedError();
await checkSelfAdminRoleOrThrow();

await prisma.users.update({
where: { id: userId },
Expand Down
7 changes: 6 additions & 1 deletion src/features/auth/utils/get-session.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use server";
import "server-only";
import { UnauthorizedError } from "@/error-classes";
import { NotAllowedError, UnauthorizedError } from "@/error-classes";
// import { redirect } from "next/navigation";
import { auth } from "./auth";

Expand All @@ -13,6 +13,11 @@ export async function checkSelfAuth() {
return session;
}

export async function checkSelfAdminRoleOrThrow() {
const role = await getSelfRole();
if (role !== "ADMIN") throw new NotAllowedError();
}

export async function getUserId() {
const { user } = await checkSelfAuth();
return user.id;
Expand Down
7 changes: 2 additions & 5 deletions src/features/update-status/components/contents-table.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
"use server";
import { StatusCodeView } from "@/components/status-code-view";
import { NotAllowedError } from "@/error-classes";
import { getSelfRole } from "@/features/auth/utils/get-session";
import { checkSelfAdminRoleOrThrow } from "@/features/auth/utils/get-session";
import type { ContentsContext } from "@/features/dump/stores/contents-context";
import { DumpTable } from "@/features/update-status/components/dump-table";
import prisma from "@/prisma";

export async function ContentsTable() {
try {
// FIXME: hasAdminRoleOrThrow関数を作る
const role = await getSelfRole();
if (role !== "ADMIN") throw new NotAllowedError();
await checkSelfAdminRoleOrThrow();

const contents = await prisma.contents.findMany({
select: {
Expand Down
6 changes: 2 additions & 4 deletions src/features/update-status/components/news-table.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
"use server";
import { StatusCodeView } from "@/components/status-code-view";
import { NotAllowedError } from "@/error-classes";
import { getSelfRole } from "@/features/auth/utils/get-session";
import { checkSelfAdminRoleOrThrow } from "@/features/auth/utils/get-session";
import type { NewsContext } from "@/features/dump/stores/news-context";
import { DumpTable } from "@/features/update-status/components/dump-table";
import prisma from "@/prisma";

export async function NewsTable() {
try {
const role = await getSelfRole();
await checkSelfAdminRoleOrThrow();

if (role !== "ADMIN") throw new NotAllowedError();
const news = await prisma.news.findMany({
select: {
id: true,
Expand Down

0 comments on commit ea89589

Please sign in to comment.