import {z} from 'zod' export type GetApiApiV1EmployeesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV1EmployeesResponse = z.object({employees: z.array(z.object({id: z.string(), fullName: z.string(), socialName: z.string().nullable(), status: z.enum(["active", "terminated", "on-hold"]), workEmail: z.string().nullable(), personalEmail: z.string().nullable(), workPhoneNumber: z.string().nullable(), personalPhoneNumber: z.string().nullable(), cpf: z.string().nullable(), cnpj: z.string().nullable(), rg: z.string().nullable(), position: z.string().nullable(), area: z.string().nullable(), managerFullName: z.string().nullable(), birthDate: z.string().nullable(), contractType: z.enum(["pj", "clt", "intern", "apprentice", "temporary", "third-party"]).nullable(), contractEndDate: z.string().nullable(), admittedAt: z.string().nullable(), terminatedAt: z.string().nullable(), address: z.object({zipCode: z.string().nullable(), streetName: z.string().nullable(), streetNumber: z.string().nullable(), complement: z.string().nullable(), neighborhood: z.string().nullable(), city: z.string().nullable(), state: z.string().nullable(), country: z.string().nullable()}).nullable(), customFields: z.array(z.object({label: z.string(), type: z.enum(["text", "select"]), value: z.string()})), bankAccount: z.object({bank: z.string().nullable(), branch: z.string().nullable(), accountNumber: z.string().nullable(), accountType: z.enum(["checking", "savings", "salary", "payment"]).nullable(), pixKey: z.string().nullable()}).nullable(), salaryCents: z.number().nullable(), sources: z.array(z.union([z.object({id: z.literal("manual"), platform: z.literal("manual")}), z.object({id: z.string(), platform: z.literal("sheets")}), z.object({id: z.string(), platform: z.string()})])), createdAt: z.string(), assetCount: z.number(), phoneAccountCount: z.number(), externalPhoneAccountCount: z.number()}))}); 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 }; /** * Lista todos os colaboradores da empresa * * GET /api/v1/employees */ export const getApiApiV1Employees = async (options?: GetApiApiV1EmployeesOptions) => { const {baseUrl = "https://api.salvy.com.br", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/employees${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiApiV1EmployeesResponse, await res.json()) };