Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

25.5 KB
Raw Blame
// SPDX-License-Identifier: LicenseRef-Carbon-Commercial
// Carbon Enterprise file, licensed only under the Carbon Commercial License
// (packages/ee/LICENSE). Not AGPL. Running, modifying, or copying it beyond those terms requires a commercial license.

import { createHash } from "node:crypto";
import type { KyselyTx } from "@carbon/database/client";
import { loadAccountCodesById } from "../../../core/account-mapping";
import {
  type CardChargeSource,
  chargeLineDescription,
  loadCardChargeSources,
  validateChargeAccountMapping
} from "../../../core/card-charge-source";
import { ChargeSyncerBase } from "../../../core/charge-syncer";
import {
  buildDimensionValueMappingEntityId,
  buildDimensionValueMappingLookup,
  ensureDimensionValueExternalIds,
  getDimensionValueMappings,
  resolveDimensionValueLabels,
  upsertDimensionValueMapping
} from "../../../core/dimension-mapping";
import {
  type ChargeCostingResult,
  type CostingLine,
  loadChargeCostingLines,
  toTransactionCurrencyLines
} from "../../../core/document-costing";
import {
  CHARGE_CREDIT_PROVIDERS,
  JournalEntrySyncError,
  type PostingSyncSettings,
  resolvePostingSyncSettings
} from "../../../core/posting";
import type {
  BatchSyncResult,
  ShouldSyncContext,
  SyncResult
} from "../../../core/types";
import { throwXeroApiError } from "../../../core/utils";
import { parseDotnetDate, type Xero } from "../models";
import { parseXeroTrackingTarget, type XeroProvider } from "../provider";
import { assertXeroMoneyPrecision } from "../serialize";
import type { XeroJournalDimensionArgs } from "./journal-entry";

/**
 * XeroChargeSyncer — Carbon charges (Ramp card spend) → Xero bank
 * transactions (push-only, create-only; entityType "charge").
 *
 * Xero has no charge object: a card charge IS a spend-money bank transaction
 * on the card account — Xero models a credit card as a BANK-type account of
 * BankAccountType CREDITCARD — and a merchant refund is receive-money on the
 * same account. Xero derives the posting itself (debit each line's account,
 * credit the card account for SPEND; the reverse for RECEIVE), which is
 * exactly what Carbon's "Charge" journal booked, so the lines are
 * that journal's coded lines (shared `loadChargeCostingLines`,
 * card-liability line excluded) and the two ledgers cannot drift. While this
 * syncer is enabled the journal itself is DOC_BACKED-excluded per row
 * (core/posting.ts), never pushed twice.
 *
 * Only a Posted `Charge` or `Credit` with a merchant supplier is pushed —
 * Xero is in `CHARGE_CREDIT_PROVIDERS`, so a refund is a RECEIVE rather than
 * a journal entry; the other three charge types are money movements
 * with no vendor and stay journal entries. Every skip here is mirrored by the
 * policy, so a skipped row's journal keeps pushing — the spend always reaches
 * Xero as exactly one of the two.
 *
 * A Voided charge that was pushed is deleted remotely (Xero's
 * delete is a POST carrying Status DELETED) and its mapping tombstoned —
 * the same contract as the Rillet transaction syncers.
 *
 * Unmapped accounts fail as the structured UNMAPPED_ACCOUNTS Warning, same as
 * bills — never a silent fallback account.
 */

/** The Carbon `charge` header as the syncer reads it. */
export type XeroCardCharge = CardChargeSource;

/** Costing lines are the shared shape; aliased so the mapper's tests read against a stable name. */
export type XeroChargePostingJournalLine = CostingLine;

/** The parts of the costing result the pure mapper consumes. */
export type XeroChargeCosting = Pick<
  ChargeCostingResult,
  | "lines"
  | "documentTotal"
  | "decimalPlaces"
  | "baseCurrencyCode"
  | "postingDate"
  | "transactionDate"
  | "currencyCode"
  | "exchangeRate"
  | "cardAccountId"
>;

/** Fields Xero assigns; never part of a write payload. */
export type XeroBankTransactionWriteOmit =
  | "UpdatedDateUTC"
  | "BankTransactionID";

export type XeroBankTransactionWrite = Omit<
  Xero.BankTransaction,
  XeroBankTransactionWriteOmit
>;

/**
 * Tracking for one line from the company's dimension slots — the same
 * slot → `tracking:<TrackingCategoryID>` resolution the manual-journal
 * mapper uses. At most one entry per slot, and slots are capped at Xero's
 * org-wide two active categories (`XERO_MAX_JOURNAL_DIMENSION_SLOTS`). A
 * slotted dimension whose option is unmapped is dropped from the line — the
 * journal syncer's drop policy; the caller has already auto-created opt-in
 * options.
 */
function buildXeroLineTracking(
  line: CostingLine,
  dimensions: XeroJournalDimensionArgs | undefined
): Xero.ManualJournalTracking[] {
  const tracking: Xero.ManualJournalTracking[] = [];
  if (!dimensions) return tracking;
  for (const slot of dimensions.slots) {
    const trackingCategoryId = parseXeroTrackingTarget(slot.target);
    if (!trackingCategoryId) continue;
    const dimension = line.dimensions?.find(
      (candidate) => candidate.dimensionId === slot.dimensionId
    );
    if (!dimension) continue;
    const trackingOptionId = dimensions.optionIdsByValue.get(
      buildDimensionValueMappingEntityId(
        dimension.dimensionId,
        dimension.valueId
      )
    );
    if (!trackingOptionId) continue; // drop policy — recorded by the caller
    tracking.push({
      TrackingCategoryID: trackingCategoryId,
      TrackingOptionID: trackingOptionId
    });
  }
  return tracking;
}

/**
 * Map a Carbon charge to the Xero bank-transaction write payload.
 * Pure — exported for tests. `costing.lines` are the posted journal's coded
 * lines (card-liability line already excluded), base-currency and
 * debit-signed; `costing.exchangeRate` converts them to the card's
 * transaction currency (rounded at the document currency boundary). A Charge
 * is a `SPEND`, a Credit (merchant refund) a `RECEIVE`; Xero carries the
 * direction on `Type`, so line amounts are sent as positive magnitudes. Throws
 * structured Warnings when the journal is missing or an account (line or
 * card) is unmapped, and refuses principal Xero's two-decimal monetary
 * boundary cannot represent.
 */
export function mapChargeToXeroBankTransaction(args: {
  charge: XeroCardCharge;
  costing: XeroChargeCosting;
  vendorRemoteId: string;
  accountCodesById: ReadonlyMap<string, string>;
  dimensions?: XeroJournalDimensionArgs;
}): XeroBankTransactionWrite {
  const { charge, costing } = args;

  validateChargeAccountMapping({
    charge,
    costing,
    accountsById: args.accountCodesById,
    providerName: "Xero"
  });
  // Presence asserted by the guard above.
  const cardAccountCode = args.accountCodesById.get(costing.cardAccountId)!;

  const transactionLines = toTransactionCurrencyLines(costing.lines, {
    exchangeRate: costing.exchangeRate,
    documentTotal: costing.documentTotal,
    decimalPlaces: costing.decimalPlaces
  });
  assertXeroMoneyPrecision(...transactionLines.map((line) => line.amount));

  // A Credit's costing lines are credit-signed (negative). Negate rather than
  // abs: a contra line inside a document keeps its relative sign, so the
  // lines still sum to the document total Xero recomputes.
  const isCredit = charge.type === "Credit";

  const lineItems: Xero.BankTransactionLineItem[] = transactionLines.map(
    (line) => {
      const tracking = buildXeroLineTracking(line, args.dimensions);
      const description = chargeLineDescription(charge, line);
      return {
        ...(description ? { Description: description } : {}),
        Quantity: 1,
        UnitAmount: isCredit ? -line.amount : line.amount,
        // Presence asserted above; the non-null assertion is the mapped code.
        AccountCode: args.accountCodesById.get(line.accountId!)!,
        TaxType: "NONE",
        ...(tracking.length > 0 ? { Tracking: tracking } : {})
      };
    }
  );

  return {
    Type: isCredit ? "RECEIVE" : "SPEND",
    Contact: { ContactID: args.vendorRemoteId },
    BankAccount: { Code: cardAccountCode },
    Date: costing.postingDate,
    Reference: `${charge.chargeId} [carbon:${charge.companyId}:${charge.id}]`,
    Status: "AUTHORISED",
    // Tax-neutral replay: the card posting folds tax into cost, so the lines
    // already embed it; let Xero total the NONE-taxed lines.
    LineAmountTypes: "NoTax",
    CurrencyCode: costing.currencyCode,
    // Pin every foreign snapshot, including negotiated 1:1 rates (the bill
    // convention).
    CurrencyRate:
      costing.currencyCode !== costing.baseCurrencyCode
        ? costing.exchangeRate
        : undefined,
    LineItems: lineItems
  };
}

export class XeroChargeSyncer extends ChargeSyncerBase<
  XeroCardCharge,
  Xero.BankTransaction,
  XeroBankTransactionWriteOmit
> {
  // Per-instance caches — a drain reuses one syncer across its claimed
  // operations, so account codes, the Xero chart, settings and the
  // dimension-value lookup are each fetched at most once per drain
  private accountCodesByIdPromise?: Promise<Map<string, string>>;
  private chartOfAccountsPromise?: Promise<Xero.Account[]>;
  private postingSyncSettingsPromise?: Promise<PostingSyncSettings>;
  private dimensionValueMappingsPromise?: Promise<Map<string, string>>;

  private get xeroProvider(): XeroProvider {
    return this.provider as XeroProvider;
  }

  // =================================================================
  // 1. CACHED INPUTS (account codes, chart, settings, dimension values)
  // =================================================================

  private getAccountCodesById(): Promise<Map<string, string>> {
    if (!this.accountCodesByIdPromise) {
      this.accountCodesByIdPromise = loadAccountCodesById(this.database, {
        companyId: this.companyId,
        integration: this.provider.id
      });
    }
    return this.accountCodesByIdPromise;
  }

  /** Xero chart of accounts, read once per syncer instance (BANK-type check). */
  private getChartOfAccounts(): Promise<Xero.Account[]> {
    if (!this.chartOfAccountsPromise) {
      this.chartOfAccountsPromise = this.xeroProvider.listChartOfAccounts();
    }
    return this.chartOfAccountsPromise;
  }

  /**
   * Per-company posting-sync settings from
   * `companyIntegration.metadata.settings.postingSync` — the dimension slots
   * are what a charge line's tracking is resolved through.
   */
  private getPostingSyncSettings(): Promise<PostingSyncSettings> {
    if (!this.postingSyncSettingsPromise) {
      this.postingSyncSettingsPromise = (async () => {
        const integration = await this.database
          .selectFrom("companyIntegration")
          .select("metadata")
          .where("id", "=", this.provider.id)
          .where("companyId", "=", this.companyId)
          .executeTakeFirst();

        return resolvePostingSyncSettings(integration?.metadata);
      })();
    }
    return this.postingSyncSettingsPromise;
  }

  /**
   * `<dimensionId>:<valueId>` → Xero TrackingOptionID from the
   * dimension-value mapping rows (entityType "dimensionValue"). Mutated in
   * place by the autoCreate flow so later pushes in the same drain reuse the
   * created options.
   */
  private getDimensionValueMappings(): Promise<Map<string, string>> {
    if (!this.dimensionValueMappingsPromise) {
      this.dimensionValueMappingsPromise = (async () => {
        const mappings = await getDimensionValueMappings(this.database, {
          companyId: this.companyId,
          integration: this.provider.id
        });
        if (mappings.error) {
          throw new Error(
            `Failed to load dimension value mappings: ${mappings.error}`
          );
        }
        return buildDimensionValueMappingLookup(mappings.data ?? []);
      })();
    }
    return this.dimensionValueMappingsPromise;
  }

  /**
   * autoCreate (opt-in per slot for Xero): create missing tracking options
   * BY NAME — the value's resolved READABLE label — under the slot's
   * tracking category, then store the mapping and update the lookup in
   * place. Same flow as the manual-journal syncer.
   */
  private async ensureAutoCreatedDimensionValues(
    lines: ReadonlyArray<CostingLine>,
    settings: PostingSyncSettings,
    mappings: Map<string, string>
  ): Promise<void> {
    await ensureDimensionValueExternalIds({
      lines,
      slots: settings.dimensionSlots,
      defaultAutoCreate: false, // Xero: opt-in avoids surprise list writes
      mappings,
      resolveLabels: (values) =>
        resolveDimensionValueLabels(this.database, { values }),
      createExternalValue: async (slot, label) => {
        const trackingCategoryId = parseXeroTrackingTarget(slot.target);
        if (!trackingCategoryId) {
          throw new Error(`Unknown Xero dimension target "${slot.target}"`);
        }
        const created = await this.xeroProvider.createTrackingOption(
          trackingCategoryId,
          label
        );
        return created.TrackingOptionID;
      },
      persistMapping: async (value, externalId, label) => {
        const persisted = await upsertDimensionValueMapping(this.database, {
          companyId: this.companyId,
          integration: this.provider.id,
          dimensionId: value.dimensionId,
          valueId: value.valueId,
          externalId,
          externalName: label
        });
        if (persisted.error) {
          throw new Error(
            `Failed to store dimension value mapping: ${persisted.error}`
          );
        }
      }
    });
  }

  // =================================================================
  // 2. TIMESTAMP EXTRACTION
  // =================================================================

  protected getRemoteUpdatedAt(remote: Xero.BankTransaction): Date | null {
    if (!remote.UpdatedDateUTC) return null;
    return parseDotnetDate(remote.UpdatedDateUTC);
  }

  // =================================================================
  // 3. LOCAL FETCH (Single + Batch)
  // =================================================================

  async fetchLocal(id: string): Promise<XeroCardCharge | null> {
    const charges = await this.fetchChargesByIds([id]);
    return charges.get(id) ?? null;
  }

  protected async fetchLocalBatch(
    ids: string[]
  ): Promise<Map<string, XeroCardCharge>> {
    return this.fetchChargesByIds(ids);
  }

  private async fetchChargesByIds(
    ids: string[]
  ): Promise<Map<string, XeroCardCharge>> {
    return loadCardChargeSources(this.database, {
      ids,
      companyId: this.companyId,
      integration: this.provider.id
    });
  }

  // =================================================================
  // 4. REMOTE FETCH (Single + Batch)
  // =================================================================

  async fetchRemote(id: string): Promise<Xero.BankTransaction | null> {
    const response = await this.xeroProvider.request<{
      BankTransactions: Xero.BankTransaction[];
    }>("GET", `/BankTransactions/${id}`);

    if (response.error) return null;
    return response.data?.BankTransactions?.[0] ?? null;
  }

  protected async fetchRemoteBatch(
    ids: string[]
  ): Promise<Map<string, Xero.BankTransaction>> {
    const result = new Map<string, Xero.BankTransaction>();
    for (const id of ids) {
      const bankTransaction = await this.fetchRemote(id);
      if (bankTransaction) {
        result.set(bankTransaction.BankTransactionID, bankTransaction);
      }
    }
    return result;
  }

  // =================================================================
  // 5. SHOULD SYNC — mirrors isDocBackedCharge exactly
  // =================================================================

  protected shouldSync(
    context: ShouldSyncContext<XeroCardCharge, Xero.BankTransaction>
  ): boolean | string {
    if (context.direction === "pull") {
      return "Card charges are push-only; pulling bank transactions from Xero is not supported";
    }
    const local = context.localEntity;
    if (!local) return true;
    if (local.status !== "Posted") {
      return `Charge must be posted before syncing (current status: ${local.status})`;
    }
    if (local.type !== "Charge" && local.type !== "Credit") {
      return `Charge type ${local.type} is a money movement, not a charge — it syncs as a journal entry`;
    }
    if (
      local.type === "Credit" &&
      !CHARGE_CREDIT_PROVIDERS.has(this.provider.id)
    ) {
      return "Card credits (merchant refunds) sync as journal entries for this provider";
    }
    if (!local.supplierId) {
      return "Card charge has no merchant supplier — it syncs as a journal entry";
    }
    return true;
  }

  // =================================================================
  // 6. TRANSFORMATION (Carbon -> Xero)
  // =================================================================

  protected async mapToRemote(
    local: XeroCardCharge
  ): Promise<XeroBankTransactionWrite> {
    // JIT dependency: vendor (contact) before the document
    let vendorRemoteId = local.supplierExternalId;
    if (!vendorRemoteId && local.supplierId) {
      vendorRemoteId = await this.ensureDependencySynced(
        "vendor",
        local.supplierId
      );
    }
    if (!vendorRemoteId) {
      throw new Error(
        `Cannot sync card charge ${local.id}: No supplier linked or supplier not synced to Xero`
      );
    }

    const costing = await loadChargeCostingLines(this.database, {
      companyId: this.companyId,
      chargeId: local.id
    });

    const accountCodesById = await this.getAccountCodesById();

    // Xero /BankTransactions rejects a BankAccount that is not a BANK-type
    // account (a credit card is one: BankAccountType CREDITCARD). Resolve the
    // mapped code against the chart and refuse early with a fixable Warning
    // instead of an opaque Xero 400 — the gate the payment syncer runs on its
    // bank account. An unmapped card account falls through to the mapper's
    // UNMAPPED_ACCOUNTS with the standard metadata; an empty chart means the
    // read failed (listChartOfAccounts is forgiving), so Xero's own
    // validation stays the gate rather than parking every charge.
    const cardAccountCode = accountCodesById.get(costing.cardAccountId);
    if (cardAccountCode) {
      const chart = await this.getChartOfAccounts();
      const cardAccount = chart.find(
        (account) => account.Code === cardAccountCode
      );
      if (chart.length > 0 && cardAccount?.Type !== "BANK") {
        throw new JournalEntrySyncError({
          errorCode: "UNMAPPED_ACCOUNTS",
          warning: true,
          message: `Cannot sync card charge: the card-liability account must map to a Xero bank-type account — a credit card account in Xero (mapped code ${cardAccountCode}). Map it under the integration's Accounts tab, then retry.`,
          metadata: {
            chargeId: local.id,
            cardAccountId: costing.cardAccountId,
            cardAccountCode
          }
        });
      }
    }

    // Dimension slots: resolve the value-mapping lookup and auto-create
    // missing tracking options (opt-in per slot) before mapping, so freshly
    // created options are never dropped as unmapped
    const settings = await this.getPostingSyncSettings();
    let dimensionValueMappings: Map<string, string> | undefined;
    if (settings.dimensionSlots.length > 0) {
      dimensionValueMappings = await this.getDimensionValueMappings();
      await this.ensureAutoCreatedDimensionValues(
        costing.lines,
        settings,
        dimensionValueMappings
      );
    }

    return mapChargeToXeroBankTransaction({
      charge: local,
      costing,
      vendorRemoteId,
      accountCodesById,
      ...(dimensionValueMappings
        ? {
            dimensions: {
              slots: settings.dimensionSlots,
              optionIdsByValue: dimensionValueMappings
            }
          }
        : {})
    });
  }

  // =================================================================
  // 7. TRANSFORMATION (Xero -> Carbon) - Not supported (push-only)
  // =================================================================

  protected async mapToLocal(
    _remote: Xero.BankTransaction
  ): Promise<Partial<XeroCardCharge>> {
    throw new Error(
      "Card charges are push-only. Cannot map from Xero to Carbon."
    );
  }

  protected async upsertLocal(
    _tx: KyselyTx,
    _data: Partial<XeroCardCharge>,
    _remoteId: string
  ): Promise<string> {
    throw new Error(
      "Card charges are push-only. Cannot upsert locally from Xero."
    );
  }

  // =================================================================
  // 8. UPSERT REMOTE (create-only; pushToAccounting hard-skips mapped ids)
  // =================================================================

  protected async upsertRemote(
    data: XeroBankTransactionWrite,
    localId: string
  ): Promise<string> {
    // Xero's key expires after six minutes. The exact Carbon reference is the
    // durable recovery path when a create succeeded but its mapping did not.
    // https://developer.xero.com/documentation/guides/idempotent-requests/idempotency/
    const existing = await this.xeroProvider.request<{
      BankTransactions: Xero.BankTransaction[];
    }>(
      "GET",
      `/BankTransactions?where=${encodeURIComponent(`Reference==${JSON.stringify(data.Reference)}`)}&page=1`
    );
    if (existing.error) throwXeroApiError("recover card charge", existing);
    if (!Array.isArray(existing.data?.BankTransactions))
      throw new Error("Xero card charge lookup returned no transaction list");
    const matches = existing.data.BankTransactions;
    if (matches.length > 1)
      throw new Error(
        "Multiple Xero charges match this Carbon charge; resolve duplicates before retrying"
      );
    const match = matches[0];
    if (match) {
      if (
        match.Reference !== data.Reference ||
        match.Status !== "AUTHORISED" ||
        !match.BankTransactionID
      )
        throw new Error(
          "Xero card charge recovery found a deleted or inconsistent transaction"
        );
      return match.BankTransactionID;
    }
    const result = await this.xeroProvider.request<{
      BankTransactions: Xero.BankTransaction[];
    }>("PUT", "/BankTransactions?unitdp=4", {
      body: JSON.stringify({ BankTransactions: [data] }),
      headers: {
        "Idempotency-Key": createHash("sha256")
          .update(`${this.companyId}:charge:${localId}`)
          .digest("hex")
      }
    });

    if (result.error) {
      throwXeroApiError("create bank transaction", result);
    }

    const created = result.data?.BankTransactions?.[0];
    if (!created?.BankTransactionID) {
      throw new Error(
        "Xero API returned success but no BankTransactionID was returned for card charge"
      );
    }

    return created.BankTransactionID;
  }

  protected async upsertRemoteBatch(
    data: Array<{ localId: string; payload: XeroBankTransactionWrite }>
  ): Promise<Map<string, string>> {
    const result = new Map<string, string>();
    // One transaction per PUT keeps per-charge idempotency simple; charge
    // volumes per drain are small (claim limit 20)
    for (const { localId, payload } of data) {
      result.set(localId, await this.upsertRemote(payload, localId));
    }
    return result;
  }

  /**
   * Xero has no DELETE verb for bank transactions: POSTing the transaction
   * with Status DELETED is the delete (spend/receive money only). Xero
   * refuses once the transaction is reconciled, and that refusal surfaces as
   * the operation's failure rather than a silent tombstone. The read/update
   * shape follows Xero's official SDK example; live sandbox verification is
   * still required for reconciled transactions and locked accounting periods.
   */
  protected async deleteRemote(remoteId: string): Promise<void> {
    // Follow Xero's official SDK example: read the transaction, remove
    // response-only timestamps/contact, then update that specific resource.
    // https://github.com/XeroAPI/xero-node-oauth2-app/blob/master/src/app.ts
    const path = `/BankTransactions/${encodeURIComponent(remoteId)}`;
    const current = await this.xeroProvider.request<{
      BankTransactions: Xero.BankTransaction[];
    }>("GET", path);
    if (current.error)
      throwXeroApiError("read bank transaction before delete", current);
    const remote = current.data?.BankTransactions?.[0];
    if (remote?.BankTransactionID !== remoteId)
      throw new Error(
        "Xero did not return the charge; deletion is unconfirmed"
      );
    if (remote.Status === "DELETED") return;
    const {
      UpdatedDateUTC: _updatedAt,
      Contact: _contact,
      ...payload
    } = remote;
    const result = await this.xeroProvider.request<{
      BankTransactions: Xero.BankTransaction[];
    }>("POST", path, {
      body: JSON.stringify({
        BankTransactions: [{ ...payload, Status: "DELETED" }]
      })
    });
    if (result.error) throwXeroApiError("delete bank transaction", result);
    const deleted = result.data?.BankTransactions?.[0];
    if (deleted?.BankTransactionID !== remoteId || deleted.Status !== "DELETED")
      throw new Error("Xero did not confirm the charge was deleted");
  }

  // =================================================================
  // 9. PULL WORKFLOW - Not supported (push-only)
  // =================================================================

  async pullFromAccounting(remoteId: string): Promise<SyncResult> {
    return {
      status: "error",
      action: "none",
      remoteId,
      error:
        "Card charges are push-only: pulling bank transactions from Xero into Carbon is not supported"
    };
  }

  async pullBatchFromAccounting(remoteIds: string[]): Promise<BatchSyncResult> {
    const results: SyncResult[] = remoteIds.map((remoteId) => ({
      status: "error",
      action: "none",
      remoteId,
      error:
        "Card charges are push-only: pulling bank transactions from Xero into Carbon is not supported"
    }));

    return {
      results,
      successCount: 0,
      errorCount: results.length,
      skippedCount: 0
    };
  }
}