import {z} from 'zod' export type IdParam = string; export type UserParam = string; export type DeletedParam = number; export type CreatedByParam = string; export type UpdatedByParam = string; export type CreatedAtParam = string; export type UpdatedAtParam = string; export type BroadcastFlow = {_id?: IdParam | undefined, user?: UserParam | undefined, deleted?: DeletedParam | undefined, createdBy?: CreatedByParam | undefined, updatedBy?: UpdatedByParam | undefined, createdAt?: CreatedAtParam | undefined, updatedAt?: UpdatedAtParam | undefined, title?: string | undefined, type?: ('scheduled' | 'uploaded' | 'sftp') | undefined, parentBroadcastFlow?: IdParam | undefined, campaign?: IdParam | undefined, broadcastTemplate?: IdParam | undefined, patientMessageTemplate?: IdParam | undefined, review?: ({user?: IdParam | undefined, status?: ('approved' | 'declined') | undefined, declinedReason?: string | undefined}) | undefined, message?: string | undefined, messageOverrides?: (Array<{text?: string | undefined, language?: string | undefined, channel?: string | undefined, subject?: string | undefined, patientMessageTemplate?: IdParam | undefined, secureChat?: boolean | undefined}>) | undefined, sendAt?: string | undefined, sentAt?: string | undefined, configuration?: ({secureChat?: boolean | undefined, contactPreference?: ('all-contacts' | 'primary-contact-only' | 'uploaded-contact-only') | undefined, simulate?: boolean | undefined, shouldReprocessFilters?: boolean | undefined}) | undefined, processingStatus?: ('pre-processing' | 'processing-upload' | 'rows-processed' | 'sending-messages' | 'success' | 'failure') | undefined, upload?: IdParam | undefined, fileUpload?: IdParam | undefined, mapping?: IdParam | undefined, rowCount?: number | undefined, stats?: ({totalMessagesSent?: number | undefined, messagesSuccess?: number | undefined, messagesFailed?: number | undefined, messagesSkipped?: number | undefined}) | undefined}; export type UpdateApiBroadcastFlowsBroadcastFlowIdArgs = {broadcastFlowId: string, body: BroadcastFlow}; export type UpdateApiBroadcastFlowsBroadcastFlowIdOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const idParam = z.string().min(24).max(24); export const userParam = z.string().min(24).max(24); export const deletedParam = z.union([z.literal(0), z.literal(1)]); export const createdByParam = z.string().min(24).max(24); export const updatedByParam = z.string().min(24).max(24); export const createdAtParam = z.string(); export const updatedAtParam = z.string(); export const broadcastFlow = z.object({_id: idParam.optional(), user: userParam.optional(), deleted: deletedParam.optional(), createdBy: createdByParam.optional(), updatedBy: updatedByParam.optional(), createdAt: createdAtParam.optional(), updatedAt: updatedAtParam.optional(), title: z.string().optional(), type: z.enum(["scheduled", "uploaded", "sftp"]).optional(), parentBroadcastFlow: idParam.optional(), campaign: idParam.optional(), broadcastTemplate: idParam.optional(), patientMessageTemplate: idParam.optional(), review: z.object({user: idParam.optional(), status: z.enum(["approved", "declined"]).optional(), declinedReason: z.string().optional()}).optional(), message: z.string().optional(), messageOverrides: z.array(z.object({text: z.string().optional(), language: z.string().optional(), channel: z.string().optional(), subject: z.string().optional(), patientMessageTemplate: idParam.optional(), secureChat: z.boolean().optional()})).optional(), sendAt: z.string().optional(), sentAt: z.string().optional(), configuration: z.object({secureChat: z.boolean().optional(), contactPreference: z.enum(["all-contacts", "primary-contact-only", "uploaded-contact-only"]).optional(), simulate: z.boolean().optional(), shouldReprocessFilters: z.boolean().optional()}).optional(), processingStatus: z.enum(["pre-processing", "processing-upload", "rows-processed", "sending-messages", "success", "failure"]).optional(), upload: idParam.optional(), fileUpload: idParam.optional(), mapping: idParam.optional(), rowCount: z.number().int().optional(), stats: z.object({totalMessagesSent: z.number().int().optional(), messagesSuccess: z.number().int().optional(), messagesFailed: z.number().int().optional(), messagesSkipped: z.number().int().optional()}).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Update broadcast flow * * PUT /broadcastFlows/{broadcastFlowId} */ export const updateApiBroadcastFlowsBroadcastFlowId = async (args: UpdateApiBroadcastFlowsBroadcastFlowIdArgs, options?: UpdateApiBroadcastFlowsBroadcastFlowIdOptions) => { const {baseUrl = "https://api.lumahealth.io/api/v2", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/broadcastFlows/${encodeURIComponent(String(args.broadcastFlowId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(broadcastFlow, await res.json()) };