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 custom fields for accounting module using redis

Brad Barbine395a2267037
diff --git a/apps/carbon/app/modules/accounting/accounting.service.ts b/apps/carbon/app/modules/accounting/accounting.service.ts
index 66d21281cf..291f513598 100644
--- a/apps/carbon/app/modules/accounting/accounting.service.ts
+++ b/apps/carbon/app/modules/accounting/accounting.service.ts
@@ -140,7 +140,10 @@ export async function getAccountsList(
classes?: string[];
}
) {
- let query = client.from("account").select("number, name").eq("active", true);
+ let query = client
+ .from("account")
+ .select("number, name, incomeBalance")
+ .eq("active", true);
if (args?.type) {
query = query.eq("type", args.type);
diff --git a/apps/carbon/app/modules/settings/settings.service.ts b/apps/carbon/app/modules/settings/settings.service.ts
index bad9c5d306..6988cc2a5b 100644
--- a/apps/carbon/app/modules/settings/settings.service.ts
+++ b/apps/carbon/app/modules/settings/settings.service.ts
@@ -1,4 +1,5 @@
import type { Database, Json } from "@carbon/database";
+import { redis } from "@carbon/redis";
import type { SupabaseClient } from "@supabase/supabase-js";
import type { z } from "zod";
import { SUPABASE_API_URL } from "~/config/env";
@@ -222,30 +223,41 @@ export async function upsertCustomField(
updatedBy: string;
})
) {
- if ("createdBy" in customField) {
- const sortOrders = await client
- .from("customField")
- .select("sortOrder")
- .eq("customFieldTableId", customField.customFieldTableId);
-
- if (sortOrders.error) return sortOrders;
- const maxSortOrder = sortOrders.data.reduce((max, item) => {
- return Math.max(max, item.sortOrder);
- }, 0);
-
+ try {
+ // delete all the existing cache keys that start with customFields:
+ redis.keys("customFields:*").then(function (keys) {
+ const pipeline = redis.pipeline();
+ keys.forEach(function (key) {
+ pipeline.del(key);
+ });
+ return pipeline.exec();
+ });
+ } finally {
+ if ("createdBy" in customField) {
+ const sortOrders = await client
+ .from("customField")
+ .select("sortOrder")
+ .eq("customFieldTableId", customField.customFieldTableId);
+
+ if (sortOrders.error) return sortOrders;
+ const maxSortOrder = sortOrders.data.reduce((max, item) => {
+ return Math.max(max, item.sortOrder);
+ }, 0);
+
+ return client
+ .from("customField")
+ .insert([{ ...customField, sortOrder: maxSortOrder + 1 }]);
+ }
return client
.from("customField")
- .insert([{ ...customField, sortOrder: maxSortOrder + 1 }]);
+ .update(
+ sanitize({
+ ...customField,
+ updatedBy: customField.updatedBy,
+ })
+ )
+ .eq("id", customField.id);
}
- return client
- .from("customField")
- .update(
- sanitize({
- ...customField,
- updatedBy: customField.updatedBy,
- })
- )
- .eq("id", customField.id);
}
export async function updateCustomFieldsSortOrder(
diff --git a/apps/carbon/app/modules/shared/shared.service.ts b/apps/carbon/app/modules/shared/shared.service.ts
index 2dc6e177cb..786f575d7d 100644
--- a/apps/carbon/app/modules/shared/shared.service.ts
+++ b/apps/carbon/app/modules/shared/shared.service.ts
@@ -1,5 +1,7 @@
import type { Database } from "@carbon/database";
+import { redis } from "@carbon/redis";
import type { SupabaseClient } from "@supabase/supabase-js";
+import type { CustomFieldsTableType } from "../settings";
export async function deleteNote(
client: SupabaseClient<Database>,
@@ -8,6 +10,52 @@ export async function deleteNote(
return client.from("note").update({ active: false }).eq("id", noteId);
}
+export async function getCustomFieldsCacheKey(args?: {
+ module?: string;
+ table?: string;
+}) {
+ return `customFields:${args?.module ?? ""}:${args?.table ?? ""}`;
+}
+
+export async function getCustomFieldsSchemas(
+ client: SupabaseClient<Database>,
+ args: {
+ module?: string;
+ table?: string;
+ }
+) {
+ const key = await getCustomFieldsCacheKey(args);
+ let schema: CustomFieldsTableType[] | null = null;
+
+ try {
+ schema = JSON.parse((await redis.get(key)) || "null");
+ } finally {
+ if (schema) {
+ return {
+ data: schema as CustomFieldsTableType[],
+ error: null,
+ };
+ }
+
+ const query = client.from("customFieldTables").select("*");
+
+ if (args.module) {
+ query.eq("module", args.module);
+ }
+
+ if (args.table) {
+ query.eq("table", args.table);
+ }
+
+ const result = await query;
+ if (result.data) {
+ await redis.set(key, JSON.stringify(result.data));
+ }
+
+ return result;
+ }
+}
+
export async function getNotes(
client: SupabaseClient<Database>,
documentId: string
diff --git a/apps/carbon/app/routes/x+/accounting+/_layout.tsx b/apps/carbon/app/routes/x+/accounting+/_layout.tsx
index 5a0449a97f..9588d58cfb 100644
--- a/apps/carbon/app/routes/x+/accounting+/_layout.tsx
+++ b/apps/carbon/app/routes/x+/accounting+/_layout.tsx
@@ -3,11 +3,13 @@ import type { LoaderFunctionArgs, MetaFunction } from "@remix-run/node";
import { json, redirect } from "@remix-run/node";
import { Outlet } from "@remix-run/react";
import { GroupedContentSidebar } from "~/components/Layout";
+import logger from "~/lib/logger";
import {
getAccountsList,
getBaseCurrency,
useAccountingSubmodules,
} from "~/modules/accounting";
+import { getCustomFieldsSchemas } from "~/modules/shared";
import { requirePermissions } from "~/services/auth";
import { flash } from "~/services/session.server";
import type { Handle } from "~/utils/handle";
@@ -29,49 +31,35 @@ export async function loader({ request }: LoaderFunctionArgs) {
view: "accounting",
});
- const [baseCurrency, balanceSheetAccounts, incomeStatementAccounts] =
- await Promise.all([
- getBaseCurrency(client),
- getAccountsList(client, {
- type: "Posting",
- incomeBalance: "Balance Sheet",
- }),
- getAccountsList(client, {
- type: "Posting",
- incomeBalance: "Income Statement",
- }),
- ]);
+ // the ABCs of accounting
+ const [accounts, baseCurrency, customFields] = await Promise.all([
+ getAccountsList(client, {
+ type: "Posting",
+ }),
+ getBaseCurrency(client),
+ getCustomFieldsSchemas(client, {
+ module: "Accounting",
+ }),
+ ]);
- if (balanceSheetAccounts.error) {
+ if (accounts.error) {
return redirect(
path.to.authenticatedRoot,
- await flash(
- request,
- error(
- balanceSheetAccounts.error,
- "Failed to fetch balance sheet accounts"
- )
- )
+ await flash(request, error(accounts.error, "Failed to fetch accounts"))
);
}
- if (incomeStatementAccounts.error) {
- return redirect(
- path.to.authenticatedRoot,
- await flash(
- request,
- error(
- incomeStatementAccounts.error,
- "Failed to fetch income statement accounts"
- )
- )
- );
+ if (customFields.error) {
+ logger.error(customFields.error);
}
return json({
baseCurrency: baseCurrency.data,
- balanceSheetAccounts: balanceSheetAccounts.data ?? [],
- incomeStatementAccounts: incomeStatementAccounts.data ?? [],
+ balanceSheetAccounts:
+ accounts.data.filter((a) => a.incomeBalance === "Balance Sheet") ?? [],
+ incomeStatementAccounts:
+ accounts.data.filter((a) => a.incomeBalance === "Income Statement") ?? [],
+ customFields: customFields.data ?? [],
});
}
diff --git a/package-lock.json b/package-lock.json
index b5ab4d5345..c510bf42c2 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -57,7 +57,7 @@
"@trigger.dev/remix": "^2.3.16",
"@trigger.dev/sdk": "^2.3.16",
"@vercel/analytics": "^1.0.2",
- "@vercel/remix": "2.5.1",
+ "@vercel/remix": "2.8.1",
"@visx/axis": "3.3.0",
"@visx/curve": "3.3.0",
"@visx/drag": "3.3.0",
@@ -142,6 +142,26 @@
}
}
},
+ "apps/carbon/node_modules/@vercel/remix": {
+ "version": "2.8.1",
+ "resolved": "https://registry.npmjs.org/@vercel/remix/-/remix-2.8.1.tgz",
+ "integrity": "sha512-7UGVAxP5mJsJFcLp0ERr8whWfX3tK9gx9zu8TqHH3ICR2OixUYXXg2XLD0M7U9dUHH43aGYr5EVefKcYbuGX1Q==",
+ "dependencies": {
+ "@vercel/static-config": "3.0.0",
+ "isbot": "^3.6.8",
+ "ts-morph": "12.0.0"
+ },
+ "engines": {
+ "node": ">=18.0.0"
+ },
+ "peerDependencies": {
+ "@remix-run/dev": "2.8.1",
+ "@remix-run/node": "2.8.1",
+ "@remix-run/server-runtime": "2.8.1",
+ "react": "*",
+ "react-dom": "*"
+ }
+ },
"node_modules/@aashutoshrathi/word-wrap": {
"version": "1.2.6",
"resolved": "https://registry.npmjs.org/@aashutoshrathi/word-wrap/-/word-wrap-1.2.6.tgz",
@@ -6835,6 +6855,7 @@
"version": "2.5.1",
"resolved": "https://registry.npmjs.org/@remix-run/node/-/node-2.5.1.tgz",
"integrity": "sha512-UI442xzHAiokmsfrYOabMQB024+IizmRhZBGcNa42QjJWsNqogy1bNwYhzEpB6oQEB1wF3vwOKK1AD1/iYA/9A==",
+ "peer": true,
"dependencies": {
"@remix-run/server-runtime": "2.5.1",
"@remix-run/web-fetch": "^4.4.2",
@@ -6861,6 +6882,7 @@
"version": "2.5.1",
"resolved": "https://registry.npmjs.org/@remix-run/server-runtime/-/server-runtime-2.5.1.tgz",
"integrity": "sha512-bP31jrVbYTJ2eP5sxZfDgT1YyXzDlzsfMxGYVzpaoLCYDJAekq1QpHLLXKGOXhmyb46O9rdhlQKfwD6WpAxr3A==",
+ "peer": true,
"dependencies": {
"@remix-run/router": "1.14.2",
"@types/cookie": "^0.6.0",
@@ -6885,6 +6907,7 @@
"version": "0.7.4",
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.7.4.tgz",
"integrity": "sha512-l3BikUxvPOcn5E74dZiq5BGsTb5yEwhaTSzccU6t4sDOH8NWJCstKO5QT2CvtFoK6F0saL7p9xHAqHOlCPJygA==",
+ "peer": true,
"engines": {
"node": ">= 8"
}
@@ -6925,6 +6948,7 @@
"version": "1.14.2",
"resolved": "https://registry.npmjs.org/@remix-run/router/-/router-1.14.2.tgz",
"integrity": "sha512-ACXpdMM9hmKZww21yEqWwiLws/UPLhNKvimN8RrYSqPSvB3ov7sLvAcfvaxePeLvccTQKGdkDIhLYApZVDFuKg==",
+ "peer": true,
"engines": {
"node": ">=14.0.0"
}
@@ -8485,6 +8509,17 @@
"url": "https://github.com/sponsors/colinhacks"
}
},
+ "node_modules/@ts-morph/common": {
+ "version": "0.11.1",
+ "resolved": "https://registry.npmjs.org/@ts-morph/common/-/common-0.11.1.tgz",
+ "integrity": "sha512-7hWZS0NRpEsNV8vWJzg7FEz6V8MaLNeJOmwmghqUXTpzk16V1LLZhdo+4QvE/+zv4cVci0OviuJFnqhEfoV3+g==",
+ "dependencies": {
+ "fast-glob": "^3.2.7",
+ "minimatch": "^3.0.4",
+ "mkdirp": "^1.0.4",
+ "path-browserify": "^1.0.1"
+ }
+ },
"node_modules/@tufjs/canonical-json": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/@tufjs/canonical-json/-/canonical-json-1.0.0.tgz",
@@ -9531,54 +9566,35 @@
}
}
},
- "node_modules/@vercel/remix": {
- "version": "2.5.1",
- "resolved": "https://registry.npmjs.org/@vercel/remix/-/remix-2.5.1.tgz",
- "integrity": "sha512-b38Av3htXlF5L/Gy0vufWtSPLQOCKTYGLANWIrpLnB2m3Ro8Q56SgkBE1iMqVnHQ6A32fyAXg9y0BrVRRLhy6A==",
+ "node_modules/@vercel/static-config": {
+ "version": "3.0.0",
+ "resolved": "https://registry.npmjs.org/@vercel/static-config/-/static-config-3.0.0.tgz",
+ "integrity": "sha512-2qtvcBJ1bGY0dYGYh3iM7yGKkk971FujLEDXzuW5wcZsPr1GSEjO/w2iSr3qve6nDDtBImsGoDEnus5FI4+fIw==",
"dependencies": {
- "@remix-run/node": "2.5.1",
- "@remix-run/server-runtime": "2.5.1",
- "isbot": "^3.6.8"
- },
- "engines": {
- "node": ">=18.0.0"
- },
- "peerDependencies": {
- "react": "*",
- "react-dom": "*"
+ "ajv": "8.6.3",
+ "json-schema-to-ts": "1.6.4",
+ "ts-morph": "12.0.0"
}
},
- "node_modules/@vercel/remix/node_modules/@remix-run/server-runtime": {
- "version": "2.5.1",
- "resolved": "https://registry.npmjs.org/@remix-run/server-runtime/-/server-runtime-2.5.1.tgz",
- "integrity": "sha512-bP31jrVbYTJ2eP5sxZfDgT1YyXzDlzsfMxGYVzpaoLCYDJAekq1QpHLLXKGOXhmyb46O9rdhlQKfwD6WpAxr3A==",
+ "node_modules/@vercel/static-config/node_modules/ajv": {
+ "version": "8.6.3",
+ "resolved": "https://registry.npmjs.org/ajv/-/ajv-8.6.3.tgz",
+ "integrity": "sha512-SMJOdDP6LqTkD0Uq8qLi+gMwSt0imXLSV080qFVwJCpH9U6Mb+SUGHAXM0KNbcBPguytWyvFxcHgMLe2D2XSpw==",
"dependencies": {
- "@remix-run/router": "1.14.2",
- "@types/cookie": "^0.6.0",
- "@web3-storage/multipart-parser": "^1.0.0",
- "cookie": "^0.6.0",
- "set-cookie-parser": "^2.4.8",
- "source-map": "^0.7.3"
- },
- "engines": {
- "node": ">=18.0.0"
- },
- "peerDependencies": {
- "typescript": "^5.1.0"
+ "fast-deep-equal": "^3.1.1",
+ "json-schema-traverse": "^1.0.0",
+ "require-from-string": "^2.0.2",
+ "uri-js": "^4.2.2"
},
- "peerDependenciesMeta": {
- "typescript": {
- "optional": true
- }
+ "funding": {
+ "type": "github",
+ "url": "https://github.com/sponsors/epoberezkin"
}
},
- "node_modules/@vercel/remix/node_modules/source-map": {
- "version": "0.7.4",
- "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.7.4.tgz",
- "integrity": "sha512-l3BikUxvPOcn5E74dZiq5BGsTb5yEwhaTSzccU6t4sDOH8NWJCstKO5QT2CvtFoK6F0saL7p9xHAqHOlCPJygA==",
- "engines": {
- "node": ">= 8"
- }
+ "node_modules/@vercel/static-config/node_modules/json-schema-traverse": {
+ "version": "1.0.0",
+ "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz",
+ "integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug=="
},
"node_modules/@visx/axis": {
"version": "3.3.0",
@@ -12008,6 +12024,11 @@
"node": ">= 0.12.0"
}
},
+ "node_modules/code-block-writer": {
+ "version": "10.1.1",
+ "resolved": "https://registry.npmjs.org/code-block-writer/-/code-block-writer-10.1.1.tgz",
+ "integrity": "sha512-67ueh2IRGst/51p0n6FvPrnRjAGHY5F8xdjkgrYE7DDzpJe6qA07RYQ9VcoUeo5ATOjSOiWpSL3SWBRRbempMw=="
+ },
"node_modules/collect-v8-coverage": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/collect-v8-coverage/-/collect-v8-coverage-1.0.2.tgz",
@@ -19170,6 +19191,20 @@
"jju": "^1.1.0"
}
},
+ "node_modules/json-schema-to-ts": {
+ "version": "1.6.4",
+ "resolved": "https://registry.npmjs.org/json-schema-to-ts/-/json-schema-to-ts-1.6.4.tgz",
+ "integrity": "sha512-pR4yQ9DHz6itqswtHCm26mw45FSNfQ9rEQjosaZErhn5J3J2sIViQiz8rDaezjKAhFGpmsoczYVBgGHzFw/stA==",
+ "dependencies": {
+ "@types/json-schema": "^7.0.6",
+ "ts-toolbelt": "^6.15.5"
+ }
+ },
+ "node_modules/json-schema-to-ts/node_modules/ts-toolbelt": {
+ "version": "6.15.5",
+ "resolved": "https://registry.npmjs.org/ts-toolbelt/-/ts-toolbelt-6.15.5.tgz",
+ "integrity": "sha512-FZIXf1ksVyLcfr7M317jbB67XFJhOO1YqdTcuGaq9q5jLUoTikukZ+98TPjKiP2jC5CgmYdWWYs0s2nLSU0/1A=="
+ },
"node_modules/json-schema-traverse": {
"version": "0.4.1",
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz",
@@ -24350,6 +24385,11 @@
"node": ">=0.10.0"
}
},
+ "node_modules/path-browserify": {
+ "version": "1.0.1",
+ "resolved": "https://registry.npmjs.org/path-browserify/-/path-browserify-1.0.1.tgz",
+ "integrity": "sha512-b7uo2UCUOYZcnF/3ID0lulOJi/bafxa1xPe7ZPsammBSpjSWQkjNxlt635YGS2MiR9GjvuXCtz2emr3jbsz98g=="
+ },
"node_modules/path-exists": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz",
@@ -29109,6 +29149,15 @@
"resolved": "https://registry.npmjs.org/ts-interface-checker/-/ts-interface-checker-0.1.13.tgz",
"integrity": "sha512-Y/arvbn+rrz3JCKl9C4kVNfTfSm2/mEp5FSz5EsZSANGPSlQrpRI5M4PKF+mJnE52jOO90PnPSc3Ur3bTQw0gA=="
},
+ "node_modules/ts-morph": {
+ "version": "12.0.0",
+ "resolved": "https://registry.npmjs.org/ts-morph/-/ts-morph-12.0.0.tgz",
+ "integrity": "sha512-VHC8XgU2fFW7yO1f/b3mxKDje1vmyzFXHWzOYmKEkCEwcLjDtbdLgBQviqj4ZwP4MJkQtRo6Ha2I29lq/B+VxA==",
+ "dependencies": {
+ "@ts-morph/common": "~0.11.0",
+ "code-block-writer": "^10.1.1"
+ }
+ },
"node_modules/ts-toolbelt": {
"version": "9.6.0",
"resolved": "https://registry.npmjs.org/ts-toolbelt/-/ts-toolbelt-9.6.0.tgz",
@@ -32436,6 +32485,7 @@
"@carbon/jest": "*",
"@carbon/tailwind": "*",
"@carbon/tsconfig": "*",
+ "@carbon/utils": "*",
"@testing-library/dom": "^8.11.1",
"@types/dompurify": "^2.4.0",
"@types/jest": "^26.0.22",
diff --git a/packages/react/package.json b/packages/react/package.json
index e0a28d26bf..78d1b64f62 100644
--- a/packages/react/package.json
+++ b/packages/react/package.json
@@ -52,6 +52,7 @@
"@carbon/jest": "*",
"@carbon/tailwind": "*",
"@carbon/tsconfig": "*",
+ "@carbon/utils": "*",
"@testing-library/dom": "^8.11.1",
"@types/dompurify": "^2.4.0",
"@types/jest": "^26.0.22",