Files
contact-front/src/app/(pages)/(main)/progress/message/page.tsx

140 lines
5.6 KiB
TypeScript

'use client'
import { useMemo, useState } from "react";
import { useMounted } from "@kri/common/hooks";
import AppPageTitle from "@/components/app-shell/AppPageTitle";
import { LoadingOverlayAdapter } from "@/components/app-shell/LoadingOverlayAdapter";
import KriDateRangePicker from "@/components/common/KriDateRangePicker";
import { useSelectedProject } from "@/hooks/useSelectedProject";
import { useSendResultSummaryQuery } from "@/query/message";
import { clientEnv } from "@/lib/env.client";
import { MessageType } from "@kri/common/noti-hub";
import type { SendResultSummaryResponse } from "@kri/common/noti-hub";
const SEND_TYPE_ORDER = [MessageType.SMS, MessageType.LMS_MMS, MessageType.EMAIL, MessageType.KKO] as const;
const SEND_TYPE_LABEL: Record<number, string> = {
[MessageType.SMS]: "단문",
[MessageType.LMS_MMS]: "장문",
[MessageType.EMAIL]: "메일",
[MessageType.KKO]: "알림톡",
};
type SummaryRow = {
sendTy: number;
price: number;
totalCount: number;
cost: number;
successCount: number;
failCount: number;
standByCount: number;
};
const sum = (list: SendResultSummaryResponse[], field: keyof SendResultSummaryResponse) =>
list.reduce((acc, item) => acc + (item[field] as number | undefined ?? 0), 0);
const buildRows = (data: SendResultSummaryResponse[]): SummaryRow[] =>
SEND_TYPE_ORDER.map((sendTy) => {
const matched = data.filter((item) => item.sendTy === sendTy);
return {
sendTy,
price: matched[0]?.price ?? 0,
totalCount: sum(matched, "totalCount"),
cost: sum(matched, "cost"),
successCount: sum(matched, "successCount"),
failCount: sum(matched, "failCount"),
standByCount: sum(matched, "standByCount"),
};
});
const moneyUnit = (value: number) => `${value.toLocaleString()}원`;
const caseUnit = (value: number) => `${value.toLocaleString()}건`;
const MessageProgressPage = () => {
const mounted = useMounted();
const { selectedProject } = useSelectedProject();
const [dateRange, setDateRange] = useState({ from: "", to: "" });
const prjctCd = selectedProject?.prjctCd ?? "";
const itrntPrjctCd = selectedProject?.itrntPrjctCd ?? "";
const enabled = mounted && !!prjctCd;
const summaryQuery = useSendResultSummaryQuery(
{
reqstSvc: clientEnv.REQUEST_SVC,
itrntPrjctCd,
prjctCd,
sendStartDt: dateRange.from ? `${dateRange.from}T00:00:00` : undefined,
sendEndDt: dateRange.to ? `${dateRange.to}T23:59:59` : undefined,
},
enabled,
);
const rows = useMemo(() => buildRows(summaryQuery.data ?? []), [summaryQuery.data]);
const total = useMemo(
() => ({
totalCount: rows.reduce((acc, row) => acc + row.totalCount, 0),
cost: rows.reduce((acc, row) => acc + row.cost, 0),
successCount: rows.reduce((acc, row) => acc + row.successCount, 0),
failCount: rows.reduce((acc, row) => acc + row.failCount, 0),
standByCount: rows.reduce((acc, row) => acc + row.standByCount, 0),
}),
[rows],
);
if (!mounted) return <LoadingOverlayAdapter force />;
return (
<div className="space-y-6 text-sm">
<AppPageTitle titleText="발송 현황" />
<KriDateRangePicker value={dateRange} onChange={setDateRange} className="w-80" />
<div className="rounded-md border overflow-hidden">
<table className="w-full text-sm">
<thead className="bg-primary text-primary-foreground">
<tr>
<th className="h-10 px-3 text-center font-semibold border-b">유형</th>
<th className="h-10 px-3 text-center font-semibold border-b">단가</th>
<th className="h-10 px-3 text-center font-semibold border-b">대상수</th>
<th className="h-10 px-3 text-center font-semibold border-b">비용</th>
<th className="h-10 px-3 text-center font-semibold border-b">성공</th>
<th className="h-10 px-3 text-center font-semibold border-b">실패</th>
<th className="h-10 px-3 text-center font-semibold border-b">발송중</th>
</tr>
</thead>
<tbody>
{rows.map((row) => (
<tr key={row.sendTy} className="border-b last:border-b-0">
<td className="h-10 px-3 text-center">{SEND_TYPE_LABEL[row.sendTy]}</td>
<td className="h-10 px-3 text-center">{moneyUnit(row.price)}</td>
<td className="h-10 px-3 text-center">{caseUnit(row.totalCount)}</td>
<td className="h-10 px-3 text-center">{moneyUnit(row.cost)}</td>
<td className="h-10 px-3 text-center">{caseUnit(row.successCount)}</td>
<td className="h-10 px-3 text-center">{caseUnit(row.failCount)}</td>
<td className="h-10 px-3 text-center">{caseUnit(row.standByCount)}</td>
</tr>
))}
<tr className="bg-gray-50 font-semibold">
<td className="h-10 px-3 text-center" colSpan={2}>합계</td>
<td className="h-10 px-3 text-center">{caseUnit(total.totalCount)}</td>
<td className="h-10 px-3 text-center">{moneyUnit(total.cost)}</td>
<td className="h-10 px-3 text-center">{caseUnit(total.successCount)}</td>
<td className="h-10 px-3 text-center">{caseUnit(total.failCount)}</td>
<td className="h-10 px-3 text-center">{caseUnit(total.standByCount)}</td>
</tr>
</tbody>
</table>
</div>
<p className="text-xs text-muted-foreground">
※ 보낸 내역을 토대로 계산된 값으로 실제 정산 금액과 차이가 발생할 수 있습니다.
</p>
</div>
);
};
export default MessageProgressPage;