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.

feat: pull api key from integrations database

Brad Barbin16c06c74fe9d
diff --git a/apps/carbon/app/jobs/get-exchange-rates.server.ts b/apps/carbon/app/jobs/get-exchange-rates.server.ts
index e17eb8743b..06b88e8973 100644
--- a/apps/carbon/app/jobs/get-exchange-rates.server.ts
+++ b/apps/carbon/app/jobs/get-exchange-rates.server.ts
@@ -1,9 +1,13 @@
import { intervalTrigger } from "@trigger.dev/sdk";
-import { exchangeRatesClient } from "~/lib/exchange-rates.server";
+import z from "zod";
+import { getExchangeRatesClient } from "~/lib/exchange-rates.server";
import { getSupabaseServiceRole } from "~/lib/supabase";
import { triggerClient } from "~/lib/trigger.server";
const supabaseClient = getSupabaseServiceRole();
+const metadataSchema = z.object({
+ apiKey: z.string(),
+});
export const job = triggerClient.defineJob({
id: "get-exchange-rates",
@@ -13,7 +17,22 @@ export const job = triggerClient.defineJob({
seconds: 60 * 60 * 8, // thrice a day
}),
run: async (payload, io, ctx) => {
- // TODO: get client dynamically from DB
+ const integration = await supabaseClient
+ .from("integration")
+ .select("active, metadata")
+ .eq("name", "exchangeRatesV1")
+ .maybeSingle();
+
+ const integrationMetadata = metadataSchema.safeParse(
+ integration?.data?.metadata
+ );
+
+ if (!integrationMetadata.success || integration?.data?.active !== true)
+ return;
+ const exchangeRatesClient = getExchangeRatesClient(
+ integrationMetadata.data.apiKey
+ );
+
if (!exchangeRatesClient) return;
await io.logger.info(`💵 Exchange Rates Job: ${payload.lastTimestamp}`);
@@ -21,7 +40,7 @@ export const job = triggerClient.defineJob({
try {
const rates = await exchangeRatesClient.getExchangeRates();
- const timestamp = new Date().toISOString();
+ const updatedAt = new Date().toISOString();
await io.logger.info(JSON.stringify(rates));
const { error } = await supabaseClient
.from("currencyExchangeRate")
@@ -29,7 +48,7 @@ export const job = triggerClient.defineJob({
Object.entries(rates).map(([currency, exchangeRate]) => ({
currency,
exchangeRate,
- timestamp,
+ updatedAt,
}))
);
if (error) {
@@ -39,7 +58,7 @@ export const job = triggerClient.defineJob({
io.logger.log("Success");
} catch (err) {
- // TODO: notify SRE
+ // TODO: notify someone
await io.logger.error(JSON.stringify(err));
}
},
diff --git a/apps/carbon/app/lib/exchange-rates.server.ts b/apps/carbon/app/lib/exchange-rates.server.ts
index a73ec36eaa..c6e254495b 100644
--- a/apps/carbon/app/lib/exchange-rates.server.ts
+++ b/apps/carbon/app/lib/exchange-rates.server.ts
@@ -76,14 +76,16 @@ export class ExchangeRatesClient {
}
}
-const apiKey = process.env.EXCHANGE_RATES_API_KEY;
-const apiUrl = process.env.EXCHANGE_RATES_API_URL;
-
-export const exchangeRatesClient =
- apiKey && apiUrl
+export const getExchangeRatesClient = (
+ apiKey?: string,
+ apiUrl: string = "http://api.exchangeratesapi.io/v1/latest",
+ baseCurrency: CurrencyCode = "USD"
+) => {
+ return typeof apiKey === "string"
? new ExchangeRatesClient({
apiKey,
apiUrl,
- baseCurrency: "USD",
+ baseCurrency,
})
: undefined;
+};
diff --git a/apps/carbon/app/modules/settings/ui/useSettingsSidebar.tsx b/apps/carbon/app/modules/settings/ui/useSettingsSidebar.tsx
index 8712dfd4e9..a20131aabb 100644
--- a/apps/carbon/app/modules/settings/ui/useSettingsSidebar.tsx
+++ b/apps/carbon/app/modules/settings/ui/useSettingsSidebar.tsx
@@ -1,3 +1,4 @@
+import { CgSync } from "react-icons/cg";
import { GoNumber } from "react-icons/go";
import { TbBuildingFactory2 } from "react-icons/tb";
import { usePermissions } from "~/hooks";
@@ -19,6 +20,12 @@ const settingsRoutes: AuthenticatedRouteGroup[] = [
{
name: "System",
routes: [
+ {
+ name: "Integrations",
+ to: path.to.integrations,
+ role: "employee",
+ icon: <CgSync />,
+ },
{
name: "Sequences",
to: path.to.sequences,
diff --git a/apps/carbon/app/utils/path.ts b/apps/carbon/app/utils/path.ts
index 86f3851ed5..aa65a7e11a 100644
--- a/apps/carbon/app/utils/path.ts
+++ b/apps/carbon/app/utils/path.ts
@@ -213,6 +213,9 @@ export const path = {
groups: `${x}/users/groups`,
holiday: (id: string) => generatePath(`${x}/resources/holidays/${id}`),
holidays: `${x}/resources/holidays`,
+ integration: (name: string) =>
+ generatePath(`${x}/settings/integrations/${name}`),
+ integrations: `${x}/settings/integrations`,
inventory: `${x}/inventory`,
invoicing: `${x}/invoicing`,
jobs: `${x}/jobs`,
diff --git a/apps/carbon/package.json b/apps/carbon/package.json
index d430d5fbbc..84d3b64a70 100644
--- a/apps/carbon/package.json
+++ b/apps/carbon/package.json
@@ -56,6 +56,7 @@
"d3-array": "^3.2.4",
"framer-motion": "^10.13.1",
"isbot": "3.6.13",
+ "localforage": "^1.10.0",
"lodash.debounce": "^4.0.8",
"lodash.words": "^4.2.0",
"nanostores": "^0.9.3",
diff --git a/package-lock.json b/package-lock.json
index 7e932a4011..0a5d06bdd5 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -73,6 +73,7 @@
"d3-array": "^3.2.4",
"framer-motion": "^10.13.1",
"isbot": "3.6.13",
+ "localforage": "^1.10.0",
"lodash.debounce": "^4.0.8",
"lodash.words": "^4.2.0",
"nanostores": "^0.9.3",
@@ -15415,6 +15416,11 @@
"integrity": "sha512-Ius2VYcGNk7T90CppJqcIkS5ooHUZyIQK+ClZfMfMNFEF9VSE73Fq+906u/CWu92x4gzZMWOwfFYckPObzdEbA==",
"dev": true
},
+ "node_modules/immediate": {
+ "version": "3.0.6",
+ "resolved": "https://registry.npmjs.org/immediate/-/immediate-3.0.6.tgz",
+ "integrity": "sha512-XXOFtyqDjNDAQxVfYxuF7g9Il/IbWmmlQg2MYKOH8ExIT1qg6xc4zyS3HaEEATgs1btfzxq15ciUiY7gjSXRGQ=="
+ },
"node_modules/immer": {
"version": "9.0.21",
"resolved": "https://registry.npmjs.org/immer/-/immer-9.0.21.tgz",
@@ -17517,6 +17523,14 @@
"node": ">= 0.8.0"
}
},
+ "node_modules/lie": {
+ "version": "3.1.1",
+ "resolved": "https://registry.npmjs.org/lie/-/lie-3.1.1.tgz",
+ "integrity": "sha512-RiNhHysUjhrDQntfYSfY4MU24coXXdEOgw9WGcKHNeEwffDYbF//u87M1EWaMGzuFoSbqW0C9C6lEEhDOAswfw==",
+ "dependencies": {
+ "immediate": "~3.0.5"
+ }
+ },
"node_modules/lilconfig": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/lilconfig/-/lilconfig-2.1.0.tgz",
@@ -17645,6 +17659,14 @@
"url": "https://github.com/sponsors/antfu"
}
},
+ "node_modules/localforage": {
+ "version": "1.10.0",
+ "resolved": "https://registry.npmjs.org/localforage/-/localforage-1.10.0.tgz",
+ "integrity": "sha512-14/H1aX7hzBBmmh7sGPd+AOMkkIrHM3Z1PAyGgZigA1H1p5O5ANnMyWzvpAETtG68/dC4pC0ncy3+PPGzXZHPg==",
+ "dependencies": {
+ "lie": "3.1.1"
+ }
+ },
"node_modules/locate-path": {
"version": "6.0.0",
"resolved": "https://registry.npmjs.org/locate-path/-/locate-path-6.0.0.tgz",
diff --git a/package.json b/package.json
index 07d7da0824..1014199e10 100755
--- a/package.json
+++ b/package.json
@@ -34,7 +34,6 @@
"deploy": "turbo run deploy",
"dev": "turbo run dev --filter=./apps/* --filter=./packages/*",
"dev:apps": "turbo run dev --filter=./apps/*",
- "dev:shared": "turbo run dev --filter=./shared",
"dev:www": "turbo run dev --filter=./www",
"dev:packages": "turbo run dev --filter=./packages/*",
"format": "prettier --write \"**/*.{ts,tsx,md}\"",
diff --git a/packages/database/SCHEMA.md b/packages/database/SCHEMA.md
index ada67328b8..c009aadd2d 100644
--- a/packages/database/SCHEMA.md
+++ b/packages/database/SCHEMA.md
@@ -7404,7 +7404,7 @@ CREATE OR REPLACE VIEW "employeeSummary" WITH(SECURITY_INVOKER=true) AS
CREATE TABLE "currencyExchangeRate" (
"currency" TEXT NOT NULL,
"exchangeRate" NUMERIC NOT NULL,
- "timestamp" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
+ "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
CONSTRAINT "currencyExchangeRate_pkey" PRIMARY KEY ("currency")
);
@@ -7433,7 +7433,7 @@ CREATE TABLE integration (
"updatedBy" TEXT,
CONSTRAINT integration_pkey PRIMARY KEY ("name"),
- CHECK (
+ CONSTRAINT integration_metadata CHECK (
active = false OR
json_matches_schema(jsonschema, metadata)
),
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index 8186d793d7..a5be472f2e 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -1669,17 +1669,17 @@ export interface Database {
Row: {
currency: string;
exchangeRate: number;
- timestamp: string;
+ updatedAt: string;
};
Insert: {
currency: string;
exchangeRate: number;
- timestamp?: string;
+ updatedAt?: string;
};
Update: {
currency?: string;
exchangeRate?: number;
- timestamp?: string;
+ updatedAt?: string;
};
Relationships: [];
};
diff --git a/packages/database/supabase/migrations/20240118035549_currency-exchange-rates.sql b/packages/database/supabase/migrations/20240118035549_currency-exchange-rates.sql
index 7495eafd40..b8117fe35f 100644
--- a/packages/database/supabase/migrations/20240118035549_currency-exchange-rates.sql
+++ b/packages/database/supabase/migrations/20240118035549_currency-exchange-rates.sql
@@ -1,7 +1,7 @@
CREATE TABLE "currencyExchangeRate" (
"currency" TEXT NOT NULL,
"exchangeRate" NUMERIC NOT NULL,
- "timestamp" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
+ "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
CONSTRAINT "currencyExchangeRate_pkey" PRIMARY KEY ("currency")
);