import {z} from 'zod' export type TaskRecurrenceInfo = {schedule?: ('None' | 'Daily' | 'Weekdays' | 'Weekly' | 'Monthly' | 'Yearly') | undefined, endingCondition?: ('None' | 'Count' | 'Date') | undefined, endingAfter?: number | undefined, endingOn?: string | undefined}; export type TMAttachmentInfo = {id?: string | undefined, type?: ('File' | 'Note' | 'Event' | 'Card') | undefined}; export type TMCreateTaskRequest = {subject: string, assignees: Array<{id?: string | undefined}>, completenessCondition?: ('Simple' | 'AllAssignees' | 'Percentage') | undefined, startDate?: string | undefined, dueDate?: string | undefined, color?: ('Black' | 'Red' | 'Orange' | 'Yellow' | 'Green' | 'Blue' | 'Purple' | 'Magenta') | undefined, section?: string | undefined, description?: string | undefined, recurrence?: TaskRecurrenceInfo | undefined, attachments?: Array | undefined}; export type CreateApiTeamMessagingV1ChatsChatIdTasksArgs = {chatId: string, body: TMCreateTaskRequest}; export type CreateApiTeamMessagingV1ChatsChatIdTasksOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const taskRecurrenceInfo = z.object({schedule: z.enum(["None", "Daily", "Weekdays", "Weekly", "Monthly", "Yearly"]).optional(), endingCondition: z.enum(["None", "Count", "Date"]).optional(), endingAfter: z.number().int().gte(1).lte(10).optional(), endingOn: z.string().optional()}); export const taskAttachment = z.object({id: z.string().optional(), type: z.literal("File").optional(), name: z.string().optional(), contentUri: z.string().optional()}); export const tMTaskInfo = z.object({id: z.string().optional(), creationTime: z.string().optional(), lastModifiedTime: z.string().optional(), type: z.literal("Task").optional(), creator: z.object({id: z.string().optional()}).optional(), chatIds: z.array(z.string()).optional(), status: z.enum(["Pending", "InProgress", "Completed"]).optional(), subject: z.string().optional(), assignees: z.array(z.object({id: z.string().optional(), status: z.enum(["Pending", "Completed"]).optional()})).optional(), completenessCondition: z.enum(["Simple", "AllAssignees", "Percentage"]).optional(), completenessPercentage: z.number().int().gte(0).lte(100).optional(), startDate: z.string().optional(), dueDate: z.string().optional(), color: z.enum(["Black", "Red", "Orange", "Yellow", "Green", "Blue", "Purple", "Magenta"]).optional(), section: z.string().optional(), description: z.string().optional(), recurrence: taskRecurrenceInfo.optional(), attachments: z.array(taskAttachment).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 }; /** * Create Task * * POST /team-messaging/v1/chats/{chatId}/tasks */ export const createApiTeamMessagingV1ChatsChatIdTasks = async (args: CreateApiTeamMessagingV1ChatsChatIdTasksArgs, options?: CreateApiTeamMessagingV1ChatsChatIdTasksOptions) => { const {baseUrl = "https://platform.ringcentral.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/team-messaging/v1/chats/${encodeURIComponent(String(args.chatId))}/tasks${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(tMTaskInfo, await res.json()) };