Files
frontend/apps/st/src/components/module/post-submit-verification-results.ts
T

86 lines
2.7 KiB
TypeScript

import { statisticsApi } from "@/entities/statistics/api/statistics-service";
import type { VerificationResultListResponse } from "@/entities/statistics/model/statistics-interface";
import type { VerificationResultDialogQueryContext } from "./VerificationResultDialog";
const POST_SUBMIT_VRFC_MTHD_CODES = ["NV", "IMR", "HB"] as const;
export const POST_SUBMIT_COMPLETE_MESSAGE = "제출 완료되었습니다.";
export type OpenVerificationResultDialogOptions = {
notify?: (message: string) => void;
/** 미통과 검증 결과가 없을 때 제출 완료 알림 (기본 true, IMR 오류 catch 등에서는 false) */
notifySubmitComplete?: boolean;
submitCompleteMessage?: string;
};
function responseHasVerificationResults(response: VerificationResultListResponse | null): boolean {
const total = response?.data?.pageInfo?.totalElements;
if (typeof total === "number" && total > 0) return true;
const list = response?.data?.verifyResultList;
return Array.isArray(list) && list.length > 0;
}
/**
* 제출 직후 `VerificationResultDialog`에 표시할 미통과(prcsYn=0) 검증 결과가
* 일반검증(NV)·I-MR(IMR)·HB 중 하나라도 있는지 조회합니다.
*/
export async function hasAnyPostSubmitVerificationResult(
ctx: VerificationResultDialogQueryContext,
): Promise<boolean> {
const base = {
sewerBzmnCd: ctx.sewerBzmnCd,
ctpvNm: ctx.ctpvNm,
sggNm: ctx.sggNm,
statsYr: ctx.statsYr,
statsCd: ctx.statsCd,
prcsYnList: "0",
rejectCodeList: "0,1,2",
page: 1,
size: 1,
};
const responses = await Promise.all(
POST_SUBMIT_VRFC_MTHD_CODES.map((vrfcMthdCd) =>
statisticsApi.getVerificationResultList({ ...base, vrfcMthdCd }),
),
);
return responses.some(responseHasVerificationResults);
}
/**
* 미통과 검증 결과가 있으면 다이얼로그를 열고, 없으면 제출 완료 알림을 표시합니다.
* @returns 다이얼로그를 열었으면 true
*/
export async function openVerificationResultDialogIfNeeded(
ctx: VerificationResultDialogQueryContext | null | undefined,
setOpen: (open: boolean) => void,
options?: OpenVerificationResultDialogOptions,
): Promise<boolean> {
const {
notify,
notifySubmitComplete = true,
submitCompleteMessage = POST_SUBMIT_COMPLETE_MESSAGE,
} = options ?? {};
const notifyComplete = () => {
if (notifySubmitComplete && notify) {
notify(submitCompleteMessage);
}
};
if (!ctx) {
notifyComplete();
return false;
}
if (await hasAnyPostSubmitVerificationResult(ctx)) {
setOpen(true);
return true;
}
notifyComplete();
return false;
}