import { ok } from "@tailor-platform/erp-kit/core"; import { vi } from "vitest"; import type { BusinessPartnerQueries, CoaManagementQueries, FinancialAccountingCommands, FinancialAccountingQueries, OrganizationQueries, PrimitivesQueries, } from "../module"; export type Company = NonNullable< Awaited>["value"]["company"] >; export type CustomerAccountReference = NonNullable< Awaited>["value"]["account"] >; export type Currency = NonNullable< Awaited>["value"]["currency"] >; export type Account = NonNullable< Awaited>["value"]["account"] >; export const mockOrganizationQueries = (store: { companies?: Company[] }) => ({ getCompany: vi.fn((_db, input) => Promise.resolve( ok({ company: store.companies?.find((company) => company.id === input.id) ?? null }), ), ), }); export const mockCustomerAccountQueries = (store: { customerAccounts?: CustomerAccountReference[]; }) => ({ getCustomerAccount: vi.fn((_db, input) => Promise.resolve( ok({ account: store.customerAccounts?.find((account) => account.id === input.customerAccountId) ?? null, }), ), ), }); export const mockPrimitivesQueries = (store: { currencies?: Currency[] }) => ({ getCurrency: vi.fn((_db, input) => Promise.resolve( ok({ currency: ("id" in input ? store.currencies?.find((currency) => currency.id === input.id) : store.currencies?.find((currency) => currency.code === input.code)) ?? null, }), ), ), }); export const mockCoaManagementQueries = (store: { accounts?: Account[] }) => ({ getAccount: vi.fn((_db, input) => Promise.resolve( ok({ account: ("id" in input ? store.accounts?.find((account) => account.id === input.id) : store.accounts?.find( (account) => account.companyId === input.companyId && account.code === input.code, )) ?? null, }), ), ), listAccounts: vi.fn(() => Promise.resolve(ok({ items: store.accounts ?? [], hasNextPage: false })), ), }); const journalEntry = { id: "journal-entry-1", companyId: "company-1", accountingPeriodId: "accounting-period-1", entryDate: new Date("2024-01-20"), status: "POSTED", description: "INCOMING PAYMENT", sourceDocumentType: "INCOMING_PAYMENT", sourceDocumentId: "incoming-payment-1", reversalOfId: null, postedAt: new Date("2024-01-20"), createdAt: new Date("2024-01-20"), updatedAt: new Date("2024-01-20"), }; export const mockFinancialAccountingCommands = () => ({ createJournalEntry: vi.fn(() => Promise.resolve(ok({ journalEntry }))) as unknown as ReturnType< typeof vi.fn >, postJournalEntry: vi.fn(() => Promise.resolve(ok({ journalEntry }))) as unknown as ReturnType< typeof vi.fn >, }); export const mockFinancialAccountingQueries = (accountingPeriod: object | null = {}) => ({ getPeriodByDate: vi.fn(() => Promise.resolve( ok({ accountingPeriod: accountingPeriod === null ? null : { id: "accounting-period-1", companyId: "company-1", fiscalYearId: "fiscal-year-1", name: "January 2024", startDate: new Date("2024-01-01"), endDate: new Date("2024-01-31"), periodType: "OPERATING", status: "OPEN", createdAt: new Date("2024-01-01"), updatedAt: new Date("2024-01-01"), ...accountingPeriod, }, }), ), ) as unknown as ReturnType>, });