import {z} from 'zod' export type CommentsAttributes = {title?: string | undefined, comment?: string | undefined}; export type InventoryJournalEntryCreateParameters = {adjustment_quantity: number, inventory_adjustment_reason_id: number, current_quantity?: number | undefined, part_id: number, part_location_detail_id: number, comments_attributes?: Array | undefined} | {adjustment_quantity?: number | undefined, inventory_adjustment_reason_id: number, current_quantity: number, part_id: number, part_location_detail_id: number, comments_attributes?: Array | undefined}; export type CreateApiInventoryJournalEntriesArgs = {body: InventoryJournalEntryCreateParameters}; export type CreateApiInventoryJournalEntriesOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, accountToken?: string | undefined, fetch?: typeof fetch | undefined}; export const id = z.number().int().gte(1); export const createdAt = z.string(); export const updatedAt = z.string(); export const inventoryJournalEntryCreated = z.object({id: id, created_at: createdAt.optional(), updated_at: updatedAt.optional(), account_id: id, user_id: z.number().int(), part_id: z.number().int(), part_location_detail_id: z.number().int(), previous_quantity: z.string(), current_quantity: z.string(), adjustment_quantity: z.string(), work_order_part_line_item_id: z.number().int().nullable(), inventory_adjustment_reason_id: z.number().int().nullable(), comments_count: z.number().int(), adjustment_type: z.enum(["initial", "manual", "work_order", "purchase_order", "bulk_update", "transfer"]), transfer_part_location_id: z.number().int().nullable(), purchase_order_line_item_id: z.number().int().nullable(), work_order_id: z.number().int().nullable(), unit_cost_cents: z.string().nullable().optional(), total_inventory_value_cents: z.string().nullable().optional(), vendor_id: z.number().int().nullable(), metadata: z.object({id: z.number().int().optional(), number: z.string().optional(), type: z.enum(["PurchaseOrder", "WorkOrder"]).optional()}).nullable().optional()}).nullable(); 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 Inventory Journal Entry * * POST /inventory_journal_entries */ export const createApiInventoryJournalEntries = async (args: CreateApiInventoryJournalEntriesArgs, options?: CreateApiInventoryJournalEntriesOptions) => { const {baseUrl = "https://secure.fleetio.com/api", fetch: fetchFn = fetch, apiKey, accountToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("Authorization", apiKey) if (accountToken !== undefined) headers.set("Account-Token", accountToken) const url = `${baseUrl.replace(/\/+$/, '')}/inventory_journal_entries${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(inventoryJournalEntryCreated, await res.json()) };