import {z} from 'zod' export type AddressFields = {city?: (string | null) | undefined, countryIso?: (string | null) | undefined, countryName?: (string | null) | undefined, formattedAddress?: (string | null) | undefined, inlineAddress?: (string | null) | undefined, street?: (string | null) | undefined, zipcode?: (string | null) | undefined}; export type ContactFields = {cellPhone?: (string | null) | undefined, companyName?: (string | null) | undefined, displayName?: (string | null) | undefined, email?: (string | null) | undefined, firstName?: (string | null) | undefined, function?: (string | null) | undefined, invertedDisplayName?: (string | null) | undefined, lastName?: (string | null) | undefined, phone?: (string | null) | undefined, service?: (string | null) | undefined, title?: (string | null) | undefined}; export type CustomerType = 'Business' | 'Consumer' | 'Government'; export type DiscountType = 'Percent' | 'Fixed'; export type Discount = {type?: DiscountType | undefined, value?: number | undefined}; export type ProductFamily = {id?: number | undefined, label?: (string | null) | undefined, number?: (string | null) | undefined}; export type ProductType = 'Product' | 'Service' | 'Text'; export type UnitType = 'Time' | 'Weight' | 'Area' | 'Volume' | 'Dimension' | 'Other'; export type Unit = {code?: (string | null) | undefined, conversion?: number | undefined, decimals?: (number | null) | undefined, id?: (number | null) | undefined, name?: (string | null) | undefined, type?: UnitType | undefined}; export type VatRegion = 'FR' | 'EU' | 'IE' | 'DOM' | 'Corse' | 'Monaco'; export type Vat = {default?: boolean | undefined, id?: (number | null) | undefined, label?: (string | null) | undefined, rate?: (number | null) | undefined, region?: VatRegion | undefined}; export type Product = {active?: boolean | undefined, defaultQuantity?: number | undefined, description?: (string | null) | undefined, family?: ProductFamily | undefined, id?: number | undefined, imageUrl?: (string | null) | undefined, internalId?: (string | null) | undefined, internalNotes?: (string | null) | undefined, isUnitPriceTaxIncluded?: boolean | undefined, margin?: number | undefined, marginRate?: number | undefined, markupRate?: number | undefined, metadata?: ({[key: string]: string | null} | null) | undefined, name?: (string | null) | undefined, number?: (string | null) | undefined, purchasePrice?: number | undefined, taxExcludedPrice?: number | undefined, taxIncludedPrice?: number | undefined, type?: ProductType | undefined, unit?: Unit | undefined, unitPrice?: number | undefined, vat?: Vat | undefined}; export type LineType = 'Empty' | 'Product' | 'Service' | 'Subtotal' | 'Text' | 'NewPage'; export type SaleDocumentLine = {classificationId?: (number | null) | undefined, date?: (string | null) | undefined, description?: (string | null) | undefined, discount?: Discount | undefined, id?: number | undefined, marginAmount?: number | undefined, markupRate?: number | undefined, product?: Product | undefined, productId?: (number | null) | undefined, productInternalId?: (string | null) | undefined, productNumber?: (string | null) | undefined, purchasePrice?: (number | null) | undefined, quantity?: (number | null) | undefined, taxExcludedAmount?: (number | null) | undefined, taxExcludedPrice?: (number | null) | undefined, taxIncludedAmount?: (number | null) | undefined, taxIncludedPrice?: (number | null) | undefined, type?: LineType | undefined, unit?: Unit | undefined, vat?: Vat | undefined}; export type QuoteStatus = 'Pending' | 'Accepted' | 'Refused' | 'Closed' | 'Sent' | 'Invoiced'; export type VatSummary = {taxExcludedAmount?: number | undefined, vat?: Vat | undefined, vatAmount?: number | undefined}; export type Quote = {acceptedAt?: (string | null) | undefined, balance?: number | undefined, billingAddress?: AddressFields | undefined, billingContact?: ContactFields | undefined, customerElectronicAddress?: (string | null) | undefined, customerId?: (number | null) | undefined, customerName?: (string | null) | undefined, customerNumber?: (string | null) | undefined, customerOrderReference?: (string | null) | undefined, customerSiret?: (string | null) | undefined, customerType?: CustomerType | undefined, customerVatNumber?: (string | null) | undefined, date?: (string | null) | undefined, deposit?: Discount | undefined, footerNotes?: (string | null) | undefined, globalDiscount?: Discount | undefined, grossTaxExcludedAmount?: number | undefined, headerNotes?: (string | null) | undefined, id?: number | undefined, internalId?: (string | null) | undefined, internalNotes?: (string | null) | undefined, liableToVat?: (boolean | null) | undefined, lines?: (Array | null) | undefined, metadata?: ({[key: string]: string | null} | null) | undefined, number?: (string | null) | undefined, pdfUrl?: (string | null) | undefined, publicLink?: (string | null) | undefined, refusedAt?: (string | null) | undefined, region?: VatRegion | undefined, sentAt?: (string | null) | undefined, shippingAddress?: AddressFields | undefined, shippingAmount?: number | undefined, shippingContact?: ContactFields | undefined, shippingVat?: Vat | undefined, status?: QuoteStatus | undefined, subject?: (string | null) | undefined, taxAmount?: number | undefined, taxExcludedAmount?: number | undefined, taxIncludedAmount?: number | undefined, useTaxIncludedPrices?: boolean | undefined, validityDate?: (string | null) | undefined, vatNote?: (string | null) | undefined, vatReverseCharge?: boolean | undefined, vats?: (Array | null) | undefined, vendorReference?: (string | null) | undefined}; export type CreateApiV1SalesquotesArgs = {body: Quote}; export type CreateApiV1SalesquotesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const addressFields = z.object({city: z.string().nullable().optional(), countryIso: z.string().nullable().optional(), countryName: z.string().nullable().optional(), formattedAddress: z.string().nullable().optional(), inlineAddress: z.string().nullable().optional(), street: z.string().nullable().optional(), zipcode: z.string().nullable().optional()}); export const contactFields = z.object({cellPhone: z.string().nullable().optional(), companyName: z.string().nullable().optional(), displayName: z.string().nullable().optional(), email: z.string().nullable().optional(), firstName: z.string().nullable().optional(), function: z.string().nullable().optional(), invertedDisplayName: z.string().nullable().optional(), lastName: z.string().nullable().optional(), phone: z.string().nullable().optional(), service: z.string().nullable().optional(), title: z.string().nullable().optional()}); export const customerType = z.enum(["Business", "Consumer", "Government"]); export const discountType = z.enum(["Percent", "Fixed"]); export const discount = z.object({type: discountType.optional(), value: z.number().optional()}); export const productFamily = z.object({id: z.number().int().optional(), label: z.string().nullable().optional(), number: z.string().nullable().optional()}); export const productType = z.enum(["Product", "Service", "Text"]); export const unitType = z.enum(["Time", "Weight", "Area", "Volume", "Dimension", "Other"]); export const unit = z.object({code: z.string().nullable().optional(), conversion: z.number().optional(), decimals: z.number().nullable().optional(), id: z.number().int().nullable().optional(), name: z.string().nullable().optional(), type: unitType.optional()}); export const vatRegion = z.enum(["FR", "EU", "IE", "DOM", "Corse", "Monaco"]); export const vat = z.object({default: z.boolean().optional(), id: z.number().int().nullable().optional(), label: z.string().nullable().optional(), rate: z.number().nullable().optional(), region: vatRegion.optional()}); export const product = z.object({active: z.boolean().optional(), defaultQuantity: z.number().optional(), description: z.string().nullable().optional(), family: productFamily.optional(), id: z.number().int().optional(), imageUrl: z.string().nullable().optional(), internalId: z.string().nullable().optional(), internalNotes: z.string().nullable().optional(), isUnitPriceTaxIncluded: z.boolean().optional(), margin: z.number().optional(), marginRate: z.number().optional(), markupRate: z.number().optional(), metadata: z.record(z.string(), z.string().nullable()).nullable().optional(), name: z.string().nullable().optional(), number: z.string().nullable().optional(), purchasePrice: z.number().optional(), taxExcludedPrice: z.number().optional(), taxIncludedPrice: z.number().optional(), type: productType.optional(), unit: unit.optional(), unitPrice: z.number().optional(), vat: vat.optional()}); export const lineType = z.enum(["Empty", "Product", "Service", "Subtotal", "Text", "NewPage"]); export const saleDocumentLine = z.object({classificationId: z.number().int().nullable().optional(), date: z.string().nullable().optional(), description: z.string().nullable().optional(), discount: discount.optional(), id: z.number().int().optional(), marginAmount: z.number().optional(), markupRate: z.number().optional(), product: product.optional(), productId: z.number().int().nullable().optional(), productInternalId: z.string().nullable().optional(), productNumber: z.string().nullable().optional(), purchasePrice: z.number().nullable().optional(), quantity: z.number().nullable().optional(), taxExcludedAmount: z.number().nullable().optional(), taxExcludedPrice: z.number().nullable().optional(), taxIncludedAmount: z.number().nullable().optional(), taxIncludedPrice: z.number().nullable().optional(), type: lineType.optional(), unit: unit.optional(), vat: vat.optional()}); export const quoteStatus = z.enum(["Pending", "Accepted", "Refused", "Closed", "Sent", "Invoiced"]); export const vatSummary = z.object({taxExcludedAmount: z.number().optional(), vat: vat.optional(), vatAmount: z.number().optional()}); export const quote = z.object({acceptedAt: z.string().nullable().optional(), balance: z.number().optional(), billingAddress: addressFields.optional(), billingContact: contactFields.optional(), customerElectronicAddress: z.string().nullable().optional(), customerId: z.number().int().nullable().optional(), customerName: z.string().nullable().optional(), customerNumber: z.string().nullable().optional(), customerOrderReference: z.string().nullable().optional(), customerSiret: z.string().nullable().optional(), customerType: customerType.optional(), customerVatNumber: z.string().nullable().optional(), date: z.string().nullable().optional(), deposit: discount.optional(), footerNotes: z.string().nullable().optional(), globalDiscount: discount.optional(), grossTaxExcludedAmount: z.number().optional(), headerNotes: z.string().nullable().optional(), id: z.number().int().optional(), internalId: z.string().nullable().optional(), internalNotes: z.string().nullable().optional(), liableToVat: z.boolean().nullable().optional(), lines: z.array(saleDocumentLine).nullable().optional(), metadata: z.record(z.string(), z.string().nullable()).nullable().optional(), number: z.string().nullable().optional(), pdfUrl: z.string().nullable().optional(), publicLink: z.string().nullable().optional(), refusedAt: z.string().nullable().optional(), region: vatRegion.optional(), sentAt: z.string().nullable().optional(), shippingAddress: addressFields.optional(), shippingAmount: z.number().optional(), shippingContact: contactFields.optional(), shippingVat: vat.optional(), status: quoteStatus.optional(), subject: z.string().nullable().optional(), taxAmount: z.number().optional(), taxExcludedAmount: z.number().optional(), taxIncludedAmount: z.number().optional(), useTaxIncludedPrices: z.boolean().optional(), validityDate: z.string().nullable().optional(), vatNote: z.string().nullable().optional(), vatReverseCharge: z.boolean().optional(), vats: z.array(vatSummary).nullable().optional(), vendorReference: z.string().nullable().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 a new quote * * POST /v1/salesquotes */ export const createApiV1Salesquotes = async (args: CreateApiV1SalesquotesArgs, options?: CreateApiV1SalesquotesOptions) => { const {baseUrl = "https://api.altoviz.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/salesquotes${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(quote, await res.json()) };