import {z} from 'zod' export type PatchApiCompaniesCompanySlugPurchasesPurchaseIdDeleteOptions = {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 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 }; /** * Sets the deleted flag for a purchase. The purchase is not deleted, but a reverse transaction is created and the "deleted" property is set to true. * * PATCH /companies/{companySlug}/purchases/{purchaseId}/delete */ export const patchApiCompaniesCompanySlugPurchasesPurchaseIdDelete = async (options?: PatchApiCompaniesCompanySlugPurchasesPurchaseIdDeleteOptions) => { 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 (fikenApiOauth !== undefined) headers.set('authorization', `Bearer ${fikenApiOauth}`) const url = `${baseUrl.replace(/\/+$/, '')}/companies/${encodeURIComponent(String(args.companySlug))}/purchases/${encodeURIComponent(String(args.purchaseId))}/delete${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(purchaseResult, await res.json()) };