Skip to content

Commit

Permalink
refactor: 💡 throw errors
Browse files Browse the repository at this point in the history
  • Loading branch information
s-hirano-ist committed Oct 15, 2024
1 parent ea89589 commit da437dc
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/features/auth/actions/change-role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import "server-only";
import { SUCCESS_MESSAGES } from "@/constants";
import { wrapServerSideErrorForClient } from "@/error-wrapper";
import { checkSelfAdminRoleOrThrow } from "@/features/auth/utils/get-session";
import { hasAdminPermissionOrThrow } from "@/features/auth/utils/get-session";
import prisma from "@/prisma";
import type { ServerAction } from "@/types";
import { sendLineNotifyMessage } from "@/utils/fetch-message";
Expand All @@ -14,7 +14,7 @@ export async function changeRole(
role: Role,
): Promise<ServerAction<undefined>> {
try {
await checkSelfAdminRoleOrThrow();
await hasAdminPermissionOrThrow();

await prisma.users.update({
where: { id: userId },
Expand Down
4 changes: 2 additions & 2 deletions src/features/auth/actions/sign-in.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ async function createSelfLoginHistory(
ipAddress: string | undefined,
userAgent: string | undefined,
) {
// MEMO: auth()によって制限をかけると動かない
const user = await prisma.users.findUniqueOrThrow({
where: { username },
select: { id: true },
});
const userId = user.id;

return await prisma.loginHistories.create({
data: {
userId,
userId: user.id,
ipAddress,
userAgent,
},
Expand Down
22 changes: 18 additions & 4 deletions src/features/auth/utils/get-session.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
"use server";
import "server-only";
import { NotAllowedError, UnauthorizedError } from "@/error-classes";
// import { redirect } from "next/navigation";
import { auth } from "./auth";
import {
checkAdminPermission,
checkPostPermission,
checkUpdateStatusPermission,
} from "./role";

export async function checkSelfAuth() {
const session = await auth();
Expand All @@ -13,9 +17,19 @@ export async function checkSelfAuth() {
return session;
}

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

export async function hasSelfPostPermissionOrThrow() {
const hasPostPermission = await checkPostPermission();
if (!hasPostPermission) throw new NotAllowedError();
}

export async function hasUpdateStatusPermissionOrThrow() {
const hasUpdateStatusPermission = await checkUpdateStatusPermission();
if (!hasUpdateStatusPermission) throw new NotAllowedError();
}

export async function getUserId() {
Expand Down
1 change: 1 addition & 0 deletions src/features/auth/utils/header-info.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import "server-only";
import { headers } from "next/headers";

export function getLoginUserInfo() {
Expand Down
1 change: 0 additions & 1 deletion src/features/auth/utils/role.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
"use server";
import "server-only";
import { UnexpectedError } from "@/error-classes";
import prisma from "@/prisma";
Expand Down
10 changes: 5 additions & 5 deletions src/features/dump/actions/add-contents.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"use server";
import "server-only";
import { SUCCESS_MESSAGES } from "@/constants";
import { NotAllowedError } from "@/error-classes";
import { wrapServerSideErrorForClient } from "@/error-wrapper";
import { getUserId } from "@/features/auth/utils/get-session";
import { checkPostPermission } from "@/features/auth/utils/role";
import {
getUserId,
hasSelfPostPermissionOrThrow,
} from "@/features/auth/utils/get-session";
import type { ContentsContext } from "@/features/dump/stores/contents-context";
import { validateContents } from "@/features/dump/utils/validate-contents";
import prisma from "@/prisma";
Expand All @@ -16,8 +17,7 @@ export async function addContents(
formData: FormData,
): Promise<ServerAction<ContentsContext>> {
try {
const hasPostPermission = await checkPostPermission();
if (!hasPostPermission) throw new NotAllowedError();
await hasSelfPostPermissionOrThrow();

const userId = await getUserId();

Expand Down
14 changes: 7 additions & 7 deletions src/features/dump/actions/add-news.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"use server";
import "server-only";
import { SUCCESS_MESSAGES } from "@/constants";
import { NotAllowedError } from "@/error-classes";
import { wrapServerSideErrorForClient } from "@/error-wrapper";
import { getUserId } from "@/features/auth/utils/get-session";
import { checkPostPermission } from "@/features/auth/utils/role";
import {
getUserId,
hasSelfPostPermissionOrThrow,
} from "@/features/auth/utils/get-session";
import type { NewsContext } from "@/features/dump/stores/news-context";
import { validateCategory } from "@/features/dump/utils/validate-category";
import { validateNews } from "@/features/dump/utils/validate-news";
Expand All @@ -20,13 +21,13 @@ export async function addNews(
formData: FormData,
): Promise<ServerAction<NewsContext>> {
try {
const hasPostPermission = await checkPostPermission();
if (!hasPostPermission) throw new NotAllowedError();
await hasSelfPostPermissionOrThrow();

const userId = await getUserId();

const hasCategory = formData.get("new_category") !== null;

if (hasCategory) {
const userId = await getUserId();
const category = await prisma.categories.create({
data: { userId, ...validateCategory(formData) },
});
Expand All @@ -37,7 +38,6 @@ export async function addNews(
formData.set("category", String(category.id));
}

const userId = await getUserId();
const createdNews = await prisma.news.create({
data: { userId, ...validateNews(formData) },
select: {
Expand Down
4 changes: 2 additions & 2 deletions src/features/profile/actions/change-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export async function changeScope(
checked: boolean,
): Promise<ServerAction<undefined>> {
try {
const scope = checked ? "PUBLIC" : "PRIVATE";

const userId = await getUserId();

const scope = checked ? "PUBLIC" : "PRIVATE";
await prisma.users.update({
where: { id: userId },
data: { scope },
Expand Down
11 changes: 6 additions & 5 deletions src/features/update-status/actions/change-contents-status.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"use server";
import "server-only";
import { SUCCESS_MESSAGES } from "@/constants";
import { NotAllowedError, UnexpectedError } from "@/error-classes";
import { UnexpectedError } from "@/error-classes";
import { wrapServerSideErrorForClient } from "@/error-wrapper";
import { getUserId } from "@/features/auth/utils/get-session";
import { checkUpdateStatusPermission } from "@/features/auth/utils/role";
import {
getUserId,
hasUpdateStatusPermissionOrThrow,
} from "@/features/auth/utils/get-session";
import type { Status, UpdateOrRevert } from "@/features/update-status/types";
import prisma from "@/prisma";
import type { ServerAction } from "@/types";
Expand Down Expand Up @@ -68,8 +70,7 @@ export async function changeContentsStatus(
changeType: UpdateOrRevert,
): Promise<ServerAction<ToastMessage>> {
try {
const hasUpdateStatusPermission = await checkUpdateStatusPermission();
if (!hasUpdateStatusPermission) throw new NotAllowedError();
await hasUpdateStatusPermissionOrThrow();

const data = formatChangeStatusMessage(
await handleStatusChange(changeType),
Expand Down
11 changes: 6 additions & 5 deletions src/features/update-status/actions/change-news-status.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"use server";
import "server-only";
import { SUCCESS_MESSAGES } from "@/constants";
import { NotAllowedError, UnexpectedError } from "@/error-classes";
import { UnexpectedError } from "@/error-classes";
import { wrapServerSideErrorForClient } from "@/error-wrapper";
import { getUserId } from "@/features/auth/utils/get-session";
import { checkUpdateStatusPermission } from "@/features/auth/utils/role";
import {
getUserId,
hasUpdateStatusPermissionOrThrow,
} from "@/features/auth/utils/get-session";
import type { Status, UpdateOrRevert } from "@/features/update-status/types";
import prisma from "@/prisma";
import type { ServerAction } from "@/types";
Expand Down Expand Up @@ -68,8 +70,7 @@ export async function changeNewsStatus(
updateOrRevert: UpdateOrRevert,
): Promise<ServerAction<ToastMessage>> {
try {
const hasUpdateStatusPermission = await checkUpdateStatusPermission();
if (!hasUpdateStatusPermission) throw new NotAllowedError();
await hasUpdateStatusPermissionOrThrow();

const data = formatChangeStatusMessage(
await handleStatusChange(updateOrRevert),
Expand Down
4 changes: 2 additions & 2 deletions src/features/update-status/components/contents-table.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"use server";
import { StatusCodeView } from "@/components/status-code-view";
import { checkSelfAdminRoleOrThrow } from "@/features/auth/utils/get-session";
import { hasAdminPermissionOrThrow } 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 {
await checkSelfAdminRoleOrThrow();
await hasAdminPermissionOrThrow();

const contents = await prisma.contents.findMany({
select: {
Expand Down
4 changes: 2 additions & 2 deletions src/features/update-status/components/news-table.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"use server";
import { StatusCodeView } from "@/components/status-code-view";
import { checkSelfAdminRoleOrThrow } from "@/features/auth/utils/get-session";
import { hasAdminPermissionOrThrow } 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 {
await checkSelfAdminRoleOrThrow();
await hasAdminPermissionOrThrow();

const news = await prisma.news.findMany({
select: {
Expand Down

0 comments on commit da437dc

Please sign in to comment.