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 dependencies to operations

Brad Barbinbb5094d75f3d
diff --git a/apps/erp/app/routes/x+/job+/methods+/get.tsx b/apps/erp/app/routes/x+/job+/methods+/get.tsx
index 45c7317815..507b1aabd1 100644
--- a/apps/erp/app/routes/x+/job+/methods+/get.tsx
+++ b/apps/erp/app/routes/x+/job+/methods+/get.tsx
@@ -63,7 +63,7 @@ export async function action({ request }: ActionFunctionArgs) {
if (makeMethod.error) {
return json({
error: makeMethod.error
- ? "Failed to insert job material make method"
+ ? "Failed to update method from job method"
: null,
});
}
diff --git a/packages/database/supabase/functions/lib/scheduling/scheduling-engine.ts b/packages/database/supabase/functions/lib/scheduling/scheduling-engine.ts
index a31eb819bb..e2083aeadc 100644
--- a/packages/database/supabase/functions/lib/scheduling/scheduling-engine.ts
+++ b/packages/database/supabase/functions/lib/scheduling/scheduling-engine.ts
@@ -52,11 +52,114 @@ class SchedulingEngine {
]);
}
- async addDependencies() {
- console.log({
- operationsToSchedule: this.operationsToSchedule,
- operationsByJobMakeMethodId: this.operationsByJobMakeMethodId,
- });
+ async addDependencies(): Promise<void> {
+ const operationDependencies: Record<string, string[]> = {};
+
+ // Initialize dependencies for all operations
+ for (const operation of this.operationsToSchedule) {
+ if (!operation.id) continue;
+ operationDependencies[operation.id] = [];
+ }
+
+ // Process dependencies within the same level (sequential operations)
+ for (let i = 0; i < this.operationsToSchedule.length; i++) {
+ const currentOp = this.operationsToSchedule[i];
+ if (!currentOp.id) continue;
+
+ // Each operation depends on the previous operation in the sequence
+ if (i > 0) {
+ const prevOp = this.operationsToSchedule[i - 1];
+ // Use type assertion to access jobMakeMethodId
+ const currentOpWithMethod = currentOp as BaseOperation & {
+ jobMakeMethodId?: string;
+ };
+ const prevOpWithMethod = prevOp as BaseOperation & {
+ jobMakeMethodId?: string;
+ };
+
+ if (
+ prevOp.id &&
+ prevOpWithMethod.jobMakeMethodId &&
+ currentOpWithMethod.jobMakeMethodId &&
+ prevOpWithMethod.jobMakeMethodId ===
+ currentOpWithMethod.jobMakeMethodId
+ ) {
+ operationDependencies[currentOp.id].push(prevOp.id);
+ }
+ }
+ }
+
+ // Process subassembly dependencies
+ const queue = [...this.operationsToSchedule];
+ while (queue.length > 0) {
+ const operation = queue.shift();
+ if (!operation?.id) continue;
+
+ // Use type assertion to access jobMakeMethodId
+ const operationWithMethod = operation as BaseOperation & {
+ jobMakeMethodId?: string;
+ };
+ if (!operationWithMethod.jobMakeMethodId) continue;
+
+ // Find all subassemblies associated with this operation
+ const subassemblyMaterials = await this.db
+ .selectFrom("jobMaterialWithMakeMethodId")
+ .select(["id", "jobMakeMethodId", "jobOperationId"])
+ .where("jobMakeMethodId", "=", operationWithMethod.jobMakeMethodId)
+ .where("methodType", "=", "Make")
+ .execute();
+
+ for (const material of subassemblyMaterials) {
+ if (!material.jobMakeMethodId) continue;
+
+ // Get the last operation of the subassembly
+ const subassemblyOperations = await this.db
+ .selectFrom("jobOperation")
+ .select(["id"])
+ .where("jobMakeMethodId", "=", material.jobMakeMethodId)
+ .orderBy("order", "desc")
+ .limit(1)
+ .execute();
+
+ const lastSubassemblyOp = subassemblyOperations[0];
+ if (!lastSubassemblyOp?.id) continue;
+
+ // If the subassembly is associated with a specific operation
+ if (material.jobOperationId) {
+ if (operationDependencies[material.jobOperationId]) {
+ operationDependencies[material.jobOperationId].push(
+ lastSubassemblyOp.id
+ );
+ }
+ } else {
+ // If not associated with a specific operation, the first operation
+ // of the parent depends on the last operation of the subassembly
+ const firstParentOp = this.operationsToSchedule.find(
+ (op) =>
+ (op as BaseOperation & { jobMakeMethodId?: string })
+ .jobMakeMethodId === operationWithMethod.jobMakeMethodId
+ );
+ if (firstParentOp?.id) {
+ operationDependencies[firstParentOp.id].push(lastSubassemblyOp.id);
+ }
+ }
+ }
+ }
+
+ // Store the dependencies on the operations using the dedicated dependencies column
+ for (const operation of this.operationsToSchedule) {
+ if (!operation.id) continue;
+
+ // Update the operation with its dependencies using the text[] column
+ await this.db
+ .updateTable("jobOperation")
+ .set({
+ // @ts-ignore - dependencies column exists in the database but not in the TypeScript types
+ dependencies: operationDependencies[operation.id],
+ })
+ .where("id", "=", operation.id)
+ .execute();
+ }
}
async assign() {
@@ -190,6 +293,8 @@ class SchedulingEngine {
const jobMethodTree = jobMethodTrees.data?.[0] as JobMethodTreeItem;
if (!jobMethodTree) throw new Error("Method tree not found");
+ console.log({ jobMethodTree });
+
const operationsToSchedule: BaseOperation[] = [];
const queue: JobMethodTreeItem[] = [jobMethodTree];
const validMaterialIds: string[] = [];
diff --git a/packages/database/supabase/functions/scheduler/index.ts b/packages/database/supabase/functions/scheduler/index.ts
index 4a4c5133c3..0feaa2d837 100644
--- a/packages/database/supabase/functions/scheduler/index.ts
+++ b/packages/database/supabase/functions/scheduler/index.ts
@@ -51,14 +51,14 @@ serve(async (req: Request) => {
});
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.addDependencies();
+
return new Response(
JSON.stringify({
success: true,