Files
frontend/apps/sn/src/features/dredging/api/response-adapters.ts
T
2026-09-15 10:00:42 +09:00

84 lines
2.8 KiB
TypeScript

import type { DredgingPhoto, DredgingRecord, PipeMatch } from "../model/types";
type NearestPipeResponseItem = {
locationKey: string;
failureCode?: "PIPE_NOT_FOUND" | string | null;
failureMessage?: string | null;
selected?: {
fid: string;
mngInstCd: string;
fcltMngNo: string;
pipeDiameterMm?: number;
pipeLengthM?: number;
pipeMaterial?: string;
installationYear?: number | null;
swtrmtZoneFid?: string | null;
swtrmtZoneNm?: string | null;
distanceMeters: number;
snappedLongitude: number;
snappedLatitude: number;
} | null;
};
const nearestPipeFailureMessage = (item: NearestPipeResponseItem) => {
switch (item.failureCode) {
case "PIPE_NOT_FOUND":
return "50m 이내에 관로가 없습니다. 지도에서 준설위치를 다시 확인해주세요.";
default:
return (
item.failureMessage?.trim() ??
"최근접 관로를 자동으로 연결하지 못했습니다."
);
}
};
export type DredgingRecordListResponse = {
items: DredgingRecord[];
total: number;
page: number;
size: number;
};
/** 목록의 기존 raw/wrapper와 페이지 필드를 그대로 유지하는 응답 경계다. */
export const toDredgingRecordPage = (body: any): DredgingRecordListResponse =>
(body?.data ?? body) as DredgingRecordListResponse;
/** 사진 목록의 기존 null-to-empty fallback만 적용하며 첨부·촬영 원본 필드는 보존한다. */
export const toDredgingPhotos = (body: any): DredgingPhoto[] =>
(body?.data ?? body ?? []) as DredgingPhoto[];
/** 최근접 관로 응답의 wire 필드와 기본값을 화면 PipeMatch 모델로 명시적으로 매핑한다. */
export const toNearestPipeMatches = (body: any): Map<string, PipeMatch> => {
const items = (body?.data ?? body) as NearestPipeResponseItem[];
const matches = new Map<string, PipeMatch>();
const failureMessages: string[] = [];
for (const item of items ?? []) {
if (!item.selected) {
failureMessages.push(nearestPipeFailureMessage(item));
continue;
}
matches.set(item.locationKey, {
fid: item.selected.fid,
mngInstCd: item.selected.mngInstCd,
fcltMngNo: item.selected.fcltMngNo,
pipeDiameterMm: item.selected.pipeDiameterMm ?? 0,
pipeLengthM: item.selected.pipeLengthM ?? 0,
pipeMaterial: item.selected.pipeMaterial ?? "",
installationYear: item.selected.installationYear ?? null,
swtrmtZoneFid: item.selected.swtrmtZoneFid ?? null,
swtrmtZoneNm: item.selected.swtrmtZoneNm ?? "",
distanceMeters: item.selected.distanceMeters,
snappedLongitude: item.selected.snappedLongitude,
snappedLatitude: item.selected.snappedLatitude,
isMock: false,
});
}
if (failureMessages.length > 0) {
throw new Error(Array.from(new Set(failureMessages)).join(" "));
}
return matches;
};