import type { ApiRouteDeps } from "@opengeni/core"; import type { Settings } from "@opengeni/config"; import { ExternalActorContinuation } from "@opengeni/contracts/external-identities"; import { FikenOAuthStartResponse, type AccessGrant, type ConnectionMetadata, type FikenCompanySummary, type FikenConnectionMetadata, type FikenOAuthStartRequest } from "@opengeni/contracts"; import { type Database } from "@opengeni/db"; import { type FetchLike } from "@opengeni/network"; import { HTTPException } from "hono/http-exception"; /** Shared native/embedded token adapter. Provider verification happens before * persistence; the caller reauthorizes inside its durable commit transaction. */ export declare function prepareFikenTokenInstall(deps: ApiRouteDeps, grant: Pick, raw: unknown, expectedVersion?: number): Promise<(tx: Database) => Promise>; export type FikenOperation = "companies.list" | "contacts.list" | "contact.create" | "products.list" | "invoices.list" | "invoice.get" | "invoice_draft.create" | "bank_accounts.list" | "purchases.list" | "sales.list"; export declare class FikenProviderError extends Error { readonly code: string; readonly status: number | null; constructor(code: string, status?: number | null, providerMessage?: string | null); } export declare class FikenCredentialVerificationError extends HTTPException { constructor(message: string); } export type FikenPage = { page: number; pageSize: number; pageCount: number | null; resultCount: number | null; }; type FikenContext = { accountId: string; workspaceId: string; subjectId: string | null; sessionId: string | null; }; type FikenProviderAuthorization = () => Promise; /** * Validates a pasted Fiken personal API token before it enters encrypted * storage, and discovers the companies it can act on. The companies list is * the connection's bounded metadata; company slugs are re-validated by Fiken * itself on every later call. */ export declare function verifyFikenApiToken(token: string, fetchImpl?: FetchLike): Promise<{ companies: FikenCompanySummary[]; }>; export declare function fikenCredentialBundle(token: string): Record; export declare function resolveFikenConnectionForTool(input: { db: Database; grant: AccessGrant; sessionId: string | null; requestedConnectionId?: string; }): Promise<{ connection: ConnectionMetadata; metadata: FikenConnectionMetadata; context: FikenContext; }>; export type FikenListInput = { companySlug?: string | undefined; page?: number | undefined; pageSize?: number | undefined; }; export declare class FikenClient { private readonly db; private readonly settings; private readonly connection; private readonly metadata; private readonly context; private readonly fetchImpl; private readonly authorizeProviderRequest?; private readonly resolveCredential; private expectedConnectionVersion; constructor(db: Database, settings: Settings, connection: ConnectionMetadata, metadata: FikenConnectionMetadata, context: FikenContext, fetchImpl?: FetchLike, authorizeProviderRequest?: FikenProviderAuthorization | undefined); listCompanies(input?: { page?: number; pageSize?: number; }): Promise<{ companies: Record[]; page: FikenPage | null; }>; listContacts(input: FikenListInput & { name?: string | undefined; email?: string | undefined; organizationNumber?: string | undefined; customer?: boolean | undefined; supplier?: boolean | undefined; inactive?: boolean | undefined; }): Promise<{ companySlug: string; contacts: Record[]; page: FikenPage | null; }>; createContact(input: { companySlug?: string | undefined; name: string; email?: string | undefined; organizationNumber?: string | undefined; phoneNumber?: string | undefined; customer?: boolean | undefined; supplier?: boolean | undefined; }): Promise<{ companySlug: string; contactId: number; receipt: { credentialRole: "fiken_api_token"; credentialLabel: "Fiken API token"; connectionId: string; operation: FikenOperation; operationId?: string; }; }>; listProducts(input: FikenListInput & { name?: string | undefined; active?: boolean | undefined; }): Promise<{ companySlug: string; products: Record[]; page: FikenPage | null; }>; listInvoices(input: FikenListInput & { issueDateGe?: string | undefined; issueDateLe?: string | undefined; customerId?: number | undefined; settled?: boolean | undefined; invoiceNumber?: string | undefined; }): Promise<{ companySlug: string; invoices: Record[]; page: FikenPage | null; }>; getInvoice(input: { companySlug?: string | undefined; invoiceId: number; }): Promise<{ companySlug: string; invoice: Record | null; }>; createInvoiceDraft(input: { companySlug?: string | undefined; operationId: string; customerId: number; daysUntilDueDate: number; invoiceText?: string | undefined; yourReference?: string | undefined; ourReference?: string | undefined; currency?: string | undefined; bankAccountNumber?: string | undefined; lines: Array<{ description?: string | undefined; productId?: number | undefined; unitPriceCents?: number | undefined; vatType?: string | undefined; quantity: number; discountPercent?: number | undefined; incomeAccount?: string | undefined; comment?: string | undefined; }>; }): Promise<{ companySlug: string; draftId: number | null; draft: Record; alreadyExisted: boolean; receipt: { credentialRole: "fiken_api_token"; credentialLabel: "Fiken API token"; connectionId: string; operation: FikenOperation; operationId?: string; }; } | { draft?: never; companySlug: string; draftId: number; alreadyExisted: boolean; receipt: { credentialRole: "fiken_api_token"; credentialLabel: "Fiken API token"; connectionId: string; operation: FikenOperation; operationId?: string; }; }>; listBankAccounts(input: FikenListInput & { inactive?: boolean | undefined; }): Promise<{ companySlug: string; bankAccounts: Record[]; page: FikenPage | null; }>; listPurchases(input: FikenListInput & { dateGe?: string | undefined; dateLe?: string | undefined; paid?: boolean | undefined; }): Promise<{ companySlug: string; purchases: Record[]; page: FikenPage | null; }>; listSales(input: FikenListInput & { dateGe?: string | undefined; dateLe?: string | undefined; settled?: boolean | undefined; }): Promise<{ companySlug: string; sales: Record[]; page: FikenPage | null; }>; /** * The company scope for a call: an explicit slug wins, then the connection's * configured default, then the only verified company. Ambiguity is an error * that lists the available slugs instead of guessing. */ private companySlugFor; private request; private readResponse; private markNeedsReauth; private currentAuthority; private credentialFor; private receipt; private recordAudit; } export declare function createFikenClient(deps: { db: Database; settings: Settings; fikenFetch?: typeof fetch; authorizeProviderRequest?: FikenProviderAuthorization; }, resolved: Awaited>): FikenClient; export declare class FikenOAuthCallbackError extends Error { readonly reason: string; constructor(reason: string); } export declare function startFikenOAuth(deps: ApiRouteDeps, input: { accountId: string; workspaceId: string; subjectId: string; requestUrl: string; payload: FikenOAuthStartRequest; externalContinuation?: ExternalActorContinuation; connectAttemptId?: string; }): Promise; export declare function completeFikenOAuthCallback(deps: ApiRouteDeps, input: { code?: string | undefined; state?: string | undefined; error?: string | undefined; requestUrl: string; }): Promise<{ redirectTo: string; exactReturn?: boolean; }>; export {};