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 handler for dependencies

Brad Barbindbe7520621b4
diff --git a/apps/erp/app/routes/x+/job+/$jobId.status.tsx b/apps/erp/app/routes/x+/job+/$jobId.status.tsx
index 2bdfb9cba7..df49574a54 100644
--- a/apps/erp/app/routes/x+/job+/$jobId.status.tsx
+++ b/apps/erp/app/routes/x+/job+/$jobId.status.tsx
@@ -61,6 +61,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
const [scheduler] = await Promise.all([
serviceRole.functions.invoke("scheduler", {
body: {
+ type: "schedule",
jobId: id,
companyId,
userId,
diff --git a/packages/database/supabase/functions/lib/scheduling/scheduling-engine.ts b/packages/database/supabase/functions/lib/scheduling/scheduling-engine.ts
index afaf27a106..a31eb819bb 100644
--- a/packages/database/supabase/functions/lib/scheduling/scheduling-engine.ts
+++ b/packages/database/supabase/functions/lib/scheduling/scheduling-engine.ts
@@ -52,6 +52,13 @@ class SchedulingEngine {
]);
}
+ async addDependencies() {
+ console.log({
+ operationsToSchedule: this.operationsToSchedule,
+ operationsByJobMakeMethodId: this.operationsByJobMakeMethodId,
+ });
+ }
+
async assign() {
await this.materialManager.assignOperationsToMaterials(
this.validMaterialIds,
diff --git a/packages/database/supabase/functions/scheduler/index.ts b/packages/database/supabase/functions/scheduler/index.ts
index 06cfc7db79..4a4c5133c3 100644
--- a/packages/database/supabase/functions/scheduler/index.ts
+++ b/packages/database/supabase/functions/scheduler/index.ts
@@ -10,12 +10,20 @@ import { getSupabaseServiceRole } from "../lib/supabase.ts";
const pool = getConnectionPool(1);
const db = getDatabaseClient<DB>(pool);
-const payloadValidator = z.object({
- jobId: z.string(),
- companyId: z.string(),
- userId: z.string(),
-});
-
+const payloadValidator = z.discriminatedUnion("type", [
+ z.object({
+ type: z.literal("schedule"),
+ jobId: z.string(),
+ companyId: z.string(),
+ userId: z.string(),
+ }),
+ z.object({
+ type: z.literal("dependencies"),
+ jobId: z.string(),
+ companyId: z.string(),
+ userId: z.string(),
+ }),
+]);
serve(async (req: Request) => {
if (req.method === "OPTIONS") {
return new Response("ok", { headers: corsHeaders });
@@ -23,14 +31,8 @@ serve(async (req: Request) => {
const payload = await req.json();
try {
- const { jobId, companyId, userId } = payloadValidator.parse(payload);
-
- console.log({
- function: "scheduler",
- jobId,
- companyId,
- userId,
- });
+ const validatedPayload = payloadValidator.parse(payload);
+ const { type, companyId, userId, jobId } = validatedPayload;
const client = await getSupabaseServiceRole(
req.headers.get("Authorization"),
@@ -38,23 +40,55 @@ serve(async (req: Request) => {
companyId
);
- const engine = new SchedulingEngine({ client, db, jobId, companyId });
+ switch (validatedPayload.type) {
+ case "schedule": {
+ console.log({
+ function: "scheduler",
+ type,
+ jobId,
+ companyId,
+ userId,
+ });
+ const engine = new SchedulingEngine({ client, db, jobId, companyId });
+
+ await engine.addDependencies(); // TODO: Remove this
- await engine.initialize();
- await Promise.all([
- engine.prioritize(SchedulingStrategy.LeastTime),
- engine.assign(),
- ]);
+ await engine.initialize();
+ await Promise.all([
+ engine.prioritize(SchedulingStrategy.LeastTime),
+ engine.assign(),
+ ]);
- return new Response(
- JSON.stringify({
- success: true,
- }),
- {
- headers: { ...corsHeaders, "Content-Type": "application/json" },
- status: 200,
+ return new Response(
+ JSON.stringify({
+ success: true,
+ }),
+ {
+ headers: { ...corsHeaders, "Content-Type": "application/json" },
+ status: 200,
+ }
+ );
}
- );
+ case "dependencies": {
+ const engine = new SchedulingEngine({ client, db, jobId, companyId });
+
+ await engine.initialize();
+ await engine.addDependencies();
+
+ return new Response(
+ JSON.stringify({
+ success: true,
+ }),
+ {
+ headers: { ...corsHeaders, "Content-Type": "application/json" },
+ status: 200,
+ }
+ );
+ }
+ default: {
+ throw new Error(`Unsupported operation type: ${type}`);
+ }
+ }
} catch (err) {
console.error(err);
return new Response(JSON.stringify(err), {
diff --git a/packages/database/supabase/migrations/20250429130223_operation-dependencies.sql b/packages/database/supabase/migrations/20250429130223_operation-dependencies.sql
new file mode 100644
index 0000000000..dfc130af03
--- /dev/null
+++ b/packages/database/supabase/migrations/20250429130223_operation-dependencies.sql
@@ -0,0 +1,2 @@
+ALTER TABLE "jobOperation" ADD COLUMN "dependencies" TEXT[] DEFAULT '{}';
+