import { calendlyRequest, uuidFromUri } from './client.ts'
import type { CalendlyAuthInput, DryRunResult, JsonRecord } from './types.ts'
import { compactRecord, optionalString, requireRecord } from './types.ts'
import { requireAuthMode } from './auth.ts'
export type CancelScheduledEventInput = CalendlyAuthInput & {
/** Scheduled event uuid or URI. */
event: string
reason?: string
confirm?: boolean
dryRun?: boolean
}
export type CancelScheduledEventResult = {
canceled: true
event: string
reason: string | null
}
/**
* Cancel a Calendly scheduled event. Requires `confirm: true`, or use `dryRun: true`.
* @example
* import cancelScheduledEvent from 'kody:@kody/calendly/cancel-scheduled-event'
* const preview = await cancelScheduledEvent({
* event: 'AAAAAAAAAAAAAAAA',
* reason: 'Need to reschedule',
* dryRun: true,
* })
*/
export async function cancelScheduledEvent(
input: CancelScheduledEventInput,
): Promise<
CancelScheduledEventResult | DryRunResult<{ method: string; path: string; body: JsonRecord }>
> {
const event = optionalString(input.event, 'event')
if (!event) throw new Error('cancel-scheduled-event requires event (uuid or URI).')
const uuid = uuidFromUri(event)
const path = `/scheduled_events/${uuid}/cancellation`
const body = compactRecord({
reason: optionalString(input.reason, 'reason'),
})
if (input.dryRun) {
return { dryRun: true, wouldCall: { method: 'POST', path, body } }
}
if (input.confirm !== true) {
throw new Error(
'Canceling a Calendly event requires confirm: true after explicit user approval, or dryRun: true.',
)
}
await calendlyRequest({
...input,
operation: 'scheduledEvents.write',
method: 'POST',
path,
body,
})
return {
canceled: true,
event: uuid,
reason: optionalString(input.reason, 'reason') ?? null,
}
}
/**
* Cancel a Calendly scheduled event. Requires `confirm: true`, or use `dryRun: true`.
* @example
* import cancelScheduledEvent from 'kody:@kody/calendly/cancel-scheduled-event'
* const preview = await cancelScheduledEvent({ event: 'AAA', dryRun: true })
*/
export default async function cancelScheduledEventEntrypoint(
params: Partial<CancelScheduledEventInput> & Record<string, unknown> = {},
) {
const input = requireRecord(params, 'cancel-scheduled-event')
return cancelScheduledEvent({
event: optionalString(input.event, 'event') ?? '',
reason: optionalString(input.reason, 'reason'),
integrationName: optionalString(input.integrationName, 'integrationName'),
integration: optionalString(input.integration, 'integration'),
account: optionalString(input.account, 'account'),
secretName: optionalString(input.secretName, 'secretName'),
auth: requireAuthMode(input.auth),
confirm: input.confirm === true,
dryRun: input.dryRun === true,
})
}