156 行
4.1 KiB
TypeScript
156 行
4.1 KiB
TypeScript
import { apiRequestData } from "./http";
|
|
|
|
export type NotificationChannelType = "wecom" | "wechat_work";
|
|
export type NotificationChannelMode = "mock" | "wecom_webhook" | "wecom_app_message";
|
|
export type NotificationDomainScope =
|
|
| "workflow"
|
|
| "reimbursement"
|
|
| "leave"
|
|
| "procurement"
|
|
| "travel"
|
|
| "asset"
|
|
| "inventory";
|
|
|
|
export type NotificationChannelRouting = {
|
|
roleCodes: string[];
|
|
domainScopes: NotificationDomainScope[];
|
|
allowedTypes: string[];
|
|
};
|
|
|
|
export type NotificationChannelConfig = {
|
|
mode: NotificationChannelMode;
|
|
webhookUrl: string | null;
|
|
timeoutMs: number;
|
|
routing: NotificationChannelRouting;
|
|
secretConfigured: boolean;
|
|
secretMasked: string | null;
|
|
};
|
|
|
|
export type NotificationChannelWriteConfig = {
|
|
mode: NotificationChannelMode;
|
|
webhookUrl: string | null;
|
|
secret?: string | null;
|
|
clearSecret?: boolean;
|
|
timeoutMs: number;
|
|
routing: NotificationChannelRouting;
|
|
};
|
|
|
|
export type NotificationChannelTemplate = {
|
|
title: string;
|
|
body: string;
|
|
footer: string;
|
|
deepLinkLabel: string;
|
|
};
|
|
|
|
export type NotificationChannel = {
|
|
id: string;
|
|
channelType: NotificationChannelType;
|
|
name: string;
|
|
enabled: boolean;
|
|
configJson: NotificationChannelConfig;
|
|
templateJson: NotificationChannelTemplate;
|
|
createdAt: string | null;
|
|
updatedAt: string | null;
|
|
};
|
|
|
|
export type NotificationChannelListResponse = {
|
|
items: NotificationChannel[];
|
|
};
|
|
|
|
export type NotificationChannelPayload = {
|
|
channelType: "wecom";
|
|
name: string;
|
|
enabled: boolean;
|
|
configJson: NotificationChannelWriteConfig;
|
|
templateJson: NotificationChannelTemplate;
|
|
};
|
|
|
|
export type NotificationChannelUpdatePayload = {
|
|
name?: string;
|
|
enabled?: boolean;
|
|
configJson?: NotificationChannelWriteConfig;
|
|
templateJson?: NotificationChannelTemplate;
|
|
};
|
|
|
|
export type NotificationChannelTestResponse = {
|
|
channel: NotificationChannel;
|
|
testResult: {
|
|
ok: boolean;
|
|
code: string;
|
|
message: string | null;
|
|
retryable: boolean | null;
|
|
previewMessage: string | null;
|
|
response: unknown;
|
|
skipped: boolean;
|
|
};
|
|
};
|
|
|
|
export type NotificationDeliveryStatus = "pending" | "success" | "failed";
|
|
|
|
export type NotificationDeliveryRecord = {
|
|
notificationId: string;
|
|
channelId: string;
|
|
channelName: string;
|
|
channelType: NotificationChannelType;
|
|
notificationType: string;
|
|
notificationTitle: string;
|
|
status: NotificationDeliveryStatus;
|
|
attemptCount: number;
|
|
latestAttemptAt: string | null;
|
|
resultCode: string | null;
|
|
errorMessage: string | null;
|
|
responseBody: unknown;
|
|
notificationCreatedAt: string | null;
|
|
};
|
|
|
|
export type NotificationDeliveryListResponse = {
|
|
items: NotificationDeliveryRecord[];
|
|
};
|
|
|
|
export async function fetchNotificationChannels() {
|
|
return apiRequestData<NotificationChannelListResponse>("/api/notifications/channels");
|
|
}
|
|
|
|
export async function createNotificationChannel(payload: NotificationChannelPayload) {
|
|
return apiRequestData<{ channel: NotificationChannel }>("/api/notifications/channels", {
|
|
method: "POST",
|
|
body: JSON.stringify(payload),
|
|
});
|
|
}
|
|
|
|
export async function updateNotificationChannel(id: string, payload: NotificationChannelUpdatePayload) {
|
|
return apiRequestData<{ channel: NotificationChannel }>(`/api/notifications/channels/${id}`, {
|
|
method: "PATCH",
|
|
body: JSON.stringify(payload),
|
|
});
|
|
}
|
|
|
|
export async function testNotificationChannel(id: string) {
|
|
return apiRequestData<NotificationChannelTestResponse>(`/api/notifications/channels/${id}/test`, {
|
|
method: "POST",
|
|
body: JSON.stringify({}),
|
|
});
|
|
}
|
|
|
|
export async function fetchNotificationDeliveries(filters: {
|
|
notificationId?: string | null;
|
|
channelId?: string | null;
|
|
status?: NotificationDeliveryStatus | "" | null;
|
|
}) {
|
|
const params = new URLSearchParams();
|
|
if (filters.notificationId) {
|
|
params.set("notificationId", filters.notificationId);
|
|
}
|
|
if (filters.channelId) {
|
|
params.set("channelId", filters.channelId);
|
|
}
|
|
if (filters.status) {
|
|
params.set("status", filters.status);
|
|
}
|
|
|
|
const queryString = params.toString();
|
|
return apiRequestData<NotificationDeliveryListResponse>(
|
|
`/api/notifications/deliveries${queryString ? `?${queryString}` : ""}`,
|
|
);
|
|
}
|