Skip to content

Built for people who want to own their automations. Join the waitlist for an invite.

Package listing

@kody/postmark

src/scheduled-server-health.ts

50 lines · 1.4 KB · TypeScript
import { PostmarkApiError, type PostmarkAuthOptions } from './core.ts'
import { searchOutboundMessages } from './messages.ts'
import { getServer } from './servers.ts'

export type ScheduledServerHealthInput = PostmarkAuthOptions & {
	count?: number
}

/**
 * Read-only current-server + recent outbound search. The same no-argument
 * contract the disabled `server-health` job uses. Never sends email.
 *
 * @example
 * import scheduledServerHealth from 'kody:@kody/postmark/scheduled-server-health'
 * const snapshot = await scheduledServerHealth()
 */
export async function runServerHealth(input: ScheduledServerHealthInput = {}) {
	try {
		const [server, outbound] = await Promise.all([
			getServer(input),
			searchOutboundMessages({ ...input, count: input.count ?? 10, offset: 0 }),
		])
		return {
			ok: true,
			server: {
				id: server.id ?? null,
				name: server.name ?? null,
				deliveryType: server.deliveryType ?? null,
				trackOpens: server.trackOpens ?? null,
				trackLinks: server.trackLinks ?? null,
			},
			recentOutboundCount: outbound.messages.length,
			outboundTotalCount: outbound.totalCount,
		}
	} catch (error) {
		if (error instanceof PostmarkApiError) {
			return {
				ok: false,
				status: error.status,
				setupUrl: error.setupUrl,
				message: error.message,
			}
		}
		throw error
	}
}

export default async function scheduledServerHealth(input: ScheduledServerHealthInput = {}) {
	return await runServerHealth(input)
}