104 lines
3.2 KiB
TypeScript
104 lines
3.2 KiB
TypeScript
import type {
|
|
ApiSettings,
|
|
MailConnectionTestResponse,
|
|
MailCredentialEnvelope,
|
|
MailImapFolderListResponse,
|
|
MailServerProfile,
|
|
MockMailboxMessageResponse
|
|
} from "@govoplan/core-webui";
|
|
import { apiFetch, apiGetList, apiPath, apiPost } from "./client";
|
|
|
|
const profileActionEndpoints = {
|
|
smtp: "test-smtp",
|
|
imap: "test-imap",
|
|
folders: "list-imap-folders"
|
|
} as const;
|
|
|
|
function runProfileAction<TResponse>(
|
|
settings: ApiSettings,
|
|
profileId: string,
|
|
action: keyof typeof profileActionEndpoints,
|
|
serverId?: string | null,
|
|
credentialId?: string | null,
|
|
campaignId?: string | null
|
|
): Promise<TResponse> {
|
|
return apiPost<TResponse>(
|
|
settings,
|
|
apiPath(
|
|
`/api/v1/mail/profiles/${encodeURIComponent(profileId)}/${profileActionEndpoints[action]}`,
|
|
{
|
|
server_id: serverId,
|
|
credential_id: credentialId,
|
|
campaign_id: campaignId
|
|
}
|
|
)
|
|
);
|
|
}
|
|
|
|
export async function listMailServerProfiles(settings: ApiSettings, includeInactive = false, campaignId?: string): Promise<MailServerProfile[]> {
|
|
return apiGetList<MailServerProfile, "profiles">(settings, "/api/v1/mail/profiles", "profiles", {
|
|
include_inactive: includeInactive ? true : undefined,
|
|
campaign_id: campaignId
|
|
});
|
|
}
|
|
|
|
export function createCampaignMailCredential(
|
|
settings: ApiSettings,
|
|
profileId: string,
|
|
campaignId: string,
|
|
payload: {
|
|
name: string;
|
|
username: string;
|
|
password: string;
|
|
server_ids: string[];
|
|
}
|
|
): Promise<MailCredentialEnvelope> {
|
|
return apiFetch<MailCredentialEnvelope>(
|
|
settings,
|
|
apiPath(
|
|
`/api/v1/mail/profiles/${encodeURIComponent(profileId)}/campaign-credentials`,
|
|
{ campaign_id: campaignId }
|
|
),
|
|
{
|
|
method: "POST",
|
|
body: JSON.stringify(payload)
|
|
}
|
|
);
|
|
}
|
|
|
|
export async function testMailProfileSmtp(
|
|
settings: ApiSettings,
|
|
profileId: string,
|
|
serverId?: string | null,
|
|
credentialId?: string | null,
|
|
campaignId?: string | null
|
|
): Promise<MailConnectionTestResponse> {
|
|
return runProfileAction<MailConnectionTestResponse>(settings, profileId, "smtp", serverId, credentialId, campaignId);
|
|
}
|
|
|
|
export async function testMailProfileImap(
|
|
settings: ApiSettings,
|
|
profileId: string,
|
|
serverId?: string | null,
|
|
credentialId?: string | null,
|
|
campaignId?: string | null
|
|
): Promise<MailConnectionTestResponse> {
|
|
return runProfileAction<MailConnectionTestResponse>(settings, profileId, "imap", serverId, credentialId, campaignId);
|
|
}
|
|
|
|
export async function listMailProfileImapFolders(
|
|
settings: ApiSettings,
|
|
profileId: string,
|
|
serverId?: string | null,
|
|
credentialId?: string | null,
|
|
campaignId?: string | null
|
|
): Promise<MailImapFolderListResponse> {
|
|
return runProfileAction<MailImapFolderListResponse>(settings, profileId, "folders", serverId, credentialId, campaignId);
|
|
}
|
|
|
|
export async function getMockMailboxMessage(settings: ApiSettings, id: string): Promise<MockMailboxMessageResponse> {
|
|
return apiFetch<MockMailboxMessageResponse>(settings, `/api/v1/dev/mailbox/messages/${encodeURIComponent(id)}`);
|
|
}
|
|
|
|
export type { MailConnectionTestResponse, MailImapFolderListResponse, MailImapFolderResponse, MailServerProfile, MailServerProfileListResponse, MockMailboxMessage, MockMailboxMessageResponse } from "@govoplan/core-webui";
|