Comp AI CRM is an open source, CRM designed for AI agents. Agentic-first CRM.
TypeScript
Refactor currency rates service to use open.er-api.com
Lewis Carhartf97823f2662f
diff --git a/apps/api/src/currency/rates.service.ts b/apps/api/src/currency/rates.service.tsindex 5c22ccaf..eefab65a 100644--- a/apps/api/src/currency/rates.service.ts+++ b/apps/api/src/currency/rates.service.ts@@ -1,6 +1,10 @@import type { Db } from "@crm/db";import { Prisma, RateSource } from "@crm/db";-import { isCurrencyCode, normalizeCurrency } from "@crm/db/currency";+import {+ CURRENCY_CODES,+ isCurrencyCode,+ normalizeCurrency,+} from "@crm/db/currency";import {readRatesRefreshedAt,readReportingCurrency,@@ -9,11 +13,15 @@ import {import { Injectable, Logger } from "@nestjs/common";import { InjectDatabase } from "../database/database.constants";-export const RATES_PROVIDER = "frankfurter.dev";+export const RATES_PROVIDER = "open.er-api.com";++const RATES_URL = "https://open.er-api.com/v6/latest";-const RATES_URL = "https://api.frankfurter.dev/v1/latest";+const RATES_TIMEOUT_MS = 6_000;-const RATES_TIMEOUT_MS = 8_000;+const RATES_ATTEMPTS = 2;++const RETRY_DELAY_MS = 400;export interface RateRefresh {ok: boolean;@@ -23,18 +31,24 @@ export interface RateRefresh {reason: string | null;}-interface FrankfurterResponse {- base?: unknown;- date?: unknown;+interface OpenExchangeResponse {+ result?: unknown;+ base_code?: unknown;+ time_last_update_unix?: unknown;rates?: Record<string, unknown>;+ "error-type"?: unknown;}function parseAsOf(value: unknown): Date | null {- if (typeof value !== "string") return null;- const date = new Date(`${value}T00:00:00.000Z`);+ if (typeof value !== "number" || !Number.isFinite(value)) return null;+ const date = new Date(value * 1000);return Number.isNaN(date.getTime()) ? null : date;}+function wait(ms: number): Promise<void> {+ return new Promise((resolve) => setTimeout(resolve, ms));+}+@Injectable()export class RatesService {private readonly logger = new Logger(RatesService.name);@@ -109,32 +123,76 @@ export class RatesService {written += 1;}+ const supported = [...CURRENCY_CODES];++ const stale = await this.db.exchangeRate.deleteMany({+ where: {+ source: RateSource.FETCHED,+ OR: [+ { baseCurrency: { notIn: supported } },+ { quoteCurrency: { notIn: supported } },+ ],+ },+ });++ if (stale.count > 0) {+ this.logger.log({+ message: "Dropped fetched rates for currencies that are not supported",+ base,+ dropped: stale.count,+ });+ }+return written;}private async fetch(base: string,+ ): Promise<{ rates: Map<string, Prisma.Decimal>; asOf: Date } | null> {+ for (let attempt = 1; attempt <= RATES_ATTEMPTS; attempt += 1) {+ const quotes = await this.attempt(base, attempt);+ if (quotes) return quotes;++ if (attempt < RATES_ATTEMPTS) await wait(RETRY_DELAY_MS);+ }++ return null;+ }++ private async attempt(+ base: string,+ attempt: number,): Promise<{ rates: Map<string, Prisma.Decimal>; asOf: Date } | null> {try {- const response = await fetch(- `${RATES_URL}?base=${encodeURIComponent(base)}`,- {- headers: { accept: "application/json" },- signal: AbortSignal.timeout(RATES_TIMEOUT_MS),- },- );+ const response = await fetch(`${RATES_URL}/${encodeURIComponent(base)}`, {+ headers: { accept: "application/json" },+ signal: AbortSignal.timeout(RATES_TIMEOUT_MS),+ });if (!response.ok) {this.logger.warn({message: "Exchange rate request failed",status: response.status,base,+ attempt,+ });+ return null;+ }++ const body = (await response.json()) as OpenExchangeResponse;++ if (body.result !== "success") {+ this.logger.warn({+ message: "Exchange rate provider refused the request",+ base,+ attempt,+ errorType:+ typeof body["error-type"] === "string" ? body["error-type"] : null,});return null;}- const body = (await response.json()) as FrankfurterResponse;- const asOf = parseAsOf(body.date) ?? new Date();+ const asOf = parseAsOf(body.time_last_update_unix) ?? new Date();const rates = new Map<string, Prisma.Decimal>();for (const [code, value] of Object.entries(body.rates ?? {})) {@@ -155,6 +213,7 @@ export class RatesService {this.logger.warn({message: "Exchange rate response carried no usable rates",base,+ attempt,});return null;}@@ -164,6 +223,7 @@ export class RatesService {this.logger.warn({message: "Exchange rates unavailable",base,+ attempt,reason: error instanceof Error ? error.message : String(error),});return null;diff --git a/apps/app/app/(app)/[slug]/settings/currencies/currency-settings.tsx b/apps/app/app/(app)/[slug]/settings/currencies/currency-settings.tsxindex a54effb7..104b4442 100644--- a/apps/app/app/(app)/[slug]/settings/currencies/currency-settings.tsx+++ b/apps/app/app/(app)/[slug]/settings/currencies/currency-settings.tsx@@ -165,7 +165,7 @@ export function CurrencySettings() {<CardTitle>Exchange rates</CardTitle><CardDescription>How many {reportingCurrency} one unit of each currency buys. Fetched- daily from the European Central Bank; a rate you enter here wins.+ daily from open.er-api.com; a rate you enter here wins.</CardDescription><CardAction><Buttondiff --git a/docs/api.md b/docs/api.mdindex d457aacb..3eb3ccb7 100644--- a/docs/api.md+++ b/docs/api.md@@ -612,6 +612,36 @@ call site — one place to be wrong instead of six.`isWellFormedCurrency` exists because `Intl.NumberFormat` throws a`RangeError` on anything that is not three letters, so a *formatter* has tocheck shape even where membership is not its business.+- **`CURRENCIES` is ten currencies, and that is the whole of what this CRM+ supports.** The world's ten most-traded — USD, EUR, JPY, GBP, CNY, AUD, CAD,+ CHF, HKD, SGD — in that order, and the pickers render it in array order rather+ than alphabetically so the answer is usually the first or second row. It+ started as fifty-three, which is a scroll bar in front of a decision that is+ `USD` nine times out of ten.+ - **Supported means supported everywhere**, and `isCurrencyCode` is the single+ gate: it is what `deals.contracts.ts` refines on, what `RatesService` filters+ the feed through, and what `readReportingCurrency` requires of a stored+ value. Offering ten and converting a hundred and sixty-five was two answers+ to one question — the picker said one thing and the rate table another.+ - **Everything offered is therefore convertible.** The feed covers all ten, so+ choosing from a picker cannot produce the unconverted deal the disclosure+ above exists to explain. That disclosure stays, because two things can still+ reach it: a row written before the list shrank, and a reporting-currency+ change made while the feed is unreachable.+ - **A refresh prunes as well as writes**, deleting `FETCHED` rows for the+ current base whose quote currency is not in the ten. Without that, an install+ that ran the old code keeps a hundred and fifty-six rates nothing will ever+ refresh, and a legacy deal stays convertible at a frozen number — which is+ the split this rule just removed, surviving in the data. It leaves `MANUAL`+ rows alone: a rate a person typed is not ours to delete, and the form can no+ longer create one outside the ten anyway.+ - **A deal in an unsupported currency is disclosed, not hidden.** Its+ `baseAmount` goes null on the next re-rate, which takes it out of every total+ and puts it in `unconverted` — so the dashboard says *N deals in SEK are not+ included* rather than quietly counting or quietly dropping them. Editing such+ a deal's currency is the fix, and the picker offers only the ten.+ - **There is no three-decimal currency left in the ten**, so the KWD caveat+ below is now only about a legacy row rather than anything a rep can enter.- **`minorUnitsOf` is why conversion rounds correctly.** JPY has no decimalplaces and KWD has three; `applyRate` rounds to the *reporting* currency'sown units rather than to two. The `×100` cents transport across tRPC is@@ -620,6 +650,28 @@ call site — one place to be wrong instead of six.minor unit, and neither can `Decimal(14, 2)`. A KWD deal is stored to thefils, not the fils-tenth.+**One feed, keyless: `open.er-api.com`.** It answers+`GET /v6/latest/{BASE}` with `{ result, base_code, time_last_update_unix, rates }`+and needs no account, which is what keeps it out of `.env` entirely.++- **It replaced `api.frankfurter.dev`, for coverage as much as uptime.**+ Frankfurter is ECB-backed and publishes 29 currencies and — the part that+ actually bit — could not be asked for a base outside its own set, so a+ workspace reporting in a currency it does not quote got nothing at all. It was+ also timing out intermittently on an 8-second budget. The replacement quotes+ 166 and accepts any of them as the base, which covers all ten of ours with+ room for the list to grow.+- **`result` is checked, not just the status code.** An unsupported base comes+ back **HTTP 200** with `{"result":"error","error-type":"unsupported-code"}`, so+ `response.ok` alone would read a refusal as success, find no `rates`, and log+ the misleading "carried no usable rates" instead of the reason the provider+ gave.+- **Two attempts, 6 seconds each.** The failure that prompted this was a+ transient timeout on a single try, and one retry costs a few hundred+ milliseconds against a request that normally answers in under 150ms. The+ ceiling stays low enough for the interactive Refresh button on the settings+ page to be worth pressing.+**The fetcher is in the API, and that is a deliberate exception to the rule atthe top of this file.** A daily rate is not intelligence: it decides nothingabout a person or a company, there is no confidence model, no identity to matchdiff --git a/docs/environment.md b/docs/environment.mdindex 96e43f2b..9fecc019 100644--- a/docs/environment.md+++ b/docs/environment.md@@ -361,9 +361,10 @@ The absences are deliberate:internal, and it is seeded into the sync's "us" set alongside the `User`table. Two sources for one fact is how a colleague becomes a lead.- **No `GMAIL_BACKFILL_DAYS`.** There is no backfill.-- **No exchange-rate provider variable.** The rate feed is a constant in- `apps/api/src/currency/rates.service.ts` and needs no key; an install that- cannot reach it types its own rates on Settings → Currencies instead. See+- **No exchange-rate provider variable.** The rate feed is `open.er-api.com`, a+ constant in `apps/api/src/currency/rates.service.ts`, and it needs no account;+ an install that cannot reach it types its own rates on Settings → Currencies+ instead. See[the currency rules](./api.md#a-deal-is-sold-in-one-currency-and-reported-in-another).`POST /internal/sync/rates` wants a **daily** schedule beside the five-minuteGoogle one.diff --git a/packages/db/prisma/seed.ts b/packages/db/prisma/seed.tsindex 7941dff7..8f240607 100644--- a/packages/db/prisma/seed.ts+++ b/packages/db/prisma/seed.ts@@ -484,15 +484,27 @@ const SEED_RATES: Record<string, number> = {const DEAL_CURRENCIES = ["USD", "USD", "USD", "EUR", "GBP", "JPY", "CAD"];+let seedBaseIsUsd = true;+async function seedRates(): Promise<number> {const asOf = daysFromNow(-1);- await db.appSetting.upsert({+ const setting = await db.appSetting.upsert({where: { id: "app" },create: { id: "app", reportingCurrency: "USD" },- update: { reportingCurrency: "USD" },+ update: {},+ select: { reportingCurrency: true },});+ seedBaseIsUsd = (setting.reportingCurrency ?? "USD").toUpperCase() === "USD";++ if (!seedBaseIsUsd) {+ console.log(+ `Reporting currency is ${setting.reportingCurrency} — seeding amounts ` ++ "without a converted figure; the rates cron will fill them in.",+ );+ }+for (const [quoteCurrency, rate] of Object.entries(SEED_RATES)) {await db.exchangeRate.upsert({where: {@@ -522,6 +534,10 @@ function money(usdAmount: number, currency: string) {const places = currency === "JPY" ? 0 : 2;const amount = Number((usdAmount / rate).toFixed(places));+ if (!seedBaseIsUsd) {+ return { amount, currency, baseAmount: null, fxRate: null };+ }+return {amount,currency,@@ -578,7 +594,7 @@ async function seedDeals(currency,baseAmount,fxRate,- fxRateAt: daysFromNow(-1),+ fxRateAt: fxRate === null ? null : daysFromNow(-1),};})(),expectedCloseDate: daysFromNow(diff --git a/packages/db/src/currency.ts b/packages/db/src/currency.tsindex 9ccad623..e9471525 100644--- a/packages/db/src/currency.ts+++ b/packages/db/src/currency.ts@@ -7,59 +7,16 @@ export interface CurrencyMeta {export const DEFAULT_REPORTING_CURRENCY = "USD";const CURRENCY_LIST: readonly CurrencyMeta[] = [- { code: "AED", name: "UAE Dirham", minorUnits: 2 },- { code: "ARS", name: "Argentine Peso", minorUnits: 2 },+ { code: "USD", name: "US Dollar", minorUnits: 2 },+ { code: "EUR", name: "Euro", minorUnits: 2 },+ { code: "JPY", name: "Japanese Yen", minorUnits: 0 },+ { code: "GBP", name: "Pound Sterling", minorUnits: 2 },+ { code: "CNY", name: "Chinese Yuan", minorUnits: 2 },{ code: "AUD", name: "Australian Dollar", minorUnits: 2 },- { code: "BGN", name: "Bulgarian Lev", minorUnits: 2 },- { code: "BHD", name: "Bahraini Dinar", minorUnits: 3 },- { code: "BRL", name: "Brazilian Real", minorUnits: 2 },{ code: "CAD", name: "Canadian Dollar", minorUnits: 2 },{ code: "CHF", name: "Swiss Franc", minorUnits: 2 },- { code: "CLP", name: "Chilean Peso", minorUnits: 0 },- { code: "CNY", name: "Chinese Yuan", minorUnits: 2 },- { code: "COP", name: "Colombian Peso", minorUnits: 2 },- { code: "CZK", name: "Czech Koruna", minorUnits: 2 },- { code: "DKK", name: "Danish Krone", minorUnits: 2 },- { code: "EGP", name: "Egyptian Pound", minorUnits: 2 },- { code: "EUR", name: "Euro", minorUnits: 2 },- { code: "GBP", name: "Pound Sterling", minorUnits: 2 },{ code: "HKD", name: "Hong Kong Dollar", minorUnits: 2 },- { code: "HUF", name: "Hungarian Forint", minorUnits: 2 },- { code: "IDR", name: "Indonesian Rupiah", minorUnits: 2 },- { code: "ILS", name: "Israeli New Shekel", minorUnits: 2 },- { code: "INR", name: "Indian Rupee", minorUnits: 2 },- { code: "ISK", name: "Icelandic Króna", minorUnits: 0 },- { code: "JOD", name: "Jordanian Dinar", minorUnits: 3 },- { code: "JPY", name: "Japanese Yen", minorUnits: 0 },- { code: "KES", name: "Kenyan Shilling", minorUnits: 2 },- { code: "KRW", name: "South Korean Won", minorUnits: 0 },- { code: "KWD", name: "Kuwaiti Dinar", minorUnits: 3 },- { code: "MAD", name: "Moroccan Dirham", minorUnits: 2 },- { code: "MXN", name: "Mexican Peso", minorUnits: 2 },- { code: "MYR", name: "Malaysian Ringgit", minorUnits: 2 },- { code: "NGN", name: "Nigerian Naira", minorUnits: 2 },- { code: "NOK", name: "Norwegian Krone", minorUnits: 2 },- { code: "NZD", name: "New Zealand Dollar", minorUnits: 2 },- { code: "OMR", name: "Omani Rial", minorUnits: 3 },- { code: "PEN", name: "Peruvian Sol", minorUnits: 2 },- { code: "PHP", name: "Philippine Peso", minorUnits: 2 },- { code: "PKR", name: "Pakistani Rupee", minorUnits: 2 },- { code: "PLN", name: "Polish Złoty", minorUnits: 2 },- { code: "QAR", name: "Qatari Riyal", minorUnits: 2 },- { code: "RON", name: "Romanian Leu", minorUnits: 2 },- { code: "RSD", name: "Serbian Dinar", minorUnits: 2 },- { code: "SAR", name: "Saudi Riyal", minorUnits: 2 },- { code: "SEK", name: "Swedish Krona", minorUnits: 2 },{ code: "SGD", name: "Singapore Dollar", minorUnits: 2 },- { code: "THB", name: "Thai Baht", minorUnits: 2 },- { code: "TND", name: "Tunisian Dinar", minorUnits: 3 },- { code: "TRY", name: "Turkish Lira", minorUnits: 2 },- { code: "TWD", name: "New Taiwan Dollar", minorUnits: 2 },- { code: "UAH", name: "Ukrainian Hryvnia", minorUnits: 2 },- { code: "USD", name: "US Dollar", minorUnits: 2 },- { code: "UYU", name: "Uruguayan Peso", minorUnits: 2 },- { code: "VND", name: "Vietnamese Dong", minorUnits: 0 },- { code: "ZAR", name: "South African Rand", minorUnits: 2 },] as const;export const CURRENCIES = CURRENCY_LIST;diff --git a/packages/db/test/currency.spec.ts b/packages/db/test/currency.spec.tsindex 58fc3ebd..0a5a8ab7 100644--- a/packages/db/test/currency.spec.ts+++ b/packages/db/test/currency.spec.ts@@ -1,6 +1,7 @@import { describe, expect, it } from "bun:test";import type { Db } from "../src/client";import {+ CURRENCIES,currencyName,isCurrencyCode,isWellFormedCurrency,@@ -59,7 +60,23 @@ describe("normalizeCurrency and isCurrencyCode", () => {}});- it("refuses a three-letter string that is not a currency", () => {+ it("offers only the ten currencies most of the world trades in", () => {+ expect(CURRENCIES.map((entry) => entry.code)).toEqual([+ "USD",+ "EUR",+ "JPY",+ "GBP",+ "CNY",+ "AUD",+ "CAD",+ "CHF",+ "HKD",+ "SGD",+ ]);+ });++ it("refuses a real currency it does not offer, and anything that is not one", () => {+ expect(isCurrencyCode("SEK")).toBe(false);expect(isCurrencyCode("ZZZ")).toBe(false);expect(isCurrencyCode("QQ")).toBe(false);expect(isCurrencyCode("")).toBe(false);@@ -81,14 +98,12 @@ describe("normalizeCurrency and isCurrencyCode", () => {describe("minorUnitsOf", () => {it("knows the currencies that are not two-decimal", () => {expect(minorUnitsOf("JPY")).toBe(0);- expect(minorUnitsOf("KRW")).toBe(0);- expect(minorUnitsOf("KWD")).toBe(3);- expect(minorUnitsOf("BHD")).toBe(3);});it("assumes two for anything else, so an amount still round-trips", () => {expect(minorUnitsOf("USD")).toBe(2);expect(minorUnitsOf("ZZZ")).toBe(2);+ expect(minorUnitsOf("SEK")).toBe(2);});});