233 lines
7.7 KiB
TypeScript
233 lines
7.7 KiB
TypeScript
"use client";
|
|
|
|
import { keepPreviousData, useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import { nextApi } from "@/lib/http-client";
|
|
import { qk } from "@/query/key/keys";
|
|
import type {
|
|
BulkSendInfo,
|
|
ChannelType,
|
|
SendResultSummaryRequest,
|
|
SendResultSummaryResponse,
|
|
Template,
|
|
TemplateSummary,
|
|
} from "@kri/common/noti-hub";
|
|
import type { FileResponse, PageRequest, PageResponse } from "@kri/common";
|
|
import type { ProjectMainTemplateUpdateRequest } from "@/types/project";
|
|
import type {
|
|
MessageBulkSendResponse,
|
|
MessageSendBulkRequest,
|
|
MessageSendChannel,
|
|
MessageSendSingleRequest,
|
|
SendResultInfo,
|
|
} from "@/types/message";
|
|
|
|
export function useTemplateListQuery(chnnlTy: ChannelType, itrntPrjctCd: string, enabled: boolean) {
|
|
return useQuery({
|
|
queryKey: qk.templateList(chnnlTy, itrntPrjctCd),
|
|
queryFn: () => nextApi.get<TemplateSummary[]>(`/message-template/list/${chnnlTy}/${itrntPrjctCd}`),
|
|
enabled: enabled && !!itrntPrjctCd,
|
|
staleTime: 30_000,
|
|
meta: { ignoreGlobalLoading: true },
|
|
});
|
|
}
|
|
|
|
export function useMainTemplateIdxQuery(prjctIdx: number, chnnlTy: ChannelType, enabled: boolean) {
|
|
return useQuery({
|
|
queryKey: qk.mainTemplate(prjctIdx, chnnlTy),
|
|
queryFn: () => nextApi.get<number | null>(`/project/main-template/${prjctIdx}/${chnnlTy}`),
|
|
enabled: enabled && prjctIdx > 0,
|
|
staleTime: 0,
|
|
refetchOnMount: "always",
|
|
});
|
|
}
|
|
|
|
export function useTemplateQuery(tmplatIdx: number, enabled: boolean) {
|
|
return useQuery({
|
|
queryKey: qk.template(tmplatIdx),
|
|
queryFn: () => nextApi.get<Template>(`/message-template/${tmplatIdx}`),
|
|
enabled: enabled && tmplatIdx > 0,
|
|
staleTime: 0,
|
|
refetchOnMount: "always",
|
|
// meta: { ignoreGlobalLoading: true },
|
|
});
|
|
}
|
|
|
|
export function useSaveTemplateMutation(chnnlTy: ChannelType, itrntPrjctCd: string) {
|
|
const qc = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: async (v: {
|
|
method: "put" | "patch";
|
|
template: Template;
|
|
newFiles?: File[];
|
|
deleteFileNames?: string[];
|
|
}) => {
|
|
const { method, template, newFiles = [], deleteFileNames = [] } = v;
|
|
|
|
const formData = new FormData();
|
|
formData.append(
|
|
"template",
|
|
new Blob([JSON.stringify(template)], { type: "application/json" }),
|
|
);
|
|
newFiles.forEach((file) => formData.append("files", file));
|
|
deleteFileNames.forEach((name) => formData.append("deleteFileNames", name));
|
|
|
|
return method === "put"
|
|
? nextApi.putForm<Template>(`/message-template`, formData)
|
|
: nextApi.request<Template>(`/message-template`, { method: "PATCH", form: formData });
|
|
},
|
|
onSuccess: (res) => {
|
|
if (res.tmplatIdx) {
|
|
qc.setQueryData(qk.template(res.tmplatIdx), res);
|
|
}
|
|
qc.invalidateQueries({ queryKey: qk.templateList(chnnlTy, itrntPrjctCd) });
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 템플릿 첨부파일 단건 다운로드 (GET /template/{tmplatIdx}/attachment?fileName=...)
|
|
*/
|
|
export function useDownloadTemplateAttachmentMutation(tmplatIdx: number) {
|
|
return useMutation({
|
|
mutationFn: (fileName: string) =>
|
|
nextApi.get<ArrayBuffer>(`/message-template/${tmplatIdx}/attachment?fileName=${encodeURIComponent(fileName)}`, {
|
|
expectBinary: true,
|
|
}),
|
|
});
|
|
}
|
|
|
|
export function useUpdateMainTemplateMutation(prjctIdx: number, chnnlTy: ChannelType) {
|
|
const qc = useQueryClient();
|
|
const key = qk.mainTemplate(prjctIdx, chnnlTy);
|
|
|
|
return useMutation({
|
|
mutationFn: async (req: ProjectMainTemplateUpdateRequest) => {
|
|
await nextApi.patch(`/project/main-template`, req);
|
|
return req.tmplatIdx ?? null;
|
|
},
|
|
onMutate: async (req) => {
|
|
const prev = qc.getQueryData<number | null>(key) ?? null;
|
|
qc.setQueryData<number | null>(key, req.tmplatIdx ?? null);
|
|
return { prev };
|
|
},
|
|
onError: (_err, _req, ctx) => {
|
|
if (ctx?.prev !== undefined) qc.setQueryData<number | null>(key, ctx.prev);
|
|
},
|
|
onSettled: () => {
|
|
qc.invalidateQueries({ queryKey: key });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useUploadTemplateBodyImageMutation(itrntPrjctCd: string) {
|
|
return useMutation({
|
|
mutationFn: async (file: File): Promise<string> => {
|
|
const formData = new FormData();
|
|
formData.append("file", file);
|
|
return nextApi.putForm<string>(`/message-template/body-image/${itrntPrjctCd}`, formData);
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 대량발송 리스트 조회 (GET /report/bulk-list/{sendTy}/{reqstSvc}/{prjctCd})
|
|
*/
|
|
export function useBulkSendListQuery(sendTy: number, reqstSvc: string, prjctCd: string, enabled: boolean) {
|
|
return useQuery({
|
|
queryKey: qk.bulkSendList(sendTy, reqstSvc, prjctCd),
|
|
queryFn: () => nextApi.get<BulkSendInfo[]>(`/message-report/bulk-list/${sendTy}/${reqstSvc}/${prjctCd}`),
|
|
enabled: enabled && !!reqstSvc && !!prjctCd,
|
|
staleTime: 0,
|
|
refetchOnMount: "always",
|
|
meta: { ignoreGlobalLoading: true },
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 대량발송 상세 결과 조회 (POST /report/bulk-detail/{bulkSendKey})
|
|
*/
|
|
export function useBulkSendDetailQuery(bulkSendKey: string, pageRequest: PageRequest, enabled: boolean) {
|
|
return useQuery({
|
|
queryKey: qk.bulkSendDetail(bulkSendKey, pageRequest),
|
|
queryFn: () => nextApi.post<PageResponse<SendResultInfo>>(`/message-report/bulk-detail/${bulkSendKey}`, pageRequest),
|
|
enabled: enabled && !!bulkSendKey,
|
|
staleTime: 0,
|
|
placeholderData: keepPreviousData,
|
|
meta: { ignoreGlobalLoading: true },
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 대량발송 결과 엑셀 다운로드 (POST /report/bulk-detail/excel/{bulkSendKey})
|
|
*/
|
|
export function useBulkSendDetailExcelMutation() {
|
|
return useMutation({
|
|
mutationFn: (bulkSendKey: string) =>
|
|
nextApi.post<FileResponse>(`/message-report/bulk-detail-excel/${bulkSendKey}`, undefined),
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 발송 결과 축약 조회 (POST /report/send-result-summary)
|
|
*/
|
|
export function useSendResultSummaryQuery(request: SendResultSummaryRequest, enabled: boolean) {
|
|
return useQuery({
|
|
queryKey: qk.sendResultSummary(request),
|
|
queryFn: () => nextApi.post<SendResultSummaryResponse[]>(`/message-report/send-result-summary`, request),
|
|
enabled,
|
|
staleTime: 0,
|
|
meta: { ignoreGlobalLoading: true },
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 단건 발송 (POST /message/{channel}/single/{prjctIdx}, contactServer 경유)
|
|
*/
|
|
export function useSendSingleMessageMutation(channel: MessageSendChannel, prjctIdx: number) {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: (req: MessageSendSingleRequest) =>
|
|
nextApi.post<void>(`/message/${channel}/single/${prjctIdx}`, req),
|
|
onSuccess: (_data, req) => {
|
|
queryClient.invalidateQueries({ queryKey: ["message-report", "send-result", String(req.sid)] });
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 대량 발송 (POST /message/{channel}/bulk/{prjctIdx}, contactServer 경유)
|
|
*/
|
|
export function useSendBulkMessageMutation(channel: MessageSendChannel, prjctIdx: number) {
|
|
return useMutation({
|
|
mutationFn: (req: MessageSendBulkRequest) =>
|
|
nextApi.post<MessageBulkSendResponse>(`/message/${channel}/bulk/${prjctIdx}`, req),
|
|
});
|
|
}
|
|
|
|
/**
|
|
* SID 단위 발송 결과 목록 조회 (GET /report/send-result/{sid}/{prjctCd}/{reqstSvc})
|
|
*/
|
|
export function useSendResultBySidQuery(
|
|
sid: string,
|
|
prjctCd: string,
|
|
reqstSvc: string,
|
|
channelTy: ChannelType | undefined,
|
|
enabled: boolean,
|
|
) {
|
|
return useQuery({
|
|
queryKey: qk.sendResultBySid(sid, prjctCd, reqstSvc, channelTy),
|
|
queryFn: () => {
|
|
const query = channelTy !== undefined ? `?channelTy=${channelTy}` : "";
|
|
return nextApi.get<SendResultInfo[]>(
|
|
`/message-report/send-result/${encodeURIComponent(sid)}/${prjctCd}/${reqstSvc}${query}`,
|
|
);
|
|
},
|
|
enabled: enabled && !!sid && !!prjctCd && !!reqstSvc,
|
|
staleTime: 0,
|
|
refetchOnMount: "always",
|
|
meta: { ignoreGlobalLoading: true },
|
|
});
|
|
}
|