import {z} from 'zod' export type GetApiCompaniesCompanySlugPurchasesArgs = {page?: number | undefined, pageSize?: number | undefined, date?: string | undefined, dateLe?: string | undefined, dateLt?: string | undefined, dateGe?: string | undefined, dateGt?: string | undefined, lastModified?: string | undefined, lastModifiedLe?: string | undefined, lastModifiedLt?: string | undefined, lastModifiedGe?: string | undefined, lastModifiedGt?: string | undefined, sortBy?: ('date asc' | 'date desc') | undefined, paid?: boolean | undefined, settledDate?: string | undefined, settledDateLe?: string | undefined, settledDateLt?: string | undefined, settledDateGe?: string | undefined, settledDateGt?: string | undefined, contactId?: number | undefined}; export type GetApiCompaniesCompanySlugPurchasesOptions = {baseUrl?: string | undefined, fikenApiOauth?: string | undefined, fetch?: typeof fetch | undefined}; export const orderLine = z.object({lineId: z.number().int().optional(), description: z.string().max(200), netPrice: z.number().int().optional(), vat: z.number().int().optional(), account: z.string().optional(), vatType: z.string(), netPriceInCurrency: z.number().int().optional(), vatInCurrency: z.number().int().optional(), projectId: z.number().int().optional()}); export const address = z.object({streetAddress: z.string().optional(), streetAddressLine2: z.string().optional(), city: z.string().optional(), postCode: z.string().optional(), country: z.string()}); export const contactPerson = z.object({contactPersonId: z.number().int().optional(), name: z.string(), email: z.string(), phoneNumber: z.string().optional(), address: address.optional()}); export const contactNote = z.object({author: z.string().optional(), note: z.string().optional()}); export const attachment = z.object({uuid: z.string().optional(), filename: z.string().optional(), identifier: z.string().optional(), downloadUrl: z.string().optional(), downloadUrlWithFikenNormalUserCredentials: z.string().optional(), comment: z.string().optional(), type: z.enum(["invoice", "reminder", "unspecified", "ocr", "bank_statement"]).optional()}); export const contact = z.object({contactId: z.number().int().optional(), createdDate: z.string().optional(), lastModifiedDate: z.string().optional(), name: z.string().min(1).max(200), email: z.string().max(200).optional(), organizationNumber: z.string().max(200).optional(), customerNumber: z.number().int().optional(), customerAccountCode: z.string().optional(), phoneNumber: z.string().max(20).optional(), memberNumber: z.number().optional(), memberNumberString: z.string().optional(), supplierNumber: z.number().int().optional(), supplierAccountCode: z.string().optional(), customer: z.boolean().optional(), supplier: z.boolean().optional(), bankAccountNumber: z.string().max(11).optional(), contactPerson: z.array(contactPerson).optional(), notes: z.array(contactNote).optional(), currency: z.string().optional(), language: z.string().nullable().optional(), inactive: z.boolean().optional(), daysUntilInvoicingDueDate: z.number().int().optional(), discount: z.number().optional(), address: address.optional(), groups: z.array(z.string()).optional(), documents: z.array(attachment).optional()}); export const payment = z.object({paymentId: z.number().int().optional(), date: z.string(), account: z.string(), amount: z.number().int(), amountInNok: z.number().int().optional(), currency: z.string().optional(), fee: z.number().int().optional()}); export const projectResult = z.object({projectId: z.number().int().optional(), number: z.string().optional(), name: z.string().optional(), description: z.string().optional(), startDate: z.string().optional(), endDate: z.string().optional(), contact: contact.optional(), completed: z.boolean().optional()}); export const purchaseResult = z.object({purchaseId: z.number().int().optional(), transactionId: z.number().int().optional(), identifier: z.string().optional(), date: z.string(), dueDate: z.string().optional(), kind: z.enum(["cash_purchase", "supplier"]), paid: z.boolean(), lines: z.array(orderLine), supplier: contact.optional(), currency: z.string(), paymentAccount: z.string().optional(), paymentDate: z.string().optional(), payments: z.array(payment).optional(), purchaseAttachments: z.array(attachment).optional(), kid: z.string().optional(), project: z.array(projectResult).optional(), deleted: z.boolean().optional(), settled: z.boolean().optional(), settledDate: z.string().optional()}); export const getApiCompaniesCompanySlugPurchasesResponse = z.array(purchaseResult); 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 }; /** * Returns all purchases for given company * * GET /companies/{companySlug}/purchases */ export const getApiCompaniesCompanySlugPurchases = async (args: GetApiCompaniesCompanySlugPurchasesArgs, options?: GetApiCompaniesCompanySlugPurchasesOptions) => { const {baseUrl = "https://api.fiken.no/api/v2", fetch: fetchFn = fetch, fikenApiOauth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.date != null) query.append("date", String(args.date)) if (args.dateLe != null) query.append("dateLe", String(args.dateLe)) if (args.dateLt != null) query.append("dateLt", String(args.dateLt)) if (args.dateGe != null) query.append("dateGe", String(args.dateGe)) if (args.dateGt != null) query.append("dateGt", String(args.dateGt)) if (args.lastModified != null) query.append("lastModified", String(args.lastModified)) if (args.lastModifiedLe != null) query.append("lastModifiedLe", String(args.lastModifiedLe)) if (args.lastModifiedLt != null) query.append("lastModifiedLt", String(args.lastModifiedLt)) if (args.lastModifiedGe != null) query.append("lastModifiedGe", String(args.lastModifiedGe)) if (args.lastModifiedGt != null) query.append("lastModifiedGt", String(args.lastModifiedGt)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.paid != null) query.append("paid", String(args.paid)) if (args.settledDate != null) query.append("settledDate", String(args.settledDate)) if (args.settledDateLe != null) query.append("settledDateLe", String(args.settledDateLe)) if (args.settledDateLt != null) query.append("settledDateLt", String(args.settledDateLt)) if (args.settledDateGe != null) query.append("settledDateGe", String(args.settledDateGe)) if (args.settledDateGt != null) query.append("settledDateGt", String(args.settledDateGt)) if (args.contactId != null) query.append("contactId", String(args.contactId)) if (fikenApiOauth !== undefined) headers.set('authorization', `Bearer ${fikenApiOauth}`) const url = `${baseUrl.replace(/\/+$/, '')}/companies/${encodeURIComponent(String(args.companySlug))}/purchases${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(getApiCompaniesCompanySlugPurchasesResponse, await res.json()) };