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: add edge function for evaluating code

Brad Barbinee5f4b4f35ff
diff --git a/packages/database/src/swagger-docs-schema.ts b/packages/database/src/swagger-docs-schema.ts
index 3e08ecb9b2..e80d0fe11f 100644
--- a/packages/database/src/swagger-docs-schema.ts
+++ b/packages/database/src/swagger-docs-schema.ts
@@ -11061,6 +11061,150 @@ export default {
tags: ["salesOrderLine"],
},
},
+ "/configurationRule": {
+ get: {
+ parameters: [
+ {
+ $ref: "#/parameters/rowFilter.configurationRule.itemId",
+ },
+ {
+ $ref: "#/parameters/rowFilter.configurationRule.field",
+ },
+ {
+ $ref: "#/parameters/rowFilter.configurationRule.code",
+ },
+ {
+ $ref: "#/parameters/rowFilter.configurationRule.companyId",
+ },
+ {
+ $ref: "#/parameters/rowFilter.configurationRule.updatedAt",
+ },
+ {
+ $ref: "#/parameters/rowFilter.configurationRule.updatedBy",
+ },
+ {
+ $ref: "#/parameters/select",
+ },
+ {
+ $ref: "#/parameters/order",
+ },
+ {
+ $ref: "#/parameters/range",
+ },
+ {
+ $ref: "#/parameters/rangeUnit",
+ },
+ {
+ $ref: "#/parameters/offset",
+ },
+ {
+ $ref: "#/parameters/limit",
+ },
+ {
+ $ref: "#/parameters/preferCount",
+ },
+ ],
+ responses: {
+ "200": {
+ description: "OK",
+ schema: {
+ items: {
+ $ref: "#/definitions/configurationRule",
+ },
+ type: "array",
+ },
+ },
+ "206": {
+ description: "Partial Content",
+ },
+ },
+ tags: ["configurationRule"],
+ },
+ post: {
+ parameters: [
+ {
+ $ref: "#/parameters/body.configurationRule",
+ },
+ {
+ $ref: "#/parameters/select",
+ },
+ {
+ $ref: "#/parameters/preferPost",
+ },
+ ],
+ responses: {
+ "201": {
+ description: "Created",
+ },
+ },
+ tags: ["configurationRule"],
+ },
+ delete: {
+ parameters: [
+ {
+ $ref: "#/parameters/rowFilter.configurationRule.itemId",
+ },
+ {
+ $ref: "#/parameters/rowFilter.configurationRule.field",
+ },
+ {
+ $ref: "#/parameters/rowFilter.configurationRule.code",
+ },
+ {
+ $ref: "#/parameters/rowFilter.configurationRule.companyId",
+ },
+ {
+ $ref: "#/parameters/rowFilter.configurationRule.updatedAt",
+ },
+ {
+ $ref: "#/parameters/rowFilter.configurationRule.updatedBy",
+ },
+ {
+ $ref: "#/parameters/preferReturn",
+ },
+ ],
+ responses: {
+ "204": {
+ description: "No Content",
+ },
+ },
+ tags: ["configurationRule"],
+ },
+ patch: {
+ parameters: [
+ {
+ $ref: "#/parameters/rowFilter.configurationRule.itemId",
+ },
+ {
+ $ref: "#/parameters/rowFilter.configurationRule.field",
+ },
+ {
+ $ref: "#/parameters/rowFilter.configurationRule.code",
+ },
+ {
+ $ref: "#/parameters/rowFilter.configurationRule.companyId",
+ },
+ {
+ $ref: "#/parameters/rowFilter.configurationRule.updatedAt",
+ },
+ {
+ $ref: "#/parameters/rowFilter.configurationRule.updatedBy",
+ },
+ {
+ $ref: "#/parameters/body.configurationRule",
+ },
+ {
+ $ref: "#/parameters/preferReturn",
+ },
+ ],
+ responses: {
+ "204": {
+ description: "No Content",
+ },
+ },
+ tags: ["configurationRule"],
+ },
+ },
"/salesOrderTransaction": {
get: {
parameters: [
@@ -14433,6 +14577,9 @@ export default {
{
$ref: "#/parameters/rowFilter.job.tags",
},
+ {
+ $ref: "#/parameters/rowFilter.job.configuration",
+ },
{
$ref: "#/parameters/select",
},
@@ -14579,6 +14726,9 @@ export default {
{
$ref: "#/parameters/rowFilter.job.tags",
},
+ {
+ $ref: "#/parameters/rowFilter.job.configuration",
+ },
{
$ref: "#/parameters/preferReturn",
},
@@ -14679,6 +14829,9 @@ export default {
{
$ref: "#/parameters/rowFilter.job.tags",
},
+ {
+ $ref: "#/parameters/rowFilter.job.configuration",
+ },
{
$ref: "#/parameters/body.job",
},
@@ -15495,6 +15648,9 @@ export default {
{
$ref: "#/parameters/rowFilter.quoteLines.externalNotes",
},
+ {
+ $ref: "#/parameters/rowFilter.quoteLines.configuration",
+ },
{
$ref: "#/parameters/rowFilter.quoteLines.thumbnailPath",
},
@@ -22452,6 +22608,9 @@ export default {
{
$ref: "#/parameters/rowFilter.jobs.tags",
},
+ {
+ $ref: "#/parameters/rowFilter.jobs.configuration",
+ },
{
$ref: "#/parameters/rowFilter.jobs.name",
},
@@ -35538,6 +35697,9 @@ export default {
{
$ref: "#/parameters/rowFilter.quoteLine.externalNotes",
},
+ {
+ $ref: "#/parameters/rowFilter.quoteLine.configuration",
+ },
{
$ref: "#/parameters/select",
},
@@ -35681,6 +35843,9 @@ export default {
{
$ref: "#/parameters/rowFilter.quoteLine.externalNotes",
},
+ {
+ $ref: "#/parameters/rowFilter.quoteLine.configuration",
+ },
{
$ref: "#/parameters/preferReturn",
},
@@ -35778,6 +35943,9 @@ export default {
{
$ref: "#/parameters/rowFilter.quoteLine.externalNotes",
},
+ {
+ $ref: "#/parameters/rowFilter.quoteLine.configuration",
+ },
{
$ref: "#/parameters/body.quoteLine",
},
@@ -46265,6 +46433,44 @@ export default {
},
type: "object",
},
+ configurationRule: {
+ required: ["itemId", "field", "code", "companyId", "updatedAt"],
+ properties: {
+ itemId: {
+ description:
+ "Note:\nThis is a Primary Key.<pk/>\nThis is a Foreign Key to `item.id`.<fk table='item' column='id'/>",
+ format: "text",
+ type: "string",
+ },
+ field: {
+ description: "Note:\nThis is a Primary Key.<pk/>",
+ format: "text",
+ type: "string",
+ },
+ code: {
+ format: "text",
+ type: "string",
+ },
+ companyId: {
+ description:
+ "Note:\nThis is a Foreign Key to `company.id`.<fk table='company' column='id'/>",
+ format: "text",
+ type: "string",
+ },
+ updatedAt: {
+ default: "now()",
+ format: "timestamp with time zone",
+ type: "string",
+ },
+ updatedBy: {
+ description:
+ "Note:\nThis is a Foreign Key to `user.id`.<fk table='user' column='id'/>",
+ format: "text",
+ type: "string",
+ },
+ },
+ type: "object",
+ },
salesOrderTransaction: {
required: ["id", "salesOrderId", "type", "userId", "createdAt"],
properties: {
@@ -47999,6 +48205,9 @@ export default {
},
type: "array",
},
+ configuration: {
+ format: "jsonb",
+ },
},
type: "object",
},
@@ -48539,6 +48748,9 @@ export default {
externalNotes: {
format: "jsonb",
},
+ configuration: {
+ format: "jsonb",
+ },
thumbnailPath: {
format: "text",
type: "string",
@@ -51648,6 +51860,9 @@ export default {
},
type: "array",
},
+ configuration: {
+ format: "jsonb",
+ },
name: {
format: "text",
type: "string",
@@ -54230,7 +54445,7 @@ export default {
properties: {
id: {
description:
- "Note:\nThis is a Foreign Key to `supplierLocation.id`.<fk table='supplierLocation' column='id'/>",
+ "Note:\nThis is a Primary Key.<pk/>\nThis is a Foreign Key to `supplierLocation.id`.<fk table='supplierLocation' column='id'/>",
format: "text",
type: "string",
},
@@ -54279,7 +54494,7 @@ export default {
},
supplierLocationId: {
description:
- "Note:\nThis is a Primary Key.<pk/>\nThis is a Foreign Key to `supplierLocation.id`.<fk table='supplierLocation' column='id'/>",
+ "Note:\nThis is a Foreign Key to `supplierLocation.id`.<fk table='supplierLocation' column='id'/>",
format: "text",
type: "string",
},
@@ -57706,6 +57921,9 @@ export default {
externalNotes: {
format: "jsonb",
},
+ configuration: {
+ format: "jsonb",
+ },
},
type: "object",
},
@@ -65730,6 +65948,57 @@ export default {
in: "query",
type: "string",
},
+ "body.configurationRule": {
+ name: "configurationRule",
+ description: "configurationRule",
+ required: false,
+ in: "body",
+ schema: {
+ $ref: "#/definitions/configurationRule",
+ },
+ },
+ "rowFilter.configurationRule.itemId": {
+ name: "itemId",
+ required: false,
+ format: "text",
+ in: "query",
+ type: "string",
+ },
+ "rowFilter.configurationRule.field": {
+ name: "field",
+ required: false,
+ format: "text",
+ in: "query",
+ type: "string",
+ },
+ "rowFilter.configurationRule.code": {
+ name: "code",
+ required: false,
+ format: "text",
+ in: "query",
+ type: "string",
+ },
+ "rowFilter.configurationRule.companyId": {
+ name: "companyId",
+ required: false,
+ format: "text",
+ in: "query",
+ type: "string",
+ },
+ "rowFilter.configurationRule.updatedAt": {
+ name: "updatedAt",
+ required: false,
+ format: "timestamp with time zone",
+ in: "query",
+ type: "string",
+ },
+ "rowFilter.configurationRule.updatedBy": {
+ name: "updatedBy",
+ required: false,
+ format: "text",
+ in: "query",
+ type: "string",
+ },
"body.salesOrderTransaction": {
name: "salesOrderTransaction",
description: "salesOrderTransaction",
@@ -67980,6 +68249,13 @@ export default {
in: "query",
type: "string",
},
+ "rowFilter.job.configuration": {
+ name: "configuration",
+ required: false,
+ format: "jsonb",
+ in: "query",
+ type: "string",
+ },
"body.purchaseOrderFavorite": {
name: "purchaseOrderFavorite",
description: "purchaseOrderFavorite",
@@ -68813,6 +69089,13 @@ export default {
in: "query",
type: "string",
},
+ "rowFilter.quoteLines.configuration": {
+ name: "configuration",
+ required: false,
+ format: "jsonb",
+ in: "query",
+ type: "string",
+ },
"rowFilter.quoteLines.thumbnailPath": {
name: "thumbnailPath",
required: false,
@@ -72798,6 +73081,13 @@ export default {
in: "query",
type: "string",
},
+ "rowFilter.jobs.configuration": {
+ name: "configuration",
+ required: false,
+ format: "jsonb",
+ in: "query",
+ type: "string",
+ },
"rowFilter.jobs.name": {
name: "name",
required: false,
@@ -80738,6 +81028,13 @@ export default {
in: "query",
type: "string",
},
+ "rowFilter.quoteLine.configuration": {
+ name: "configuration",
+ required: false,
+ format: "jsonb",
+ in: "query",
+ type: "string",
+ },
"body.jobMaterialWithMakeMethodId": {
name: "jobMaterialWithMakeMethodId",
description: "jobMaterialWithMakeMethodId",
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index daf3360d9a..c1a9f12f7d 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -27109,14 +27109,14 @@ export type Database = {
},
{
foreignKeyName: "partner_id_fkey"
- columns: ["id"]
+ columns: ["supplierLocationId"]
isOneToOne: false
referencedRelation: "supplierLocation"
referencedColumns: ["id"]
},
{
foreignKeyName: "partner_id_fkey"
- columns: ["supplierLocationId"]
+ columns: ["id"]
isOneToOne: false
referencedRelation: "supplierLocation"
referencedColumns: ["id"]
@@ -28322,14 +28322,14 @@ export type Database = {
Relationships: [
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["supplierCountryCode"]
+ columns: ["customerCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
},
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["customerCountryCode"]
+ columns: ["supplierCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
@@ -29933,14 +29933,14 @@ export type Database = {
Relationships: [
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["customerCountryCode"]
+ columns: ["paymentCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
},
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["paymentCountryCode"]
+ columns: ["customerCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
@@ -32509,7 +32509,6 @@ export type Database = {
Enums: {
accountingPeriodStatus: "Inactive" | "Active"
configurationParameterDataType: "text" | "numeric" | "boolean" | "list"
- configurationSourceDocument: "Quote Line" | "Job"
costLedgerType:
| "Direct Cost"
| "Revaluation"
diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.ts
index daf3360d9a..c1a9f12f7d 100644
--- a/packages/database/supabase/functions/lib/types.ts
+++ b/packages/database/supabase/functions/lib/types.ts
@@ -27109,14 +27109,14 @@ export type Database = {
},
{
foreignKeyName: "partner_id_fkey"
- columns: ["id"]
+ columns: ["supplierLocationId"]
isOneToOne: false
referencedRelation: "supplierLocation"
referencedColumns: ["id"]
},
{
foreignKeyName: "partner_id_fkey"
- columns: ["supplierLocationId"]
+ columns: ["id"]
isOneToOne: false
referencedRelation: "supplierLocation"
referencedColumns: ["id"]
@@ -28322,14 +28322,14 @@ export type Database = {
Relationships: [
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["supplierCountryCode"]
+ columns: ["customerCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
},
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["customerCountryCode"]
+ columns: ["supplierCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
@@ -29933,14 +29933,14 @@ export type Database = {
Relationships: [
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["customerCountryCode"]
+ columns: ["paymentCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
},
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["paymentCountryCode"]
+ columns: ["customerCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
@@ -32509,7 +32509,6 @@ export type Database = {
Enums: {
accountingPeriodStatus: "Inactive" | "Active"
configurationParameterDataType: "text" | "numeric" | "boolean" | "list"
- configurationSourceDocument: "Quote Line" | "Job"
costLedgerType:
| "Direct Cost"
| "Revaluation"
diff --git a/packages/database/supabase/functions/sandbox/index.ts b/packages/database/supabase/functions/sandbox/index.ts
new file mode 100644
index 0000000000..683c50e3bf
--- /dev/null
+++ b/packages/database/supabase/functions/sandbox/index.ts
@@ -0,0 +1,61 @@
+import { serve } from "https://deno.land/[email protected]/http/server.ts";
+import { transpile } from "https://deno.land/x/[email protected]/mod.ts";
+import { z } from "https://deno.land/x/[email protected]/mod.ts";
+import { corsHeaders } from "../lib/headers.ts";
+
+const payloadValidator = z.object({
+ code: z.string(),
+ parameters: z.record(z.unknown()),
+});
+
+async function ts(code: string) {
+ try {
+ // Transpile TypeScript to JavaScript
+ const jsCode = await transpile(
+ `export function configure(params: Params) {${code}}`
+ );
+
+ return await import(`data:application/typescript;base64,${btoa(jsCode)}`);
+ } catch (err) {
+ console.error("Transpilation error:", err);
+ throw new Error(`Failed to transpile TypeScript: ${err.message}`);
+ }
+}
+
+serve(async (req: Request) => {
+ if (req.method === "OPTIONS") {
+ return new Response("ok", { headers: corsHeaders });
+ }
+
+ try {
+ const payload = await req.json();
+ const { code, parameters } = payloadValidator.parse(payload);
+
+ console.log({
+ function: "sandbox",
+ code,
+ parameters,
+ });
+
+ let result;
+ try {
+ const mod = await ts(code);
+ console.log({ mod });
+ result = await mod.configure(parameters);
+ console.log({ result });
+ } catch (err) {
+ console.error("Execution error:", err);
+ throw new Error(`Failed to execute code: ${err.message}`);
+ }
+
+ return new Response(JSON.stringify({ result }), {
+ headers: { ...corsHeaders, "Content-Type": "application/json" },
+ status: 200,
+ });
+ } catch (err) {
+ return new Response(JSON.stringify({ error: err.message }), {
+ headers: { ...corsHeaders, "Content-Type": "application/json" },
+ status: 500,
+ });
+ }
+});