마에스트로 상세 화면에 [업그레이드] 버튼 추가
This commit is contained in:
@@ -0,0 +1,191 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useState, useTransition } from "react";
|
||||||
|
import { useRouter } from "next/navigation";
|
||||||
|
import { ArrowUp } from "lucide-react";
|
||||||
|
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { ACCOUNT_TYPES } from "@/lib/constants";
|
||||||
|
import { formatDateTime, getAccountTypeLabel, getRequestStatusLabel } from "@/lib/utils";
|
||||||
|
import type { MaestroDetail } from "@/lib/maestros";
|
||||||
|
|
||||||
|
type UpgradeRequestsSectionProps = {
|
||||||
|
maestroID: number;
|
||||||
|
accountType: number;
|
||||||
|
totalCount: number;
|
||||||
|
upgradeRequests: MaestroDetail["upgradeRequests"];
|
||||||
|
};
|
||||||
|
|
||||||
|
const upgradeOptions = [
|
||||||
|
{ value: ACCOUNT_TYPES.BASIC_20, label: "1만원 (20명)" },
|
||||||
|
{ value: ACCOUNT_TYPES.STANDARD_50, label: "2만원 (50명)" },
|
||||||
|
{ value: ACCOUNT_TYPES.PRO_100, label: "3만원 (100명)" },
|
||||||
|
{ value: ACCOUNT_TYPES.SCHOOL_500, label: "4만원 (500명)" },
|
||||||
|
{ value: ACCOUNT_TYPES.SCHOOL_1000, label: "5만원 (1,000명)" },
|
||||||
|
] as const;
|
||||||
|
|
||||||
|
const selectClass =
|
||||||
|
"h-9 rounded-md border bg-background px-3 text-sm outline-none transition-colors focus:border-ring focus:ring-3 focus:ring-ring/30 disabled:opacity-50";
|
||||||
|
|
||||||
|
export function UpgradeRequestsSection({
|
||||||
|
maestroID,
|
||||||
|
accountType,
|
||||||
|
totalCount,
|
||||||
|
upgradeRequests,
|
||||||
|
}: UpgradeRequestsSectionProps) {
|
||||||
|
const router = useRouter();
|
||||||
|
const [isPending, startTransition] = useTransition();
|
||||||
|
const [isCreating, setIsCreating] = useState(false);
|
||||||
|
const [selectedAccountType, setSelectedAccountType] = useState<number | null>(null);
|
||||||
|
const [errorMessage, setErrorMessage] = useState("");
|
||||||
|
|
||||||
|
const isUpgradeEnabled =
|
||||||
|
selectedAccountType !== null && selectedAccountType > accountType;
|
||||||
|
const isDisabled = isCreating || isPending;
|
||||||
|
|
||||||
|
async function handleUpgrade() {
|
||||||
|
if (selectedAccountType === null) return;
|
||||||
|
if (!window.confirm("업그레이드 신청을 등록하시겠습니까?")) return;
|
||||||
|
|
||||||
|
setIsCreating(true);
|
||||||
|
setErrorMessage("");
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(
|
||||||
|
`/api/maestros/${maestroID}/upgrade-requests`,
|
||||||
|
{
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({ requestedAccountType: selectedAccountType }),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const body = (await response.json().catch(() => null)) as {
|
||||||
|
message?: string;
|
||||||
|
} | null;
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(body?.message ?? "업그레이드 신청 등록에 실패했습니다.");
|
||||||
|
}
|
||||||
|
|
||||||
|
setSelectedAccountType(null);
|
||||||
|
startTransition(() => {
|
||||||
|
router.refresh();
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
setErrorMessage(
|
||||||
|
error instanceof Error
|
||||||
|
? error.message
|
||||||
|
: "업그레이드 신청 등록에 실패했습니다."
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
setIsCreating(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<section className="rounded-lg border bg-background p-5">
|
||||||
|
<div className="flex items-start justify-between gap-2">
|
||||||
|
<div>
|
||||||
|
<h2 className="text-base font-semibold">업그레이드 신청 이력</h2>
|
||||||
|
<p className="mt-1 text-sm text-muted-foreground">
|
||||||
|
전체 {totalCount.toLocaleString()}건 중 최근 20건
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div className="flex flex-col items-end gap-1">
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<select
|
||||||
|
className={selectClass}
|
||||||
|
disabled={isDisabled}
|
||||||
|
onChange={(e) =>
|
||||||
|
setSelectedAccountType(
|
||||||
|
e.target.value === "" ? null : Number(e.target.value)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
value={selectedAccountType ?? ""}
|
||||||
|
>
|
||||||
|
<option value="">요금제 선택</option>
|
||||||
|
{upgradeOptions.map((opt) => (
|
||||||
|
<option
|
||||||
|
disabled={opt.value <= accountType}
|
||||||
|
key={opt.value}
|
||||||
|
value={opt.value}
|
||||||
|
>
|
||||||
|
{opt.label}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
<Button
|
||||||
|
disabled={isDisabled || !isUpgradeEnabled}
|
||||||
|
onClick={() => void handleUpgrade()}
|
||||||
|
size="sm"
|
||||||
|
type="button"
|
||||||
|
variant="outline"
|
||||||
|
>
|
||||||
|
<ArrowUp aria-hidden="true" className="size-4" />
|
||||||
|
업그레이드
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
{errorMessage ? (
|
||||||
|
<p className="text-xs text-destructive">{errorMessage}</p>
|
||||||
|
) : null}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="mt-4 overflow-hidden rounded-lg border">
|
||||||
|
<div className="overflow-x-auto">
|
||||||
|
<table className="w-full min-w-[560px] border-collapse text-sm">
|
||||||
|
<thead className="bg-muted/60">
|
||||||
|
<tr className="border-b">
|
||||||
|
{["신청ID", "현재", "요청", "신청일시", "상태"].map(
|
||||||
|
(header) => (
|
||||||
|
<th
|
||||||
|
className="h-10 px-3 text-left text-xs font-medium text-muted-foreground"
|
||||||
|
key={header}
|
||||||
|
>
|
||||||
|
{header}
|
||||||
|
</th>
|
||||||
|
)
|
||||||
|
)}
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{upgradeRequests.length > 0 ? (
|
||||||
|
upgradeRequests.map((request) => (
|
||||||
|
<tr
|
||||||
|
className="border-b last:border-b-0"
|
||||||
|
key={request.maestroUpgradeID}
|
||||||
|
>
|
||||||
|
<td className="h-11 px-3 align-middle">
|
||||||
|
{request.maestroUpgradeID}
|
||||||
|
</td>
|
||||||
|
<td className="h-11 px-3 align-middle">
|
||||||
|
{getAccountTypeLabel(request.registeredAccountType)}
|
||||||
|
</td>
|
||||||
|
<td className="h-11 px-3 align-middle">
|
||||||
|
{getAccountTypeLabel(request.requestedAccountType)}
|
||||||
|
</td>
|
||||||
|
<td className="h-11 px-3 align-middle">
|
||||||
|
{formatDateTime(request.requestedDateTime)}
|
||||||
|
</td>
|
||||||
|
<td className="h-11 px-3 align-middle">
|
||||||
|
{getRequestStatusLabel(request.status)}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
))
|
||||||
|
) : (
|
||||||
|
<tr>
|
||||||
|
<td
|
||||||
|
className="h-24 px-3 text-center text-sm text-muted-foreground"
|
||||||
|
colSpan={5}
|
||||||
|
>
|
||||||
|
업그레이드 신청 이력이 없습니다.
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
)}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -9,11 +9,11 @@ import {
|
|||||||
formatDateTime,
|
formatDateTime,
|
||||||
getAccountTypeLabel,
|
getAccountTypeLabel,
|
||||||
getActivateStatusLabel,
|
getActivateStatusLabel,
|
||||||
getRequestStatusLabel,
|
|
||||||
} from "@/lib/utils";
|
} from "@/lib/utils";
|
||||||
import { MaestroEditForm } from "./MaestroEditForm";
|
import { MaestroEditForm } from "./MaestroEditForm";
|
||||||
import { StudentsSection } from "./StudentsSection";
|
import { StudentsSection } from "./StudentsSection";
|
||||||
import { ExtensionRequestsSection } from "./ExtensionRequestsSection";
|
import { ExtensionRequestsSection } from "./ExtensionRequestsSection";
|
||||||
|
import { UpgradeRequestsSection } from "./UpgradeRequestsSection";
|
||||||
|
|
||||||
type MaestroDetailPageProps = {
|
type MaestroDetailPageProps = {
|
||||||
params: Promise<{
|
params: Promise<{
|
||||||
@@ -100,23 +100,12 @@ export default async function MaestroDetailPage({
|
|||||||
totalCount={detail.counts.extensionRequests}
|
totalCount={detail.counts.extensionRequests}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<section className="rounded-lg border bg-background p-5">
|
<UpgradeRequestsSection
|
||||||
<h2 className="text-base font-semibold">업그레이드 신청 이력</h2>
|
accountType={maestro.accountType}
|
||||||
<p className="mt-1 text-sm text-muted-foreground">
|
maestroID={maestroID}
|
||||||
전체 {detail.counts.upgradeRequests.toLocaleString()}건 중 최근 20건
|
totalCount={detail.counts.upgradeRequests}
|
||||||
</p>
|
upgradeRequests={detail.upgradeRequests}
|
||||||
<SimpleTable
|
/>
|
||||||
emptyText="업그레이드 신청 이력이 없습니다."
|
|
||||||
headers={["신청ID", "현재", "요청", "신청일시", "상태"]}
|
|
||||||
rows={detail.upgradeRequests.map((request) => [
|
|
||||||
request.maestroUpgradeID,
|
|
||||||
getAccountTypeLabel(request.registeredAccountType),
|
|
||||||
getAccountTypeLabel(request.requestedAccountType),
|
|
||||||
formatDateTime(request.requestedDateTime),
|
|
||||||
getRequestStatusLabel(request.status),
|
|
||||||
])}
|
|
||||||
/>
|
|
||||||
</section>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<section className="rounded-lg border bg-background p-5">
|
<section className="rounded-lg border bg-background p-5">
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
import { z } from "zod";
|
||||||
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
|
import { createUpgradeRequest } from "@/lib/upgrade-requests";
|
||||||
|
import { ApiError } from "@/lib/errors";
|
||||||
|
import { withApiHandler } from "@/lib/api-handler";
|
||||||
|
import { logger } from "@/lib/logger";
|
||||||
|
import { ACCOUNT_TYPES } from "@/lib/constants";
|
||||||
|
|
||||||
|
const CTX = "maestros/[id]/upgrade-requests";
|
||||||
|
|
||||||
|
const paidAccountTypeValues = [
|
||||||
|
ACCOUNT_TYPES.BASIC_20,
|
||||||
|
ACCOUNT_TYPES.STANDARD_50,
|
||||||
|
ACCOUNT_TYPES.PRO_100,
|
||||||
|
ACCOUNT_TYPES.SCHOOL_500,
|
||||||
|
ACCOUNT_TYPES.SCHOOL_1000,
|
||||||
|
] as const;
|
||||||
|
|
||||||
|
const postBodySchema = z.object({
|
||||||
|
requestedAccountType: z.coerce
|
||||||
|
.number()
|
||||||
|
.int()
|
||||||
|
.refine((v) => (paidAccountTypeValues as readonly number[]).includes(v)),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const POST = withApiHandler<{ id: string }>(
|
||||||
|
CTX,
|
||||||
|
async (request, { params, t0 }) => {
|
||||||
|
const { id } = await params;
|
||||||
|
const maestroID = Number(id);
|
||||||
|
|
||||||
|
if (!Number.isInteger(maestroID) || maestroID <= 0) {
|
||||||
|
throw new ApiError("Invalid maestro id", 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
const body = postBodySchema.safeParse(
|
||||||
|
await request.json().catch(() => ({}))
|
||||||
|
);
|
||||||
|
if (!body.success) {
|
||||||
|
throw new ApiError("Invalid request body", 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await createUpgradeRequest(
|
||||||
|
maestroID,
|
||||||
|
body.data.requestedAccountType
|
||||||
|
);
|
||||||
|
|
||||||
|
logger.info(CTX, "created", {
|
||||||
|
maestroID,
|
||||||
|
maestroUpgradeID: result.maestroUpgradeID,
|
||||||
|
requestedAccountType: body.data.requestedAccountType,
|
||||||
|
duration: Date.now() - t0,
|
||||||
|
});
|
||||||
|
|
||||||
|
return NextResponse.json(result, { status: 201 });
|
||||||
|
}
|
||||||
|
);
|
||||||
@@ -167,6 +167,44 @@ export async function approveUpgradeRequest(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function createUpgradeRequest(
|
||||||
|
maestroID: number,
|
||||||
|
requestedAccountType: number
|
||||||
|
): Promise<{ maestroUpgradeID: number; registeredAccountType: number; requestedDateTime: string; status: number }> {
|
||||||
|
const maestro = await db.maestro.findUnique({
|
||||||
|
where: { MaestroID: maestroID },
|
||||||
|
select: { ActivateStatus: true, AccountType: true },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!maestro) {
|
||||||
|
throw new ApiError("마에스트로를 찾을 수 없습니다.", 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
const request = await db.maestro_upgrade.create({
|
||||||
|
data: {
|
||||||
|
MaestroID: maestroID,
|
||||||
|
RegisteredActivateStatus: maestro.ActivateStatus,
|
||||||
|
RegisteredAccountType: maestro.AccountType,
|
||||||
|
RequestedAccountType: requestedAccountType,
|
||||||
|
RequestedDateTime: new Date(),
|
||||||
|
Status: REQUEST_STATUSES.REQUESTED,
|
||||||
|
},
|
||||||
|
select: {
|
||||||
|
MaestroUpgradeID: true,
|
||||||
|
RegisteredAccountType: true,
|
||||||
|
RequestedDateTime: true,
|
||||||
|
Status: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
maestroUpgradeID: request.MaestroUpgradeID,
|
||||||
|
registeredAccountType: request.RegisteredAccountType,
|
||||||
|
requestedDateTime: request.RequestedDateTime.toISOString(),
|
||||||
|
status: request.Status,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export async function rejectUpgradeRequest(
|
export async function rejectUpgradeRequest(
|
||||||
maestroUpgradeID: number
|
maestroUpgradeID: number
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
|
|||||||
Reference in New Issue
Block a user