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.

fix: set active version to archived on new active version for procedure and quality doc

Brad Barbin66f323967008
diff --git a/apps/erp/app/modules/items/items.service.ts b/apps/erp/app/modules/items/items.service.ts
index a11da8e2af..f3a1e321c6 100644
--- a/apps/erp/app/modules/items/items.service.ts
+++ b/apps/erp/app/modules/items/items.service.ts
@@ -741,8 +741,7 @@ export async function getMaterialUsedIn(
)
.eq("itemId", itemId)
.eq("companyId", companyId)
- .limit(100)
- .order("createdAt", { ascending: false }),
+ .limit(100),
]);
return {
@@ -1422,8 +1421,8 @@ export async function getPartUsedIn(
)
.eq("itemId", itemId)
.eq("companyId", companyId)
- .limit(100)
- .order("createdAt", { ascending: false }),
+ .limit(100),
+
client
.from("quoteMaterial")
.select(
@@ -1456,8 +1455,7 @@ export async function getPartUsedIn(
)
.eq("itemId", itemId)
.eq("companyId", companyId)
- .limit(100)
- .order("createdAt", { ascending: false }),
+ .limit(100),
]);
return {
diff --git a/packages/database/supabase/migrations/20251001211621_quality-document-versions.sql b/packages/database/supabase/migrations/20251001211621_quality-document-versions.sql
new file mode 100644
index 0000000000..8b8892c458
--- /dev/null
+++ b/packages/database/supabase/migrations/20251001211621_quality-document-versions.sql
@@ -0,0 +1,56 @@
+-- Add indexes for procedure table
+
+CREATE INDEX IF NOT EXISTS idx_procedure_name_company_id ON procedure(name, "companyId");
+CREATE INDEX IF NOT EXISTS idx_quality_document_name ON "qualityDocument"(name);
+
+
+-- Create trigger function to archive other active quality documents with same name
+CREATE OR REPLACE FUNCTION archive_other_quality_documents()
+RETURNS TRIGGER AS $$
+BEGIN
+ -- Only proceed if status is being updated to 'Active'
+ IF NEW.status = 'Active' AND (OLD.status IS NULL OR OLD.status != 'Active') THEN
+ -- Archive all other documents with same name and company that are currently Active
+ UPDATE "qualityDocument"
+ SET status = 'Archived'
+ WHERE name = NEW.name
+ AND "companyId" = NEW."companyId"
+ AND id != NEW.id
+ AND status = 'Active';
+ END IF;
+
+ RETURN NEW;
+END;
+$$ LANGUAGE plpgsql;
+
+-- Create trigger on qualityDocument table
+CREATE TRIGGER trigger_archive_other_quality_documents
+ AFTER UPDATE OF status ON "qualityDocument"
+ FOR EACH ROW
+ EXECUTE FUNCTION archive_other_quality_documents();
+
+-- Create trigger function to archive other active procedures with same name
+CREATE OR REPLACE FUNCTION archive_other_procedures()
+RETURNS TRIGGER AS $$
+BEGIN
+ -- Only proceed if status is being updated to 'Active'
+ IF NEW.status = 'Active' AND (OLD.status IS NULL OR OLD.status != 'Active') THEN
+ -- Archive all other procedures with same name and company that are currently Active
+ UPDATE procedure
+ SET status = 'Archived'
+ WHERE name = NEW.name
+ AND "companyId" = NEW."companyId"
+ AND id != NEW.id
+ AND status = 'Active';
+ END IF;
+
+ RETURN NEW;
+END;
+$$ LANGUAGE plpgsql;
+
+-- Create trigger on procedure table
+CREATE TRIGGER trigger_archive_other_procedures
+ AFTER UPDATE OF status ON procedure
+ FOR EACH ROW
+ EXECUTE FUNCTION archive_other_procedures();
+