Comp AI CRM is an open source, CRM designed for AI agents. Agentic-first CRM.
Currency — read when touching deal amounts, totals, or rates
Two amounts, and only one is ever summed. (_sum: { amount: true } once added euros
to dollars and printed $2.0M, silently.)
amount+currencyis what the customer pays. Never converted in place.baseAmountis the only column any total, chart, average or sort may touch;fxRate/fxRateAtrecord how it got there.baseCurrencysays whatbaseAmountis denominated in — without it a figure converted against a stale base is indistinguishable from a correct one.countedWhere(base)filters every money aggregate. Counts are deliberately not filtered, so stage groups read counts and sums from separate queries.pendingWhere(base)= nullbaseAmountor wrongbaseCurrency, and it matches null explicitly —{ not: base }isNULL, not true, for a null column, so such rows were invisible everywhere.- Compose with
AND, never spread — it contains anOR, and so does the deals list's ownwhere. - Every writer of
baseAmountwritesbaseCurrencyin the same statement:ConversionServiceandprisma/seed.ts.
- The rate is resolved once and frozen.
create/updatecallConversionService.dealFieldswhenamountorcurrencychanges, reading the unchanged one back in the same call. Converting on read makes a closed quarter change value every morning. - A missing rate is a null, disclosed not zeroed — it falls out of
_sumautomatically, andunconvertedcounts those rows so the UI can say 3 deals in CHF are not included. fillMissing()never touches a converted deal;rerateAll()is the only thing that overwrites a frozen rate, and only on a reporting-currency change.
ExchangeRate.rate = units of baseCurrency per unit of quoteCurrency, so
baseAmount = amount × rate everywhere. The feed quotes the other way; RatesService
inverts on ingest.
MANUALbeatsFETCHED(unique on(base, quote, source)), which makes the fetcher optional — Settings → Currencies is the manual path.resolveRaterefuses a rate ≤ 0.- Re-rating deduplicates codes through a
Set—currencywas free text, sousdandUSDwere two groups each updating every variant. MAX_AMOUNT_CENTS(deals.contracts.ts) is whatDecimal(14, 2)holds;baseAmountisDecimal(24, 4)so amount × rate still fits.- Reporting currency is
AppSetting.reportingCurrency, read only throughreadReportingCurrency. - Codes are validated against
isCurrencyCode(@crm/db/currency), not a regex —z.string().length(3)acceptedZZZ.isWellFormedCurrencyis separate becauseIntlthrows on non-three-letter input. CURRENCIESis eleven currencies and that is all this CRM supports — USD, EUR, JPY, GBP, CNY, AUD, CAD, CHF, HKD, SGD, ZAR, in array order.isCurrencyCodeis the single gate for the picker, the feed filter and the stored setting. A refresh prunesFETCHEDrows outside the list and leavesMANUALalone.applyRaterounds to the reporting currency'sminorUnitsOf, not to two. The ×100 cents transport cannot represent a three-decimal minor unit; nor canDecimal(14, 2).
Feed: open.er-api.com, keyless, two attempts at 6s. Check result, not just
the status — an unsupported base returns HTTP 200 with {"result":"error"}. An
unreachable provider warns and returns { ok: false }; only the interactive refresh
throws.
The fetcher is in the API deliberately: a daily rate decides nothing, and
DealsService needs it synchronously to write baseAmount in the same transaction.
Any judgement about it belongs in apps/agent.
POST /internal/sync/rates is guarded by CRON_SECRET (timingSafeEquals) and
fails closed when unset. A route is not a schedule — add it to
apps/api/vercel.json in the same change or nothing runs it.