Files
frontend/apps/as/src/widgets/pbadms-sprt/lib/attachments-form-utils.ts
T

83 lines
2.4 KiB
TypeScript

import { getDefaultFileKey } from "@/shared/lib";
import type { EsntlAtchFileMngTreeResponseDto } from "@/shared/model";
export type FieldPrefix =
| "fnrsCnsltnMng"
| "instlSncnMng"
| "designCnstnMng"
| "sewerBscPlan"
| "sewerBscPlanDtl";
export const ETC_FILE_KEY_PREFIX = "etc::";
export interface EsntlAtchFileListItem {
esntlAtchFileId: string;
atchFileGroupId: string;
}
export function atchFileGroupIdPath(fieldPrefix: FieldPrefix): string {
return `${fieldPrefix}.atchFileGroupId`;
}
export function createUploadKey(esntlAtchFileId: string, file: File): string {
return `${esntlAtchFileId}::${getDefaultFileKey(file)}`;
}
export function createEtcUploadKey(file: File): string {
return `${ETC_FILE_KEY_PREFIX}${getDefaultFileKey(file)}`;
}
export function isEtcFileKey(fileKey: string): boolean {
return fileKey.startsWith(ETC_FILE_KEY_PREFIX);
}
export function toFileAccept(rmrk: string | null | undefined): string {
if (!rmrk?.trim()) return "";
return rmrk
.split(",")
.map((ext) => ext.trim().toLowerCase())
.filter(Boolean)
.map((ext) => (ext.startsWith(".") ? ext : `.${ext}`))
.join(",");
}
export function getEsntlAtchFileGroupId(item: EsntlAtchFileMngTreeResponseDto): string {
return item.atchFileList?.[0]?.atflCd ?? "";
}
export function toEsntlAtchFileSaveList(
esntlFileList?: EsntlAtchFileMngTreeResponseDto[],
): EsntlAtchFileListItem[] {
const esntlMap = new Map<string, string>();
for (const item of esntlFileList ?? []) {
const atchFileGroupId = getEsntlAtchFileGroupId(item);
if (!item.esntlAtchFileId || !atchFileGroupId) continue;
esntlMap.set(item.esntlAtchFileId, atchFileGroupId);
}
return Array.from(esntlMap.entries()).map(([esntlAtchFileId, atchFileGroupId]) => ({
esntlAtchFileId,
atchFileGroupId,
}));
}
export function isFormEsntlAtchComplete(
essentialFiles: EsntlAtchFileMngTreeResponseDto[],
esntlAtchFileList: EsntlAtchFileListItem[],
): boolean {
if (essentialFiles.length === 0) return true;
const requiredFiles = essentialFiles.filter((file) => file.esntlAtchYn === "Y");
if (requiredFiles.length === 0) return true;
const attachedIds = new Set(
esntlAtchFileList
.filter((item) => Boolean(item.atchFileGroupId))
.map((item) => item.esntlAtchFileId),
);
return requiredFiles.every((file) => attachedIds.has(file.esntlAtchFileId));
}