Files
contact-front/src/components/list-contact/listContactVariables.ts

57 lines
2.1 KiB
TypeScript

import { LIST_IS_USE_LABEL } from '@/components/table-column/ListTableColumn'
import { ListHeaderConst } from '@/const/listConst'
import type { listHeader, ListSearchWithFullResult } from '@/types/list'
import type { ProjectSurvey } from '@/types/project'
import { isAssignHeaderType } from '@/utils/listAccess'
import { substituteVariables } from '@/lib/variableSubstitution'
export const resolveListHeaderValue = (
header: listHeader,
detail?: ListSearchWithFullResult,
): string => {
if (header.listHderTy === ListHeaderConst.MEMO) return detail?.memo ?? ''
if (header.listHderTy === ListHeaderConst.IS_USE) {
return detail?.use != null ? (LIST_IS_USE_LABEL[detail.use] ?? '') : ''
}
if (isAssignHeaderType(header.listHderTy)) {
return detail?.assignList?.[header.listHderTy] ?? ''
}
return detail?.itemList?.[header.listHderId] ?? ''
}
export const buildListContactVariableValues = (
listHeaders: listHeader[],
detail: ListSearchWithFullResult | undefined,
sid: string,
): Record<string, string> => {
const values: Record<string, string> = {}
// 화면 노출 여부와 무관하게 전체 헤더 값을 치환 대상으로 사용한다.
for (const header of listHeaders) {
if (header.listHderTy === ListHeaderConst.SID) continue
values[header.listHderNm] = resolveListHeaderValue(header, detail)
}
// 같은 이름의 커스텀 헤더가 있어도 예약 토큰 SID에는 실제 SID를 사용한다.
values.SID = sid
return values
}
// 알림톡 변수 값: 헤더 값에 더해 설문명(#{설문명})을 대상자 값으로 채운 설문 URL로 치환한다.
// 같은 이름의 헤더가 있으면 헤더 값을 우선한다.
export const buildKkoVariableValues = (
headerValues: Record<string, string>,
surveys: ProjectSurvey[],
): Record<string, string> => {
const values: Record<string, string> = { ...headerValues }
for (const survey of surveys) {
const surveyName = survey.surveyName?.trim()
const surveyUrl = survey.surveyUrl?.trim()
if (!surveyName || !surveyUrl || surveyName in values) continue
values[surveyName] = substituteVariables(surveyUrl, headerValues)
}
return values
}